]> mj.ucw.cz Git - libucw.git/blob - ucw/fb-temp.c
c9b34ba5dc5a995575ac87e317c2afeb8a3b31b6
[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/conf.h"
13 #include "ucw/fastbuf.h"
14 #include "ucw/threads.h"
15 #include "ucw/lfs.h"
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <sys/fcntl.h>
20 #include <stdlib.h>
21 #include <sys/time.h>
22 #include <errno.h>
23
24 static char *temp_prefix = "temp";
25 static char *temp_dir;
26 static int public_dir = 1;
27
28 static struct cf_section temp_config = {
29   CF_ITEMS {
30     CF_STRING("Dir", &temp_dir),
31     CF_STRING("Prefix", &temp_prefix),
32     CF_INT("PublicDir", &public_dir),
33     CF_END
34   }
35 };
36
37 static void CONSTRUCTOR temp_global_init(void)
38 {
39   cf_declare_section("Tempfiles", &temp_config, 0);
40 }
41
42 void
43 temp_file_name(char *name_buf, int *open_flags)
44 {
45   char *dir = temp_dir;
46   if (!dir && !(dir = getenv("TMPDIR")))
47     dir = "/tmp";
48
49   int len;
50   if (public_dir)
51     {
52       struct timeval tv;
53       if (gettimeofday(&tv, NULL))
54         die("gettimeofday() failed: %m");
55       len = snprintf(name_buf, TEMP_FILE_NAME_LEN, "%s/%s%u", dir, temp_prefix, (uns) tv.tv_usec);
56       if (open_flags)
57         *open_flags = O_EXCL;
58     }
59   else
60     {
61       struct ucwlib_context *ctx = ucwlib_thread_context();
62       int cnt = ++ctx->temp_counter;
63       int pid = getpid();
64       if (ctx->thread_id == pid)
65         len = snprintf(name_buf, TEMP_FILE_NAME_LEN, "%s/%s%d-%d", dir, temp_prefix, pid, cnt);
66       else
67         len = snprintf(name_buf, TEMP_FILE_NAME_LEN, "%s/%s%d-%d-%d", dir, temp_prefix, pid, ctx->thread_id, cnt);
68       if (open_flags)
69         *open_flags = 0;
70     }
71   ASSERT(len < TEMP_FILE_NAME_LEN);
72 }
73
74 struct fastbuf *
75 bopen_tmp_file(struct fb_params *params)
76 {
77   char name[TEMP_FILE_NAME_LEN];
78   int fd = open_tmp(name, O_RDWR | O_CREAT | O_TRUNC, 0600);
79   struct fastbuf *fb = bopen_fd_name(fd, params, name);
80   bconfig(fb, BCONFIG_IS_TEMP_FILE, 1);
81   return fb;
82 }
83
84 int
85 open_tmp(char *name_buf, int open_flags, int mode)
86 {
87   int create_flags, fd, retry = 10;
88   do
89     {
90       temp_file_name(name_buf, &create_flags);
91       fd = ucw_open(name_buf, open_flags | create_flags, mode);
92     }
93   while (fd < 0 && errno == EEXIST && retry --);
94   if (fd < 0)
95     die("Unable to create temp file %s: %m", name_buf);
96   return fd;
97 }
98
99 struct fastbuf *
100 bopen_tmp(uns buflen)
101 {
102   return bopen_tmp_file(&(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
103 }
104
105 void bfix_tmp_file(struct fastbuf *fb, const char *name)
106 {
107   int was_temp = bconfig(fb, BCONFIG_IS_TEMP_FILE, 0);
108   ASSERT(was_temp == 1);
109   if (rename(fb->name, name))
110     die("Cannot rename %s to %s: %m", fb->name, name);
111   bclose(fb);
112 }
113
114 #ifdef TEST
115
116 #include "ucw/getopt.h"
117
118 int main(int argc, char **argv)
119 {
120   log_init(NULL);
121   if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0)
122     die("Hey, whaddya want?");
123
124   struct fastbuf *f = bopen_tmp(65536);
125   bputsn(f, "Hello, world!");
126   bclose(f);
127   return 0;
128 }
129
130 #endif