]> mj.ucw.cz Git - libucw.git/blob - lib/fb-file.c
Fastbufs now work better on unseekable files.
[libucw.git] / lib / fb-file.c
1 /*
2  *      UCW Library -- Fast Buffered I/O on Files
3  *
4  *      (c) 1997--2004 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/fastbuf.h"
12 #include "lib/lfs.h"
13
14 #include <string.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17
18 struct fb_file {
19   struct fastbuf fb;
20   int fd;                               /* File descriptor */
21   int is_temp_file;                     /* 0=normal file, 1=temporary file, delete on close, -1=shared FD */
22 };
23 #define FB_FILE(f) ((struct fb_file *)(f)->is_fastbuf)
24 #define FB_BUFFER(f) (byte *)(FB_FILE(f) + 1)
25
26 static int
27 bfd_refill(struct fastbuf *f)
28 {
29   f->bptr = f->buffer = FB_BUFFER(f);
30   int l = read(FB_FILE(f)->fd, f->buffer, f->bufend-f->buffer);
31   if (l < 0)
32     die("Error reading %s: %m", f->name);
33   f->bstop = f->buffer + l;
34   f->pos += l;
35   return l;
36 }
37
38 static void
39 bfd_spout(struct fastbuf *f)
40 {
41   int l = f->bptr - f->buffer;
42   byte *c = f->buffer;
43
44   f->pos += l;
45   while (l)
46     {
47       int z = write(FB_FILE(f)->fd, c, l);
48       if (z <= 0)
49         die("Error writing %s: %m", f->name);
50       l -= z;
51       c += z;
52     }
53   f->bptr = f->buffer = FB_BUFFER(f);
54 }
55
56 static int
57 bfd_seek(struct fastbuf *f, sh_off_t pos, int whence)
58 {
59   if (whence == SEEK_SET && pos == f->pos)
60     return 1;
61
62   sh_off_t l = sh_seek(FB_FILE(f)->fd, pos, whence);
63   if (l < 0)
64     return 0;
65   f->pos = l;
66   return 1;
67 }
68
69 static void
70 bfd_close(struct fastbuf *f)
71 {
72   switch (FB_FILE(f)->is_temp_file)
73     {
74     case 1:
75       if (unlink(f->name) < 0)
76         log(L_ERROR, "unlink(%s): %m", f->name);
77     case 0:
78       close(FB_FILE(f)->fd);
79     }
80   xfree(f);
81 }
82
83 static int
84 bfd_config(struct fastbuf *f, uns item, int value)
85 {
86   switch (item)
87     {
88     case BCONFIG_IS_TEMP_FILE:
89       FB_FILE(f)->is_temp_file = value;
90       return 0;
91     default:
92       return -1;
93     }
94 }
95
96 static struct fastbuf *
97 bfdopen_internal(int fd, uns buflen, byte *name)
98 {
99   int namelen = strlen(name) + 1;
100   struct fb_file *F = xmalloc(sizeof(struct fb_file) + buflen + namelen);
101   struct fastbuf *f = &F->fb;
102
103   bzero(F, sizeof(*F));
104   f->buffer = (byte *)(F+1);
105   f->bptr = f->bstop = f->buffer;
106   f->bufend = f->buffer + buflen;
107   f->name = f->bufend;
108   memcpy(f->name, name, namelen);
109   F->fd = fd;
110   f->refill = bfd_refill;
111   f->spout = bfd_spout;
112   f->seek = bfd_seek;
113   f->close = bfd_close;
114   f->config = bfd_config;
115   f->can_overwrite_buffer = 2;
116   return f;
117 }
118
119 struct fastbuf *
120 bopen_try(byte *name, uns mode, uns buflen)
121 {
122   int fd = sh_open(name, mode, 0666);
123   if (fd < 0)
124     return NULL;
125   struct fastbuf *b = bfdopen_internal(fd, buflen, name);
126   if (mode & O_APPEND)
127     bfd_seek(b, 0, SEEK_END);
128   return b;
129 }
130
131 struct fastbuf *
132 bopen(byte *name, uns mode, uns buflen)
133 {
134   if (!buflen)
135     return bopen_mm(name, mode);
136   struct fastbuf *b = bopen_try(name, mode, buflen);
137   if (!b)
138     die("Unable to %s file %s: %m",
139         (mode & O_CREAT) ? "create" : "open", name);
140   return b;
141 }
142
143 struct fastbuf *
144 bfdopen(int fd, uns buflen)
145 {
146   byte x[32];
147
148   sprintf(x, "fd%d", fd);
149   return bfdopen_internal(fd, buflen, x);
150 }
151
152 struct fastbuf *
153 bfdopen_shared(int fd, uns buflen)
154 {
155   struct fastbuf *f = bfdopen(fd, buflen);
156   FB_FILE(f)->is_temp_file = -1;
157   return f;
158 }
159
160 void
161 bfilesync(struct fastbuf *b)
162 {
163   bflush(b);
164   if (fsync(FB_FILE(b)->fd) < 0)
165     log(L_ERROR, "fsync(%s) failed: %m", b->name);
166 }
167
168 #ifdef TEST
169
170 int main(int argc UNUSED, char **argv UNUSED)
171 {
172   struct fastbuf *f, *t;
173
174   f = bopen("/etc/profile", O_RDONLY, 16);
175   t = bfdopen(1, 13);
176   bbcopy(f, t, 100);
177   printf("%d %d\n", (int)btell(f), (int)btell(t));
178   bclose(f);
179   bclose(t);
180   return 0;
181 }
182
183 #endif