• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.5 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • io
ktar.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 David Faure <faure@kde.org>
3  Copyright (C) 2003 Leo Savernik <l.savernik@aon.at>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "ktar.h"
21 
22 #include <stdlib.h> // strtol
23 #include <time.h> // time()
24 #include <assert.h>
25 
26 #include <QtCore/QDir>
27 #include <QtCore/QFile>
28 #include <kdebug.h>
29 #include <kmimetype.h>
30 #include <ktemporaryfile.h>
31 
32 #include <kfilterdev.h>
33 #include <kfilterbase.h>
34 
38 
39 // Mime types of known filters
40 static const char application_gzip[] = "application/x-gzip";
41 static const char application_bzip[] = "application/x-bzip";
42 static const char application_lzma[] = "application/x-lzma";
43 static const char application_xz[] = "application/x-xz";
44 static const char application_zip[] = "application/zip";
45 
46 class KTar::KTarPrivate
47 {
48 public:
49  KTarPrivate(KTar *parent)
50  : q(parent),
51  tarEnd( 0 ),
52  tmpFile( 0 )
53  {
54  }
55 
56  KTar *q;
57  QStringList dirList;
58  qint64 tarEnd;
59  KTemporaryFile* tmpFile;
60  QString mimetype;
61  QByteArray origFileName;
62 
63  bool fillTempFile(const QString & fileName);
64  bool writeBackTempFile( const QString & fileName );
65  void fillBuffer( char * buffer, const char * mode, qint64 size, time_t mtime,
66  char typeflag, const char * uname, const char * gname );
67  void writeLonglink(char *buffer, const QByteArray &name, char typeflag,
68  const char *uname, const char *gname);
69  qint64 readRawHeader(char *buffer);
70  bool readLonglink(char *buffer, QByteArray &longlink);
71  qint64 readHeader(char *buffer, QString &name, QString &symlink);
72 };
73 
74 KTar::KTar( const QString& fileName, const QString & _mimetype )
75  : KArchive( fileName ), d(new KTarPrivate(this))
76 {
77  d->mimetype = _mimetype;
78 }
79 
80 KTar::KTar( QIODevice * dev )
81  : KArchive( dev ), d(new KTarPrivate(this))
82 {
83  Q_ASSERT( dev );
84 }
85 
86 // Only called when a filename was given
87 bool KTar::createDevice(QIODevice::OpenMode mode)
88 {
89  if (d->mimetype.isEmpty()) {
90  // Find out mimetype manually
91 
92  KMimeType::Ptr mime;
93  if (mode != QIODevice::WriteOnly && QFile::exists(fileName())) {
94  // Give priority to file contents: if someone renames a .tar.bz2 to .tar.gz,
95  // we can still do the right thing here.
96  mime = KMimeType::findByFileContent(fileName());
97  if (mime == KMimeType::defaultMimeTypePtr()) {
98  // Unable to determine mimetype from contents, get it from file name
99  mime = KMimeType::findByPath(fileName(), 0, true);
100  }
101  } else {
102  mime = KMimeType::findByPath(fileName(), 0, true);
103  }
104 
105  //kDebug(7041) << mode << mime->name();
106 
107  if (mime->is(QString::fromLatin1("application/x-compressed-tar")) || mime->is(QString::fromLatin1(application_gzip))) {
108  // gzipped tar file (with possibly invalid file name), ask for gzip filter
109  d->mimetype = QString::fromLatin1(application_gzip);
110  } else if (mime->is(QString::fromLatin1("application/x-bzip-compressed-tar")) || mime->is(QString::fromLatin1(application_bzip))) {
111  // bzipped2 tar file (with possibly invalid file name), ask for bz2 filter
112  d->mimetype = QString::fromLatin1(application_bzip);
113  } else if (mime->is(QString::fromLatin1("application/x-lzma-compressed-tar")) || mime->is(QString::fromLatin1(application_lzma))) {
114  // lzma compressed tar file (with possibly invalid file name), ask for xz filter
115  d->mimetype = QString::fromLatin1(application_lzma);
116  } else if (mime->is(QString::fromLatin1("application/x-xz-compressed-tar")) || mime->is(QString::fromLatin1(application_xz))) {
117  // xz compressed tar file (with possibly invalid name), ask for xz filter
118  d->mimetype = QString::fromLatin1(application_xz);
119  }
120  }
121 
122  if (d->mimetype == QLatin1String("application/x-tar")) {
123  return KArchive::createDevice(mode);
124  } else if (mode == QIODevice::WriteOnly) {
125  if (!KArchive::createDevice(mode))
126  return false;
127  if (!d->mimetype.isEmpty()) {
128  // Create a compression filter on top of the KSaveFile device that KArchive created.
129  //kDebug(7041) << "creating KFilterDev for" << d->mimetype;
130  QIODevice *filterDev = KFilterDev::device(device(), d->mimetype);
131  Q_ASSERT(filterDev);
132  setDevice(filterDev);
133  }
134  return true;
135  } else {
136  // The compression filters are very slow with random access.
137  // So instead of applying the filter to the device,
138  // the file is completely extracted instead,
139  // and we work on the extracted tar file.
140  // This improves the extraction speed by the tar ioslave dramatically,
141  // if the archive file contains many files.
142  // This is because the tar ioslave extracts one file after the other and normally
143  // has to walk through the decompression filter each time.
144  // Which is in fact nearly as slow as a complete decompression for each file.
145 
146  Q_ASSERT(!d->tmpFile);
147  d->tmpFile = new KTemporaryFile();
148  d->tmpFile->setPrefix(QLatin1String("ktar-"));
149  d->tmpFile->setSuffix(QLatin1String(".tar"));
150  d->tmpFile->open();
151  //kDebug(7041) << "creating tempfile:" << d->tmpFile->fileName();
152 
153  setDevice(d->tmpFile);
154  return true;
155  }
156 }
157 
158 KTar::~KTar()
159 {
160  // mjarrett: Closes to prevent ~KArchive from aborting w/o device
161  if( isOpen() )
162  close();
163 
164  delete d->tmpFile;
165  delete d;
166 }
167 
168 void KTar::setOrigFileName( const QByteArray & fileName ) {
169  if ( !isOpen() || !(mode() & QIODevice::WriteOnly) )
170  {
171  kWarning(7041) << "KTar::setOrigFileName: File must be opened for writing first.\n";
172  return;
173  }
174  d->origFileName = fileName;
175 }
176 
177 qint64 KTar::KTarPrivate::readRawHeader( char *buffer ) {
178  // Read header
179  qint64 n = q->device()->read( buffer, 0x200 );
180  // we need to test if there is a prefix value because the file name can be null
181  // and the prefix can have a value and in this case we don't reset n.
182  if ( n == 0x200 && (buffer[0] != 0 || buffer[0x159] != 0) ) {
183  // Make sure this is actually a tar header
184  if (strncmp(buffer + 257, "ustar", 5)) {
185  // The magic isn't there (broken/old tars), but maybe a correct checksum?
186 
187  int check = 0;
188  for( uint j = 0; j < 0x200; ++j )
189  check += buffer[j];
190 
191  // adjust checksum to count the checksum fields as blanks
192  for( uint j = 0; j < 8 /*size of the checksum field including the \0 and the space*/; j++ )
193  check -= buffer[148 + j];
194  check += 8 * ' ';
195 
196  QByteArray s = QByteArray::number( check, 8 ); // octal
197 
198  // only compare those of the 6 checksum digits that mean something,
199  // because the other digits are filled with all sorts of different chars by different tars ...
200  // Some tars right-justify the checksum so it could start in one of three places - we have to check each.
201  if( strncmp( buffer + 148 + 6 - s.length(), s.data(), s.length() )
202  && strncmp( buffer + 148 + 7 - s.length(), s.data(), s.length() )
203  && strncmp( buffer + 148 + 8 - s.length(), s.data(), s.length() ) ) {
204  kWarning(7041) << "KTar: invalid TAR file. Header is:" << QByteArray( buffer+257, 5 )
205  << "instead of ustar. Reading from wrong pos in file?"
206  << "checksum=" << QByteArray( buffer + 148 + 6 - s.length(), s.length() );
207  return -1;
208  }
209  }/*end if*/
210  } else {
211  // reset to 0 if 0x200 because logical end of archive has been reached
212  if (n == 0x200) n = 0;
213  }/*end if*/
214  return n;
215 }
216 
217 bool KTar::KTarPrivate::readLonglink(char *buffer,QByteArray &longlink) {
218  qint64 n = 0;
219  //kDebug() << "reading longlink from pos " << device()->pos();
220  QIODevice *dev = q->device();
221  // read size of longlink from size field in header
222  // size is in bytes including the trailing null (which we ignore)
223  qint64 size = QByteArray( buffer + 0x7c, 12 ).trimmed().toLongLong( 0, 8 /*octal*/ );
224 
225  size--; // ignore trailing null
226  longlink.resize(size);
227  qint64 offset = 0;
228  while (size > 0) {
229  int chunksize = qMin(size, 0x200LL);
230  n = dev->read( longlink.data() + offset, chunksize );
231  if (n == -1) return false;
232  size -= chunksize;
233  offset += 0x200;
234  }/*wend*/
235  // jump over the rest
236  const int skip = 0x200 - (n % 0x200);
237  if (skip <= 0x200) {
238  if (dev->read(buffer,skip) != skip)
239  return false;
240  }
241  return true;
242 }
243 
244 qint64 KTar::KTarPrivate::readHeader( char *buffer, QString &name, QString &symlink ) {
245  name.truncate(0);
246  symlink.truncate(0);
247  while (true) {
248  qint64 n = readRawHeader(buffer);
249  if (n != 0x200) return n;
250 
251  // is it a longlink?
252  if (strcmp(buffer,"././@LongLink") == 0) {
253  char typeflag = buffer[0x9c];
254  QByteArray longlink;
255  readLonglink(buffer,longlink);
256  switch (typeflag) {
257  case 'L': name = QFile::decodeName(longlink); break;
258  case 'K': symlink = QFile::decodeName(longlink); break;
259  }/*end switch*/
260  } else {
261  break;
262  }/*end if*/
263  }/*wend*/
264 
265  // if not result of longlink, read names directly from the header
266  if (name.isEmpty())
267  // there are names that are exactly 100 bytes long
268  // and neither longlink nor \0 terminated (bug:101472)
269  name = QFile::decodeName(QByteArray(buffer, 100));
270  if (symlink.isEmpty())
271  symlink = QFile::decodeName(QByteArray(buffer + 0x9d /*?*/, 100));
272 
273  return 0x200;
274 }
275 
276 /*
277  * If we have created a temporary file, we have
278  * to decompress the original file now and write
279  * the contents to the temporary file.
280  */
281 bool KTar::KTarPrivate::fillTempFile( const QString & fileName) {
282  if ( ! tmpFile )
283  return true;
284 
285  //kDebug(7041) << "filling tmpFile of mimetype" << mimetype;
286 
287  bool forced = false;
288  if ( QLatin1String(application_gzip) == mimetype || QLatin1String(application_bzip) == mimetype )
289  forced = true;
290 
291  QIODevice *filterDev = KFilterDev::deviceForFile( fileName, mimetype, forced );
292 
293  if( filterDev ) {
294  QFile* file = tmpFile;
295  Q_ASSERT(file->isOpen());
296  Q_ASSERT(file->openMode() & QIODevice::WriteOnly);
297  file->seek(0);
298  QByteArray buffer;
299  buffer.resize(8*1024);
300  if ( ! filterDev->open( QIODevice::ReadOnly ) )
301  {
302  delete filterDev;
303  return false;
304  }
305  qint64 len = -1;
306  while ( !filterDev->atEnd() && len != 0 ) {
307  len = filterDev->read(buffer.data(),buffer.size());
308  if ( len < 0 ) { // corrupted archive
309  delete filterDev;
310  return false;
311  }
312  if ( file->write(buffer.data(), len) != len ) { // disk full
313  delete filterDev;
314  return false;
315  }
316  }
317  filterDev->close();
318  delete filterDev;
319 
320  file->flush();
321  file->seek(0);
322  Q_ASSERT(file->isOpen());
323  Q_ASSERT(file->openMode() & QIODevice::ReadOnly);
324  } else {
325  kDebug(7041) << "no filterdevice found!";
326  }
327 
328  //kDebug( 7041 ) << "filling tmpFile finished.";
329  return true;
330 }
331 
332 bool KTar::openArchive( QIODevice::OpenMode mode ) {
333 
334  if ( !(mode & QIODevice::ReadOnly) )
335  return true;
336 
337  if ( !d->fillTempFile( fileName() ) )
338  return false;
339 
340  // We'll use the permission and user/group of d->rootDir
341  // for any directory we emulate (see findOrCreate)
342  //struct stat buf;
343  //stat( fileName(), &buf );
344 
345  d->dirList.clear();
346  QIODevice* dev = device();
347 
348  if ( !dev )
349  return false;
350 
351  // read dir information
352  char buffer[ 0x200 ];
353  bool ende = false;
354  do
355  {
356  QString name;
357  QString symlink;
358 
359  // Read header
360  qint64 n = d->readHeader( buffer, name, symlink );
361  if (n < 0) return false;
362  if (n == 0x200)
363  {
364  bool isdir = false;
365  bool isGlobalHeader = false;
366 
367  if ( name.endsWith( QLatin1Char( '/' ) ) )
368  {
369  isdir = true;
370  name.truncate( name.length() - 1 );
371  }
372 
373  QByteArray prefix = QByteArray(buffer + 0x159, 155);
374  if (prefix[0] != '\0') {
375  name = (QString::fromLatin1(prefix.constData()) + QLatin1Char('/') + name);
376  }
377 
378  int pos = name.lastIndexOf( QLatin1Char('/') );
379  QString nm = ( pos == -1 ) ? name : name.mid( pos + 1 );
380 
381  // read access
382  buffer[ 0x6b ] = 0;
383  char *dummy;
384  const char* p = buffer + 0x64;
385  while( *p == ' ' ) ++p;
386  int access = (int)strtol( p, &dummy, 8 );
387 
388  // read user and group
389  QString user = QString::fromLocal8Bit( buffer + 0x109 );
390  QString group = QString::fromLocal8Bit( buffer + 0x129 );
391 
392  // read time
393  buffer[ 0x93 ] = 0;
394  p = buffer + 0x88;
395  while( *p == ' ' ) ++p;
396  int time = (int)strtol( p, &dummy, 8 );
397 
398  // read type flag
399  char typeflag = buffer[ 0x9c ];
400  // '0' for files, '1' hard link, '2' symlink, '5' for directory
401  // (and 'L' for longlink fileNames, 'K' for longlink symlink targets)
402  // 'D' for GNU tar extension DUMPDIR, 'x' for Extended header referring
403  // to the next file in the archive and 'g' for Global extended header
404  if ( typeflag == 'g' )
405  isGlobalHeader = true;
406 
407  if ( typeflag == '5' )
408  isdir = true;
409 
410  bool isDumpDir = false;
411  if ( typeflag == 'D' )
412  {
413  isdir = false;
414  isDumpDir = true;
415  }
416  //kDebug(7041) << "typeflag=" << typeflag << " islink=" << ( typeflag == '1' || typeflag == '2' );
417 
418  if (isdir)
419  access |= S_IFDIR; // f*cking broken tar files
420 
421  KArchiveEntry* e;
422  if ( isdir )
423  {
424  //kDebug(7041) << "directory" << nm;
425  e = new KArchiveDirectory( this, nm, access, time, user, group, symlink );
426  }
427  else
428  {
429  // read size
430  QByteArray sizeBuffer( buffer + 0x7c, 12 );
431  qint64 size = sizeBuffer.trimmed().toLongLong( 0, 8 /*octal*/ );
432  //kDebug(7041) << "sizeBuffer='" << sizeBuffer << "' -> size=" << size;
433 
434  // for isDumpDir we will skip the additional info about that dirs contents
435  if ( isDumpDir )
436  {
437  //kDebug(7041) << nm << "isDumpDir";
438  e = new KArchiveDirectory( this, nm, access, time, user, group, symlink );
439  }
440  else
441  {
442 
443  // Let's hack around hard links. Our classes don't support that, so make them symlinks
444  if ( typeflag == '1' )
445  {
446  kDebug(7041) << "Hard link, setting size to 0 instead of" << size;
447  size = 0; // no contents
448  }
449 
450  //kDebug(7041) << "file" << nm << "size=" << size;
451  e = new KArchiveFile( this, nm, access, time, user, group, symlink,
452  dev->pos(), size );
453  }
454 
455  // Skip contents + align bytes
456  qint64 rest = size % 0x200;
457  qint64 skip = size + (rest ? 0x200 - rest : 0);
458  //kDebug(7041) << "pos()=" << dev->pos() << "rest=" << rest << "skipping" << skip;
459  if (! dev->seek( dev->pos() + skip ) )
460  kWarning(7041) << "skipping" << skip << "failed";
461  }
462 
463  if (isGlobalHeader)
464  continue;
465 
466  if ( pos == -1 )
467  {
468  if (nm == QLatin1String(".")) { // special case
469  Q_ASSERT( isdir );
470  if ( isdir )
471  setRootDir( static_cast<KArchiveDirectory *>( e ) );
472  }
473  else
474  rootDir()->addEntry( e );
475  }
476  else
477  {
478  // In some tar files we can find dir/./file => call cleanPath
479  QString path = QDir::cleanPath( name.left( pos ) );
480  // Ensure container directory exists, create otherwise
481  KArchiveDirectory * d = findOrCreate( path );
482  d->addEntry( e );
483  }
484  }
485  else
486  {
487  //qDebug("Terminating. Read %d bytes, first one is %d", n, buffer[0]);
488  d->tarEnd = dev->pos() - n; // Remember end of archive
489  ende = true;
490  }
491  } while( !ende );
492  return true;
493 }
494 
495 /*
496  * Writes back the changes of the temporary file
497  * to the original file.
498  * Must only be called if in write mode, not in read mode
499  */
500 bool KTar::KTarPrivate::writeBackTempFile( const QString & fileName )
501 {
502  if ( !tmpFile )
503  return true;
504 
505  //kDebug(7041) << "Write temporary file to compressed file" << fileName << mimetype;
506 
507  bool forced = false;
508  if (QLatin1String(application_gzip) == mimetype || QLatin1String(application_bzip) == mimetype ||
509  QLatin1String(application_lzma) == mimetype || QLatin1String(application_xz) == mimetype)
510  forced = true;
511 
512  // #### TODO this should use KSaveFile to avoid problems on disk full
513  // (KArchive uses KSaveFile by default, but the temp-uncompressed-file trick
514  // circumvents that).
515 
516  QIODevice *dev = KFilterDev::deviceForFile( fileName, mimetype, forced );
517  if( dev ) {
518  QFile* file = tmpFile;
519  if ( !dev->open(QIODevice::WriteOnly) )
520  {
521  file->close();
522  delete dev;
523  return false;
524  }
525  if ( forced )
526  static_cast<KFilterDev *>(dev)->setOrigFileName( origFileName );
527  file->seek(0);
528  QByteArray buffer;
529  buffer.resize(8*1024);
530  qint64 len;
531  while ( !file->atEnd()) {
532  len = file->read(buffer.data(), buffer.size());
533  dev->write(buffer.data(),len); // TODO error checking
534  }
535  file->close();
536  dev->close();
537  delete dev;
538  }
539 
540  //kDebug(7041) << "Write temporary file to compressed file done.";
541  return true;
542 }
543 
544 bool KTar::closeArchive() {
545  d->dirList.clear();
546 
547  bool ok = true;
548 
549  // If we are in readwrite mode and had created
550  // a temporary tar file, we have to write
551  // back the changes to the original file
552  if (d->tmpFile && (mode() & QIODevice::WriteOnly)) {
553  ok = d->writeBackTempFile( fileName() );
554  delete d->tmpFile;
555  d->tmpFile = 0;
556  setDevice(0);
557  }
558 
559  return ok;
560 }
561 
562 bool KTar::doFinishWriting( qint64 size ) {
563  // Write alignment
564  int rest = size % 0x200;
565  if ( ( mode() & QIODevice::ReadWrite ) == QIODevice::ReadWrite )
566  d->tarEnd = device()->pos() + (rest ? 0x200 - rest : 0); // Record our new end of archive
567  if ( rest )
568  {
569  char buffer[ 0x201 ];
570  for( uint i = 0; i < 0x200; ++i )
571  buffer[i] = 0;
572  qint64 nwritten = device()->write( buffer, 0x200 - rest );
573  return nwritten == 0x200 - rest;
574  }
575  return true;
576 }
577 
578 /*** Some help from the tar sources
579 struct posix_header
580 { byte offset
581  char name[100]; * 0 * 0x0
582  char mode[8]; * 100 * 0x64
583  char uid[8]; * 108 * 0x6c
584  char gid[8]; * 116 * 0x74
585  char size[12]; * 124 * 0x7c
586  char mtime[12]; * 136 * 0x88
587  char chksum[8]; * 148 * 0x94
588  char typeflag; * 156 * 0x9c
589  char linkname[100]; * 157 * 0x9d
590  char magic[6]; * 257 * 0x101
591  char version[2]; * 263 * 0x107
592  char uname[32]; * 265 * 0x109
593  char gname[32]; * 297 * 0x129
594  char devmajor[8]; * 329 * 0x149
595  char devminor[8]; * 337 * ...
596  char prefix[155]; * 345 *
597  * 500 *
598 };
599 */
600 
601 void KTar::KTarPrivate::fillBuffer( char * buffer,
602  const char * mode, qint64 size, time_t mtime, char typeflag,
603  const char * uname, const char * gname ) {
604  // mode (as in stpos())
605  assert( strlen(mode) == 6 );
606  memcpy( buffer+0x64, mode, 6 );
607  buffer[ 0x6a ] = ' ';
608  buffer[ 0x6b ] = '\0';
609 
610  // dummy uid
611  strcpy( buffer + 0x6c, " 765 ");
612  // dummy gid
613  strcpy( buffer + 0x74, " 144 ");
614 
615  // size
616  QByteArray s = QByteArray::number( size, 8 ); // octal
617  s = s.rightJustified( 11, '0' );
618  memcpy( buffer + 0x7c, s.data(), 11 );
619  buffer[ 0x87 ] = ' '; // space-terminate (no null after)
620 
621  // modification time
622  s = QByteArray::number( static_cast<qulonglong>(mtime), 8 ); // octal
623  s = s.rightJustified( 11, '0' );
624  memcpy( buffer + 0x88, s.data(), 11 );
625  buffer[ 0x93 ] = ' '; // space-terminate (no null after) -- well current tar writes a null byte
626 
627  // spaces, replaced by the check sum later
628  buffer[ 0x94 ] = 0x20;
629  buffer[ 0x95 ] = 0x20;
630  buffer[ 0x96 ] = 0x20;
631  buffer[ 0x97 ] = 0x20;
632  buffer[ 0x98 ] = 0x20;
633  buffer[ 0x99 ] = 0x20;
634 
635  /* From the tar sources :
636  Fill in the checksum field. It's formatted differently from the
637  other fields: it has [6] digits, a null, then a space -- rather than
638  digits, a space, then a null. */
639 
640  buffer[ 0x9a ] = '\0';
641  buffer[ 0x9b ] = ' ';
642 
643  // type flag (dir, file, link)
644  buffer[ 0x9c ] = typeflag;
645 
646  // magic + version
647  strcpy( buffer + 0x101, "ustar");
648  strcpy( buffer + 0x107, "00" );
649 
650  // user
651  strcpy( buffer + 0x109, uname );
652  // group
653  strcpy( buffer + 0x129, gname );
654 
655  // Header check sum
656  int check = 32;
657  for( uint j = 0; j < 0x200; ++j )
658  check += buffer[j];
659  s = QByteArray::number( check, 8 ); // octal
660  s = s.rightJustified( 6, '0' );
661  memcpy( buffer + 0x94, s.constData(), 6 );
662 }
663 
664 void KTar::KTarPrivate::writeLonglink(char *buffer, const QByteArray &name, char typeflag,
665  const char *uname, const char *gname) {
666  strcpy( buffer, "././@LongLink" );
667  qint64 namelen = name.length() + 1;
668  fillBuffer( buffer, " 0", namelen, 0, typeflag, uname, gname );
669  q->device()->write( buffer, 0x200 ); // TODO error checking
670  qint64 offset = 0;
671  while (namelen > 0) {
672  int chunksize = qMin(namelen, 0x200LL);
673  memcpy(buffer, name.data()+offset, chunksize);
674  // write long name
675  q->device()->write( buffer, 0x200 ); // TODO error checking
676  // not even needed to reclear the buffer, tar doesn't do it
677  namelen -= chunksize;
678  offset += 0x200;
679  }/*wend*/
680 }
681 
682 bool KTar::doPrepareWriting(const QString &name, const QString &user,
683  const QString &group, qint64 size, mode_t perm,
684  time_t /*atime*/, time_t mtime, time_t /*ctime*/) {
685  if ( !isOpen() )
686  {
687  kWarning(7041) << "You must open the tar file before writing to it\n";
688  return false;
689  }
690 
691  if ( !(mode() & QIODevice::WriteOnly) )
692  {
693  kWarning(7041) << "You must open the tar file for writing\n";
694  return false;
695  }
696 
697  // In some tar files we can find dir/./file => call cleanPath
698  QString fileName ( QDir::cleanPath( name ) );
699 
700  /*
701  // Create toplevel dirs
702  // Commented out by David since it's not necessary, and if anybody thinks it is,
703  // he needs to implement a findOrCreate equivalent in writeDir.
704  // But as KTar and the "tar" program both handle tar files without
705  // dir entries, there's really no need for that
706  QString tmp ( fileName );
707  int i = tmp.lastIndexOf( '/' );
708  if ( i != -1 )
709  {
710  QString d = tmp.left( i + 1 ); // contains trailing slash
711  if ( !m_dirList.contains( d ) )
712  {
713  tmp = tmp.mid( i + 1 );
714  writeDir( d, user, group ); // WARNING : this one doesn't create its toplevel dirs
715  }
716  }
717  */
718 
719  char buffer[ 0x201 ];
720  memset( buffer, 0, 0x200 );
721  if ( ( mode() & QIODevice::ReadWrite ) == QIODevice::ReadWrite )
722  device()->seek(d->tarEnd); // Go to end of archive as might have moved with a read
723 
724  // provide converted stuff we need later on
725  const QByteArray encodedFileName = QFile::encodeName(fileName);
726  const QByteArray uname = user.toLocal8Bit();
727  const QByteArray gname = group.toLocal8Bit();
728 
729  // If more than 100 chars, we need to use the LongLink trick
730  if ( fileName.length() > 99 )
731  d->writeLonglink(buffer,encodedFileName,'L',uname,gname);
732 
733  // Write (potentially truncated) name
734  strncpy( buffer, encodedFileName, 99 );
735  buffer[99] = 0;
736  // zero out the rest (except for what gets filled anyways)
737  memset(buffer+0x9d, 0, 0x200 - 0x9d);
738 
739  QByteArray permstr = QByteArray::number( (unsigned int)perm, 8 );
740  permstr = permstr.rightJustified(6, '0');
741  d->fillBuffer(buffer, permstr, size, mtime, 0x30, uname, gname);
742 
743  // Write header
744  return device()->write( buffer, 0x200 ) == 0x200;
745 }
746 
747 bool KTar::doWriteDir(const QString &name, const QString &user,
748  const QString &group, mode_t perm,
749  time_t /*atime*/, time_t mtime, time_t /*ctime*/) {
750  if ( !isOpen() )
751  {
752  kWarning(7041) << "You must open the tar file before writing to it\n";
753  return false;
754  }
755 
756  if ( !(mode() & QIODevice::WriteOnly) )
757  {
758  kWarning(7041) << "You must open the tar file for writing\n";
759  return false;
760  }
761 
762  // In some tar files we can find dir/./ => call cleanPath
763  QString dirName ( QDir::cleanPath( name ) );
764 
765  // Need trailing '/'
766  if ( !dirName.endsWith( QLatin1Char( '/' ) ) )
767  dirName += QLatin1Char( '/' );
768 
769  if ( d->dirList.contains( dirName ) )
770  return true; // already there
771 
772  char buffer[ 0x201 ];
773  memset( buffer, 0, 0x200 );
774  if ( ( mode() & QIODevice::ReadWrite ) == QIODevice::ReadWrite )
775  device()->seek(d->tarEnd); // Go to end of archive as might have moved with a read
776 
777  // provide converted stuff we need lateron
778  QByteArray encodedDirname = QFile::encodeName(dirName);
779  QByteArray uname = user.toLocal8Bit();
780  QByteArray gname = group.toLocal8Bit();
781 
782  // If more than 100 chars, we need to use the LongLink trick
783  if ( dirName.length() > 99 )
784  d->writeLonglink(buffer,encodedDirname,'L',uname,gname);
785 
786  // Write (potentially truncated) name
787  strncpy( buffer, encodedDirname, 99 );
788  buffer[99] = 0;
789  // zero out the rest (except for what gets filled anyways)
790  memset(buffer+0x9d, 0, 0x200 - 0x9d);
791 
792  QByteArray permstr = QByteArray::number( (unsigned int)perm, 8 );
793  permstr = permstr.rightJustified(6, ' ');
794  d->fillBuffer( buffer, permstr, 0, mtime, 0x35, uname, gname);
795 
796  // Write header
797  device()->write( buffer, 0x200 );
798  if ( ( mode() & QIODevice::ReadWrite ) == QIODevice::ReadWrite )
799  d->tarEnd = device()->pos();
800 
801  d->dirList.append( dirName ); // contains trailing slash
802  return true; // TODO if wanted, better error control
803 }
804 
805 bool KTar::doWriteSymLink(const QString &name, const QString &target,
806  const QString &user, const QString &group,
807  mode_t perm, time_t /*atime*/, time_t mtime, time_t /*ctime*/) {
808  if ( !isOpen() )
809  {
810  kWarning(7041) << "You must open the tar file before writing to it\n";
811  return false;
812  }
813 
814  if ( !(mode() & QIODevice::WriteOnly) )
815  {
816  kWarning(7041) << "You must open the tar file for writing\n";
817  return false;
818  }
819 
820  // In some tar files we can find dir/./file => call cleanPath
821  QString fileName ( QDir::cleanPath( name ) );
822 
823  char buffer[ 0x201 ];
824  memset( buffer, 0, 0x200 );
825  if ( ( mode() & QIODevice::ReadWrite ) == QIODevice::ReadWrite )
826  device()->seek(d->tarEnd); // Go to end of archive as might have moved with a read
827 
828  // provide converted stuff we need lateron
829  QByteArray encodedFileName = QFile::encodeName(fileName);
830  QByteArray encodedTarget = QFile::encodeName(target);
831  QByteArray uname = user.toLocal8Bit();
832  QByteArray gname = group.toLocal8Bit();
833 
834  // If more than 100 chars, we need to use the LongLink trick
835  if (target.length() > 99)
836  d->writeLonglink(buffer,encodedTarget,'K',uname,gname);
837  if ( fileName.length() > 99 )
838  d->writeLonglink(buffer,encodedFileName,'L',uname,gname);
839 
840  // Write (potentially truncated) name
841  strncpy( buffer, encodedFileName, 99 );
842  buffer[99] = 0;
843  // Write (potentially truncated) symlink target
844  strncpy(buffer+0x9d, encodedTarget, 99);
845  buffer[0x9d+99] = 0;
846  // zero out the rest
847  memset(buffer+0x9d+100, 0, 0x200 - 100 - 0x9d);
848 
849  QByteArray permstr = QByteArray::number( (unsigned int)perm, 8 );
850  permstr = permstr.rightJustified(6, ' ');
851  d->fillBuffer(buffer, permstr, 0, mtime, 0x32, uname, gname);
852 
853  // Write header
854  bool retval = device()->write( buffer, 0x200 ) == 0x200;
855  if ( ( mode() & QIODevice::ReadWrite ) == QIODevice::ReadWrite )
856  d->tarEnd = device()->pos();
857  return retval;
858 }
859 
860 void KTar::virtual_hook( int id, void* data ) {
861  KArchive::virtual_hook( id, data );
862 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Thu Feb 21 2013 11:01:08 by doxygen 1.8.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.8.5 API Reference

Skip menu "kdelibs-4.8.5 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal