SVN_SILENT made messages (.desktop file)
[kdeadmin.git] / kdat / Tape.cpp
blob49bfdc16f39cab995977c2bd2a96798284dd0aac
1 // KDat - a tar-based DAT archiver
2 // Copyright (C) 1998-2000 Sean Vyain, svyain@mail.tds.net
3 // Copyright (C) 2001-2002 Lawrence Widman, kdat@cardiothink.com
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <time.h>
22 #include <unistd.h>
23 #include <errno.h>
25 #include <QFile>
26 //Added by qt3to4:
27 #include <QByteArray>
28 #include <Q3PtrList>
30 #include <kmessagebox.h>
31 #include <klocale.h>
32 #include <kstandarddirs.h>
34 #include "IndexDlg.h"
35 #include "KDatMainWindow.h"
36 #include "Options.h"
37 #include "Tape.h"
38 #include "TapeDrive.h"
39 #include "TapeManager.h"
40 #include "kdat.h"
42 Tape::Tape()
43 : _stubbed( FALSE ),
44 _name( i18n( "New Tape" ) ),
45 _size( Options::instance()->getDefaultTapeSize() ),
46 _fptr( 0 )
48 char buffer[1024];
49 gethostname( buffer, 1024 );
50 time_t tm = time( NULL );
51 _ctime = tm;
52 _mtime = tm;
53 _id.sprintf("%s:%d", buffer, tm);
55 write();
58 Tape::Tape( const char * id )
59 : _stubbed( TRUE ),
60 _id( id ),
61 _ctime( -1 ),
62 _mtime( -1 ),
63 _name( "<unknown>" ),
64 _size( -1 ),
65 _fptr( 0 )
69 Tape::~Tape()
71 clear();
74 void Tape::format()
76 // Rewind tape.
77 if ( !TapeDrive::instance()->rewind() ) {
78 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Rewinding tape failed." ), i18n("Format Failed"));
79 return;
82 // Set block size for tape.
83 if ( !TapeDrive::instance()->setBlockSize( Options::instance()->getTapeBlockSize() ) ) {
84 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Cannot set tape block size." ), i18n("Format Failed"));
85 return;
88 // TAPE HEADER
89 int iv;
91 // KDat magic string.
92 if ( TapeDrive::instance()->write( KDAT_MAGIC, KDAT_MAGIC_LENGTH ) < KDAT_MAGIC_LENGTH ) {
93 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Writing magic string failed." ), i18n("Format Failed"));
94 return;
97 // Tape header version number.
98 iv = KDAT_TAPE_HEADER_VERSION;
99 if ( TapeDrive::instance()->write( (const char*)&iv, 4 ) < 4 ) {
100 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Writing version number failed." ), i18n("Format Failed") );
101 return;
104 // Write tape ID. Tape ID is machine name + current time.
105 iv = _id.length() + 1;
106 if ( TapeDrive::instance()->write( (const char*)&iv, 4 ) < 4 ) {
107 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Writing tape ID length failed." ), i18n("Format Failed") );
108 return;
111 #ifdef __GNUC__
112 #warning "Port Qt4: test if toUtf8 is correct"
113 #endif
114 if ( TapeDrive::instance()->write( _id.toUtf8(), iv ) < iv ) {
115 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Writing tape ID failed." ), i18n("Format Failed") );
116 return;
119 // Write end of file marker.
120 TapeDrive::instance()->close();
121 TapeDrive::instance()->open();
123 // Write new tape index.
124 write();
127 void Tape::read()
129 if ( !_stubbed ) {
130 /* 2002-01-28 LEW */
131 // printf("Can't read tape data because tape isn't stubbed\n" );
132 /* 2002-01-28 LEW */
133 return;
136 _stubbed = FALSE;
138 /* 2002-01-28 LEW */
139 // printf("Preparing to read tape data\n" );
140 // QString filename1 = locateLocal( "appdata", _id);
141 // printf("The tape data are in \"%s\"\n", filename1.ascii() );
142 /* 2002-01-28 LEW */
144 if ( !_fptr ) {
145 QString filename = locateLocal( "appdata", _id);
146 _fptr = fopen( QFile::encodeName(filename), "r" );
148 /* 2002-01-28 LEW */
149 #ifdef DEBUG
150 // printf("Opened tape archive file \"%s\". %s %d\n",
151 // filename.ascii(), __FILE__, __LINE__ );
152 #endif /* DEBUG */
153 /* 2002-01-28 LEW */
157 if ( !_fptr ) {
158 // No tape index file was found.
159 int result = KMessageBox::warningContinueCancel( KDatMainWindow::getInstance(),
160 i18n( "No index file was found for this tape.\n"
161 "Recreate the index from tape?" ),
162 i18n("Tape Index"),
163 i18n("Recreate"));
164 if (result == KMessageBox::Continue ) {
165 write();
167 IndexDlg dlg( this, KDatMainWindow::getInstance() );
168 dlg.exec();
169 TapeManager::instance()->addTape( this );
170 return;
171 } else {
172 return;
176 char buf[4096];
178 fseek( _fptr, 0, SEEK_SET );
180 // Read index file version number.
181 if ( !fgets( buf, 4096, _fptr ) ) {
182 fclose( _fptr );
183 KMessageBox::error( KDatMainWindow::getInstance(),
184 i18n( "Reading version number failed." ),
185 i18n("Index File Error") );
186 return;
188 int version = atoi( buf );
190 switch ( version ) {
191 case 1:
192 readVersion1Index( _fptr );
193 readAll( version );
194 calcRanges();
195 fclose( _fptr );
196 _fptr = NULL;
197 write();
198 break;
200 case 2:
201 readVersion2Index( _fptr );
202 readAll( version );
203 calcRanges();
204 fclose( _fptr );
205 _fptr = NULL;
206 write();
207 break;
209 case 3:
210 readVersion3Index( _fptr );
211 readAll( version );
212 calcRanges();
213 fclose( _fptr );
214 _fptr = NULL;
215 write();
216 break;
218 case 4:
219 readVersion4Index( _fptr );
220 break;
222 default:
224 KMessageBox::sorry( KDatMainWindow::getInstance(),
225 i18n( "The tape index file format is version %1. The index cannot be read by this version of KDat. Perhaps the tape index file was created by a newer version of KDat?" , version ),
226 i18n("Tape Index") );
231 void Tape::readAll( int version )
233 read();
235 Q3PtrListIterator<Archive> i( _children );
236 for ( ; i.current(); ++i ) {
237 i.current()->readAll( version );
241 void Tape::write()
243 QString filename = locateLocal( "appdata", _id);
245 /* 2002-01-28 LEW */
246 #ifdef DEBUG
247 // printf("Preparing to write the archive data to \"%s\". %s %d\n",
248 // filename.ascii(), __FILE__, __LINE__ );
249 #endif /* DEBUG */
250 /* 2002-01-28 LEW */
252 if ( !_fptr ) {
253 _fptr = fopen( QFile::encodeName(filename), "w" );
255 /* 2002-01-31 LEW */
256 #ifdef DEBUG
257 // printf("Opened new archive file \"%s\". %s %d\n",
258 // filename.ascii(), __FILE__, __LINE__ );
259 #endif /* DEBUG */
260 /* 2002-01-31 LEW */
262 if ( !_fptr ) {
263 // Suck!
264 printf( "Tape::write() -- cannot open '%s' for writing!\n", filename.ascii() );
265 return;
267 } else {
268 freopen( QFile::encodeName(filename), "w", _fptr );
269 /* 2002-01-31 LEW */
270 #ifdef DEBUG
271 // printf("Reopened new archive file \"%s\". %s %d\n",
272 // filename.ascii(), __FILE__, __LINE__ );
273 #endif /* DEBUG */
274 /* 2002-01-31 LEW */
277 int zero = 0;
279 //===== Write the tape data =====
281 fprintf( _fptr, "%d\n", KDAT_INDEX_FILE_VERSION );
282 fprintf( _fptr, "%s\n", _id.data() );
284 fwrite( &_ctime, sizeof( _ctime ), 1, _fptr );
285 fwrite( &_mtime, sizeof( _mtime ), 1, _fptr );
286 fwrite( &_size , sizeof( _size ), 1, _fptr );
287 char buf[4096];
288 memset( buf, 0, 4096 );
289 memcpy( buf, _name.ascii(), _name.length() > 4095 ? 4095 : _name.length() );
290 fwrite( buf, sizeof( char ), 4096, _fptr );
291 int ival = _children.count();
292 fwrite( &ival, sizeof( ival ), 1, _fptr );
294 // Fill in the archive offsets later...
295 int archiveTable = ftell( _fptr );
296 for ( uint i = 0; i < MAX_NUM_ARCHIVES; i++ ) {
297 fwrite( &zero, sizeof( zero ), 1, _fptr );
300 //===== Write archives =====
301 Q3PtrListIterator<Archive> i( _children );
302 int count = 0;
303 for ( ; i.current(); ++i, count++ ) {
304 // Fill in the file offset.
305 int here = ftell( _fptr );
306 fseek( _fptr, archiveTable + 4*count, SEEK_SET );
307 fwrite( &here, sizeof( here ), 1, _fptr );
308 fseek( _fptr, here, SEEK_SET );
310 i.current()->write( _fptr );
313 freopen( QFile::encodeName(filename), "r+", _fptr );
316 QString Tape::getID()
318 read();
320 return _id;
323 QString Tape::getName()
325 read();
327 return _name;
330 int Tape::getCTime()
332 read();
334 return _ctime;
337 int Tape::getMTime()
339 read();
341 return _mtime;
344 int Tape::getSize()
346 read();
348 return _size;
351 const Q3PtrList<Archive>& Tape::getChildren()
353 read();
355 return _children;
358 void Tape::setName( const QString & name )
360 /* 2002-01-31 LEW */
361 int i;
362 /* 2002-01-31 LEW */
364 read();
366 if ( !_fptr ) {
367 // Create a new empty index file.
368 write();
371 // change file to read-write so we can update it 2002-01-31 LEW
372 QString filename = locateLocal( "appdata", _id);
373 freopen( QFile::encodeName(filename), "r+", _fptr );
375 _name = name;
377 char buf[4096];
378 if( fseek( _fptr, 0, SEEK_SET ) < 0 )
380 clearerr( _fptr );
381 #ifdef DEBUG
382 QString msg = i18n("Error during fseek #1 while accessing archive: \"%1\": %2").arg( getID() ).arg( i18n(strerror( errno )) );
383 printf("%s\n", msg.toLatin1());
384 KMessageBox::error(NULL, msg, i18n("File Access Error"));
385 #endif /* DEBUG */
386 return;
388 if( fgets( buf, 4096, _fptr ) == NULL )
390 clearerr( _fptr );
391 #ifdef DEBUG
392 QString msg = i18n("Error while accessing string #1 in archive: \"%1\": %2").arg( getID() ).arg(i18n(strerror( errno )));
393 printf("%s\n", msg.toLatin1());
394 KMessageBox::error(NULL, msg, i18n("File Access Error"));
395 #endif /* DEBUG */
396 return;
398 if( fgets( buf, 4096, _fptr ) == NULL )
400 clearerr( _fptr );
401 #ifdef DEBUG
402 QString msg = i18n("Error while accessing string #2 in archive: \"%1\": %2").arg( getID() ).arg(i18n(strerror( errno )));
403 printf("%s\n", msg.toLatin1());
404 KMessageBox::error(NULL, msg, i18n("File Access Error"));
405 #endif /* DEBUG */
406 return;
408 if( fseek( _fptr, 12, SEEK_CUR ) < 0 )
410 clearerr( _fptr );
411 #ifdef DEBUG
412 QString msg = i18n("Error during fseek #2 while accessing archive: \"%1\": %2").arg( getID() ).arg(i18n(strerror( errno )));
413 printf("%s\n", msg.toLatin1());
414 KMessageBox::error(NULL, msg, i18n("File Access Error"));
415 #endif /* DEBUG */
416 return;
418 memset( buf, 0, 4096 );
419 memcpy( buf, _name.ascii(), _name.length() > 4095 ? 4095 : _name.length() );
420 i = fwrite( buf, sizeof( char ), 4096, _fptr );
421 if( ( i < 4096 ) || ( ferror( _fptr ) != 0 )){
422 clearerr( _fptr );
423 QString msg = i18n("Error while updating archive name: ");
424 msg.append( i18n(strerror( errno )) );
425 printf("%s\n", msg.latin1());
426 KMessageBox::error(NULL, msg, i18n("File Access Error"));
427 // return;
428 /* 2002-01-31 LEW */
430 fflush( _fptr );
432 /* 2002-01-31 LEW */
433 #ifdef DEBUG
434 // printf("Tape::setName. wrote \"%s\" (%d bytes) into archive ID %s. %s %d\n",
435 // _name.ascii(), _name.length(), getID().toLatin1(), __FILE__, __LINE__);
436 // printf(" The buffer size is 4096. %d bytes were written\n", i);
437 #endif /* DEBUG */
438 /* 2002-01-31 LEW */
440 // change back to read-only 2002-01-31 LEW
441 freopen( QFile::encodeName(filename), "r", _fptr );
443 TapeManager::instance()->tapeModified( this );
446 void Tape::setMTime( int mtime )
448 read();
450 if ( !_fptr ) {
451 // Create a new empty index file.
452 write();
455 // change file to read-write so we can update it 2002-01-31 LEW
456 QString filename = locateLocal( "appdata", _id);
457 freopen( QFile::encodeName(filename), "r+", _fptr );
459 _mtime = mtime;
461 char buf[4096];
462 fseek( _fptr, 0, SEEK_SET );
463 fgets( buf, 4096, _fptr );
464 fgets( buf, 4096, _fptr );
465 fseek( _fptr, 4, SEEK_CUR );
466 fwrite( &_mtime, sizeof( _mtime ), 1, _fptr );
467 fflush( _fptr );
469 // change back to read-only 2002-01-31 LEW
470 freopen( QFile::encodeName(filename), "r", _fptr );
472 TapeManager::instance()->tapeModified( this );
475 void Tape::setSize( int size )
477 read();
479 if ( !_fptr ) {
480 // Create a new empty index file.
481 write();
484 _size = size;
486 // change file to read-write so we can update it 2002-01-31 LEW
487 QString filename = locateLocal( "appdata", _id);
488 freopen( QFile::encodeName(filename), "r+", _fptr );
490 char buf[4096];
491 fseek( _fptr, 0, SEEK_SET );
492 fgets( buf, 4096, _fptr );
493 fgets( buf, 4096, _fptr );
494 fseek( _fptr, 8, SEEK_CUR );
495 fwrite( &_size, sizeof( _size ), 1, _fptr );
496 fflush( _fptr );
498 // change back to read-only 2002-01-31 LEW
499 freopen( QFile::encodeName(filename), "r", _fptr );
501 TapeManager::instance()->tapeModified( this );
504 void Tape::addChild( Archive* archive )
506 /* 2002-01-28 LEW */
507 // printf("Preparing to add archive to tape file\n" );
508 /* 2002-01-28 LEW */
510 read();
512 // change file to read-write so we can update it 2002-01-31 LEW
513 QString filename = locateLocal( "appdata", _id);
514 freopen( QFile::encodeName(filename), "r+", _fptr );
516 archive->calcRanges();
518 _children.append( archive );
520 char buf[4096];
521 fseek( _fptr, 0, SEEK_END );
522 int here = ftell( _fptr );
523 fseek( _fptr, 0, SEEK_SET );
524 fgets( buf, 4096, _fptr );
525 fgets( buf, 4096, _fptr );
526 fseek( _fptr, 12 + 4096, SEEK_CUR );
527 int ival = _children.count();
528 fwrite( &ival, sizeof( ival ), 1, _fptr );
529 fseek( _fptr, ( _children.count() - 1 ) * 4, SEEK_CUR );
530 fwrite( &here, sizeof( here ), 1, _fptr );
531 fseek( _fptr, here, SEEK_SET );
532 archive->write( _fptr );
533 fflush( _fptr );
535 setMTime( time( NULL ) );
537 // change back to read-only 2002-01-31 LEW
538 freopen( QFile::encodeName(filename), "r", _fptr );
540 /* 2002-01-28 LEW */
541 // printf("Done adding archive to tape file\n" );
542 /* 2002-01-28 LEW */
545 void Tape::removeChild( Archive* archive )
547 read();
549 while ( _children.last() != archive ) {
550 _children.removeLast();
552 _children.removeLast();
554 char buf[4096];
555 fseek( _fptr, 0, SEEK_SET );
556 fgets( buf, 4096, _fptr );
557 fgets( buf, 4096, _fptr );
558 fseek( _fptr, 12 + 4096, SEEK_CUR );
559 int ival = _children.count();
560 fwrite( &ival, sizeof( ival ), 1, _fptr );
561 int here;
562 if ( ival > 0 ) {
563 fseek( _fptr, _children.count() * 4, SEEK_CUR );
564 fread( &here, sizeof( here ), 1, _fptr );
565 } else {
566 fseek( _fptr, MAX_NUM_ARCHIVES * 4, SEEK_CUR );
567 here = ftell( _fptr );
570 QString filename = locateLocal( "appdata", _id);
571 truncate( QFile::encodeName(filename), here );
572 freopen( QFile::encodeName(filename), "r+", _fptr );
573 fflush( _fptr );
575 // change back to read-only 2002-01-31 LEW
576 freopen( QFile::encodeName(filename), "r", _fptr );
578 setMTime( time( NULL ) );
581 void Tape::clear()
583 if ( _children.count() < 1 ) {
584 return;
587 while ( _children.first() ) {
588 delete _children.first();
589 _children.removeFirst();
592 char buf[4096];
593 fseek( _fptr, 0, SEEK_SET );
594 fgets( buf, 4096, _fptr );
595 fgets( buf, 4096, _fptr );
596 fseek( _fptr, 12 + 4096, SEEK_CUR );
597 int ival = _children.count();
598 fwrite( &ival, sizeof( ival ), 1, _fptr );
599 fseek( _fptr, MAX_NUM_ARCHIVES * 4, SEEK_CUR );
600 int here = ftell( _fptr );
602 QString filename = locateLocal( "appdata", _id);
603 truncate( QFile::encodeName(filename), here );
604 freopen( QFile::encodeName(filename), "r+", _fptr );
605 fflush( _fptr );
607 // change back to read-only 2002-01-31 LEW
608 freopen( QFile::encodeName(filename), "r", _fptr );
610 setMTime( time( NULL ) );
613 void Tape::readVersion1Index( FILE* fptr )
615 fseek( fptr, 0, SEEK_CUR );
617 char buf[4096];
619 // Read the tapeID.
620 if ( !fgets( buf, 4096, fptr ) ) {
621 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading tape ID failed." ), i18n("Index File Error") );
622 return;
624 QByteArray tapeID = buf;
625 tapeID.truncate( tapeID.length() - 1 );
626 if ( tapeID !=_id ) {
627 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Tape ID on tape does not match tape ID in index file." ), i18n("Index File Error") );
628 return;
631 // Read creation time.
632 if ( !fgets( buf, 4096, fptr ) ) {
633 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading creation time failed." ), i18n("Index File Error") );
634 return;
636 _ctime = atoi( buf );
638 // Read modification time.
639 if ( !fgets( buf, 4096, fptr ) ) {
640 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading modification time failed." ), i18n("Index File Error") );
641 return;
643 _mtime = atoi( buf );
645 // Read tape name.
646 if ( !fgets( buf, 4096, fptr ) ) {
647 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading tape name failed." ), i18n("Index File Error") );
648 return;
650 _name = buf;
651 _name.truncate( _name.length() - 1 );
653 // Read tape size.
654 if ( !fgets( buf, 4096, fptr ) ) {
655 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading tape size failed." ), i18n("Index File Error") );
656 return;
658 _size = atoi( buf );
660 // Read number of archives.
661 if ( !fgets( buf, 4096, fptr ) ) {
662 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading archive count failed." ), i18n("Index File Error") );
663 return;
665 int numArchives = atoi( buf );
667 for ( ; numArchives; numArchives-- ) {
668 // Read archive name.
669 if ( !fgets( buf, 4096, fptr ) ) {
670 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading archive name failed." ), i18n("Index File Error") );
671 return;
673 QString archiveName = buf;
674 archiveName.truncate( archiveName.length() - 1 );
676 // Read archive time stamp.
677 if ( !fgets( buf, 4096, fptr ) ) {
678 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading archive time stamp failed." ), i18n("Index File Error") );
679 return;
681 int archiveTimeStamp = atoi( buf );
683 // Read archive start block.
684 if ( !fgets( buf, 4096, fptr ) ) {
685 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading archive start block failed." ), i18n("Index File Error") );
686 return;
688 int archiveStartBlock = atoi( buf );
690 // Read archive end block.
691 if ( !fgets( buf, 4096, fptr ) ) {
692 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading archive end block failed." ), i18n("Index File Error") );
693 return;
695 int archiveEndBlock = atoi( buf );
697 // Create the archive.
698 Archive* archive = new Archive( this, archiveTimeStamp, archiveName );
699 archive->setEndBlock( archiveEndBlock - archiveStartBlock );
701 _children.append( archive );
703 // Read the number of files.
704 if ( !fgets( buf, 4096, fptr ) ) {
705 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading archive file count failed." ), i18n("Index File Error") );
706 return;
708 int archiveNumFiles = atoi( buf );
710 QString tmpFileName;
711 int tmpFileSize = -1;
712 int tmpFileMTime = -1;
713 int tmpFileStartRecord = -1;
714 for ( ; archiveNumFiles; archiveNumFiles-- ) {
715 // Read file name.
716 if ( !fgets( buf, 4096, fptr ) ) {
717 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading file name failed." ), i18n("Index File Error") );
718 return;
720 QString fileName = buf;
721 fileName.truncate( fileName.length() - 1 );
723 // Read file size.
724 if ( !fgets( buf, 4096, fptr ) ) {
725 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading file size failed." ), i18n("Index File Error") );
726 return;
728 int fileSize = atoi( buf );
730 // Read file modification time.
731 if ( !fgets( buf, 4096, fptr ) ) {
732 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading file modification time failed." ), i18n("Index File Error") );
733 return;
735 int fileModTime = atoi( buf );
737 // Read file record number.
738 if ( !fgets( buf, 4096, fptr ) ) {
739 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading file record number failed." ), i18n("Index File Error") );
740 return;
742 int fileRecord = atoi( buf );
744 if ( tmpFileName.length() > 0 ) {
745 archive->addFile( tmpFileSize, tmpFileMTime, tmpFileStartRecord, fileRecord, tmpFileName );
748 tmpFileName = fileName.copy();
749 tmpFileSize = fileSize;
750 tmpFileMTime = fileModTime;
751 tmpFileStartRecord = fileRecord;
754 if ( tmpFileName.length() > 0 ) {
755 archive->addFile( tmpFileSize, tmpFileMTime, tmpFileStartRecord, archive->getEndBlock() * ( Options::instance()->getTapeBlockSize() / 512 ), tmpFileName );
760 void Tape::readVersion2Index( FILE* fptr )
762 fseek( fptr, 0, SEEK_CUR );
764 char buf[4096];
766 // Read the tapeID.
767 if ( !fgets( buf, 4096, fptr ) ) {
768 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading tape ID failed." ), i18n("Index File Error") );
769 return;
771 QByteArray tapeID = buf;
772 tapeID.truncate( tapeID.length() - 1 );
773 if ( tapeID != _id ) {
774 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Tape ID on tape does not match tape ID in index file." ), i18n("Index File Error") );
775 return;
778 // Read creation time.
779 if ( !fgets( buf, 4096, fptr ) ) {
780 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading creation time failed." ), i18n("Index File Error") );
781 return;
783 _ctime = atoi( buf );
785 // Read modification time.
786 if ( !fgets( buf, 4096, fptr ) ) {
787 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading modification time failed." ), i18n("Index File Error") );
788 return;
790 _mtime = atoi( buf );
792 // Read tape name.
793 if ( !fgets( buf, 4096, fptr ) ) {
794 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading tape name failed." ), i18n("Index File Error") );
795 return;
797 _name = buf;
798 _name.truncate( _name.length() - 1 );
800 // Read tape size.
801 if ( !fgets( buf, 4096, fptr ) ) {
802 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading tape size failed." ), i18n("Index File Error") );
803 return;
805 _size = atoi( buf );
807 // Read number of archives.
808 if ( !fgets( buf, 4096, fptr ) ) {
809 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading archive count failed." ), i18n("Index File Error") );
810 return;
812 int numArchives = atoi( buf );
814 int ival;
815 for ( ; numArchives; numArchives-- ) {
816 fread( &ival, sizeof( ival ), 1, fptr );
817 _children.append( new Archive( this, fptr, ival ) );
821 void Tape::readVersion3Index( FILE* fptr )
823 fseek( fptr, 0, SEEK_CUR );
825 char buf[4097];
826 buf[4096] = '\0';
828 // Read the tapeID.
829 if ( !fgets( buf, 4096, fptr ) ) {
830 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading tape ID failed." ), i18n("Index File Error") );
831 return;
833 QByteArray tapeID = buf;
834 tapeID.truncate( tapeID.length() - 1 );
835 if ( tapeID != _id ) {
836 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Tape ID on tape does not match tape ID in index file." ), i18n("Index File Error") );
837 return;
840 // Read creation time.
841 fread( &_ctime, sizeof( _ctime ), 1, fptr );
843 // Read modification time.
844 fread( &_mtime, sizeof( _mtime ), 1, fptr );
846 // Read tape size.
847 fread( &_size, sizeof( _size ), 1, fptr );
849 // Read tape name.
850 fread( buf, sizeof( char ), 4096, fptr );
851 _name = buf;
853 // Read number of archives.
854 int numArchives;
855 fread( &numArchives, sizeof( numArchives ), 1, fptr );
857 int ival;
858 for ( ; numArchives; numArchives-- ) {
859 fread( &ival, sizeof( ival ), 1, fptr );
860 _children.append( new Archive( this, fptr, ival ) );
864 void Tape::readVersion4Index( FILE* fptr )
866 fseek( fptr, 0, SEEK_CUR );
868 char buf[4097];
869 buf[4096] = '\0';
871 // Read the tapeID.
872 if ( !fgets( buf, 4096, fptr ) ) {
873 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Reading tape ID failed." ), i18n("Index File Error") );
874 return;
876 QByteArray tapeID = buf;
877 tapeID.truncate( tapeID.length() - 1 );
878 if ( tapeID != _id ) {
879 KMessageBox::error( KDatMainWindow::getInstance(), i18n( "Tape ID on tape does not match tape ID in index file." ), i18n("Index File Error") );
880 return;
883 // Read creation time.
884 fread( &_ctime, sizeof( _ctime ), 1, fptr );
886 // Read modification time.
887 fread( &_mtime, sizeof( _mtime ), 1, fptr );
889 // Read tape size.
890 fread( &_size, sizeof( _size ), 1, fptr );
892 // Read tape name.
893 fread( buf, sizeof( char ), 4096, fptr );
894 _name = buf;
896 // Read number of archives.
897 int numArchives;
898 fread( &numArchives, sizeof( numArchives ), 1, fptr );
900 int ival;
901 for ( ; numArchives; numArchives-- ) {
902 fread( &ival, sizeof( ival ), 1, fptr );
903 _children.append( new Archive( this, fptr, ival ) );
906 /* 2002-01-31 LEW */
907 #ifdef DEBUG
908 printf("Read contents of archive file \"%s\". %s %d\n",
909 _name.toLatin1(), __FILE__, __LINE__ );
910 #endif /* DEBUG */
911 /* 2002-01-31 LEW */
914 void Tape::calcRanges()
916 Q3PtrListIterator<Archive> it( getChildren() );
917 for ( ; it.current(); ++it ) {
918 it.current()->calcRanges();