]> mj.ucw.cz Git - libucw.git/blob - lib/fb-file.c
Use xmalloc_zero().
[libucw.git] / lib / fb-file.c
1 /*
2  *      Sherlock Library -- Fast Buffered I/O on Files
3  *
4  *      (c) 1997--2000 Martin Mares <mj@ucw.cz>
5  */
6
7 #include "lib/lib.h"
8 #include "lib/fastbuf.h"
9 #include "lib/lfs.h"
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15
16 static int
17 bfd_refill(struct fastbuf *f)
18 {
19   int l = read(f->fd, f->buffer, f->buflen);
20
21   if (l < 0)
22     die("Error reading %s: %m", f->name);
23   f->bptr = f->buffer;
24   f->bstop = f->buffer + l;
25   f->pos = f->fdpos;
26   f->fdpos += l;
27   return l;
28 }
29
30 static void
31 bfd_spout(struct fastbuf *f)
32 {
33   int l = f->bptr - f->buffer;
34   char *c = f->buffer;
35
36   while (l)
37     {
38       int z = write(f->fd, c, l);
39       if (z <= 0)
40         die("Error writing %s: %m", f->name);
41       f->fdpos += z;
42       l -= z;
43       c += z;
44     }
45   f->bptr = f->buffer;
46   f->pos = f->fdpos;
47 }
48
49 static void
50 bfd_seek(struct fastbuf *f, sh_off_t pos, int whence)
51 {
52   sh_off_t l;
53
54   if (whence == SEEK_SET && pos == f->fdpos)
55     return;
56
57   l = sh_seek(f->fd, pos, whence);
58   if (l < 0)
59     die("lseek on %s: %m", f->name);
60   f->fdpos = f->pos = l;
61 }
62
63 static void
64 bfd_close(struct fastbuf *f)
65 {
66   close(f->fd);
67 }
68
69 static struct fastbuf *
70 bfdopen_internal(int fd, uns buflen, byte *name)
71 {
72   int namelen = strlen(name) + 1;
73   struct fastbuf *b = xmalloc_zero(sizeof(struct fastbuf) + buflen + namelen);
74
75   b->buflen = buflen;
76   b->buffer = (char *)(b+1);
77   b->bptr = b->bstop = b->buffer;
78   b->bufend = b->buffer + buflen;
79   b->name = b->bufend;
80   strcpy(b->name, name);
81   b->fd = fd;
82   b->refill = bfd_refill;
83   b->spout = bfd_spout;
84   b->seek = bfd_seek;
85   b->close = bfd_close;
86   return b;
87 }
88
89 struct fastbuf *
90 bopen(byte *name, uns mode, uns buffer)
91 {
92   int fd = sh_open(name, mode, 0666);
93   if (fd < 0)
94     die("Unable to %s file %s: %m",
95         (mode & O_CREAT) ? "create" : "open", name);
96   return bfdopen_internal(fd, buffer, name);
97 }
98
99 struct fastbuf *
100 bfdopen(int fd, uns buffer)
101 {
102   byte x[32];
103
104   sprintf(x, "fd%d", fd);
105   return bfdopen_internal(fd, buffer, x);
106 }
107
108 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
109 {
110   uns rf = f->bstop - f->bptr;
111
112   if (!l)
113     return;
114   if (rf)
115     {
116       uns k = (rf <= l) ? rf : l;
117       bwrite(t, f->bptr, k);
118       f->bptr += k;
119       l -= k;
120     }
121   while (l >= t->buflen)
122     {
123       t->spout(t);
124       if ((uns) read(f->fd, t->buffer, t->buflen) != t->buflen)
125         die("bbcopy: %s exhausted", f->name);
126       f->pos = f->fdpos;
127       f->fdpos += t->buflen;
128       f->bstop = f->bptr = f->buffer;
129       t->bptr = t->bufend;
130       l -= t->buflen;
131     }
132   while (l)
133     {
134       uns k = t->bufend - t->bptr;
135
136       if (!k)
137         {
138           t->spout(t);
139           k = t->bufend - t->bptr;
140         }
141       if (k > l)
142         k = l;
143       bread(f, t->bptr, k);
144       t->bptr += k;
145       l -= k;
146     }
147 }
148
149 #ifdef TEST
150
151 int main(int argc, char **argv)
152 {
153   struct fastbuf *f, *t;
154   int c;
155
156   f = bopen("/etc/profile", O_RDONLY, 16);
157   t = bfdopen(1, 13);
158   bbcopy(f, t, 100);
159   bclose(f);
160   bclose(t);
161 }
162
163 #endif