X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Ffb-temp.c;h=896870ea11e133033789c59c51096f2eca2b553a;hb=117c8e6725fca812c6ef9ebc26d3133f980ecb7e;hp=4d924d504928fc10b0a9c3d83947f8c3349dca6d;hpb=0aa0feda15b70ddcd717ee1421b2c39ba196e581;p=libucw.git diff --git a/ucw/fb-temp.c b/ucw/fb-temp.c index 4d924d50..896870ea 100644 --- a/ucw/fb-temp.c +++ b/ucw/fb-temp.c @@ -1,83 +1,18 @@ /* * UCW Library -- Temporary Fastbufs * - * (c) 2002--2007 Martin Mares + * (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/fastbuf.h" -#include "ucw/threads.h" +#include +#include #include -#include -#include -#include -#include -#include - -static char *temp_prefix; -static int public_dir = 1; - -static struct cf_section temp_config = { - CF_ITEMS { - 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 *prefix; - int free_prefix = 0; - if (temp_prefix) - prefix = temp_prefix; - else - { - char *env = getenv("TMPDIR"); - if (env) - { - prefix = xmalloc(strlen(env) + 6); - sprintf(prefix, "%s/temp", env); - free_prefix = 1; - } - else - prefix = "/tmp/temp"; - } - if (public_dir) - { - struct timeval tv; - if (gettimeofday(&tv, NULL)) - die("Could not generate temp file name: %m"); - sprintf(name_buf, "%s-%u", 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) - sprintf(name_buf, "%s%d-%d", temp_prefix, pid, cnt); - else - sprintf(name_buf, "%s%d-%d-%d", temp_prefix, pid, ctx->thread_id, cnt); - if (open_flags) - *open_flags = 0; - } - if (free_prefix) - xfree(prefix); -} +#include struct fastbuf * bopen_tmp_file(struct fb_params *params) @@ -89,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 = 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) { @@ -115,13 +35,13 @@ void bfix_tmp_file(struct fastbuf *fb, const char *name) int was_temp = bconfig(fb, BCONFIG_IS_TEMP_FILE, 0); ASSERT(was_temp == 1); if (rename(fb->name, name)) - die("Cannot rename %s to %s: %m", fb->name, name); + bthrow(fb, "tmp", "Cannot rename %s to %s: %m", fb->name, name); bclose(fb); } #ifdef TEST -#include "ucw/getopt.h" +#include int main(int argc, char **argv) { @@ -130,6 +50,7 @@ int main(int argc, char **argv) die("Hey, whaddya want?"); struct fastbuf *f = bopen_tmp(65536); + ASSERT(f && f->name); bputsn(f, "Hello, world!"); bclose(f); return 0;