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