summaryrefslogtreecommitdiffstats
path: root/libk3b/jobs/k3bverificationjob.cpp
blob: e5333fe4bb5728912a47af82570289eb81eb8e3b (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
/*
 *
 * $Id: k3bisoimageverificationjob.cpp 597651 2006-10-21 08:04:01Z trueg $
 * Copyright (C) 2003-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * 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.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3bverificationjob.h"

#include <k3bdevice.h>
#include <k3bdevicehandler.h>
#include <k3bmd5job.h>
#include <k3bglobals.h>
#include <k3bdatatrackreader.h>
#include <k3bpipe.h>
#include <k3biso9660.h>

#include <kdebug.h>
#include <klocale.h>
#include <kio/global.h>
#include <kio/job.h>
#include <kio/netaccess.h>

#include <tqcstring.h>
#include <tqapplication.h>
#include <tqvaluelist.h>
#include <tqpair.h>


class K3bVerificationJobTrackEntry
{
public:
  K3bVerificationJobTrackEntry()
    : trackNumber(0) {
  }

  K3bVerificationJobTrackEntry( int tn, const TQCString& cs, const K3b::Msf& msf )
    : trackNumber(tn),
      checksum(cs),
      length(msf) {
  }

  int trackNumber;
  TQCString checksum;
  K3b::Msf length;
};


class K3bVerificationJob::Private
{
public:
  Private()
    : md5Job(0),
      device(0),
      dataTrackReader(0) {
  }

  bool canceled;
  K3bMd5Job* md5Job;
  K3bDevice::Device* device;

  K3b::Msf grownSessionSize;

  TQValueList<K3bVerificationJobTrackEntry> tracks;
  int currentTrackIndex;

  K3bDevice::DiskInfo diskInfo;
  K3bDevice::Toc toc;

  K3bDataTrackReader* dataTrackReader;

  K3b::Msf currentTrackSize;
  K3b::Msf totalSectors;
  K3b::Msf alreadyReadSectors;

  K3bPipe pipe;

  bool readSuccessful;

  bool mediumHasBeenReloaded;
};


K3bVerificationJob::K3bVerificationJob( K3bJobHandler* hdl, TQObject* parent, const char* name )
  : K3bJob( hdl, parent, name )
{
  d = new Private();

  d->md5Job = new K3bMd5Job( this );
  connect( d->md5Job, TQT_SIGNAL(infoMessage(const TQString&, int)), this, TQT_SIGNAL(infoMessage(const TQString&, int)) );
  connect( d->md5Job, TQT_SIGNAL(finished(bool)), this, TQT_SLOT(slotMd5JobFinished(bool)) );
  connect( d->md5Job, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)),
	   this, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)) );
}


K3bVerificationJob::~K3bVerificationJob()
{
  delete d;
}


void K3bVerificationJob::cancel()
{
  d->canceled = true;
  if( d->md5Job && d->md5Job->active() )
    d->md5Job->cancel();
  if( d->dataTrackReader && d->dataTrackReader->active() )
    d->dataTrackReader->cancel();
}


void K3bVerificationJob::addTrack( int trackNum, const TQCString& checksum, const K3b::Msf& length )
{
  d->tracks.append( K3bVerificationJobTrackEntry( trackNum, checksum, length ) );
}


void K3bVerificationJob::clear()
{
  d->tracks.clear();
  d->grownSessionSize = 0;
}


void K3bVerificationJob::setDevice( K3bDevice::Device* dev )
{
  d->device = dev;
}


void K3bVerificationJob::setGrownSessionSize( const K3b::Msf& s )
{
  d->grownSessionSize = s;
}


void K3bVerificationJob::start()
{
  jobStarted();

  d->canceled = false;
  d->currentTrackIndex = 0;
  d->alreadyReadSectors = 0;

  emit newTask( i18n("Checking medium") );

  d->mediumHasBeenReloaded = false;
  connect( K3bDevice::sendCommand( K3bDevice::DeviceHandler::DISKINFO, d->device ),
           TQT_SIGNAL(finished(K3bDevice::DeviceHandler*)),
           this,
           TQT_SLOT(slotDiskInfoReady(K3bDevice::DeviceHandler*)) );
}


void K3bVerificationJob::slotMediaReloaded( bool /*success*/ )
{
    // we always need to wait for the medium. Otherwise the diskinfo below
    // may run before the drive is ready!
    waitForMedia( d->device,
                  K3bDevice::STATE_COMPLETE|K3bDevice::STATE_INCOMPLETE,
                  K3bDevice::MEDIA_WRITABLE );

  d->mediumHasBeenReloaded = true;

  emit newTask( i18n("Checking medium") );

  connect( K3bDevice::sendCommand( K3bDevice::DeviceHandler::DISKINFO, d->device ),
           TQT_SIGNAL(finished(K3bDevice::DeviceHandler*)),
           this,
           TQT_SLOT(slotDiskInfoReady(K3bDevice::DeviceHandler*)) );
}


