From: Martin Mares Date: Sat, 19 Jul 2008 22:23:36 +0000 (+0200) Subject: Libucw: Split ucw/fb-temp.c. X-Git-Tag: holmes-import~379 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=cf90f199ff58a92dea4e52bbac0c2e91b37b58dc;p=libucw.git Libucw: Split ucw/fb-temp.c. Moved the low-level temporary file functions that do not depend on fastbufs to ucw/tempfile.c. --- diff --git a/ucw/Makefile b/ucw/Makefile index 68094263..8cd63745 100644 --- a/ucw/Makefile +++ b/ucw/Makefile @@ -15,7 +15,7 @@ LIBUCW_MODS= \ ipaccess \ profile \ fastbuf ff-binary ff-string ff-printf ff-unicode \ - fb-file carefulio fb-mem fb-temp fb-mmap fb-limfd fb-buffer fb-grow fb-pool fb-atomic fb-param fb-socket \ + fb-file carefulio fb-mem fb-temp tempfile fb-mmap fb-limfd fb-buffer fb-grow fb-pool fb-atomic fb-param fb-socket \ char-cat char-upper char-lower unicode stkstring \ wildmatch regex \ prime primetable random timer randomkey \ diff --git a/ucw/fb-temp.c b/ucw/fb-temp.c index c9b34ba5..f3c574b6 100644 --- a/ucw/fb-temp.c +++ b/ucw/fb-temp.c @@ -9,67 +9,10 @@ */ #include "ucw/lib.h" -#include "ucw/conf.h" #include "ucw/fastbuf.h" -#include "ucw/threads.h" -#include "ucw/lfs.h" #include -#include -#include -#include -#include -#include - -static char *temp_prefix = "temp"; -static char *temp_dir; -static int public_dir = 1; - -static struct cf_section temp_config = { - CF_ITEMS { - CF_STRING("Dir", &temp_dir), - CF_STRING("Prefix", &temp_prefix), - CF_INT("PublicDir", &public_dir), - CF_END - } -}; - -static void CONSTRUCTOR temp_global_init(void) -{ - cf_declare_section("Tempfiles", &temp_config, 0); -} - -void -temp_file_name(char *name_buf, int *open_flags) -{ - char *dir = temp_dir; - if (!dir && !(dir = getenv("TMPDIR"))) - dir = "/tmp"; - - int len; - if (public_dir) - { - struct timeval tv; - if (gettimeofday(&tv, NULL)) - die("gettimeofday() failed: %m"); - len = snprintf(name_buf, TEMP_FILE_NAME_LEN, "%s/%s%u", dir, temp_prefix, (uns) tv.tv_usec); - if (open_flags) - *open_flags = O_EXCL; - } - else - { - struct ucwlib_context *ctx = ucwlib_thread_context(); - int cnt = ++ctx->temp_counter; - int pid = getpid(); - if (ctx->thread_id == pid) - len = snprintf(name_buf, TEMP_FILE_NAME_LEN, "%s/%s%d-%d", dir, temp_prefix, pid, cnt); - else - len = snprintf(name_buf, TEMP_FILE_NAME_LEN, "%s/%s%d-%d-%d", dir, temp_prefix, pid, ctx->thread_id, cnt); - if (open_flags) - *open_flags = 0; - } - ASSERT(len < TEMP_FILE_NAME_LEN); -} +#include struct fastbuf * bopen_tmp_file(struct fb_params *params) @@ -81,21 +24,6 @@ bopen_tmp_file(struct fb_params *params) return fb; } -int -open_tmp(char *name_buf, int open_flags, int mode) -{ - int create_flags, fd, retry = 10; - do - { - temp_file_name(name_buf, &create_flags); - fd = ucw_open(name_buf, open_flags | create_flags, mode); - } - while (fd < 0 && errno == EEXIST && retry --); - if (fd < 0) - die("Unable to create temp file %s: %m", name_buf); - return fd; -} - struct fastbuf * bopen_tmp(uns buflen) { diff --git a/ucw/tempfile.c b/ucw/tempfile.c new file mode 100644 index 00000000..f8b0294b --- /dev/null +++ b/ucw/tempfile.c @@ -0,0 +1,106 @@ +/* + * UCW Library -- Temporary Files + * + * (c) 2002--2008 Martin Mares + * (c) 2008 Michal Vaner + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "ucw/lib.h" +#include "ucw/conf.h" +#include "ucw/threads.h" +#include "ucw/lfs.h" +#include "ucw/fastbuf.h" + +#include +#include +#include +#include +#include +#include + +static char *temp_prefix = "temp"; +static char *temp_dir; +static int public_dir = 1; + +static struct cf_section temp_config = { + CF_ITEMS { + CF_STRING("Dir", &temp_dir), + CF_STRING("Prefix", &temp_prefix), + CF_INT("PublicDir", &public_dir), + CF_END + } +}; + +static void CONSTRUCTOR temp_global_init(void) +{ + cf_declare_section("Tempfiles", &temp_config, 0); +} + +void +temp_file_name(char *name_buf, int *open_flags) +{ + char *dir = temp_dir; + if (!dir && !(dir = getenv("TMPDIR"))) + dir = "/tmp"; + + int len; + if (public_dir) + { + struct timeval tv; + if (gettimeofday(&tv, NULL)) + die("gettimeofday() failed: %m"); + len = snprintf(name_buf, TEMP_FILE_NAME_LEN, "%s/%s%u", dir, temp_prefix, (uns) tv.tv_usec); + if (open_flags) + *open_flags = O_EXCL; + } + else + { + struct ucwlib_context *ctx = ucwlib_thread_context(); + int cnt = ++ctx->temp_counter; + int pid = getpid(); + if (ctx->thread_id == pid) + len = snprintf(name_buf, TEMP_FILE_NAME_LEN, "%s/%s%d-%d", dir, temp_prefix, pid, cnt); + else + len = snprintf(name_buf, TEMP_FILE_NAME_LEN, "%s/%s%d-%d-%d", dir, temp_prefix, pid, ctx->thread_id, cnt); + if (open_flags) + *open_flags = 0; + } + ASSERT(len < TEMP_FILE_NAME_LEN); +} + +int +open_tmp(char *name_buf, int open_flags, int mode) +{ + int create_flags, fd, retry = 10; + do + { + temp_file_name(name_buf, &create_flags); + fd = ucw_open(name_buf, open_flags | create_flags, mode); + } + while (fd < 0 && errno == EEXIST && retry --); + if (fd < 0) + die("Unable to create temp file %s: %m", name_buf); + return fd; +} + +#ifdef TEST + +#include "ucw/getopt.h" + +int main(int argc, char **argv) +{ + log_init(NULL); + if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0) + die("Hey, whaddya want?"); + + char buf[TEMP_FILE_NAME_LEN]; + int fd = open_tmp(buf, O_RDWR | O_CREAT | O_TRUNC, 0666); + close(fd); + unlink(buf); + return 0; +} + +#endif