X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=ucw%2Ffb-temp.c;h=3ffa08c3fec99644ba1efac9cc5283e17093e99d;hb=f1155256f7a168f5e2c0097cb4e7197b79c4f041;hp=d6546e38e3879150b39c94fa43da30b2ea9e2070;hpb=031256ad2e123eec58521f8e3eb9496c197641d2;p=libucw.git diff --git a/ucw/fb-temp.c b/ucw/fb-temp.c index d6546e38..3ffa08c3 100644 --- a/ucw/fb-temp.c +++ b/ucw/fb-temp.c @@ -1,59 +1,31 @@ /* * 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 - -static char *temp_prefix = "/tmp/temp"; - -static struct cf_section temp_config = { - CF_ITEMS { - CF_STRING("Prefix", &temp_prefix), - CF_END - } -}; - -static void CONSTRUCTOR temp_global_init(void) -{ - cf_declare_section("Tempfiles", &temp_config, 0); -} - -void -temp_file_name(char *buf) -{ - struct ucwlib_context *ctx = ucwlib_thread_context(); - int cnt = ++ctx->temp_counter; - int pid = getpid(); - if (ctx->thread_id == pid) - sprintf(buf, "%s%d-%d", temp_prefix, pid, cnt); - else - sprintf(buf, "%s%d-%d-%d", temp_prefix, pid, ctx->thread_id, cnt); -} +#include struct fastbuf * bopen_tmp_file(struct fb_params *params) { char name[TEMP_FILE_NAME_LEN]; - temp_file_name(name); - struct fastbuf *fb = bopen_file(name, O_RDWR | O_CREAT | O_TRUNC, params); + int fd = open_tmp(name, O_RDWR | O_CREAT | O_TRUNC, 0600); + struct fastbuf *fb = bopen_fd_name(fd, params, name); bconfig(fb, BCONFIG_IS_TEMP_FILE, 1); return fb; } struct fastbuf * -bopen_tmp(uns buflen) +bopen_tmp(uint buflen) { return bopen_tmp_file(&(struct fb_params){ .type = FB_STD, .buffer_size = buflen }); } @@ -63,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) { @@ -78,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;