void K3bVerificationJob::slotDiskInfoReady( K3bDevice::DeviceHandler* dh )
{
  if( d->canceled ) {
    emit canceled();
    jobFinished(false);
  }

  d->diskInfo = dh->diskInfo();
  d->toc = dh->toc();
  d->totalSectors = 0;

  // just to be sure check if we actually have all the tracks
  int i = 0;
  for( TQValueList<K3bVerificationJobTrackEntry>::iterator it = d->tracks.begin();
       it != d->tracks.end(); ++i, ++it ) {

    // 0 means "last track"
    if( (*it).trackNumber == 0 )
      (*it).trackNumber = d->toc.count();

    if( (int)d->toc.count() < (*it).trackNumber ) {
        if ( d->mediumHasBeenReloaded ) {
            emit infoMessage( i18n("Internal Error: Verification job improperly initialized (%1)")
                              .arg( "Specified track number not found on medium" ), ERROR );
            jobFinished( false );
            return;
        }
        else {
            // many drives need to reload the medium to return to a proper state
            emit newTask( i18n("Reloading the medium") );
            connect( K3bDevice::reload( d->device ),
                     TQT_SIGNAL(finished(bool)),
                     this,
                     TQT_SLOT(slotMediaReloaded(bool)) );
            return;
        }
    }

    d->totalSectors += trackLength( i );
  }

  readTrack( 0 );
}


void K3bVerificationJob::readTrack( int trackIndex )
{
  d->currentTrackIndex = trackIndex;
  d->readSuccessful = true;

  d->currentTrackSize = trackLength( trackIndex );
  if( d->currentTrackSize == 0 ) {
    jobFinished(false);
    return;
  }

  emit newTask( i18n("Verifying track %1").arg( d->tracks[trackIndex].trackNumber ) );

  d->pipe.open();

  if( d->toc[d->tracks[trackIndex].trackNumber-1].type() == K3bDevice::Track::DATA ) {
    if( !d->dataTrackReader ) {
      d->dataTrackReader = new K3bDataTrackReader( this );
      connect( d->dataTrackReader, TQT_SIGNAL(percent(int)), this, TQT_SLOT(slotReaderProgress(int)) );
      //      connect( d->dataTrackReader, TQT_SIGNAL(processedSize(int, int)), this, TQT_SLOT(slotReaderProcessedSize(int, int)) );
      connect( d->dataTrackReader, TQT_SIGNAL(finished(bool)), this, TQT_SLOT(slotReaderFinished(bool)) );
      connect( d->dataTrackReader, TQT_SIGNAL(infoMessage(const TQString&, int)), this, TQT_SIGNAL(infoMessage(const TQString&, int)) );
      connect( d->dataTrackReader, TQT_SIGNAL(newTask(const TQString&)), this, TQT_SIGNAL(newSubTask(const TQString&)) );
      connect( d->dataTrackReader, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)),
	       this, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)) );
    }

    d->dataTrackReader->setDevice( d->device );
    d->dataTrackReader->setIgnoreErrors( false );
    d->dataTrackReader->setSectorSize( K3bDataTrackReader::MODE1 );

    // in case a session was grown the track size does not say anything about the verification data size
    if( d->diskInfo.mediaType() & (K3bDevice::MEDIA_DVD_PLUS_RW|K3bDevice::MEDIA_DVD_RW_OVWR) &&
	d->grownSessionSize > 0 ) {
      K3bIso9660 isoF( d->device );
      if( isoF.open() ) {
	int firstSector = isoF.primaryDescriptor().volumeSpaceSize - d->grownSessionSize.lba();
	d->dataTrackReader->setSectorRange( firstSector,
					    isoF.primaryDescriptor().volumeSpaceSize -1 );
      }
      else {
	emit infoMessage( i18n("Unable to determine the ISO9660 filesystem size."), ERROR );
	jobFinished( false );
	return;
      }
    }
    else
      d->dataTrackReader->setSectorRange( d->toc[d->tracks[trackIndex].trackNumber-1].firstSector(),
					  d->toc[d->tracks[trackIndex].trackNumber-1].firstSector() + d->currentTrackSize -1 );

    d->md5Job->setMaxReadSize( d->currentTrackSize.mode1Bytes() );

    d->dataTrackReader->writeToFd( d->pipe.in() );
    d->dataTrackReader->start();
  }
  else {
    // FIXME: handle audio tracks
  }

  d->md5Job->setFd( d->pipe.out() );
  d->md5Job->start();
}


