X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Ffb-temp.c;h=1975c8cc3f62c9c31b63d1863beb937591dd1b40;hb=b131627991008b2968b5a4ed36153f71a1abe37c;hp=4bcced6c4ecd56c5d3bac1064845807a09cd5917;hpb=5fc26988019e3d3943adf09ae4dca4db3eef625e;p=libucw.git diff --git a/lib/fb-temp.c b/lib/fb-temp.c index 4bcced6c..1975c8cc 100644 --- a/lib/fb-temp.c +++ b/lib/fb-temp.c @@ -1,7 +1,7 @@ /* * UCW Library -- Temporary Fastbufs * - * (c) 2002--2006 Martin Mares + * (c) 2002--2007 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -16,7 +16,7 @@ #include #include -static byte *temp_prefix = "/tmp/temp"; +static char *temp_prefix = "/tmp/temp"; static struct cf_section temp_config = { CF_ITEMS { @@ -31,7 +31,7 @@ static void CONSTRUCTOR temp_global_init(void) } void -temp_file_name(byte *buf) +temp_file_name(char *buf) { struct ucwlib_context *ctx = ucwlib_thread_context(); int cnt = ++ctx->temp_counter; @@ -42,16 +42,29 @@ temp_file_name(byte *buf) sprintf(buf, "%s%d-%d-%d", temp_prefix, pid, ctx->thread_id, cnt); } +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); + bconfig(fb, BCONFIG_IS_TEMP_FILE, 1); + return fb; +} + struct fastbuf * bopen_tmp(uns buflen) { - byte buf[TEMP_FILE_NAME_LEN]; - struct fastbuf *f; + return bopen_tmp_file(&(struct fb_params){ .type = FB_STD, .buffer_size = buflen }); +} - temp_file_name(buf); - f = bopen(buf, O_RDWR | O_CREAT | O_TRUNC, buflen); - bconfig(f, BCONFIG_IS_TEMP_FILE, 1); - return f; +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); + bclose(fb); } #ifdef TEST