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
|
#include "CopyJob.h"
#include "General.h"
CopyJob::CopyJob(Handle * as, Handle * ad, ssize_t asiz, bool ads) : s(as), d(ad), ds(ads), siz(asiz), cursiz(0), r(0) {
WaitFor(s, W4_STICKY | W4_READING);
WaitFor(d, W4_STICKY | W4_WRITING);
cerr << "Creating a copyjob from " << s->GetName() << " to " << d->GetName() << " of " << siz << " bytes.\n";
}
CopyJob::~CopyJob() { }
int CopyJob::Do() throw (GeneralException) {
int tr;
cerr << GetName() << " running...\n";
switch (current) {
case 0:
tr = siz >= 0 ? siz - cursiz : COPY_BUFSIZ;
cerr << "Reading " << tr << " bytes.\n";
try {
r = s->read(buffer, MIN(COPY_BUFSIZ, tr));
cerr << "Got " << r << " bytes.\n";
}
catch (IOAgain e) {
cerr << "Not enough bytes. Suspending.\n";
Suspend();
}
case 1:
if (!r) {
return TASK_DONE;
}
try {
cerr << "Writing " << r << " bytes.\n";
d->write(buffer, r);
}
catch (IOAgain e) {
current = 1;
cerr << "No more byte in the output. Suspending.\n";
Suspend();
}
current = 0;
}
cursiz += r;
if (!s->IsClosed() && (siz != cursiz)) {
throw TaskSwitch();
}
if (ds) {
delete s;
}
return TASK_DONE;
}
String CopyJob::GetName() {
return (String("CopyJob from ") + s->GetName() + " to " + d->GetName());
}
|