]> mj.ucw.cz Git - libucw.git/blob - lib/fb-file.c
Fixed includes, defines and copyrights.
[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(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->pos = b->fdpos = 0;
82   b->fd = fd;
83   b->refill = bfd_refill;
84   b->spout = bfd_spout;
85   b->seek = bfd_seek;
86   b->close = bfd_close;
87   return b;
88 }
89
90 struct fastbuf *
91 bopen(byte *name, uns mode, uns buffer)
92 {
93   int fd = sh_open(name, mode, 0666);
94   if (fd < 0)
95     die("Unable to %s file %s: %m",
96         (mode & O_CREAT) ? "create" : "open", name);
97   return bfdopen_internal(fd, buffer, name);
98 }
99
100 struct fastbuf *
101 bfdopen(int fd, uns buffer)
102 {
103   byte x[32];
104
105   sprintf(x, "fd%d", fd);
106   return bfdopen_internal(fd, buffer, x);
107 }
108
109 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
110 {
111   uns rf = f->bstop - f->bptr;
112
113   if (!l)
114     return;
115   if (rf)
116     {
117       uns k = (rf <= l) ? rf : l;
118       bwrite(t, f->bptr, k);
119       f->bptr += k;
120       l -= k;
121     }
122   while (l >= t->buflen)
123     {
124       t->spout(t);
125       if ((uns) read(f->fd, t->buffer, t->buflen) != t->buflen)
126         die("bbcopy: %s exhausted", f->name);
127       f->pos = f->fdpos;
128       f->fdpos += t->buflen;
129       f->bstop = f->bptr = f->buffer;
130       t->bptr = t->bufend;
131       l -= t->buflen;
132     }
133   while (l)
134     {
135       uns k = t->bufend - t->bptr;
136
137       if (!k)
138         {
139           t->spout(t);
140           k = t->bufend - t->bptr;
141         }
142       if (k > l)
143         k = l;
144       bread(f, t->bptr, k);
145       t->bptr += k;
146       l -= k;
147     }
148 }
149
150 #ifdef TEST
151
152 int main(int argc, char **argv)
153 {
154   struct fastbuf *f, *t;
155   int c;
156
157   f = bopen("/etc/profile", O_RDONLY, 16);
158   t = bfdopen(1, 13);
159   bbcopy(f, t, 100);
160   bclose(f);
161   bclose(t);
162 }
163
164 #endif