diff options
author | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2010-06-26 11:51:08 +0800 |
---|---|---|
committer | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2010-06-26 11:51:08 +0800 |
commit | 37cc143ec53aeafbb38389dac575e460460f8290 (patch) | |
tree | 3432b216a73345e7156dca62f0f431f6d4f81dea | |
parent | 52864f3dcc909f4413ae082c0f29aec6347ea08c (diff) | |
download | pttbbs-37cc143ec53aeafbb38389dac575e460460f8290.tar pttbbs-37cc143ec53aeafbb38389dac575e460460f8290.tar.gz pttbbs-37cc143ec53aeafbb38389dac575e460460f8290.tar.bz2 pttbbs-37cc143ec53aeafbb38389dac575e460460f8290.tar.lz pttbbs-37cc143ec53aeafbb38389dac575e460460f8290.tar.xz pttbbs-37cc143ec53aeafbb38389dac575e460460f8290.tar.zst pttbbs-37cc143ec53aeafbb38389dac575e460460f8290.zip |
* add log rotation and format checkings
git-svn-id: http://opensvn.csie.org/pttbbs/trunk@5070 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r-- | pttbbs/include/proto.h | 15 | ||||
-rw-r--r-- | pttbbs/mbbsd/cal.c | 2 | ||||
-rw-r--r-- | pttbbs/mbbsd/record.c | 54 |
3 files changed, 65 insertions, 6 deletions
diff --git a/pttbbs/include/proto.h b/pttbbs/include/proto.h index 0838f2e5..63feecfb 100644 --- a/pttbbs/include/proto.h +++ b/pttbbs/include/proto.h @@ -124,8 +124,8 @@ const char * postperm_msg(const char *bname); /* cal */ const char* money_level(int money); int vice(int money, const char* item); -int pay(int money, const char *item, ...); -int pay_as(int uid, int money, const char *item, ...); +int pay(int money, const char *item, ...) GCC_CHECK_FORMAT(2,3); +int pay_as(int uid, int money, const char *item, ...) GCC_CHECK_FORMAT(3, 4); int lockutmpmode(int unmode, int state); int unlockutmpmode(void); int x_file(void); @@ -468,17 +468,22 @@ void set_safedel_fhdr(fileheader_t *fhdr, const char *newtitle); #ifndef _BBS_UTIL_C_ void safe_delete_range(const char *fpath, int id1, int id2); #endif -int safe_article_delete(int ent, const fileheader_t *fhdr, const char *direct, const char *newtitle); +int safe_article_delete(int ent, const fileheader_t *fhdr, const char *direct, + const char *newtitle); int safe_article_delete_range(const char *direct, int from, int to); #endif int delete_file(const char *dirname, int size, int ent, int (*filecheck)()); int delete_range(const char *fpath, int id1, int id2); int search_rec(const char* dirname, int (*filecheck)()); -int append_record_forward(char *fpath, fileheader_t *record, int size, const char *origid); +int append_record_forward(char *fpath, fileheader_t *record, int size, + const char *origid); int get_sum_records(const char* fpath, int size); int substitute_ref_record(const char* direct, fileheader_t *fhdr, int ent); -inline int getindex(const char *fpath, fileheader_t *fh, int start); +int rotate_text_logfile(const char *filename, off_t max_size, + float keep_ratio); +int rotate_bin_logfile(const char *filename, off_t record_size, + off_t max_size, float keep_ratio); /* register */ int u_register(void); diff --git a/pttbbs/mbbsd/cal.c b/pttbbs/mbbsd/cal.c index 9956e24f..f06044f0 100644 --- a/pttbbs/mbbsd/cal.c +++ b/pttbbs/mbbsd/cal.c @@ -132,7 +132,7 @@ pay(int money, const char *item, ...) // compatible mode: vice int vice(int money, const char *item) { - return pay(money, item); + return pay(money, "%s", item); } static int diff --git a/pttbbs/mbbsd/record.c b/pttbbs/mbbsd/record.c index fadb9c1e..71a9b256 100644 --- a/pttbbs/mbbsd/record.c +++ b/pttbbs/mbbsd/record.c @@ -438,6 +438,60 @@ append_record_forward(char *fpath, fileheader_t * record, int size, const char * return 0; } +int +rotate_bin_logfile(const char *filename, off_t record_size, + off_t max_size, float keep_ratio) +{ + off_t sz = dashs(filename); + assert(keep_ratio >= 0 && keep_ratio <= 1.0f); + + if (sz < max_size) + return 0; + + // delete from head + delete_records(filename, record_size, 1, + (1 - keep_ratio) * max_size / record_size ); + return 1; +} + +int +rotate_text_logfile(const char *filename, off_t max_size, float keep_ratio) +{ + off_t sz = dashs(filename), newsz; + char *buf, *newent; + FILE *fp; + assert(keep_ratio >= 0 && keep_ratio <= 1.0f); + + if (sz < max_size) + return 0; + + // FIXME we sould lock the file here. + // however since these log are just for reference... + // let's pretend there's no race condition with it. + + // now, calculate a starting seek point + fp = fopen(filename, "r+b"); + fseek(fp, - keep_ratio * max_size, SEEK_END); + newsz = sz - ftell(fp); + buf = (char*)malloc(newsz); + memset(buf, 0, newsz); + assert(buf); + fread(buf, newsz, 1, fp); + fclose(fp); + + // find a newline or \0 + newent = buf; + while (*newent && *newent++ != '\n') ; + + // replace file with new content + fp = fopen(filename, "wb"); + fwrite(newent, 1, newsz - (newent - buf), fp); + fclose(fp); + + free(buf); + return 1; +} + void setaidfile(char *buf, const char *bn, aidu_t aidu) { |