summaryrefslogtreecommitdiff
path: root/lib/CopyJob.cc
blob: 69ae8cb51582ca3e326928fe4fcdd1cec91f3f18 (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
/*
 *  Baltisot
 *  Copyright (C) 1999-2008 Nicolas "Pixel" Noble
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#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;
#ifndef _WIN32
    struct timeval now;
    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));
//	    std::cerr << "CopyJob: read " << r << " bytes." << std::endl;
//            std::cerr << buffer;
	}
	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);
	}
//	std::cerr << "CopyJob: wrote " << w << " bytes." << std::endl;
	tw += w;
	if (r != tw) {
//	    std::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());
}