diff options
author | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2004-05-17 17:09:52 +0800 |
---|---|---|
committer | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2004-05-17 17:09:52 +0800 |
commit | da0575f3a383237dc9192d1c7ea54c7df9a8c8a6 (patch) | |
tree | 06c279106b53759b5dbe8dd6931f54a9fb6f3072 /mbbsd | |
parent | 440d1c00eb77739f182ced3fca9fc312d767cf7f (diff) | |
download | pttbbs-da0575f3a383237dc9192d1c7ea54c7df9a8c8a6.tar pttbbs-da0575f3a383237dc9192d1c7ea54c7df9a8c8a6.tar.gz pttbbs-da0575f3a383237dc9192d1c7ea54c7df9a8c8a6.tar.bz2 pttbbs-da0575f3a383237dc9192d1c7ea54c7df9a8c8a6.tar.lz pttbbs-da0575f3a383237dc9192d1c7ea54c7df9a8c8a6.tar.xz pttbbs-da0575f3a383237dc9192d1c7ea54c7df9a8c8a6.tar.zst pttbbs-da0575f3a383237dc9192d1c7ea54c7df9a8c8a6.zip |
add stampfilefd() to avoid race condition
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@1998 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'mbbsd')
-rw-r--r-- | mbbsd/record.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/mbbsd/record.c b/mbbsd/record.c index c8393f6f..d50fb611 100644 --- a/mbbsd/record.c +++ b/mbbsd/record.c @@ -474,6 +474,37 @@ stampfile(char *fpath, fileheader_t * fh) return 0; } +int +stampfilefd(char *fpath, fileheader_t * fh) +{ + char *ip = fpath; + time_t dtime = COMMON_TIME; + struct tm *ptime; + int fd; + + if (access(fpath, X_OK | R_OK | W_OK)) + mkdir(fpath, 0755); + + while (*(++ip)) + ; + *ip++ = '/'; + + while( 1 ) { + sprintf(ip, "M.%d.A.%3.3X", (int)++dtime, rand() & 0xFFF); + if( (fd = open(fpath, O_CREAT | O_EXCL | O_WRONLY, 0644)) != -1 ) + break; + if( errno != EEXIST ) + return -1; + } + + memset(fh, 0, sizeof(fileheader_t)); + strlcpy(fh->filename, ip, sizeof(fh->filename)); + ptime = localtime(&dtime); + snprintf(fh->date, sizeof(fh->date), + "%2d/%02d", ptime->tm_mon + 1, ptime->tm_mday); + return fd; +} + void stampdir(char *fpath, fileheader_t * fh) { |