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
|
|
|
|
|
2005-08-20 02:45:49 +00:00
|
|
|
#include <list>
|
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>
|
2005-08-20 02:45:49 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2005-08-22 19:29:27 +00:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
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"
|
2005-08-24 21:55:37 +00:00
|
|
|
#include "strings.h"
|
2005-01-08 06:54:03 +00:00
|
|
|
|
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
|
|
|
|
2005-08-20 02:45:49 +00:00
|
|
|
using namespace std;
|
|
|
|
|
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
|
2005-08-25 23:07:58 +00:00
|
|
|
#define RESULTS_PER_WU 4 // an estimate of redundancy
|
2004-12-06 22:41:19 +00:00
|
|
|
|
2003-09-02 21:16:55 +00:00
|
|
|
SCHED_CONFIG config;
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2005-07-26 22:14:55 +00:00
|
|
|
int id_modulus=0, id_remainder=0;
|
|
|
|
|
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-08-25 23:07:58 +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-08-25 23:07:58 +00:00
|
|
|
sprintf(pathname, "%s/%s", config.download_dir_alt, filename);
|
|
|
|
retval = unlink(pathname);
|
2005-05-26 20:23:24 +00:00
|
|
|
}
|
|
|
|
if (retval) {
|
2005-08-25 23:07:58 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
2005-05-26 20:42:35 +00:00
|
|
|
"[WU#%d] unlink %s failed: %d\n",
|
|
|
|
wu.id, filename, retval
|
|
|
|
);
|
2005-08-25 23:07:58 +00:00
|
|
|
mthd_retval = ERR_UNLINK;
|
2005-05-26 20:23:24 +00:00
|
|
|
} else {
|
2005-08-25 23:07:58 +00:00
|
|
|
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) {
|
2005-08-20 02:45:49 +00:00
|
|
|
// the fact that no result files were found is a critical
|
|
|
|
// error if this was a successful result, but is to be
|
|
|
|
// expected if the result outcome was failure, since in
|
|
|
|
// that case there may well be no output file produced.
|
|
|
|
//
|
|
|
|
int debug_or_crit;
|
|
|
|
if (RESULT_OUTCOME_SUCCESS == result.outcome) {
|
|
|
|
debug_or_crit=SCHED_MSG_LOG::CRITICAL;
|
|
|
|
} else {
|
|
|
|
debug_or_crit=SCHED_MSG_LOG::DEBUG;
|
|
|
|
}
|
|
|
|
log_messages.printf(debug_or_crit,
|
|
|
|
"[RESULT#%d] outcome=%d client_state=%d No file %s to delete\n",
|
|
|
|
result.id, result.outcome, result.client_state, filename
|
2004-08-06 11:42:41 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
retval = unlink(pathname);
|
2005-05-26 20:23:24 +00:00
|
|
|
if (retval) {
|
2005-08-25 23:07:58 +00:00
|
|
|
mthd_retval = ERR_UNLINK;
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
|
|
|
"[RESULT#%d] unlink %s returned %d %s\n",
|
|
|
|
result.id, pathname, retval,
|
|
|
|
(retval && errno)?strerror(errno):""
|
2005-05-26 20:23:24 +00:00
|
|
|
);
|
|
|
|
} else {
|
2005-08-25 23:07:58 +00:00
|
|
|
count_deleted++;
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::NORMAL,
|
|
|
|
"[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
|
|
|
|
//
|
2005-06-02 07:04:29 +00:00
|
|
|
bool do_pass(bool retry_error) {
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_WORKUNIT wu;
|
|
|
|
DB_RESULT result;
|
2005-06-02 07:04:29 +00:00
|
|
|
bool did_something = false;
|
2003-06-04 17:21:26 +00:00
|
|
|
char buf[256];
|
2005-07-26 22:14:55 +00:00
|
|
|
char mod_clause[256];
|
2004-10-04 23:59:51 +00:00
|
|
|
int retval;
|
2005-08-11 23:53:24 +00:00
|
|
|
int result_loop_count;
|
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
|
|
|
|
2005-07-26 22:14:55 +00:00
|
|
|
if (id_modulus) {
|
|
|
|
sprintf(mod_clause, " and id %% %d = %d ",
|
|
|
|
id_modulus, id_remainder
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
strcpy(mod_clause, "");
|
|
|
|
}
|
|
|
|
|
2005-06-02 07:04:29 +00:00
|
|
|
if (retry_error) {
|
2005-08-25 23:07:58 +00:00
|
|
|
sprintf(buf, "where file_delete_state=%d or file_delete_state=%d %s limit 1000", FILE_DELETE_READY, FILE_DELETE_ERROR, mod_clause);
|
2005-06-02 07:04:29 +00:00
|
|
|
} else {
|
2005-08-25 23:07:58 +00:00
|
|
|
sprintf(buf, "where file_delete_state=%d %s limit 1000", FILE_DELETE_READY, mod_clause);
|
2005-06-02 07:04:29 +00:00
|
|
|
}
|
2003-06-04 17:21:26 +00:00
|
|
|
while (!wu.enumerate(buf)) {
|
2002-12-17 19:00:43 +00:00
|
|
|
did_something = true;
|
2005-08-20 02:45:49 +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) {
|
2005-08-25 23:07:58 +00:00
|
|
|
wu.file_delete_state = FILE_DELETE_ERROR;
|
2005-06-02 07:04:29 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "[WU#%d] update failed: %d\n", wu.id, retval);
|
2005-05-26 20:42:35 +00:00
|
|
|
} else {
|
2005-08-25 23:07:58 +00:00
|
|
|
wu.file_delete_state = FILE_DELETE_DONE;
|
2004-10-04 23:59:51 +00:00
|
|
|
}
|
2005-06-02 07:04:29 +00:00
|
|
|
sprintf(buf, "file_delete_state=%d", wu.file_delete_state);
|
|
|
|
retval= wu.update_field(buf);
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
|
2005-08-11 23:53:24 +00:00
|
|
|
for (result_loop_count=0; result_loop_count < RESULTS_PER_WU; result_loop_count++) {
|
|
|
|
|
|
|
|
if ( retry_error ) {
|
2005-08-25 23:07:58 +00:00
|
|
|
sprintf(buf, "where file_delete_state=%d or file_delete_state=%d %s limit 1000",
|
|
|
|
FILE_DELETE_READY, FILE_DELETE_ERROR, mod_clause);
|
2005-08-11 23:53:24 +00:00
|
|
|
} else {
|
2005-08-25 23:07:58 +00:00
|
|
|
sprintf(buf, "where file_delete_state=%d %s limit 1000", FILE_DELETE_READY, mod_clause);
|
2005-05-26 20:23:24 +00:00
|
|
|
}
|
2005-08-11 23:53:24 +00:00
|
|
|
|
|
|
|
while (!result.enumerate(buf)) {
|
|
|
|
did_something = true;
|
|
|
|
retval = 0;
|
|
|
|
if (!preserve_result_files) {
|
|
|
|
retval = result_delete_files(result);
|
|
|
|
}
|
|
|
|
if (retval) {
|
2005-08-25 23:07:58 +00:00
|
|
|
result.file_delete_state = FILE_DELETE_ERROR;
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "[RESULT#%d] update failed: %d\n", result.id, retval);
|
2005-08-11 23:53:24 +00:00
|
|
|
} else {
|
2005-08-25 23:07:58 +00:00
|
|
|
result.file_delete_state = FILE_DELETE_DONE;
|
2005-08-11 23:53:24 +00:00
|
|
|
}
|
2005-08-25 23:07:58 +00:00
|
|
|
sprintf(buf, "file_delete_state=%d", result.file_delete_state);
|
|
|
|
retval= result.update_field(buf);
|
2005-08-08 09:11:37 +00:00
|
|
|
}
|
2005-08-11 23:53:24 +00:00
|
|
|
}
|
2005-08-08 09:11:37 +00:00
|
|
|
|
2005-08-11 23:53:24 +00:00
|
|
|
return did_something;
|
2005-08-08 09:11:37 +00:00
|
|
|
}
|
|
|
|
|
2005-08-20 02:45:49 +00:00
|
|
|
|
|
|
|
struct FILE_RECORD {
|
|
|
|
string name;
|
|
|
|
int date_modified;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool operator == (const FILE_RECORD& fr1, const FILE_RECORD& fr2) {
|
|
|
|
return (fr1.date_modified == fr2.date_modified && fr1.name == fr2.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator < (const FILE_RECORD& fr1, const FILE_RECORD& fr2) {
|
|
|
|
if (fr1.date_modified < fr2.date_modified)
|
|
|
|
return true;
|
|
|
|
else if (fr1.date_modified > fr2.date_modified)
|
|
|
|
return false;
|
|
|
|
else if (fr1.name < fr2.name)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// list of antique files to delete. If order is
|
|
|
|
// NOT sorted by mod time (primary key) and name
|
|
|
|
// (secondary key) then unsorted==true.
|
|
|
|
//
|
|
|
|
std::list<FILE_RECORD> files_to_delete;
|
|
|
|
bool unsorted = true;
|
|
|
|
|
|
|
|
// delete files from list of old files. Returns number
|
|
|
|
// of files deleted, or negative for error.
|
|
|
|
int delete_antique_files(int max_to_delete) {
|
|
|
|
int nfiles=0;
|
|
|
|
|
|
|
|
if (files_to_delete.empty()) return 0;
|
|
|
|
|
|
|
|
// sort list and eliminate duplicates
|
|
|
|
//
|
|
|
|
if (unsorted) {
|
|
|
|
files_to_delete.sort();
|
|
|
|
files_to_delete.unique();
|
|
|
|
unsorted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (nfiles<max_to_delete && !files_to_delete.empty()) {
|
|
|
|
char timestamp[128];
|
|
|
|
char pathname[1024];
|
|
|
|
int retval;
|
|
|
|
|
2005-08-25 23:07:58 +00:00
|
|
|
FILE_RECORD fr = files_to_delete.front();
|
|
|
|
check_stop_daemons();
|
2005-08-20 02:45:49 +00:00
|
|
|
|
2005-08-25 23:07:58 +00:00
|
|
|
retval = get_file_path(
|
|
|
|
(char*)fr.name.c_str(), config.upload_dir,
|
|
|
|
config.uldl_dir_fanout, pathname
|
|
|
|
);
|
2005-08-20 02:45:49 +00:00
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
|
|
|
"get_file_path(%s) failed: %d\n",
|
|
|
|
fr.name.c_str(), retval
|
|
|
|
);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-08-25 23:07:58 +00:00
|
|
|
strcpy(timestamp, time_to_string(fr.date_modified));
|
2005-08-20 02:45:49 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::DEBUG,
|
|
|
|
"deleting [antique %s] %s\n",
|
|
|
|
timestamp, pathname
|
|
|
|
);
|
|
|
|
if (unlink(pathname)) {
|
2005-08-25 23:07:58 +00:00
|
|
|
int save_error=errno;
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
|
|
|
"unlink(%s) failed: %s\n",
|
|
|
|
pathname, strerror(save_error)
|
2005-08-20 02:45:49 +00:00
|
|
|
);
|
2005-08-25 23:07:58 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
2005-08-20 02:45:49 +00:00
|
|
|
nfiles++;
|
2005-08-25 23:07:58 +00:00
|
|
|
files_to_delete.pop_front();
|
|
|
|
}
|
2005-08-20 02:45:49 +00:00
|
|
|
} // while
|
|
|
|
return nfiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// construct a list of old files. return number of files
|
|
|
|
// added to list, or negative number for error.
|
|
|
|
//
|
|
|
|
int add_antiques_to_list(int days) {
|
|
|
|
char command[256];
|
|
|
|
char single_line[1024];
|
|
|
|
FILE *fp;
|
|
|
|
int dirlen=strlen(config.upload_dir);
|
|
|
|
struct passwd *apache_info=getpwnam("apache");
|
|
|
|
int del_time=time(0)-86400*days;
|
|
|
|
int nfiles=0;
|
|
|
|
|
|
|
|
if (!apache_info) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "no user named 'apache' found!\n");
|
2005-08-25 23:07:58 +00:00
|
|
|
return -1;
|
2005-08-20 02:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(command, "find %s -type f -mtime +%d", config.upload_dir, days);
|
|
|
|
|
|
|
|
// Now execute the command, read output on a stream. We could use
|
|
|
|
// find to also exec a 'delete' command. But we want to log all
|
|
|
|
// file names into the log, and do lots of sanity checking, so
|
|
|
|
// this way is better.
|
|
|
|
//
|
|
|
|
if (!(fp=popen(command, "r"))) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "command %s failed\n", command);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(single_line, 1024, fp)) {
|
|
|
|
char pathname[1024];
|
2005-08-25 23:07:58 +00:00
|
|
|
char *fname_at_end=NULL;
|
|
|
|
int nchars=strlen(single_line);
|
|
|
|
struct stat statbuf;
|
|
|
|
const char *err=NULL;
|
2005-08-20 02:45:49 +00:00
|
|
|
FILE_RECORD fr;
|
|
|
|
|
2005-08-25 23:07:58 +00:00
|
|
|
// We can interrupt this at any point.
|
|
|
|
// pclose() is called when process exits.
|
2005-08-20 02:45:49 +00:00
|
|
|
check_stop_daemons();
|
|
|
|
|
2005-08-25 23:07:58 +00:00
|
|
|
// Do serious sanity checking on the path before
|
|
|
|
// adding the file!!
|
|
|
|
//
|
|
|
|
if (!err && nchars > 1022) err="line too long";
|
|
|
|
if (!err && nchars < dirlen + 1) err="path shorter than upload directory name";
|
|
|
|
if (!err && single_line[nchars-1] != '\n') err="no newline terminator in line";
|
|
|
|
if (!err && strncmp(config.upload_dir, single_line, dirlen)) err="upload directory not in path";
|
|
|
|
if (!err && single_line[dirlen] != '/') err="no slash separator in path";
|
|
|
|
if (!err) single_line[nchars-1]='\0';
|
|
|
|
if (!err && stat(single_line, &statbuf)) err="stat failed";
|
|
|
|
if (!err && statbuf.st_mtime > del_time) err="file too recent";
|
|
|
|
if (!err && apache_info->pw_uid != statbuf.st_uid) err="file not owned by httpd user";
|
|
|
|
if (!err && !(fname_at_end=rindex(single_line+dirlen, '/'))) err="no trailing filename";
|
|
|
|
if (!err) fname_at_end++;
|
|
|
|
if (!err && !strlen(fname_at_end)) err="trailing filename too short";
|
|
|
|
if (!err && get_file_path(fname_at_end, config.upload_dir, config.uldl_dir_fanout, pathname)) err="get_file_path() failed";
|
|
|
|
if (!err && strcmp(pathname, single_line)) err="file in wrong hierarchical upload subdirectory";
|
|
|
|
|
|
|
|
if (err) {
|
2005-08-20 02:45:49 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
2005-08-25 23:07:58 +00:00
|
|
|
"Can't list %s for deletion: %s\n",
|
|
|
|
single_line, err
|
2005-08-20 02:45:49 +00:00
|
|
|
);
|
2005-08-25 23:07:58 +00:00
|
|
|
// This file deleting business is SERIOUS. Give up at the
|
|
|
|
// first sign of ANYTHING amiss.
|
|
|
|
//
|
|
|
|
pclose(fp);
|
|
|
|
return -3;
|
|
|
|
}
|
2005-08-20 02:45:49 +00:00
|
|
|
|
|
|
|
// insert this file onto the list
|
2005-08-25 23:07:58 +00:00
|
|
|
fr.date_modified = statbuf.st_mtime;
|
|
|
|
fr.name = fname_at_end;
|
|
|
|
files_to_delete.push_back(fr);
|
2005-08-20 02:45:49 +00:00
|
|
|
unsorted = true;
|
|
|
|
nfiles++;
|
|
|
|
|
|
|
|
} // while (fgets(single_line, 1024, fp)) {
|
|
|
|
pclose(fp);
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::DEBUG,
|
|
|
|
"Found %d antique files to delete\n",
|
|
|
|
nfiles
|
|
|
|
);
|
|
|
|
return nfiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns number of files found & added, or negative for error.
|
|
|
|
//
|
|
|
|
int find_antique_files() {
|
|
|
|
char buf[256];
|
|
|
|
DB_WORKUNIT wu;
|
|
|
|
static int nextrun=0;
|
|
|
|
|
|
|
|
check_stop_daemons();
|
|
|
|
|
|
|
|
// list files once per day
|
|
|
|
//
|
|
|
|
if (time(0) < nextrun) return 0;
|
|
|
|
nextrun = time(0)+86400;
|
|
|
|
|
|
|
|
// Find the oldest workunit. We could add "where
|
|
|
|
// file_delete_state!=FILE_DELETE_DONE" to the query, but this
|
|
|
|
// might create some race condition with the 'regular' file delete
|
|
|
|
// mechanism, so better to do it like this.
|
|
|
|
//
|
|
|
|
sprintf(buf, "order by create_time limit 1");
|
|
|
|
if (!wu.enumerate(buf)) {
|
|
|
|
// Don't ever delete files younger than a month.
|
|
|
|
//
|
|
|
|
int days = 1 + (time(0) - wu.create_time)/86400;
|
|
|
|
if (days<31) days=31;
|
|
|
|
|
2005-08-25 23:07:58 +00:00
|
|
|
return add_antiques_to_list(days);
|
2005-08-20 02:45:49 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool do_antique_pass(bool& delete_antiques) {
|
|
|
|
bool did_something=false;
|
|
|
|
|
|
|
|
if (!delete_antiques) return did_something;
|
|
|
|
|
|
|
|
// If any problems appear in deleting antique files
|
|
|
|
// immediately DISABLE this feature.
|
|
|
|
//
|
|
|
|
int howmany = find_antique_files();
|
|
|
|
if (howmany < 0) {
|
|
|
|
delete_antiques=false;
|
|
|
|
return did_something;
|
|
|
|
}
|
|
|
|
|
|
|
|
howmany = delete_antique_files(10);
|
|
|
|
if (howmany < 0) delete_antiques=false;
|
|
|
|
if (howmany > 0) did_something=true;
|
|
|
|
|
|
|
|
return did_something;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
int retval;
|
2005-08-20 02:45:49 +00:00
|
|
|
bool asynch = false, one_pass = false, retry_error = false, delete_antiques = false;
|
2002-12-17 19:00:43 +00:00
|
|
|
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;
|
2005-06-02 07:04:29 +00:00
|
|
|
} else if (!strcmp(argv[i], "-retry_error")) {
|
|
|
|
retry_error=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]));
|
2005-07-26 22:14:55 +00:00
|
|
|
} else if (!strcmp(argv[i], "-mod")) {
|
|
|
|
id_modulus = atoi(argv[++i]);
|
|
|
|
id_remainder = atoi(argv[++i]);
|
2005-08-20 02:45:49 +00:00
|
|
|
} else if (!strcmp(argv[i], "-delete_antiques")) {
|
|
|
|
// If turned on, look for and then delete files in the
|
|
|
|
// upload/ directory which are OLDER than any workunit in
|
|
|
|
// the database. Such files are ones that were uploaded
|
|
|
|
// by hosts which turned in results far after the
|
|
|
|
// deadline. These files are not deleted from the upload
|
|
|
|
// directory unless you have this option enabled.
|
|
|
|
delete_antiques = true;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-26 22:14:55 +00:00
|
|
|
if (id_modulus) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::DEBUG, "Using mod'ed WU/result enumeration. modulus = %d remainder = %d\n",
|
|
|
|
id_modulus, id_remainder);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2005-07-17 19:52:44 +00:00
|
|
|
while (1) {
|
|
|
|
if (!do_pass(retry_error)) {
|
|
|
|
if (one_pass) break;
|
2005-08-20 02:45:49 +00:00
|
|
|
if (!do_antique_pass(delete_antiques)) {
|
|
|
|
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$";
|