]> mj.ucw.cz Git - libucw.git/blob - lib/fb-file.c
implemented most of parametrized fastbufs
[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 struct fastbuf *
95 bfdopen_internal(int fd, byte *name, uns buflen)
96 {
97   ASSERT(buflen);
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   return bopen_file_try(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
122 }
123
124 struct fastbuf *
125 bopen(byte *name, uns mode, uns buflen)
126 {
127   return bopen_file(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
128 }
129
130 struct fastbuf *
131 bfdopen(int fd, uns buflen)
132 {
133   return bopen_fd(fd, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
134 }
135
136 struct fastbuf *
137 bfdopen_shared(int fd, uns buflen)
138 {
139   struct fastbuf *f = bfdopen(fd, buflen);
140   FB_FILE(f)->is_temp_file = -1;
141   return f;
142 }
143
144 void
145 bfilesync(struct fastbuf *b)
146 {
147   bflush(b);
148   if (fsync(FB_FILE(b)->fd) < 0)
149     log(L_ERROR, "fsync(%s) failed: %m", b->name);
150 }
151
152 #ifdef TEST
153
154 int main(int argc UNUSED, char **argv UNUSED)
155 {
156   struct fastbuf *f, *t;
157
158   f = bopen("/etc/profile", O_RDONLY, 16);
159   t = bfdopen(1, 13);
160   bbcopy(f, t, 100);
161   printf("%d %d\n", (int)btell(f), (int)btell(t));
162   bclose(f);
163   bclose(t);
164   return 0;
165 }
166
167 #endif