void K3bVerificationJob::slotReaderProgress( int p )
{
  emit subPercent( p );

  emit percent( 100 * ( d->alreadyReadSectors.lba() + ( p*d->currentTrackSize.lba()/100 ) ) / d->totalSectors.lba() );
}


void K3bVerificationJob::slotMd5JobFinished( bool success )
{
  d->pipe.close();

  if( success && !d->canceled && d->readSuccessful ) {
    // compare the two sums
    if( d->tracks[d->currentTrackIndex].checksum != d->md5Job->hexDigest() ) {
      emit infoMessage( i18n("Written data in track %1 differs from original.").arg(d->tracks[d->currentTrackIndex].trackNumber), ERROR );
      jobFinished(false);
    }
    else {
      emit infoMessage( i18n("Written data verified."), SUCCESS );
      ++d->currentTrackIndex;
      if( d->currentTrackIndex < (int)d->tracks.count() )
	readTrack( d->currentTrackIndex );
      else
	jobFinished(true);
    }
  }
  else {
    // The md5job emitted an error message. So there is no need to do this again
    jobFinished(false);
  }
}


void K3bVerificationJob::slotReaderFinished( bool success )
{
  d->readSuccessful = success;
  if( !d->readSuccessful )
    d->md5Job->cancel();
  else {
    d->alreadyReadSectors += trackLength( d->currentTrackIndex );

    // close the pipe and let the md5 job finish gracefully
    d->pipe.closeIn();
    //    d->md5Job->stop();
  }
}


K3b::Msf K3bVerificationJob::trackLength( int trackIndex )
{
  K3b::Msf& trackSize = d->tracks[trackIndex].length;
  const int& trackNum = d->tracks[trackIndex].trackNumber;

  if( trackSize == 0 ) {
    trackSize = d->toc[trackNum-1].length();

    if( d->diskInfo.mediaType() & (K3bDevice::MEDIA_DVD_PLUS_RW|K3bDevice::MEDIA_DVD_RW_OVWR) ) {
      K3bIso9660 isoF( d->device, d->toc[trackNum-1].firstSector().lba() );
      if( isoF.open() ) {
	trackSize = isoF.primaryDescriptor().volumeSpaceSize;
      }
      else {
	emit infoMessage( i18n("Unable to determine the ISO9660 filesystem size."), ERROR );
	return 0;
      }
    }

    //
    // A data track recorded in TAO mode has two run-out blocks which cannot be read and contain
    // zero data anyway. The problem is that I do not know of a valid method to determine if a track
    // was written in TAO (the control nibble does definitely not work, I never saw one which did not
    // equal 4).
    // So the solution for now is to simply try to read the last sector of a data track. If this is not
    // possible we assume it was written in TAO mode and reduce the length by 2 sectors
    //
    if( d->toc[trackNum-1].type() == K3bDevice::Track::DATA &&
	d->diskInfo.mediaType() & K3bDevice::MEDIA_CD_ALL ) {
      // we try twice just to be sure
      unsigned char buffer[2048];
      if( !d->device->read10( buffer, 2048, d->toc[trackNum-1].lastSector().lba(), 1 ) &&
	  !d->device->read10( buffer, 2048, d->toc[trackNum-1].lastSector().lba(), 1 ) ) {
	trackSize -= 2;
	kdDebug() << "(K3bCdCopyJob) track " << trackNum << " probably TAO recorded." << endl;
      }
    }
  }

  return trackSize;
}


#include "k3bverificationjob.moc"