2003-01-07 08:11:16 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// 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
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
// file_deleter: deletes files that are no longer needed
|
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
#include <string.h>
|
2002-12-17 19:35:38 +00:00
|
|
|
#include <stdlib.h>
|
2003-01-07 08:11:16 +00:00
|
|
|
#include <time.h>
|
2002-12-17 19:00:43 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "db.h"
|
|
|
|
#include "parse.h"
|
2003-02-10 19:51:32 +00:00
|
|
|
#include "util.h"
|
2002-12-17 19:00:43 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2003-02-10 19:51:32 +00:00
|
|
|
#define LOCKFILE "file_deleter.out"
|
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
CONFIG config;
|
|
|
|
|
2003-01-07 01:02:08 +00:00
|
|
|
void write_log(char* p) {
|
|
|
|
time_t now = time(0);
|
|
|
|
char* timestr = ctime(&now);
|
|
|
|
*(strchr(timestr, '\n')) = 0;
|
|
|
|
fprintf(stderr, "%s: %s", timestr, p);
|
|
|
|
}
|
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
int wu_delete_files(WORKUNIT& wu) {
|
|
|
|
char* p;
|
2003-01-07 01:02:08 +00:00
|
|
|
char filename[256], pathname[256], buf[MAX_BLOB_SIZE], logbuf[256];
|
2002-12-17 19:00:43 +00:00
|
|
|
bool no_delete;
|
|
|
|
|
2002-12-20 02:12:27 +00:00
|
|
|
strcpy(buf,wu.xml_doc);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
sprintf(pathname, "%s/%s", config.download_dir, filename);
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(logbuf, "deleting %s\n", pathname);
|
|
|
|
write_log(logbuf);
|
2002-12-17 19:00:43 +00:00
|
|
|
unlink(pathname);
|
|
|
|
}
|
|
|
|
}
|
2002-12-18 01:34:51 +00:00
|
|
|
p = strtok(0, "\n");
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int result_delete_files(RESULT& result) {
|
|
|
|
char* p;
|
2003-01-07 01:02:08 +00:00
|
|
|
char filename[256], pathname[256], buf[MAX_BLOB_SIZE], logbuf[256];
|
2002-12-17 19:00:43 +00:00
|
|
|
bool no_delete;
|
|
|
|
|
2002-12-20 02:12:27 +00:00
|
|
|
strcpy(buf,result.xml_doc_in);
|
|
|
|
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) {
|
|
|
|
sprintf(pathname, "%s/%s", config.upload_dir, filename);
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(logbuf, "deleting %s\n", pathname);
|
|
|
|
write_log(logbuf);
|
2002-12-17 19:00:43 +00:00
|
|
|
unlink(pathname);
|
|
|
|
}
|
|
|
|
}
|
2002-12-18 01:34:51 +00:00
|
|
|
p = strtok(0, "\n");
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return nonzero if did anything
|
|
|
|
//
|
|
|
|
bool do_pass() {
|
|
|
|
WORKUNIT wu;
|
|
|
|
RESULT result;
|
|
|
|
bool did_something = false;
|
|
|
|
|
|
|
|
wu.file_delete_state = FILE_DELETE_READY;
|
2002-12-18 01:34:51 +00:00
|
|
|
while (!db_workunit_enum_file_delete_state(wu)) {
|
2002-12-17 19:00:43 +00:00
|
|
|
did_something = true;
|
|
|
|
wu_delete_files(wu);
|
|
|
|
wu.file_delete_state = FILE_DELETE_DONE;
|
|
|
|
db_workunit_update(wu);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.file_delete_state = FILE_DELETE_READY;
|
2002-12-18 01:34:51 +00:00
|
|
|
while (!db_result_enum_file_delete_state(result)) {
|
2002-12-17 19:00:43 +00:00
|
|
|
did_something = true;
|
|
|
|
result_delete_files(result);
|
|
|
|
result.file_delete_state = FILE_DELETE_DONE;
|
|
|
|
db_result_update(result);
|
|
|
|
}
|
|
|
|
return did_something;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
int retval;
|
|
|
|
bool asynch = false, one_pass = false;
|
|
|
|
int i;
|
2003-01-07 01:02:08 +00:00
|
|
|
char buf[256];
|
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;
|
|
|
|
} else {
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf, "Unrecognized arg: %s\n", argv[i]);
|
|
|
|
write_log(buf);
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-10 19:51:32 +00:00
|
|
|
if (lock_file(LOCKFILE)) {
|
|
|
|
fprintf(stderr, "Another copy of file deleter is running\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
retval = config.parse_file();
|
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("Can't parse config file\n");
|
2002-12-17 19:00:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asynch) {
|
|
|
|
if (fork()) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
retval = boinc_db_open(config.db_name, config.db_passwd);
|
|
|
|
if (retval) {
|
|
|
|
write_log("can't open DB\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-12-17 19:00:43 +00:00
|
|
|
if (one_pass) {
|
|
|
|
do_pass();
|
|
|
|
} else {
|
|
|
|
while (1) {
|
|
|
|
if (!do_pass()) sleep(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|