2012-02-03 18:33:39 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2012 University of California
|
|
|
|
//
|
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// BOINC 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 Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-02-06 21:13:04 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "boinc_win.h"
|
|
|
|
#else
|
2012-02-03 18:33:39 +00:00
|
|
|
#include <string.h>
|
2012-02-06 21:13:04 +00:00
|
|
|
#endif
|
2012-02-03 18:33:39 +00:00
|
|
|
|
2012-02-06 22:57:36 +00:00
|
|
|
#include "crypt.h"
|
2012-02-03 18:33:39 +00:00
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "filesys.h"
|
2012-02-06 22:57:36 +00:00
|
|
|
#include "md5_file.h"
|
2012-02-03 18:33:39 +00:00
|
|
|
|
|
|
|
#include "app.h"
|
2012-02-06 21:13:04 +00:00
|
|
|
#include "client_msgs.h"
|
2012-02-03 18:33:39 +00:00
|
|
|
#include "client_state.h"
|
2012-02-06 22:57:36 +00:00
|
|
|
#include "sandbox.h"
|
2012-02-03 18:33:39 +00:00
|
|
|
|
|
|
|
#include "async_file.h"
|
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
vector<ASYNC_VERIFY*> async_verifies;
|
|
|
|
vector<ASYNC_COPY*> async_copies;
|
|
|
|
|
2012-02-06 06:06:44 +00:00
|
|
|
#define BUFSIZE 64*1024
|
|
|
|
|
2012-02-03 18:33:39 +00:00
|
|
|
int ASYNC_COPY::init(
|
2012-02-03 22:08:11 +00:00
|
|
|
ACTIVE_TASK* _atp, const char* from_path, const char* _to_path
|
|
|
|
) {
|
2012-02-03 18:33:39 +00:00
|
|
|
atp = _atp;
|
|
|
|
strcpy(to_path, _to_path);
|
2012-02-06 21:13:04 +00:00
|
|
|
|
2012-02-09 00:47:04 +00:00
|
|
|
if (log_flags.async_file_debug) {
|
|
|
|
msg_printf(atp->wup->project, MSG_INFO,
|
|
|
|
"[async] started async copy of %s", from_path
|
|
|
|
);
|
|
|
|
}
|
2012-02-03 18:33:39 +00:00
|
|
|
in = fopen(from_path, "rb");
|
|
|
|
if (!in) return ERR_FOPEN;
|
|
|
|
strcpy(temp_path, to_path);
|
|
|
|
char* p = strrchr(temp_path, '/');
|
|
|
|
strcpy(p+1, "copy_temp");
|
|
|
|
out = fopen(temp_path, "wb");
|
|
|
|
if (!out) {
|
|
|
|
fclose(in);
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
2012-02-06 23:33:23 +00:00
|
|
|
atp->async_copy = this;
|
|
|
|
async_copies.push_back(this);
|
2012-02-03 18:33:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASYNC_COPY::ASYNC_COPY() {
|
|
|
|
in = out = NULL;
|
|
|
|
atp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASYNC_COPY::~ASYNC_COPY() {
|
|
|
|
if (in) fclose(in);
|
|
|
|
if (out) fclose(out);
|
|
|
|
if (atp) {
|
|
|
|
atp->async_copy = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy a 64KB chunk.
|
|
|
|
// return nonzero if we're done (success or fail)
|
|
|
|
//
|
|
|
|
int ASYNC_COPY::copy_chunk() {
|
2012-02-06 06:06:44 +00:00
|
|
|
unsigned char buf[BUFSIZE];
|
2012-02-03 18:33:39 +00:00
|
|
|
int retval;
|
|
|
|
|
2012-02-06 06:06:44 +00:00
|
|
|
int n = fread(buf, 1, BUFSIZE, in);
|
2012-02-03 18:33:39 +00:00
|
|
|
if (n <= 0) {
|
|
|
|
// copy done. rename temp file
|
|
|
|
//
|
2012-02-03 22:08:11 +00:00
|
|
|
fclose(in);
|
|
|
|
fclose(out);
|
|
|
|
in = out = NULL;
|
2012-02-03 18:33:39 +00:00
|
|
|
retval = boinc_rename(temp_path, to_path);
|
|
|
|
if (retval) {
|
|
|
|
error(retval);
|
|
|
|
return 1;
|
|
|
|
}
|
2012-02-06 21:13:04 +00:00
|
|
|
|
2012-02-09 00:47:04 +00:00
|
|
|
if (log_flags.async_file_debug) {
|
|
|
|
msg_printf(atp->wup->project, MSG_INFO,
|
|
|
|
"[async] async copy of %s finished", to_path
|
|
|
|
);
|
|
|
|
}
|
2012-02-06 21:13:04 +00:00
|
|
|
|
|
|
|
atp->async_copy = NULL;
|
|
|
|
|
2012-02-03 18:33:39 +00:00
|
|
|
// If task is still scheduled, start it.
|
|
|
|
//
|
|
|
|
if (atp->scheduler_state == CPU_SCHED_SCHEDULED) {
|
2012-02-08 21:14:34 +00:00
|
|
|
retval = atp->start();
|
2012-02-03 18:33:39 +00:00
|
|
|
if (retval) {
|
|
|
|
error(retval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1; // tell caller we're done
|
|
|
|
} else {
|
|
|
|
int m = fwrite(buf, 1, n, out);
|
|
|
|
if (m != n) {
|
|
|
|
error(ERR_FWRITE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle the failure of a copy; error out the result
|
|
|
|
//
|
|
|
|
void ASYNC_COPY::error(int retval) {
|
2012-02-03 22:08:11 +00:00
|
|
|
atp->set_task_state(PROCESS_COULDNT_START, "ASYNC_COPY::error");
|
2012-02-03 18:33:39 +00:00
|
|
|
gstate.report_result_error(
|
|
|
|
*(atp->result), "Couldn't copy file: %s", boincerror(retval)
|
|
|
|
);
|
|
|
|
gstate.request_schedule_cpus("start failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_async_copy(ASYNC_COPY* acp) {
|
|
|
|
vector<ASYNC_COPY*>::iterator i = async_copies.begin();
|
|
|
|
while (i != async_copies.end()) {
|
|
|
|
if (*i == acp) {
|
|
|
|
async_copies.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
2012-02-03 22:08:11 +00:00
|
|
|
i++;
|
2012-02-03 18:33:39 +00:00
|
|
|
}
|
|
|
|
delete acp;
|
|
|
|
}
|
|
|
|
|
2012-02-04 00:18:37 +00:00
|
|
|
int ASYNC_VERIFY::init(FILE_INFO* _fip) {
|
|
|
|
fip = _fip;
|
2012-02-06 06:06:44 +00:00
|
|
|
md5_init(&md5_state);
|
|
|
|
get_pathname(fip, inpath, sizeof(inpath));
|
|
|
|
|
2012-02-09 00:47:04 +00:00
|
|
|
if (log_flags.async_file_debug) {
|
|
|
|
msg_printf(fip->project, MSG_INFO,
|
|
|
|
"[async] started async MD5%s of %s",
|
|
|
|
fip->download_gzipped?" and uncompress":"", fip->name
|
|
|
|
);
|
|
|
|
}
|
2012-02-06 06:06:44 +00:00
|
|
|
if (fip->download_gzipped) {
|
|
|
|
strcpy(outpath, inpath);
|
2012-02-09 00:47:04 +00:00
|
|
|
strcpy(temp_path, outpath);
|
|
|
|
char* p = strrchr(temp_path, '/');
|
|
|
|
strcpy(p+1, "verify_temp");
|
|
|
|
out = boinc_fopen(temp_path, "wb");
|
2012-02-06 06:06:44 +00:00
|
|
|
if (!out) return ERR_FOPEN;
|
|
|
|
strcat(inpath, ".gz");
|
|
|
|
gzin = gzopen(inpath, "rb");
|
|
|
|
if (gzin == Z_NULL) {
|
|
|
|
fclose(out);
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
in = fopen(inpath, "rb");
|
|
|
|
if (!in) return ERR_FOPEN;
|
|
|
|
}
|
2012-02-06 23:33:23 +00:00
|
|
|
async_verifies.push_back(this);
|
|
|
|
fip->async_verify = this;
|
2012-02-04 00:18:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-06 22:57:36 +00:00
|
|
|
// the MD5 has been computed. Finish up.
|
|
|
|
//
|
|
|
|
void ASYNC_VERIFY::finish() {
|
|
|
|
unsigned char binout[16];
|
|
|
|
char md5_buf[64];
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
md5_finish(&md5_state, binout);
|
|
|
|
for (int i=0; i<16; i++) {
|
|
|
|
sprintf(md5_buf+2*i, "%02x", binout[i]);
|
|
|
|
}
|
|
|
|
md5_buf[32] = 0;
|
|
|
|
if (fip->signature_required) {
|
|
|
|
bool verified;
|
|
|
|
retval = check_file_signature2(md5_buf, fip->file_signature,
|
|
|
|
fip->project->code_sign_key, verified
|
|
|
|
);
|
|
|
|
if (retval) {
|
|
|
|
error(retval);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!verified) {
|
|
|
|
error(ERR_RSA_FAILED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strcmp(md5_buf, fip->md5_cksum)) {
|
|
|
|
error(ERR_MD5_FAILED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-02-09 00:47:04 +00:00
|
|
|
if (log_flags.async_file_debug) {
|
|
|
|
msg_printf(fip->project, MSG_INFO,
|
|
|
|
"[async] async verify of %s finished", fip->name
|
|
|
|
);
|
|
|
|
}
|
2012-02-06 23:33:23 +00:00
|
|
|
fip->async_verify = NULL;
|
2012-02-06 22:57:36 +00:00
|
|
|
fip->status = FILE_PRESENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASYNC_VERIFY::error(int retval) {
|
2012-02-09 00:47:04 +00:00
|
|
|
if (log_flags.async_file_debug) {
|
|
|
|
msg_printf(fip->project, MSG_INFO,
|
|
|
|
"[async] async verify of %s failed: %s",
|
|
|
|
fip->name, boincerror(retval)
|
|
|
|
);
|
|
|
|
}
|
2012-02-06 23:33:23 +00:00
|
|
|
fip->async_verify = NULL;
|
2012-02-06 22:57:36 +00:00
|
|
|
fip->status = retval;
|
|
|
|
}
|
|
|
|
|
2012-02-03 18:33:39 +00:00
|
|
|
int ASYNC_VERIFY::verify_chunk() {
|
2012-02-06 06:06:44 +00:00
|
|
|
int n;
|
|
|
|
unsigned char buf[BUFSIZE];
|
|
|
|
if (fip->download_gzipped) {
|
|
|
|
n = gzread(gzin, buf, BUFSIZE);
|
|
|
|
if (n <=0) {
|
|
|
|
// done
|
2012-02-06 22:57:36 +00:00
|
|
|
//
|
2012-02-07 00:35:55 +00:00
|
|
|
gzclose(gzin);
|
2012-02-06 22:57:36 +00:00
|
|
|
fclose(out);
|
|
|
|
delete_project_owned_file(inpath, true);
|
2012-02-09 00:47:04 +00:00
|
|
|
boinc_rename(temp_path, outpath);
|
2012-02-06 22:57:36 +00:00
|
|
|
finish();
|
|
|
|
return 1;
|
2012-02-06 06:06:44 +00:00
|
|
|
} else {
|
|
|
|
int m = (int)fwrite(buf, 1, n, out);
|
|
|
|
if (m != n) {
|
|
|
|
// write failed
|
2012-02-06 22:57:36 +00:00
|
|
|
//
|
|
|
|
error(ERR_FWRITE);
|
|
|
|
return 1;
|
2012-02-06 06:06:44 +00:00
|
|
|
}
|
|
|
|
md5_append(&md5_state, buf, n);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
n = fread(buf, 1, BUFSIZE, in);
|
|
|
|
if (n <= 0) {
|
2012-02-06 22:57:36 +00:00
|
|
|
fclose(in);
|
|
|
|
finish();
|
|
|
|
return 1;
|
2012-02-06 06:06:44 +00:00
|
|
|
} else {
|
|
|
|
md5_append(&md5_state, buf, n);
|
|
|
|
}
|
|
|
|
}
|
2012-02-03 18:33:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-04 00:18:37 +00:00
|
|
|
void remove_async_verify(ASYNC_VERIFY* avp) {
|
|
|
|
vector<ASYNC_VERIFY*>::iterator i = async_verifies.begin();
|
|
|
|
while (i != async_verifies.end()) {
|
|
|
|
if (*i == avp) {
|
|
|
|
async_verifies.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
delete avp;
|
|
|
|
}
|
|
|
|
|
2012-02-03 18:33:39 +00:00
|
|
|
// If there are any async file operations,
|
|
|
|
// do a 64KB chunk of the first one and return true.
|
|
|
|
//
|
|
|
|
// Note: if there are lots of pending operations,
|
|
|
|
// it's better to finish the oldest one before starting the rest
|
|
|
|
//
|
|
|
|
bool do_async_file_ops() {
|
|
|
|
if (async_copies.size()) {
|
|
|
|
ASYNC_COPY* acp = async_copies[0];
|
|
|
|
if (acp->copy_chunk()) {
|
|
|
|
async_copies.erase(async_copies.begin());
|
|
|
|
delete acp;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (async_verifies.size()) {
|
|
|
|
if (async_verifies[0]->verify_chunk()) {
|
|
|
|
async_verifies.erase(async_verifies.begin());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|