2004-11-29 09:23:51 +00:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2003-01-07 08:11:16 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-07-02 20:57:59 +00:00
|
|
|
//
|
2003-01-07 08:11:16 +00:00
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
2003-07-02 20:57:59 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2003-01-07 08:11:16 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-07-01 20:37:09 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-07-02 20:57:59 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2003-01-07 08:11:16 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
// file_deleter: deletes files that are no longer needed
|
|
|
|
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <ctime>
|
2002-12-17 19:00:43 +00:00
|
|
|
#include <unistd.h>
|
2004-11-29 09:47:48 +00:00
|
|
|
#include <errno.h>
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2003-04-07 19:06:00 +00:00
|
|
|
#include "boinc_db.h"
|
2002-12-17 19:00:43 +00:00
|
|
|
#include "parse.h"
|
2003-02-10 19:51:32 +00:00
|
|
|
#include "util.h"
|
2003-08-15 00:45:25 +00:00
|
|
|
#include "sched_config.h"
|
2003-03-08 00:09:40 +00:00
|
|
|
#include "sched_util.h"
|
2004-04-08 08:15:23 +00:00
|
|
|
#include "sched_msgs.h"
|
2005-01-03 18:00:53 +00:00
|
|
|
#include "../lib/filesys.h"
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2003-02-10 19:51:32 +00:00
|
|
|
#define LOCKFILE "file_deleter.out"
|
2003-06-20 01:31:03 +00:00
|
|
|
#define PIDFILE "file_deleter.pid"
|
2003-02-10 19:51:32 +00:00
|
|
|
|
2004-12-06 22:41:19 +00:00
|
|
|
#define SLEEP_INTERVAL 5
|
|
|
|
|
2003-09-02 21:16:55 +00:00
|
|
|
SCHED_CONFIG config;
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2005-01-03 18:00:53 +00:00
|
|
|
// David -- not sure what the previous code was meant to do, since dir_hier_path
|
|
|
|
// always returns 0 if the last argument (eg, missing below) argument is set to
|
|
|
|
// false.
|
|
|
|
//
|
2005-01-03 18:06:57 +00:00
|
|
|
int get_file_path(char *buf, char* upload_dir, int fanout, char* path) {
|
2005-01-03 18:00:53 +00:00
|
|
|
|
|
|
|
dir_hier_path(buf, upload_dir, fanout, true, path);
|
|
|
|
if (boinc_file_exists(path))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// TODO: get rid of the old hash in about 3/2005
|
|
|
|
//
|
|
|
|
dir_hier_path(buf, upload_dir, fanout, false, path);
|
2005-01-03 18:04:48 +00:00
|
|
|
if (boinc_file_exists(path))
|
2005-01-03 18:00:53 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
int wu_delete_files(WORKUNIT& wu) {
|
|
|
|
char* p;
|
2004-05-13 18:18:22 +00:00
|
|
|
char filename[256], pathname[256], buf[LARGE_BLOB_SIZE];
|
2003-06-10 05:33:39 +00:00
|
|
|
bool no_delete=false;
|
2005-01-03 18:00:53 +00:00
|
|
|
int count_deleted = 0, retval;
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2003-04-03 18:35:40 +00:00
|
|
|
safe_strcpy(buf, wu.xml_doc);
|
2004-11-29 09:23:51 +00:00
|
|
|
|
2002-12-20 02:12:27 +00:00
|
|
|
p = strtok(buf, "\n");
|
2002-12-17 19:00:43 +00:00
|
|
|
strcpy(filename, "");
|
|
|
|
while (p) {
|
|
|
|
if (parse_str(p, "<name>", filename, sizeof(filename))) {
|
|
|
|
} else if (match_tag(p, "<file_info>")) {
|
|
|
|
no_delete = false;
|
|
|
|
strcpy(filename, "");
|
|
|
|
} else if (match_tag(p, "<no_delete/>")) {
|
|
|
|
no_delete = true;
|
|
|
|
} else if (match_tag(p, "</file_info>")) {
|
|
|
|
if (!no_delete) {
|
2005-01-03 18:06:57 +00:00
|
|
|
retval=get_file_path(filename, config.download_dir, config.uldl_dir_fanout,
|
2005-01-02 07:44:40 +00:00
|
|
|
pathname
|
|
|
|
);
|
2005-01-03 18:00:53 +00:00
|
|
|
if (retval) {
|
2004-08-06 11:42:41 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "[%s] dir_hier_path: %d\n", wu.name, retval);
|
|
|
|
} else {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::NORMAL, "[%s] deleting download/%s\n", wu.name, filename);
|
|
|
|
retval = unlink(pathname);
|
|
|
|
if (retval && strlen(config.download_dir_alt)) {
|
|
|
|
sprintf(pathname, "%s/%s", config.download_dir_alt, filename);
|
|
|
|
unlink(pathname);
|
|
|
|
}
|
|
|
|
++count_deleted;
|
|
|
|
}
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2002-12-18 01:34:51 +00:00
|
|
|
p = strtok(0, "\n");
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::DEBUG, "[%s] deleted %d file(s)\n", wu.name, count_deleted);
|
2002-12-17 19:00:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int result_delete_files(RESULT& result) {
|
|
|
|
char* p;
|
2004-05-13 18:18:22 +00:00
|
|
|
char filename[256], pathname[256], buf[LARGE_BLOB_SIZE];
|
2003-06-10 05:33:39 +00:00
|
|
|
bool no_delete=false;
|
2005-01-03 18:00:53 +00:00
|
|
|
int count_deleted = 0, retval;
|
2003-07-02 20:57:59 +00:00
|
|
|
|
2003-04-03 18:35:40 +00:00
|
|
|
safe_strcpy(buf, result.xml_doc_in);
|
2002-12-20 02:12:27 +00:00
|
|
|
p = strtok(buf,"\n");
|
2002-12-17 19:00:43 +00:00
|
|
|
while (p) {
|
|
|
|
if (parse_str(p, "<name>", filename, sizeof(filename))) {
|
|
|
|
} else if (match_tag(p, "<file_info>")) {
|
|
|
|
no_delete = false;
|
|
|
|
strcpy(filename, "");
|
|
|
|
} else if (match_tag(p, "<no_delete/>")) {
|
|
|
|
no_delete = true;
|
|
|
|
} else if (match_tag(p, "</file_info>")) {
|
|
|
|
if (!no_delete) {
|
2005-01-03 18:06:57 +00:00
|
|
|
retval=get_file_path(filename, config.upload_dir, config.uldl_dir_fanout,
|
2004-08-06 11:42:41 +00:00
|
|
|
pathname
|
2003-09-20 17:38:13 +00:00
|
|
|
);
|
2005-01-03 18:00:53 +00:00
|
|
|
if (retval) {
|
2004-08-06 11:42:41 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
2005-01-02 07:44:40 +00:00
|
|
|
"[%s] dir_hier_path: %d\n", result.name, retval
|
2004-08-06 11:42:41 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
retval = unlink(pathname);
|
|
|
|
++count_deleted;
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::NORMAL,
|
2004-11-29 09:47:48 +00:00
|
|
|
"[%s] unlinked %s; retval %d %s\n",
|
|
|
|
result.name, filename, retval, (retval && errno)?strerror(errno):""
|
2004-08-06 11:42:41 +00:00
|
|
|
);
|
|
|
|
}
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2002-12-18 01:34:51 +00:00
|
|
|
p = strtok(0, "\n");
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
2003-07-02 20:57:59 +00:00
|
|
|
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::DEBUG,
|
2003-09-20 17:38:13 +00:00
|
|
|
"[%s] deleted %d file(s)\n", result.name, count_deleted
|
|
|
|
);
|
2002-12-17 19:00:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-29 09:23:51 +00:00
|
|
|
// set by corresponding command line arguments.
|
|
|
|
static bool preserve_wu_files=false;
|
|
|
|
static bool preserve_result_files=false;
|
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
// return nonzero if did anything
|
|
|
|
//
|
|
|
|
bool do_pass() {
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_WORKUNIT wu;
|
|
|
|
DB_RESULT result;
|
2002-12-17 19:00:43 +00:00
|
|
|
bool did_something = false;
|
2003-06-04 17:21:26 +00:00
|
|
|
char buf[256];
|
2004-10-04 23:59:51 +00:00
|
|
|
int retval;
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2004-05-03 19:30:01 +00:00
|
|
|
check_stop_daemons();
|
2003-03-08 00:09:40 +00:00
|
|
|
|
2004-08-25 23:18:16 +00:00
|
|
|
sprintf(buf, "where file_delete_state=%d limit 1000", FILE_DELETE_READY);
|
2003-06-04 17:21:26 +00:00
|
|
|
while (!wu.enumerate(buf)) {
|
2002-12-17 19:00:43 +00:00
|
|
|
did_something = true;
|
2004-11-29 09:23:51 +00:00
|
|
|
|
2004-12-20 20:47:25 +00:00
|
|
|
if (!preserve_wu_files) {
|
2004-11-29 09:23:51 +00:00
|
|
|
wu_delete_files(wu);
|
2004-12-20 20:47:25 +00:00
|
|
|
}
|
2002-12-17 19:00:43 +00:00
|
|
|
wu.file_delete_state = FILE_DELETE_DONE;
|
2004-10-04 23:59:51 +00:00
|
|
|
sprintf(buf, "file_delete_state=%d", wu.file_delete_state);
|
|
|
|
retval= wu.update_field(buf);
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
|
|
|
"[%s] workunit failed to update file_delete_state with %d\n",
|
|
|
|
wu.name, retval
|
|
|
|
);
|
|
|
|
}
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
|
2004-08-25 23:18:16 +00:00
|
|
|
sprintf(buf, "where file_delete_state=%d limit 1000", FILE_DELETE_READY);
|
2003-06-04 17:21:26 +00:00
|
|
|
while (!result.enumerate(buf)) {
|
2002-12-17 19:00:43 +00:00
|
|
|
did_something = true;
|
2004-12-20 20:47:25 +00:00
|
|
|
if (!preserve_result_files) {
|
2004-11-29 09:23:51 +00:00
|
|
|
result_delete_files(result);
|
2004-12-20 20:47:25 +00:00
|
|
|
}
|
2002-12-17 19:00:43 +00:00
|
|
|
result.file_delete_state = FILE_DELETE_DONE;
|
2004-10-04 23:59:51 +00:00
|
|
|
sprintf(buf, "file_delete_state=%d", result.file_delete_state);
|
|
|
|
retval= result.update_field(buf);
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
|
|
|
"[%s] result failed to update file_delete_state with %d\n",
|
|
|
|
result.name, retval
|
|
|
|
);
|
|
|
|
}
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
return did_something;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
int retval;
|
|
|
|
bool asynch = false, one_pass = false;
|
|
|
|
int i;
|
|
|
|
|
2004-05-03 19:30:01 +00:00
|
|
|
check_stop_daemons();
|
2002-12-17 19:00:43 +00:00
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-asynch")) {
|
|
|
|
asynch = true;
|
|
|
|
} else if (!strcmp(argv[i], "-one_pass")) {
|
|
|
|
one_pass = true;
|
2004-11-29 09:23:51 +00:00
|
|
|
} else if (!strcmp(argv[i], "-preserve_wu_files")) {
|
2004-12-20 20:47:25 +00:00
|
|
|
// This option is primarily for testing.
|
|
|
|
// If enabled, the file_deleter will function 'normally'
|
|
|
|
// and will update the database,
|
|
|
|
// but will not actually delete the workunit input files.
|
|
|
|
// It's equivalent to setting <no_delete/>
|
|
|
|
// for all workunit input files.
|
|
|
|
//
|
2004-11-29 09:23:51 +00:00
|
|
|
preserve_wu_files = true;
|
|
|
|
} else if (!strcmp(argv[i], "-preserve_result_files")) {
|
2004-12-20 20:47:25 +00:00
|
|
|
// This option is primarily for testing.
|
|
|
|
// If enabled, the file_deleter will function 'normally'
|
|
|
|
// and will update the database,
|
|
|
|
// but will not actually delete the result output files.
|
|
|
|
// It's equivalent to setting <no_delete/>
|
|
|
|
// for all result output files.
|
|
|
|
//
|
2004-11-29 09:23:51 +00:00
|
|
|
preserve_result_files = true;
|
2003-06-11 23:36:40 +00:00
|
|
|
} else if (!strcmp(argv[i], "-d")) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.set_debug_level(atoi(argv[++i]));
|
2002-12-17 19:00:43 +00:00
|
|
|
} else {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "Unrecognized arg: %s\n", argv[i]);
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-02 21:16:55 +00:00
|
|
|
retval = config.parse_file("..");
|
2002-12-17 19:00:43 +00:00
|
|
|
if (retval) {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "Can't parse config file\n");
|
2002-12-17 19:00:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asynch) {
|
|
|
|
if (fork()) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-08 21:30:47 +00:00
|
|
|
// // Call lock_file after fork(), because file locks are not always inherited
|
|
|
|
// if (lock_file(LOCKFILE)) {
|
2004-04-08 08:15:23 +00:00
|
|
|
// log_messages.printf(SCHED_MSG_LOG::NORMAL, "Another copy of file deleter is running\n");
|
2003-07-08 21:30:47 +00:00
|
|
|
// exit(1);
|
|
|
|
// }
|
|
|
|
// write_pid_file(PIDFILE);
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::NORMAL, "Starting\n");
|
2003-02-27 19:29:48 +00:00
|
|
|
|
2004-01-15 23:53:13 +00:00
|
|
|
retval = boinc_db.open(config.db_name, config.db_host, config.db_user, config.db_passwd);
|
2003-01-09 07:24:27 +00:00
|
|
|
if (retval) {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "can't open DB\n");
|
2003-01-09 07:24:27 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-12-31 23:09:21 +00:00
|
|
|
install_stop_signal_handler();
|
2002-12-17 19:00:43 +00:00
|
|
|
if (one_pass) {
|
|
|
|
do_pass();
|
|
|
|
} else {
|
|
|
|
while (1) {
|
2004-12-06 22:41:19 +00:00
|
|
|
if (!do_pass()) sleep(SLEEP_INTERVAL);
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_bd0d4938a6 = "$Id$";
|