]> mj.ucw.cz Git - libucw.git/blob - ucw/fb-temp.c
Updated TODO
[libucw.git] / ucw / fb-temp.c
1 /*
2  *      UCW Library -- Temporary Fastbufs
3  *
4  *      (c) 2002--2008 Martin Mares <mj@ucw.cz>
5  *      (c) 2008 Michal Vaner <vorner@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include <ucw/lib.h>
12 #include <ucw/fastbuf.h>
13
14 #include <stdio.h>
15 #include <fcntl.h>
16
17 struct fastbuf *
18 bopen_tmp_file(struct fb_params *params)
19 {
20   char name[TEMP_FILE_NAME_LEN];
21   int fd = open_tmp(name, O_RDWR | O_CREAT | O_TRUNC, 0600);
22   struct fastbuf *fb = bopen_fd_name(fd, params, name);
23   bconfig(fb, BCONFIG_IS_TEMP_FILE, 1);
24   return fb;
25 }
26
27 struct fastbuf *
28 bopen_tmp(uns buflen)
29 {
30   return bopen_tmp_file(&(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
31 }
32
33 void bfix_tmp_file(struct fastbuf *fb, const char *name)
34 {
35   int was_temp = bconfig(fb, BCONFIG_IS_TEMP_FILE, 0);
36   ASSERT(was_temp == 1);
37   if (rename(fb->name, name))
38     bthrow(fb, "tmp", "Cannot rename %s to %s: %m", fb->name, name);
39   bclose(fb);
40 }
41
42 #ifdef TEST
43
44 #include <ucw/getopt.h>
45
46 int main(int argc, char **argv)
47 {
48   log_init(NULL);
49   if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0)
50     die("Hey, whaddya want?");
51
52   struct fastbuf *f = bopen_tmp(65536);
53   ASSERT(f && f->name);
54   bputsn(f, "Hello, world!");
55   bclose(f);
56   return 0;
57 }
58
59 #endif