2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2003-07-02 20:57:59 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This 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 2.1 of the License, or (at your option) any later version.
|
2003-07-02 20:57:59 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software 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.
|
2003-01-07 08:11:16 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2003-01-07 08:11:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
// 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"
|
2005-05-26 20:42:35 +00:00
|
|
|
#include "error_numbers.h"
|
2003-02-10 19:51:32 +00:00
|
|
|
#include "util.h"
|
2005-01-08 06:54:03 +00:00
|
|
|
#include "filesys.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"
|
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-05-26 20:23:24 +00:00
|
|
|
// Given a filename, find its full path in the upload directory hierarchy
|
|
|
|
// Return an error if file isn't there.
|
2005-01-03 18:00:53 +00:00
|
|
|
//
|
2005-05-26 20:23:24 +00:00
|
|
|
int get_file_path(char *filename, char* upload_dir, int fanout, char* path) {
|
2005-01-03 18:00:53 +00:00
|
|
|
|
2005-05-26 20:23:24 +00:00
|
|
|
dir_hier_path(filename, upload_dir, fanout, true, path);
|
|
|
|
if (boinc_file_exists(path)) {
|
2005-01-03 18:00:53 +00:00
|
|
|
return 0;
|
2005-05-26 20:23:24 +00:00
|
|
|
}
|
2005-01-03 18:00:53 +00:00
|
|
|
|
|
|
|
// TODO: get rid of the old hash in about 3/2005
|
|
|
|
//
|
2005-05-26 20:23:24 +00:00
|
|
|
dir_hier_path(filename, upload_dir, fanout, false, path);
|
|
|
|
if (boinc_file_exists(path)) {
|
2005-01-03 18:00:53 +00:00
|
|
|
return 0;
|
2005-05-26 20:23:24 +00:00
|
|
|
}
|
|
|
|
return ERR_NOT_FOUND;
|
2005-01-03 18:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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-05-26 20:23:24 +00:00
|
|
|
int count_deleted = 0, retval, mthd_retval = 0;
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2005-05-26 20:23:24 +00:00
|
|
|
if (strstr(wu.name, "nodelete")) return mthd_retval;
|
2005-01-29 23:29:54 +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-05-26 20:23:24 +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) {
|
2005-05-26 20:42:35 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
|
|
|
"[WU#%d] get_file_path: %s: %d\n",
|
|
|
|
wu.id, filename, retval
|
|
|
|
);
|
2004-08-06 11:42:41 +00:00
|
|
|
} else {
|
2005-05-26 20:42:35 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::NORMAL,
|
|
|
|
"[WU#%d] deleting %s\n", wu.id, filename
|
|
|
|
);
|
2004-08-06 11:42:41 +00:00
|
|
|
retval = unlink(pathname);
|
|
|
|
if (retval && strlen(config.download_dir_alt)) {
|
2005-05-26 20:23:24 +00:00
|
|
|
sprintf(pathname, "%s/%s", config.download_dir_alt, filename);
|
|
|
|
retval = unlink(pathname);
|
|
|
|
}
|
|
|
|
if (retval) {
|
2005-05-26 20:42:35 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
|
|
|
"[WU#%d] unlink %s failed: %d\n",
|
|
|
|
wu.id, filename, retval
|
|
|
|
);
|
|
|
|
mthd_retval = ERR_UNLINK;
|
2005-05-26 20:23:24 +00:00
|
|
|
} else {
|
|
|
|
count_deleted++;
|
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
|
|
|
}
|
2005-05-26 20:42:35 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::DEBUG, "[WU#%d] deleted %d file(s)\n", wu.id, count_deleted);
|
2005-05-26 20:23:24 +00:00
|
|
|
return mthd_retval;
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05-26 20:23:24 +00:00
|
|
|
int count_deleted = 0, retval, mthd_retval = 0;
|
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-05-26 20:42:35 +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-05-26 20:42:35 +00:00
|
|
|
"[RESULT#%d] get_file_path: %s: %d\n",
|
|
|
|
result.id, filename, retval
|
2004-08-06 11:42:41 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
retval = unlink(pathname);
|
2005-05-26 20:23:24 +00:00
|
|
|
if (retval) {
|
2005-05-26 20:42:35 +00:00
|
|
|
mthd_retval = ERR_UNLINK;
|
2005-05-26 20:23:24 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
|
|
|
"[RESULT#%d] unlink %s returned %d %s\n",
|
|
|
|
result.id, pathname, retval,
|
|
|
|
(retval && errno)?strerror(errno):""
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
count_deleted++;
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::NORMAL,
|
2005-05-26 20:42:35 +00:00
|
|
|
"[RESULT#%d] unlinked %s\n", result.id, pathname
|
2005-05-26 20:23:24 +00:00
|
|
|
);
|
|
|
|
}
|
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,
|
2005-05-26 20:42:35 +00:00
|
|
|
"[RESULT#%d] deleted %d file(s)\n", result.id, count_deleted
|
2003-09-20 17:38:13 +00:00
|
|
|
);
|
2005-05-26 20:23:24 +00:00
|
|
|
return mthd_retval;
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2005-05-26 20:42:35 +00:00
|
|
|
bool did_something = false, got_error = 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
|
|
|
|
2005-05-26 20:23:24 +00:00
|
|
|
retval = 0;
|
2004-12-20 20:47:25 +00:00
|
|
|
if (!preserve_wu_files) {
|
2005-05-26 20:23:24 +00:00
|
|
|
retval = wu_delete_files(wu);
|
2004-12-20 20:47:25 +00:00
|
|
|
}
|
2005-05-26 20:42:35 +00:00
|
|
|
if (retval) {
|
|
|
|
got_error = true;
|
|
|
|
} else {
|
2005-05-26 20:23:24 +00:00
|
|
|
wu.file_delete_state = FILE_DELETE_DONE;
|
|
|
|
sprintf(buf, "file_delete_state=%d", wu.file_delete_state);
|
|
|
|
retval= wu.update_field(buf);
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
2005-05-26 20:42:35 +00:00
|
|
|
"[WU#%d] update failed: %d\n", wu.id, retval
|
2005-05-26 20:23:24 +00:00
|
|
|
);
|
|
|
|
}
|
2004-10-04 23:59:51 +00:00
|
|
|
}
|
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;
|
2005-05-26 20:23:24 +00:00
|
|
|
retval = 0;
|
2004-12-20 20:47:25 +00:00
|
|
|
if (!preserve_result_files) {
|
2005-05-26 20:23:24 +00:00
|
|
|
retval = result_delete_files(result);
|
|
|
|
}
|
2005-05-26 20:42:35 +00:00
|
|
|
if (retval) {
|
|
|
|
got_error = true;
|
|
|
|
} else {
|
2005-05-26 20:23:24 +00:00
|
|
|
result.file_delete_state = FILE_DELETE_DONE;
|
|
|
|
sprintf(buf, "file_delete_state=%d", result.file_delete_state);
|
|
|
|
retval= result.update_field(buf);
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
2005-05-26 20:42:35 +00:00
|
|
|
"[RESULT#%d] update failed: %d\n", result.id, retval
|
2005-05-26 20:23:24 +00:00
|
|
|
);
|
|
|
|
}
|
2004-12-20 20:47:25 +00:00
|
|
|
}
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
2005-05-26 20:42:35 +00:00
|
|
|
if (got_error) {
|
|
|
|
sleep(SLEEP_INTERVAL);
|
|
|
|
}
|
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$";
|