summaryrefslogtreecommitdiffstats
path: root/kdat/BackupDlg.cpp
blob: 45b864cb92e7d1ed94513f1a5ee537715d17b908 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// $Id$
//
// KDat - a tar-based DAT archiver
// Copyright (C) 1998-2000  Sean Vyain, svyain@mail.tds.net
// Copyright (C) 2001-2002  Lawrence Widman, kdat@cardiothink.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#include <assert.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/errno.h>
#include <sys/stat.h>

#include <tqdir.h>
#include <tqlabel.h>
#include <layout.h>
#include <kpushbutton.h>
#include <kstdguiitem.h>

#include <kapplication.h>
#include <kprocess.h>
#include <kmessagebox.h>
#include <kpushbutton.h>
#include <kstdguiitem.h>

#include "Archive.h"
#include "BackupDlg.h"
#include "LoggerWidget.h"
#include "Options.h"
#include "Tape.h"
#include "TapeDrive.h"
#include "TarParser.h"
#include "Util.h"
#include <klocale.h>

#include "BackupDlg.moc"

BackupDlg::BackupDlg( const TQString & archiveName, const TQString & workingDir, const TQStringList& files, bool oneFilesystem, bool incremental,
                      const TQString & snapshot, bool removeSnapshot, int archiveSize, Tape* tape,
                      TQWidget* parent, const char* name )
        : TQDialog( parent, name, TRUE ),
          _proc( NULL ),
          _tarParser( NULL ),
          _archiveName( archiveName ),
          _workingDir( workingDir ),
          _files(files),
          _oneFilesystem( oneFilesystem ),
          _incremental( incremental ),
          _snapshot( snapshot ),
          _removeSnapshot( removeSnapshot ),
          _archiveSize( archiveSize ),
          _tape( tape ),
          _totalKBytes( 0.0 ),
          _totalRecords( 0 ),
          _startTime( 0 ),
          _archive( NULL ),
          _aborted( FALSE ),
          _numFiles( 0 ),
          _fileSize( -1 ),
          _fileMTime( -1 ),
          _fileStartRecord( -1 )
{
    // Copy the list of files to archive.

    setCaption( i18n( "KDat: Backup" ) );
    setIconText( i18n( "KDat: Backup" ) );

    resize( 515, 300 );

    /* 2002-01-26 LEW: "Time remaining" was cut off in mid-"g" 
       so we'll provide that plus some space beyond it. */
    // const int labelWidth = 96;
    const int labelWidth = 110;

    TQFrame* f1 = new TQFrame( this );
    f1->setFrameStyle( TQFrame::Panel | TQFrame::Sunken );

    TQFrame* f2 = new TQFrame( this );
    f2->setFrameStyle( TQFrame::Panel | TQFrame::Sunken );

    TQLabel* lbl1 = new TQLabel( i18n( "Elapsed time:" ), f1 );
    lbl1->setFixedSize( labelWidth, lbl1->sizeHint().height() );

    _elapsedTime = new TQLabel( i18n( "00:00:00" ), f1 );
    _elapsedTime->setFixedHeight( _elapsedTime->sizeHint().height() );

    TQLabel* lbl2 = new TQLabel( i18n( "Time remaining:" ), f2 );
    lbl2->setFixedSize( labelWidth, lbl2->sizeHint().height() );

    _timeRemaining = new TQLabel( i18n( "00:00:00" ), f2 );
    _timeRemaining->setFixedHeight( _timeRemaining->sizeHint().height() );

    TQLabel* lbl3 = new TQLabel( i18n( "Total KB:" ), f1 );
    lbl3->setFixedSize( labelWidth, lbl3->sizeHint().height() );

    TQLabel* totalKbytes = new TQLabel( Util::kbytesToString( archiveSize ), f1 );
    totalKbytes->setFixedHeight( totalKbytes->sizeHint().height() );

    TQLabel* lbl4 = new TQLabel( i18n( "KB written:" ), f2 );
    lbl4->setFixedSize( labelWidth, lbl4->sizeHint().height() );

    _kbytesWritten = new TQLabel( i18n( "0KB" ), f2 );
    _kbytesWritten->setFixedHeight( _kbytesWritten->sizeHint().height() );

    TQLabel* lbl5 = new TQLabel( i18n( "Transfer rate:" ), f1 );
    lbl5->setFixedSize( labelWidth, lbl5->sizeHint().height() );

    _transferRate = new TQLabel( i18n( "0KB/min" ), f1 );
    _transferRate->setFixedHeight( _transferRate->sizeHint().height() );

    TQLabel* lbl6 = new TQLabel( i18n( "Files:" ), f2 );
    lbl6->setFixedSize( labelWidth, lbl6->sizeHint().height() );

    _fileCount = new TQLabel( i18n( "0" ), f2 );
    _fileCount->setFixedHeight( _fileCount->sizeHint().height() );

    _log = new LoggerWidget( i18n( "Backup log:" ), this );

    _ok = new KPushButton( KStdGuiItem::ok(), this );
    _ok->setFixedSize( 80, _ok->sizeHint().height() );
    connect( _ok, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotOK() ) );
    _ok->setEnabled( FALSE );

    _save = new TQPushButton( i18n( "Save Log..." ), this );
    _save->setFixedSize( 80, _save->sizeHint().height() );
    connect( _save, TQT_SIGNAL( clicked() ), _log, TQT_SLOT( save() ) );
    _save->setEnabled( FALSE );

    _abort = new KPushButton( KStdGuiItem::cancel(), this );
    _abort->setFixedSize( 80, _abort->sizeHint().height() );
    connect( _abort, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotAbort() ) );

    TQVBoxLayout* l1 = new TQVBoxLayout( this, 8, 4 );

    TQHBoxLayout* l1_1 = new TQHBoxLayout();
    l1->addLayout( l1_1 );
    l1_1->addStrut( 3 * lbl1->height() + 16 );
    l1_1->addWidget( f1 );
    l1_1->addWidget( f2 );

    TQVBoxLayout* l1_1_1 = new TQVBoxLayout( f1, 4, 4 );

    TQHBoxLayout* l1_1_1_1 = new TQHBoxLayout();
    l1_1_1->addLayout( l1_1_1_1 );
    l1_1_1_1->addWidget( lbl1 );
    l1_1_1_1->addWidget( _elapsedTime, 1 );

    TQHBoxLayout* l1_1_1_2 = new TQHBoxLayout();
    l1_1_1->addLayout( l1_1_1_2 );
    l1_1_1_2->addWidget( lbl3 );
    l1_1_1_2->addWidget( totalKbytes, 1 );

    TQHBoxLayout* l1_1_1_3 = new TQHBoxLayout();
    l1_1_1->addLayout( l1_1_1_3 );
    l1_1_1_3->addWidget( lbl5 );
    l1_1_1_3->addWidget( _transferRate, 1 );

    TQVBoxLayout* l1_1_2 = new TQVBoxLayout( f2, 4, 4 );

    TQHBoxLayout* l1_1_2_1 = new TQHBoxLayout();
    l1_1_2->addLayout( l1_1_2_1 );
    l1_1_2_1->addWidget( lbl2 );
    l1_1_2_1->addWidget( _timeRemaining, 1 );

    TQHBoxLayout* l1_1_2_2 = new TQHBoxLayout();
    l1_1_2->addLayout( l1_1_2_2 );
    l1_1_2_2->addWidget( lbl4 );
    l1_1_2_2->addWidget( _kbytesWritten, 1 );

    TQHBoxLayout* l1_1_2_3 = new TQHBoxLayout();
    l1_1_2->addLayout( l1_1_2_3 );
    l1_1_2_3->addWidget( lbl6 );
    l1_1_2_3->addWidget( _fileCount, 1 );

    l1->addWidget( _log, 1 );

    TQHBoxLayout* l1_2 = new TQHBoxLayout();
    l1->addLayout( l1_2 );
    l1_2->addStretch( 1 );
    l1_2->addWidget( _ok );
    l1_2->addWidget( _save );
    l1_2->addWidget( _abort );
}

