00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include <stdlib.h>
00022
#include <time.h>
00023
00024
00025
00026
#include <assert.h>
00027
00028
#include <qcstring.h>
00029
#include <qdir.h>
00030
#include <qfile.h>
00031
#include <kdebug.h>
00032
#include <kmimetype.h>
00033
#include <ktempfile.h>
00034
00035
#include <kfilterdev.h>
00036
#include <kfilterbase.h>
00037
00038
#include "ktar.h"
00039
#include <kstandarddirs.h>
00040
00044
00045
class KTar::KTarPrivate
00046 {
00047
public:
00048 KTarPrivate() : tarEnd( 0 ) {}
00049
QStringList dirList;
00050
int tarEnd;
00051
KTempFile* tmpFile;
00052
QString mimetype;
00053
QCString origFileName;
00054
00055
bool fillTempFile(
const QString & filename);
00056
bool writeBackTempFile(
const QString & filename );
00057 };
00058
00059 KTar::KTar(
const QString& filename,
const QString & _mimetype )
00060 :
KArchive( 0L )
00061 {
00062 m_filename = filename;
00063 d =
new KTarPrivate;
00064 d->tmpFile = 0L;
00065 d->mimetype = _mimetype;
00066
QString mimetype( _mimetype );
00067
bool forced =
true;
00068
if ( mimetype.isEmpty() )
00069 {
00070
if ( QFile::exists( filename ) )
00071 mimetype =
KMimeType::findByFileContent( filename )->name();
00072
else
00073 mimetype =
KMimeType::findByPath( filename, 0,
true )->name();
00074
kdDebug(7041) <<
"KTar::KTar mimetype = " << mimetype <<
endl;
00075
00076
00077
if ( mimetype ==
"application/x-tgz" || mimetype ==
"application/x-targz" ||
00078 mimetype ==
"application/x-webarchive" )
00079 {
00080
00081 mimetype =
"application/x-gzip";
00082 }
00083
else if ( mimetype ==
"application/x-tbz" )
00084 {
00085 mimetype =
"application/x-bzip2";
00086 }
00087
else
00088 {
00089
00090
QFile file( filename );
00091
if ( file.open( IO_ReadOnly ) )
00092 {
00093
unsigned char firstByte = file.getch();
00094
unsigned char secondByte = file.getch();
00095
unsigned char thirdByte = file.getch();
00096
if ( firstByte == 0037 && secondByte == 0213 )
00097 mimetype =
"application/x-gzip";
00098
else if ( firstByte ==
'B' && secondByte ==
'Z' && thirdByte ==
'h' )
00099 mimetype =
"application/x-bzip2";
00100
else if ( firstByte ==
'P' && secondByte ==
'K' && thirdByte == 3 )
00101 {
00102
unsigned char fourthByte = file.getch();
00103
if ( fourthByte == 4 )
00104 mimetype =
"application/x-zip";
00105 }
00106 }
00107 file.close();
00108 }
00109 forced =
false;
00110 d->mimetype = mimetype;
00111 }
00112
00113 prepareDevice( filename, mimetype, forced );
00114 }
00115
00116
void KTar::prepareDevice(
const QString & filename,
00117
const QString & mimetype,
bool )
00118 {
00119
if(
"application/x-tar" == mimetype )
00120 setDevice(
new QFile( filename ) );
00121
else
00122 {
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 d->tmpFile =
new KTempFile(
locateLocal(
"tmp",
"ktar-"),
".tar");
00133
kdDebug( 7041 ) <<
"KTar::prepareDevice creating TempFile: " << d->tmpFile->name() <<
endl;
00134 d->tmpFile->setAutoDelete(
true);
00135
00136
00137
00138
QFile* file = d->tmpFile->file();
00139 file->close();
00140 setDevice(file);
00141 }
00142 }
00143
00144 KTar::KTar(
QIODevice * dev )
00145 :
KArchive( dev )
00146 {
00147 d =
new KTarPrivate;
00148 }
00149
00150 KTar::~KTar()
00151 {
00152
00153
if(
isOpened() )
00154
close();
00155
00156
if (d->tmpFile)
00157
delete d->tmpFile;
00158
else if ( !m_filename.isEmpty() )
00159
delete device();
00160
00161
00162
delete d;
00163 }
00164
00165 void KTar::setOrigFileName(
const QCString & fileName )
00166 {
00167
if ( !
isOpened() || !(
mode() & IO_WriteOnly) )
00168 {
00169
kdWarning(7041) <<
"KTar::setOrigFileName: File must be opened for writing first.\n";
00170
return;
00171 }
00172 d->origFileName = fileName;
00173 }
00174
00175 Q_LONG KTar::readRawHeader(
char *buffer) {
00176
00177 Q_LONG n =
device()->readBlock( buffer, 0x200 );
00178
if ( n == 0x200 && buffer[0] != 0 ) {
00179
00180
if (strncmp(buffer + 257,
"ustar", 5)) {
00181
00182
QCString s;
00183
00184
int check = 0;
00185
for( uint j = 0; j < 0x200; ++j )
00186 check += buffer[j];
00187
00188
00189
for( uint j = 0; j < 8 ; j++ )
00190 check -= buffer[148 + j];
00191 check += 8 *
' ';
00192
00193 s.sprintf(
"%o", check );
00194
00195
00196
00197
if( strncmp( buffer + 148 + 6 - s.length(), s.data(), s.length() ) ) {
00198
kdWarning(7041) <<
"KTar: invalid TAR file. Header is: " <<
QCString( buffer+257, 5 ) <<
endl;
00199
return -1;
00200 }
00201 }
00202 }
else {
00203
00204
if (n == 0x200) n = 0;
00205 }
00206
return n;
00207 }
00208
00209
bool KTar::readLonglink(
char *buffer,
QCString &longlink) {
00210 Q_LONG n = 0;
00211
QIODevice *dev =
device();
00212
00213
00214 buffer[ 0x88 ] = 0;
00215
char *dummy;
00216
const char* p = buffer + 0x7c;
00217
while( *p ==
' ' ) ++p;
00218
int size = (
int)strtol( p, &dummy, 8 );
00219
00220 longlink.resize(size);
00221 size--;
00222 dummy = longlink.data();
00223
int offset = 0;
00224
while (size > 0) {
00225
int chunksize = QMIN(size, 0x200);
00226 n = dev->readBlock( dummy + offset, chunksize );
00227
if (n == -1)
return false;
00228 size -= chunksize;
00229 offset += 0x200;
00230 }
00231
00232
int skip = 0x200 - (n % 0x200);
00233
if (skip < 0x200) {
00234
if (dev->readBlock(buffer,skip) != skip)
return false;
00235 }
00236
return true;
00237 }
00238
00239 Q_LONG KTar::readHeader(
char *buffer,
QString &name,
QString &symlink) {
00240
name.truncate(0);
00241
symlink.truncate(0);
00242
while (
true) {
00243 Q_LONG n = readRawHeader(buffer);
00244
if (n != 0x200)
return n;
00245
00246
00247
if (strcmp(buffer,
"././@LongLink") == 0) {
00248
char typeflag = buffer[0x9c];
00249
QCString longlink;
00250 readLonglink(buffer,longlink);
00251
switch (typeflag) {
00252
case 'L':
name = QFile::decodeName(longlink);
break;
00253
case 'K':
symlink = QFile::decodeName(longlink);
break;
00254 }
00255 }
else {
00256
break;
00257 }
00258 }
00259
00260
00261
if (
name.isEmpty())
00262
name = QFile::decodeName(buffer);
00263
if (
symlink.isEmpty())
00264
symlink = QFile::decodeName(buffer + 0x9d);
00265
00266
return 0x200;
00267 }
00268
00269
00270
00271
00272
00273
00274
bool KTar::KTarPrivate::fillTempFile(
const QString & filename) {
00275
if ( ! tmpFile )
00276
return true;
00277
00278
kdDebug( 7041 ) <<
00279
"KTar::openArchive: filling tmpFile of mimetype '" <<
mimetype <<
00280
"' ... " <<
endl;
00281
00282
bool forced =
false;
00283
if(
"application/x-gzip" ==
mimetype
00284 ||
"application/x-bzip2" ==
mimetype)
00285 forced =
true;
00286
00287
QIODevice *filterDev =
KFilterDev::deviceForFile( filename, mimetype, forced );
00288
00289
if( filterDev ) {
00290
QFile* file = tmpFile->file();
00291 file->close();
00292
if ( ! file->open( IO_WriteOnly ) )
00293 {
00294
delete filterDev;
00295
return false;
00296 }
00297
QByteArray buffer(8*1024);
00298
if ( ! filterDev->open( IO_ReadOnly ) )
00299 {
00300
delete filterDev;
00301
return false;
00302 }
00303 Q_LONG len;
00304
while ( !filterDev->atEnd() ) {
00305 len = filterDev->readBlock(buffer.data(),buffer.size());
00306
if ( len <= 0 ) {
00307
delete filterDev;
00308
return false;
00309 }
00310 file->writeBlock(buffer.data(),len);
00311 }
00312 filterDev->close();
00313
delete filterDev;
00314
00315 file->close();
00316
if ( ! file->open( IO_ReadOnly ) )
00317
return false;
00318 }
00319
else
00320
kdDebug( 7041 ) <<
"KTar::openArchive: no filterdevice found!" <<
endl;
00321
00322
kdDebug( 7041 ) <<
"KTar::openArchive: filling tmpFile finished." <<
endl;
00323
return true;
00324 }
00325
00326 bool KTar::openArchive(
int mode )
00327 {
00328
kdDebug( 7041 ) <<
"KTar::openArchive" <<
endl;
00329
if ( !(mode & IO_ReadOnly) )
00330
return true;
00331
00332
if ( !d->fillTempFile( m_filename ) )
00333
return false;
00334
00335
00336
00337
00338
00339
00340 d->dirList.clear();
00341
QIODevice* dev =
device();
00342
00343
00344
char buffer[ 0x200 ];
00345
bool ende =
false;
00346
do
00347 {
00348
QString name;
00349
QString symlink;
00350
00351
00352 Q_LONG n = readHeader(buffer,name,symlink);
00353
if (n < 0)
return false;
00354
if (n == 0x200)
00355 {
00356
bool isdir =
false;
00357
QString nm;
00358
00359
if ( name.right(1) ==
"/" )
00360 {
00361 isdir =
true;
00362 name = name.left( name.length() - 1 );
00363 }
00364
00365
int pos = name.findRev(
'/' );
00366
if ( pos == -1 )
00367 nm = name;
00368
else
00369 nm = name.mid( pos + 1 );
00370
00371
00372 buffer[ 0x6b ] = 0;
00373
char *dummy;
00374
const char* p = buffer + 0x64;
00375
while( *p ==
' ' ) ++p;
00376
int access = (
int)strtol( p, &dummy, 8 );
00377
00378
00379
QString user( buffer + 0x109 );
00380
QString group( buffer + 0x129 );
00381
00382
00383 buffer[ 0x93 ] = 0;
00384 p = buffer + 0x88;
00385
while( *p ==
' ' ) ++p;
00386
int time = (
int)strtol( p, &dummy, 8 );
00387
00388
00389
char typeflag = buffer[ 0x9c ];
00390
00391
00392
00393
if ( typeflag ==
'1' )
00394 isdir =
true;
00395
00396
bool isDumpDir =
false;
00397
if ( typeflag ==
'D' )
00398 {
00399 isdir =
false;
00400 isDumpDir =
true;
00401 }
00402
00403
00404
00405
if (isdir)
00406 access |= S_IFDIR;
00407
00408
KArchiveEntry* e;
00409
if ( isdir )
00410 {
00411
00412 e =
new KArchiveDirectory(
this, nm, access, time, user, group, symlink );
00413 }
00414
else
00415 {
00416
00417 buffer[ 0x88 ] = 0;
00418
char *dummy;
00419
const char* p = buffer + 0x7c;
00420
while( *p ==
' ' ) ++p;
00421
int size = (
int)strtol( p, &dummy, 8 );
00422
00423
00424
if ( isDumpDir )
00425 {
00426 e =
new KArchiveDirectory(
this, nm, access, time, user, group, symlink );
00427 }
00428
else
00429 {
00430
00431
00432
if ( typeflag ==
'1' )
00433 {
00434 size = nm.length();
00435
kdDebug(7041) <<
"HARD LINK, setting size to " << size <<
endl;
00436 }
00437
00438
00439 e =
new KArchiveFile(
this, nm, access, time, user, group, symlink,
00440 dev->at(), size );
00441 }
00442
00443
00444
int rest = size % 0x200;
00445
int skip = size + (rest ? 0x200 - rest : 0);
00446
00447
if (! dev->at( dev->at() + skip ) )
00448
kdWarning(7041) <<
"KArchive::open skipping " << skip <<
" failed" <<
endl;
00449 }
00450
00451
if ( pos == -1 )
00452 {
00453
if ( nm ==
"." )
00454 {
00455 Q_ASSERT( isdir );
00456
if ( isdir )
00457 setRootDir( static_cast<KArchiveDirectory *>( e ) );
00458 }
00459
else
00460
rootDir()->
addEntry( e );
00461 }
00462
else
00463 {
00464
00465
QString path = QDir::cleanDirPath( name.left( pos ) );
00466
00467
KArchiveDirectory * d =
findOrCreate( path );
00468 d->
addEntry( e );
00469 }
00470 }
00471
else
00472 {
00473
00474 d->tarEnd = dev->at() - n;
00475 ende =
true;
00476 }
00477 }
while( !ende );
00478
return true;
00479 }
00480
00481
00482
00483
00484
00485
00486
bool KTar::KTarPrivate::writeBackTempFile(
const QString & filename ) {
00487
if ( ! tmpFile )
00488
return true;
00489
00490
kdDebug(7041) <<
"Write temporary file to compressed file" <<
endl;
00491
kdDebug(7041) << filename <<
" " << mimetype <<
endl;
00492
00493
bool forced =
false;
00494
if(
"application/x-gzip" == mimetype
00495 ||
"application/x-bzip2" == mimetype)
00496 forced =
true;
00497
00498
00499
QIODevice *dev =
KFilterDev::deviceForFile( filename, mimetype, forced );
00500
if( dev ) {
00501
QFile* file = tmpFile->file();
00502 file->close();
00503
if ( ! file->open(IO_ReadOnly) || ! dev->open(IO_WriteOnly) )
00504 {
00505 file->close();
00506
delete dev;
00507
return false;
00508 }
00509
if ( forced )
00510 static_cast<KFilterDev *>(dev)->setOrigFileName( origFileName );
00511
QByteArray buffer(8*1024);
00512 Q_LONG len;
00513
while ( ! file->atEnd()) {
00514 len = file->readBlock(buffer.data(),buffer.size());
00515 dev->writeBlock(buffer.data(),len);
00516 }
00517 file->close();
00518 dev->close();
00519
delete dev;
00520 }
00521
00522
kdDebug(7041) <<
"Write temporary file to compressed file done." <<
endl;
00523
return true;
00524 }
00525
00526 bool KTar::closeArchive()
00527 {
00528 d->dirList.clear();
00529
00530
00531
00532
00533
if(
mode() == IO_WriteOnly)
00534
return d->writeBackTempFile( m_filename );
00535
00536
return true;
00537 }
00538
00539 bool KTar::writeDir(
const QString& name,
const QString& user,
const QString& group )
00540 {
00541 mode_t perm = 040755;
00542 time_t the_time = time(0);
00543
return writeDir(name,user,group,perm,the_time,the_time,the_time);
00544
#if 0
00545
if ( !
isOpened() )
00546 {
00547
kdWarning(7041) <<
"KTar::writeDir: You must open the tar file before writing to it\n";
00548
return false;
00549 }
00550
00551
if ( !(
mode() & IO_WriteOnly) )
00552 {
00553
kdWarning(7041) <<
"KTar::writeDir: You must open the tar file for writing\n";
00554
return false;
00555 }
00556
00557
00558
QString dirName ( QDir::cleanDirPath( name ) );
00559
00560
00561
if ( dirName.right(1) !=
"/" )
00562 dirName +=
"/";
00563
00564
if ( d->dirList.contains( dirName ) )
00565
return true;
00566
00567
char buffer[ 0x201 ];
00568 memset( buffer, 0, 0x200 );
00569
if (
mode() & IO_ReadWrite )
device()->at(d->tarEnd);
00570
00571
00572
if ( dirName.length() > 99 )
00573 {
00574 strcpy( buffer,
"././@LongLink" );
00575 fillBuffer( buffer,
" 0", dirName.length()+1,
'L', user.local8Bit(), group.local8Bit() );
00576
device()->writeBlock( buffer, 0x200 );
00577 strncpy( buffer, QFile::encodeName(dirName), 0x200 );
00578 buffer[0x200] = 0;
00579
00580
device()->writeBlock( buffer, 0x200 );
00581
00582 }
00583
else
00584 {
00585
00586 strncpy( buffer, QFile::encodeName(dirName), 0x200 );
00587 buffer[0x200] = 0;
00588 }
00589
00590 fillBuffer( buffer,
" 40755", 0, 0x35, user.local8Bit(), group.local8Bit());
00591
00592
00593
device()->writeBlock( buffer, 0x200 );
00594
if (
mode() & IO_ReadWrite ) d->tarEnd =
device()->at();
00595
00596 d->dirList.append( dirName );
00597
return true;
00598
#endif
00599
}
00600
00601 bool KTar::prepareWriting(
const QString& name,
const QString& user,
const QString& group, uint size )
00602 {
00603 mode_t dflt_perm = 0100644;
00604 time_t the_time = time(0);
00605
return prepareWriting(name,user,group,size,dflt_perm,
00606 the_time,the_time,the_time);
00607 }
00608
00609 bool KTar::doneWriting( uint size )
00610 {
00611
00612
int rest = size % 0x200;
00613
if (
mode() & IO_ReadWrite )
00614 d->tarEnd =
device()->at() + (rest ? 0x200 - rest : 0);
00615
if ( rest )
00616 {
00617
char buffer[ 0x201 ];
00618
for( uint i = 0; i < 0x200; ++i )
00619 buffer[i] = 0;
00620 Q_LONG nwritten =
device()->writeBlock( buffer, 0x200 - rest );
00621
return nwritten == 0x200 - rest;
00622 }
00623
return true;
00624 }
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
void KTar::fillBuffer(
char * buffer,
00650
const char * mode,
int size, time_t mtime,
char typeflag,
00651
const char * uname,
const char * gname )
00652 {
00653
00654 assert( strlen(mode) == 6 );
00655 strcpy( buffer+0x64, mode );
00656 buffer[ 0x6a ] =
' ';
00657 buffer[ 0x6b ] =
'\0';
00658
00659
00660 strcpy( buffer + 0x6c,
" 765 ");
00661
00662 strcpy( buffer + 0x74,
" 144 ");
00663
00664
00665
QCString s;
00666 s.sprintf(
"%o", size);
00667 s = s.rightJustify( 11,
' ' );
00668 strcpy( buffer + 0x7c, s.data() );
00669 buffer[ 0x87 ] =
' ';
00670
00671
00672 s.sprintf(
"%lo", static_cast<unsigned long>(mtime) );
00673 s = s.rightJustify( 11,
' ' );
00674 strcpy( buffer + 0x88, s.data() );
00675 buffer[ 0x93 ] =
' ';
00676
00677
00678 buffer[ 0x94 ] = 0x20;
00679 buffer[ 0x95 ] = 0x20;
00680 buffer[ 0x96 ] = 0x20;
00681 buffer[ 0x97 ] = 0x20;
00682 buffer[ 0x98 ] = 0x20;
00683 buffer[ 0x99 ] = 0x20;
00684
00685
00686
00687
00688
00689
00690 buffer[ 0x9a ] =
'\0';
00691 buffer[ 0x9b ] =
' ';
00692
00693
00694 buffer[ 0x9c ] = typeflag;
00695
00696
00697 strcpy( buffer + 0x101,
"ustar");
00698 strcpy( buffer + 0x107,
"00" );
00699
00700
00701 strcpy( buffer + 0x109, uname );
00702
00703 strcpy( buffer + 0x129, gname );
00704
00705
00706
int check = 32;
00707
for( uint j = 0; j < 0x200; ++j )
00708 check += buffer[j];
00709 s.sprintf(
"%o", check );
00710 s = s.rightJustify( 7,
' ' );
00711 strcpy( buffer + 0x94, s.data() );
00712 }
00713
00714
void KTar::writeLonglink(
char *buffer,
const QCString &name,
char typeflag,
00715
const char *uname,
const char *gname) {
00716 strcpy( buffer,
"././@LongLink" );
00717
int namelen =
name.length() + 1;
00718 fillBuffer( buffer,
" 0", namelen, 0, typeflag, uname, gname );
00719
device()->writeBlock( buffer, 0x200 );
00720
int offset = 0;
00721
while (namelen > 0) {
00722
int chunksize = QMIN(namelen, 0x200);
00723 memcpy(buffer,
name.data()+offset, chunksize);
00724
00725
device()->writeBlock( buffer, 0x200 );
00726
00727 namelen -= chunksize;
00728 offset += 0x200;
00729 }
00730 }
00731
00732 bool KTar::prepareWriting(
const QString& name,
const QString& user,
00733
const QString& group, uint size, mode_t perm,
00734 time_t atime, time_t mtime, time_t ctime) {
00735
return KArchive::prepareWriting(name,user,group,size,perm,atime,mtime,ctime);
00736 }
00737
00738
bool KTar::prepareWriting_impl(
const QString &name,
const QString &user,
00739
const QString &group, uint size, mode_t perm,
00740 time_t , time_t mtime, time_t ) {
00741
if ( !
isOpened() )
00742 {
00743
kdWarning(7041) <<
"KTar::prepareWriting: You must open the tar file before writing to it\n";
00744
return false;
00745 }
00746
00747
if ( !(
mode() & IO_WriteOnly) )
00748 {
00749
kdWarning(7041) <<
"KTar::prepareWriting: You must open the tar file for writing\n";
00750
return false;
00751 }
00752
00753
00754
QString fileName ( QDir::cleanDirPath( name ) );
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
char buffer[ 0x201 ];
00776 memset( buffer, 0, 0x200 );
00777
if (
mode() & IO_ReadWrite )
device()->at(d->tarEnd);
00778
00779
00780
QCString encodedFilename = QFile::encodeName(fileName);
00781
QCString uname = user.local8Bit();
00782
QCString gname = group.local8Bit();
00783
00784
00785
if (
fileName.length() > 99 )
00786 writeLonglink(buffer,encodedFilename,
'L',uname,gname);
00787
00788
00789 strncpy( buffer, encodedFilename, 99 );
00790 buffer[99] = 0;
00791
00792 memset(buffer+0x9d, 0, 0x200 - 0x9d);
00793
00794
QCString permstr;
00795 permstr.sprintf(
"%o",perm);
00796 permstr.rightJustify(6,
' ');
00797 fillBuffer(buffer, permstr, size, mtime, 0x30, uname, gname);
00798
00799
00800
return device()->writeBlock( buffer, 0x200 ) == 0x200;
00801 }
00802
00803 bool KTar::writeDir(
const QString& name,
const QString& user,
00804
const QString& group, mode_t perm,
00805 time_t atime, time_t mtime, time_t ctime) {
00806
return KArchive::writeDir(name,user,group,perm,atime,mtime,ctime);
00807 }
00808
00809
bool KTar::writeDir_impl(
const QString &name,
const QString &user,
00810
const QString &group, mode_t perm,
00811 time_t , time_t mtime, time_t ) {
00812
if ( !
isOpened() )
00813 {
00814
kdWarning(7041) <<
"KTar::writeDir: You must open the tar file before writing to it\n";
00815
return false;
00816 }
00817
00818
if ( !(
mode() & IO_WriteOnly) )
00819 {
00820
kdWarning(7041) <<
"KTar::writeDir: You must open the tar file for writing\n";
00821
return false;
00822 }
00823
00824
00825
QString dirName ( QDir::cleanDirPath( name ) );
00826
00827
00828
if ( dirName.right(1) !=
"/" )
00829 dirName +=
"/";
00830
00831
if ( d->dirList.contains( dirName ) )
00832
return true;
00833
00834
char buffer[ 0x201 ];
00835 memset( buffer, 0, 0x200 );
00836
if (
mode() & IO_ReadWrite )
device()->at(d->tarEnd);
00837
00838
00839
QCString encodedDirname = QFile::encodeName(dirName);
00840
QCString uname = user.local8Bit();
00841
QCString gname = group.local8Bit();
00842
00843
00844
if ( dirName.length() > 99 )
00845 writeLonglink(buffer,encodedDirname,
'L',uname,gname);
00846
00847
00848 strncpy( buffer, encodedDirname, 99 );
00849 buffer[99] = 0;
00850
00851 memset(buffer+0x9d, 0, 0x200 - 0x9d);
00852
00853
QCString permstr;
00854 permstr.sprintf(
"%o",perm);
00855 permstr.rightJustify(6,
' ');
00856 fillBuffer( buffer, permstr, 0, mtime, 0x35, uname, gname);
00857
00858
00859
device()->writeBlock( buffer, 0x200 );
00860
if (
mode() & IO_ReadWrite ) d->tarEnd =
device()->at();
00861
00862 d->dirList.append( dirName );
00863
return true;
00864 }
00865
00866 bool KTar::writeSymLink(
const QString &name,
const QString &target,
00867
const QString &user,
const QString &group,
00868 mode_t perm, time_t atime, time_t mtime, time_t ctime) {
00869
return KArchive::writeSymLink(name,target,user,group,perm,atime,mtime,ctime);
00870 }
00871
00872
bool KTar::writeSymLink_impl(
const QString &name,
const QString &target,
00873
const QString &user,
const QString &group,
00874 mode_t perm, time_t , time_t mtime, time_t ) {
00875
if ( !
isOpened() )
00876 {
00877
kdWarning(7041) <<
"KTar::writeSymLink: You must open the tar file before writing to it\n";
00878
return false;
00879 }
00880
00881
if ( !(
mode() & IO_WriteOnly) )
00882 {
00883
kdWarning(7041) <<
"KTar::writeSymLink: You must open the tar file for writing\n";
00884
return false;
00885 }
00886
00887
device()->flush();
00888
00889
00890
QString fileName ( QDir::cleanDirPath( name ) );
00891
00892
char buffer[ 0x201 ];
00893 memset( buffer, 0, 0x200 );
00894
if (
mode() & IO_ReadWrite )
device()->at(d->tarEnd);
00895
00896
00897
QCString encodedFilename = QFile::encodeName(fileName);
00898
QCString encodedTarget = QFile::encodeName(target);
00899
QCString uname = user.local8Bit();
00900
QCString gname = group.local8Bit();
00901
00902
00903
if (target.length() > 99)
00904 writeLonglink(buffer,encodedTarget,
'K',uname,gname);
00905
if (
fileName.length() > 99 )
00906 writeLonglink(buffer,encodedFilename,
'L',uname,gname);
00907
00908
00909 strncpy( buffer, encodedFilename, 99 );
00910 buffer[99] = 0;
00911
00912 strncpy(buffer+0x9d, encodedTarget, 99);
00913 buffer[0x9d+99] = 0;
00914
00915 memset(buffer+0x9d+100, 0, 0x200 - 100 - 0x9d);
00916
00917
QCString permstr;
00918 permstr.sprintf(
"%o",perm);
00919 permstr.rightJustify(6,
' ');
00920 fillBuffer(buffer, permstr, 0, mtime, 0x32, uname, gname);
00921
00922
00923
bool retval =
device()->writeBlock( buffer, 0x200 ) == 0x200;
00924
if (
mode() & IO_ReadWrite ) d->tarEnd =
device()->at();
00925
return retval;
00926 }
00927
00928
void KTar::virtual_hook(
int id,
void* data ) {
00929
switch (
id) {
00930
case VIRTUAL_WRITE_SYMLINK: {
00931 WriteSymlinkParams *params = reinterpret_cast<WriteSymlinkParams *>(data);
00932 params->retval = writeSymLink_impl(*params->name,*params->target,
00933 *params->user,*params->group,params->perm,
00934 params->atime,params->mtime,params->ctime);
00935
break;
00936 }
00937
case VIRTUAL_WRITE_DIR: {
00938 WriteDirParams *params = reinterpret_cast<WriteDirParams *>(data);
00939 params->retval = writeDir_impl(*params->name,*params->user,
00940 *params->group,params->perm,
00941 params->atime,params->mtime,params->ctime);
00942
break;
00943 }
00944
case VIRTUAL_PREPARE_WRITING: {
00945 PrepareWritingParams *params = reinterpret_cast<PrepareWritingParams *>(data);
00946 params->retval = prepareWriting_impl(*params->name,*params->user,
00947 *params->group,params->size,params->perm,
00948 params->atime,params->mtime,params->ctime);
00949
break;
00950 }
00951
default:
00952 KArchive::virtual_hook(
id, data );
00953 }
00954 }
00955