]> mj.ucw.cz Git - libucw.git/blob - lib/fb-file.c
9caf9a189aaf45c33a2b7f83dc6381ab45bac0c9
[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, -1 if not a real file */
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 void
57 bfd_seek(struct fastbuf *f, sh_off_t pos, int whence)
58 {
59   sh_off_t l;
60
61   if (whence == SEEK_SET && pos == f->pos)
62     return;
63
64   l = sh_seek(FB_FILE(f)->fd, pos, whence);
65   if (l < 0)
66     die("lseek on %s: %m", f->name);
67   f->pos = l;
68 }
69
70 static void
71 bfd_close(struct fastbuf *f)
72 {
73   switch (FB_FILE(f)->is_temp_file)
74     {
75     case 1:
76       if (unlink(f->name) < 0)
77         log(L_ERROR, "unlink(%s): %m", f->name);
78     case 0:
79       close(FB_FILE(f)->fd);
80     }
81   xfree(f);
82 }
83
84 static int
85 bfd_config(struct fastbuf *f, uns item, int value)
86 {
87   switch (item)
88     {
89     case BCONFIG_IS_TEMP_FILE:
90       FB_FILE(f)->is_temp_file = value;
91       return 0;
92     default:
93       return -1;
94     }
95 }
96
97 static struct fastbuf *
98 bfdopen_internal(int fd, uns buflen, byte *name)
99 {
100   int namelen = strlen(name) + 1;
101   struct fb_file *F = xmalloc(sizeof(struct fb_file) + buflen + namelen);
102   struct fastbuf *f = &F->fb;
103
104   bzero(F, sizeof(*F));
105   f->buffer = (byte *)(F+1);
106   f->bptr = f->bstop = f->buffer;
107   f->bufend = f->buffer + buflen;
108   f->name = f->bufend;
109   memcpy(f->name, name, namelen);
110   F->fd = fd;
111   f->refill = bfd_refill;
112   f->spout = bfd_spout;
113   f->seek = bfd_seek;
114   f->close = bfd_close;
115   f->config = bfd_config;
116   f->can_overwrite_buffer = 2;
117   return f;
118 }
119
120 struct fastbuf *
121 bopen(byte *name, uns mode, uns buflen)
122 {
123   struct fastbuf *b;
124   int fd;
125
126   if (!buflen)
127     return bopen_mm(name, mode);
128   fd = sh_open(name, mode, 0666);
129   if (fd < 0)
130     die("Unable to %s file %s: %m",
131         (mode & O_CREAT) ? "create" : "open", name);
132   b = bfdopen_internal(fd, buflen, name);
133   if (mode & O_APPEND)
134     bfd_seek(b, 0, SEEK_END);
135   return b;
136 }
137
138 struct fastbuf *
139 bfdopen(int fd, uns buflen)
140 {
141   byte x[32];
142
143   sprintf(x, "fd%d", fd);
144   return bfdopen_internal(fd, buflen, x);
145 }
146
147 struct fastbuf *
148 bfdopen_shared(int fd, uns buflen)
149 {
150   struct fastbuf *f = bfdopen(fd, buflen);
151   FB_FILE(f)->is_temp_file = -1;
152   return f;
153 }
154
155 void
156 bfilesync(struct fastbuf *b)
157 {
158   bflush(b);
159   if (fsync(FB_FILE(b)->fd) < 0)
160     log(L_ERROR, "fsync(%s) failed: %m", b->name);
161 }
162
163 #ifdef TEST
164
165 int main(int argc, char **argv)
166 {
167   struct fastbuf *f, *t;
168
169   f = bopen("/etc/profile", O_RDONLY, 16);
170   t = bfdopen(1, 13);
171   bbcopy(f, t, 100);
172   printf("%d %d\n", (int)btell(f), (int)btell(t));
173   bclose(f);
174   bclose(t);
175   return 0;
176 }
177
178 #endif