]> mj.ucw.cz Git - libucw.git/blob - lib/fb-file.c
it is possible to specify whether the caller only wants the header or also
[libucw.git] / lib / fb-file.c
1 /*
2  *      Sherlock Library -- Fast Buffered I/O on Files
3  *
4  *      (c) 1997--2002 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
26 static int
27 bfd_refill(struct fastbuf *f)
28 {
29   int l = read(FB_FILE(f)->fd, f->buffer, f->bufend-f->buffer);
30   if (l < 0)
31     die("Error reading %s: %m", f->name);
32   f->bptr = f->buffer;
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   char *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;
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     case BCONFIG_CAN_OVERWRITE:
93       return 2;
94     default:
95       return -1;
96     }
97 }
98
99 static struct fastbuf *
100 bfdopen_internal(int fd, uns buflen, byte *name)
101 {
102   int namelen = strlen(name) + 1;
103   struct fb_file *F = xmalloc(sizeof(struct fb_file) + buflen + namelen);
104   struct fastbuf *f = &F->fb;
105
106   bzero(F, sizeof(*F));
107   f->buffer = (char *)(F+1);
108   f->bptr = f->bstop = f->buffer;
109   f->bufend = f->buffer + buflen;
110   f->name = f->bufend;
111   memcpy(f->name, name, namelen);
112   F->fd = fd;
113   f->refill = bfd_refill;
114   f->spout = bfd_spout;
115   f->seek = bfd_seek;
116   f->close = bfd_close;
117   f->config = bfd_config;
118   return f;
119 }
120
121 struct fastbuf *
122 bopen(byte *name, uns mode, uns buffer)
123 {
124   struct fastbuf *b;
125   int fd;
126
127   if (!buffer)
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, buffer, 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 buffer)
141 {
142   byte x[32];
143
144   sprintf(x, "fd%d", fd);
145   return bfdopen_internal(fd, buffer, x);
146 }
147
148 struct fastbuf *
149 bfdopen_shared(int fd, uns buffer)
150 {
151   struct fastbuf *f = bfdopen(fd, buffer);
152   FB_FILE(f)->is_temp_file = -1;
153   return f;
154 }
155
156 #ifdef TEST
157
158 int main(int argc, char **argv)
159 {
160   struct fastbuf *f, *t;
161
162   f = bopen("/etc/profile", O_RDONLY, 16);
163   t = bfdopen(1, 13);
164   bbcopy(f, t, 100);
165   printf("%d %d\n", (int)btell(f), (int)btell(t));
166   bclose(f);
167   bclose(t);
168   return 0;
169 }
170
171 #endif