static volatile const char *BOINCrcsid="$Id$"; // The contents of this file are subject to the BOINC 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://boinc.berkeley.edu/license_1.0.txt // // 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 #include #include #include #include #include "boinc_db.h" #include "parse.h" #include "util.h" #include "sched_config.h" #include "sched_util.h" #include "sched_msgs.h" #define LOCKFILE "file_deleter.out" #define PIDFILE "file_deleter.pid" SCHED_CONFIG config; int wu_delete_files(WORKUNIT& wu) { char* p; char filename[256], pathname[256], buf[LARGE_BLOB_SIZE]; bool no_delete=false; int count_deleted = 0, retval; safe_strcpy(buf, wu.xml_doc); p = strtok(buf, "\n"); strcpy(filename, ""); while (p) { if (parse_str(p, "", filename, sizeof(filename))) { } else if (match_tag(p, "")) { no_delete = false; strcpy(filename, ""); } else if (match_tag(p, "")) { no_delete = true; } else if (match_tag(p, "")) { if (!no_delete) { retval = dir_hier_path( filename, config.download_dir, config.uldl_dir_fanout, pathname ); if (retval) { 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; } } } p = strtok(0, "\n"); } log_messages.printf(SCHED_MSG_LOG::DEBUG, "[%s] deleted %d file(s)\n", wu.name, count_deleted); return 0; } int result_delete_files(RESULT& result) { char* p; char filename[256], pathname[256], buf[LARGE_BLOB_SIZE]; bool no_delete=false; int count_deleted = 0, retval; safe_strcpy(buf, result.xml_doc_in); p = strtok(buf,"\n"); while (p) { if (parse_str(p, "", filename, sizeof(filename))) { } else if (match_tag(p, "")) { no_delete = false; strcpy(filename, ""); } else if (match_tag(p, "")) { no_delete = true; } else if (match_tag(p, "")) { if (!no_delete) { retval = dir_hier_path( filename, config.upload_dir, config.uldl_dir_fanout, pathname ); if (retval) { log_messages.printf(SCHED_MSG_LOG::CRITICAL, "[%s] dir_hier_path: %d\n", result.name, retval ); } else { retval = unlink(pathname); ++count_deleted; log_messages.printf(SCHED_MSG_LOG::NORMAL, "[%s] unlinked %s; retval %d\n", result.name, filename, retval ); } } } p = strtok(0, "\n"); } log_messages.printf(SCHED_MSG_LOG::DEBUG, "[%s] deleted %d file(s)\n", result.name, count_deleted ); return 0; } // return nonzero if did anything // bool do_pass() { DB_WORKUNIT wu; DB_RESULT result; bool did_something = false; char buf[256]; int retval; check_stop_daemons(); sprintf(buf, "where file_delete_state=%d limit 1000", FILE_DELETE_READY); while (!wu.enumerate(buf)) { did_something = true; wu_delete_files(wu); 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, "[%s] workunit failed to update file_delete_state with %d\n", wu.name, retval ); } } sprintf(buf, "where file_delete_state=%d limit 1000", FILE_DELETE_READY); while (!result.enumerate(buf)) { did_something = true; result_delete_files(result); 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, "[%s] result failed to update file_delete_state with %d\n", result.name, retval ); } } return did_something; } int main(int argc, char** argv) { int retval; bool asynch = false, one_pass = false; int i; check_stop_daemons(); for (i=1; i