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