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