#ifdef HAVE_CONFIG_H #include "config.h" #endif #include "CopyJob.h" #include "gettext.h" CopyJob::CopyJob(Handle * as, Handle * ad, ssize_t asiz, bool ads, bool add, int ashape) : s(as), d(ad), ds(ads), dd(add), siz(asiz), cursiz(0), r(0), w(0), tw(0), shape(ashape) { s->SetNonBlock(); d->SetNonBlock(); WaitFor(s, W4_READING); #ifndef _WIN32 struct timezone tz; if (shape > 0) { gettimeofday(&start, &tz); } #endif } CopyJob::~CopyJob() { if (ds) { delete s; } if (dd) { delete d; } } int CopyJob::Do() throw (GeneralException) { int tr; struct timeval now; #ifndef _WIN32 struct timezone tz; if (shape > 0) { gettimeofday(&now, &tz); } #endif switch (current) { case 0: tr = siz >= 0 ? siz - cursiz : COPY_BUFSIZ; try { r = s->read(buffer, MIN(COPY_BUFSIZ, tr)); // cerr << "CopyJob: read " << r << " bytes." << endl; } catch (IOAgain e) { WaitFor(s, W4_READING); Suspend(TASK_ON_HOLD); } if (!r) { return TASK_DONE; } tw = 0; case 1: current = 1; try { w = d->write(buffer + tw, r - tw); } catch (IOAgain e) { WaitFor(d, W4_WRITING); Suspend(TASK_ON_HOLD); } // cerr << "CopyJob: wrote " << w << " bytes." << endl; tw += w; if (r != tw) { // cerr << "CopyJob: We did expect to write a total of " << r << " bytes and we achieved " << tw << " bytes so far.\n"; WaitFor(d, W4_WRITING); Suspend(TASK_ON_HOLD); } current = 0; } cursiz += r; if (!s->IsClosed() && (siz != cursiz)) { WaitFor(s, W4_READING); Suspend(TASK_ON_HOLD); } return TASK_DONE; } String CopyJob::GetName() { return (String(_("CopyJob from ")) + s->GetName() + _(" to ") + d->GetName()); }