BackupDlg::~BackupDlg()
{
    delete _tarParser;
}

void BackupDlg::show()
{
    bool bGoOn = false;

    _archive = new Archive( _tape, time( NULL ), _archiveName.utf8() );

    chdir( TQFile::encodeName(_workingDir) );

    if ( _removeSnapshot ) {
        unlink( TQFile::encodeName(_snapshot) );
    }

    _tarParser = new TarParser();
    connect( _tarParser, TQT_SIGNAL( sigEntry( const TQString &, int, int, int ) ), this, TQT_SLOT( slotEntry( const TQString &, int, int, int ) ) );

    _proc = new KProcess();
    *_proc << Options::instance()->getTarCommand();
    if ( _oneFilesystem ) {
        *_proc << "-l";
    }
    if ( _incremental ) {
        *_proc << "-g" << _snapshot;
    }
    *_proc << "-Spcf" << "-";

    // Append the list of files to archive.
    if ( _files.count() == 1 && _files.first() == "." ) {
        dev_t device = 0;
        struct stat info;
        if ( _oneFilesystem ) {
            if ( lstat( ".", &info ) == 0 ) {
                device = info.st_dev;
            }
        }

        // Backup all files in current working directory.
        TQDir dir;
	//roland
        //TQStringList::Iterator i = dir.entryList( TQDir::All, TQDir::Name | TQDir::DirsFirst ).begin();
        TQStringList FilesList = dir.entryList( TQDir::All, TQDir::Name | TQDir::DirsFirst );
        TQStringList::Iterator i = FilesList.begin();
	//roland
        for ( ; !(*i).isNull() ; ++i ) {
            if ( *i != "." && *i != ".." ) {
                if ( _oneFilesystem ) {
                    if ( lstat( TQFile::encodeName(*i), &info ) == 0 )
                    {
                        if ( info.st_dev == device ) {
                            *_proc << *i;
			    bGoOn = true;
                        }
                    }
                } else {
                    *_proc << *i;
		    bGoOn = true;
                }
            }
        }
    } else {
        // Backup listed files only.
      /* 2002-01-28 LEW */
      // printf("Fixing to list the files/dirs to be dumped:\n");
      /* 2002-01-28 LEW */
        for ( TQStringList::Iterator it = _files.begin();
              it != _files.end();
              ++it ) {
	  /* 2002-01-28 LEW */
	  // printf("tar argument: \"%s\"\n", (*it).latin1());
	  /* 2002-01-28 LEW */
            *_proc << *it;
	    bGoOn = true;
	}
    }

    if (bGoOn == false) {
        KMessageBox::information(this, i18n("No files to back up. Aborting."));
        slotAbort();
        return;
    }          

    connect( _proc, TQT_SIGNAL( processExited( KProcess* ) ), this, TQT_SLOT( slotProcessExited( KProcess* ) ) );
    connect( _proc, TQT_SIGNAL( receivedStdout( KProcess*, char*, int ) ), this, TQT_SLOT( slotStdout( KProcess*, char*, int ) ) );

    startTimer( 1000 );

    _proc->start( KProcess::NotifyOnExit, KProcess::Stdout );

    TQDialog::show();
}

