summaryrefslogtreecommitdiff
path: root/lib/CopyJob.cc
blob: 1e151c30ad2dfaf45b72836a984a04d38f7b0d55 (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
#include "CopyJob.h"
#include "General.h"
#include "config.h"

CopyJob::CopyJob(Handle * as, Handle * ad, ssize_t asiz, bool ads, bool add) : s(as), d(ad), ds(ads), dd(add), siz(asiz), cursiz(0), r(0) {
    s->SetNonBlock();
    d->SetNonBlock();
    WaitFor(s, W4_READING);
}

CopyJob::~CopyJob() {
    if (ds) {
	delete s;
    }
    
    if (dd) {
	delete d;
    }
}

int CopyJob::Do() throw (GeneralException) {
    int tr;
    
    switch (current) {
    case 0:
        tr = siz >= 0 ? siz - cursiz : COPY_BUFSIZ;
	try {
	    r = s->read(buffer, MIN(COPY_BUFSIZ, tr));
	}
	catch (IOAgain e) {
	    WaitFor(s, W4_READING);
	    Suspend(TASK_ON_HOLD);
	}
	if (!r) {
	    return TASK_DONE;
	}
    case 1:
	try {
	    d->write(buffer, r);
	}
	catch (IOAgain e) {
	    current = 1;
	    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());
}