diff --git a/checkin_notes b/checkin_notes index 32f54814cb..f42571bd3f 100755 --- a/checkin_notes +++ b/checkin_notes @@ -22280,3 +22280,14 @@ David 7 Jan 2005 sched/ validate_util.C + +David 7 Jan 2005 + - moved dir hierarchy code from lib/util.C to sched/sched_util.C + - buffer was too small by 1 byte in md5_file.C + + lib/ + md5_file.C + util.C,h + sched/ + file_deleter.C + sched_util.C,h diff --git a/lib/md5_file.C b/lib/md5_file.C index 5b072102cc..e7d8354f14 100644 --- a/lib/md5_file.C +++ b/lib/md5_file.C @@ -78,9 +78,8 @@ int md5_block(const unsigned char* data, int nbytes, char* output) { return 0; } -std::string md5_string(const unsigned char* data, int nbytes) -{ - char output[32]; +std::string md5_string(const unsigned char* data, int nbytes) { + char output[MD5_LEN]; md5_block(data, nbytes, output); return std::string(output); } diff --git a/lib/util.C b/lib/util.C index 7774b4f6e3..d00cc78325 100755 --- a/lib/util.C +++ b/lib/util.C @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include @@ -49,7 +48,6 @@ #include "error_numbers.h" #include "filesys.h" -#include "md5_file.h" #include "util.h" #ifdef _USING_FCGI_ @@ -754,78 +752,6 @@ void update_average( avg_time = now; } -static void filename_hash_old(const char* filename, int fanout, char* dir) { - int sum=0; - const char* p = filename; - - while (*p) sum += *p++; - sum %= fanout; - sprintf(dir, "%x", sum); -} - -static void filename_hash(const char* filename, int fanout, char* dir) { - std::string s = md5_string((const unsigned char*)filename, strlen(filename)); - int x = strtol(s.substr(1, 7).c_str(), 0, 16); - sprintf(dir, "%x", x % fanout); -} - -// given a filename, compute its path in a directory hierarchy -// If create is true, create the directory if needed -// NOTE: this first time around I used a bad hash function. -// During the period of transition to the good hash function, -// programs to look for files (validator, assimilator, file deleter) -// will have to try both the old and new variants. -// We can phase this out after everyone is caught up. -// -int dir_hier_path( - const char* filename, const char* root, int fanout, bool new_hash, - char* path, bool create -) { - char dir[256], dirpath[256]; - int retval; - - if (fanout==0) { - sprintf(path, "%s/%s", root, filename); - return 0; - } - - if (new_hash) { - filename_hash(filename, fanout, dir); - } else { - filename_hash_old(filename, fanout, dir); - } - - sprintf(dirpath, "%s/%s", root, dir); - if (create) { - retval = boinc_mkdir(dirpath); - if (retval && (retval != EEXIST)) { - return ERR_MKDIR; - } - } - sprintf(path, "%s/%s", dirpath, filename); - return 0; -} - -int dir_hier_url( - const char* filename, const char* root, int fanout, bool new_hash, - char* result -) { - char dir[256]; - - if (fanout==0) { - sprintf(result, "%s/%s", root, filename); - return 0; - } - - if (new_hash) { - filename_hash(filename, fanout, dir); - } else { - filename_hash_old(filename, fanout, dir); - } - sprintf(result, "%s/%s/%s", root, dir, filename); - return 0; -} - void mysql_timestamp(double dt, char* p) { struct tm* tmp; time_t t = (time_t)dt; diff --git a/lib/util.h b/lib/util.h index 282db1def8..1b043dcaeb 100755 --- a/lib/util.h +++ b/lib/util.h @@ -119,20 +119,6 @@ extern int boinc_thread_cpu_time(HANDLE thread_handle, double& cpu); extern void update_average(double, double, double, double&, double&); -// convert filename to path in a hierarchical directory system -// -extern int dir_hier_path( - const char* filename, const char* root, int fanout, bool new_hash, - char* result, bool create=false -); - -// convert filename to URL in a hierarchical directory system -// -extern int dir_hier_url( - const char* filename, const char* root, int fanout, bool new_hash, - char* result -); - extern int boinc_calling_thread_cpu_time(double&); // convert UNIX time to MySQL timestamp (yyyymmddhhmmss) diff --git a/sched/file_deleter.C b/sched/file_deleter.C index 7605622f87..25aea4e47d 100644 --- a/sched/file_deleter.C +++ b/sched/file_deleter.C @@ -30,10 +30,11 @@ #include "boinc_db.h" #include "parse.h" #include "util.h" +#include "filesys.h" + #include "sched_config.h" #include "sched_util.h" #include "sched_msgs.h" -#include "../lib/filesys.h" #define LOCKFILE "file_deleter.out" #define PIDFILE "file_deleter.pid" diff --git a/sched/sched_util.C b/sched/sched_util.C index 835e538963..b3184edd3f 100644 --- a/sched/sched_util.C +++ b/sched/sched_util.C @@ -21,12 +21,15 @@ using namespace std; #include #include +#include #include #include #include #include "filesys.h" +#include "md5_file.h" #include "error_numbers.h" + #include "sched_msgs.h" #include "sched_util.h" @@ -121,4 +124,76 @@ void get_log_path(char* p, char* filename) { mkdir(path, 0777); } +static void filename_hash_old(const char* filename, int fanout, char* dir) { + int sum=0; + const char* p = filename; + + while (*p) sum += *p++; + sum %= fanout; + sprintf(dir, "%x", sum); +} + +static void filename_hash(const char* filename, int fanout, char* dir) { + std::string s = md5_string((const unsigned char*)filename, strlen(filename)); + int x = strtol(s.substr(1, 7).c_str(), 0, 16); + sprintf(dir, "%x", x % fanout); +} + +// given a filename, compute its path in a directory hierarchy +// If create is true, create the directory if needed +// NOTE: this first time around I used a bad hash function. +// During the period of transition to the good hash function, +// programs to look for files (validator, assimilator, file deleter) +// will have to try both the old and new variants. +// We can phase this out after everyone is caught up. +// +int dir_hier_path( + const char* filename, const char* root, int fanout, bool new_hash, + char* path, bool create +) { + char dir[256], dirpath[256]; + int retval; + + if (fanout==0) { + sprintf(path, "%s/%s", root, filename); + return 0; + } + + if (new_hash) { + filename_hash(filename, fanout, dir); + } else { + filename_hash_old(filename, fanout, dir); + } + + sprintf(dirpath, "%s/%s", root, dir); + if (create) { + retval = boinc_mkdir(dirpath); + if (retval && (retval != EEXIST)) { + return ERR_MKDIR; + } + } + sprintf(path, "%s/%s", dirpath, filename); + return 0; +} + +int dir_hier_url( + const char* filename, const char* root, int fanout, bool new_hash, + char* result +) { + char dir[256]; + + if (fanout==0) { + sprintf(result, "%s/%s", root, filename); + return 0; + } + + if (new_hash) { + filename_hash(filename, fanout, dir); + } else { + filename_hash_old(filename, fanout, dir); + } + sprintf(result, "%s/%s/%s", root, dir, filename); + return 0; +} + const char *BOINC_RCSID_affa6ef1e4 = "$Id$"; diff --git a/sched/sched_util.h b/sched/sched_util.h index dfc12aca30..a4a1ff6b74 100644 --- a/sched/sched_util.h +++ b/sched/sched_util.h @@ -36,4 +36,18 @@ extern void install_stop_signal_handler(); extern int try_fopen(char* path, FILE*& f, char* mode); extern void get_log_path(char*, char*); +// convert filename to path in a hierarchical directory system +// +extern int dir_hier_path( + const char* filename, const char* root, int fanout, bool new_hash, + char* result, bool create=false +); + +// convert filename to URL in a hierarchical directory system +// +extern int dir_hier_url( + const char* filename, const char* root, int fanout, bool new_hash, + char* result +); + #endif