void BackupDlg::slotProcessExited( KProcess* )
{
    updateStats();

    _archive->setEndBlock( _totalRecords / ( Options::instance()->getTapeBlockSize() / 512 ) );

    if ( _fileName.length() > 0 ) {
        _archive->addFile( _fileSize, _fileMTime, _fileStartRecord, _totalRecords, _fileName );
        _fileName = TQString();
    }

    TapeDrive::instance()->close();
    TapeDrive::instance()->open();

    TQT_TQOBJECT(this)->killTimers();
    delete _proc;

    _tape->addChild( _archive );

    _ok->setEnabled( TRUE );
    _ok->setDefault( TRUE );
    _save->setEnabled( TRUE );
    _abort->setEnabled( FALSE );
}

// the KProcess passes the arguments to tar, and tar's output is piped here.
// The output is shown to _tarParser->slotData(), which figures out which files
// are going to tape and their file parameters, and saves these data into
// kdat's archive.  The raw data are then written to tape with write().
// 2002-01-27 LEW
void BackupDlg::slotStdout( KProcess*, char* buf, int len )
{
    // Don't start throughput timer until the first block of data is written.
    if ( _startTime == 0 ) {
        _startTime = time( NULL );
    }

    /* 2002-01-26 LEW */
    // printf("got a line from tar, length %d bytes...\n", len);
    /* 2002-01-26 LEW */

    // Pass the data through the tar parser to extract the file info.
    _tarParser->slotData( buf, len );

    _totalKBytes += (float)len / 1024.0;
    assert( len % 512 == 0 );
    _totalRecords += len / 512;
    if ( TapeDrive::instance()->write( buf, len ) < len ) {
        _log->append( i18n( "*** Write failed, giving up." ) );

        if ( _proc ) {
            _proc->kill();
        }

        slotProcessExited( 0 );
    }
}

