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