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