void BackupDlg::slotEntry( const TQString& name, int size, int mtime, int record )
{
    if ( _fileName.length() > 0 ) {
        _archive->addFile( _fileSize, _fileMTime, _fileStartRecord, record, _fileName );
    }

    /* 2002-01-28 LEW */
    // printf("BackupDlg::slotEntry called with \"%s\", %d bytes\n", name.latin1(), size);
    /* 2002-01-28 LEW */

    _fileName        = name;
    _fileSize        = size;
    _fileMTime       = mtime;
    _fileStartRecord = record;

    TQString tmp;
    tmp.setNum( ++_numFiles );
    _fileCount->setText( tmp );
    _log->append( name );
}

void BackupDlg::slotOK()
{
    if ( _aborted ) {
        reject();
    } else {
        accept();
    }
}

void BackupDlg::slotAbort()
{
    TQT_TQOBJECT(this)->killTimers();
    if ( _proc ) {
        _proc->kill();
        delete _proc;
    }
    delete _archive;
    _aborted = TRUE;

    _ok->setEnabled( TRUE );
    _ok->setDefault( TRUE );
    _save->setEnabled( TRUE );
    _abort->setEnabled( FALSE );
}

void BackupDlg::timerEvent( TQTimerEvent* )
{
    updateStats();
}

void BackupDlg::updateStats()
{
    if ( _startTime == 0 ) {
        return;
    }

    TQString str;

    int elapsed = time( NULL ) - _startTime;
    str = TQString::fromUtf8( TQCString().sprintf( i18n( "%02d:%02d:%02d" ).utf8().data(), elapsed / 3600, elapsed / 60 % 60, elapsed % 60 ) );
    _elapsedTime->setText( str );

    int remain = 0;
    if ( (int)_totalKBytes > 0 ) {
        remain = (int)(( (float)_archiveSize - _totalKBytes ) * (float)elapsed / _totalKBytes);
    }
    if ( remain < 0 ) {
        remain = 0;
    }
    str = TQString::fromUtf8( TQCString().sprintf( i18n( "%02d:%02d:%02d" ).utf8().data(), remain / 3600, remain / 60 % 60, remain % 60 ) );
    _timeRemaining->setText( str );

    str = Util::kbytesToString( (int)_totalKBytes );
    _kbytesWritten->setText( str );

    if ( elapsed > 0 ) {
        str = i18n( "%1/min" ).arg(Util::kbytesToString( (int)_totalKBytes *60 / elapsed ) );
        _transferRate->setText( str );
    }
}