Use HAVE_UNISTD_H when including unistd.h.
[idzebra-moved-to-github.git] / bfile / bfile.c
1 /* $Id: bfile.c,v 1.44 2005-06-14 20:28:53 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #ifdef WIN32
28 #include <io.h>
29 #endif
30
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #include <yaz/xmalloc.h>
36 #include <idzebra/util.h>
37 #include <idzebra/bfile.h>
38 #include "mfile.h"
39 #include "cfile.h"
40
41 struct BFile_struct
42 {
43     MFile mf;
44     Zebra_lock_rdwr rdwr_lock;
45     struct CFile_struct *cf;
46     char *alloc_buf;
47     int block_size;
48     int alloc_buf_size;
49     zint last_block;
50     zint free_list;
51     zint root_block;
52     char *magic;
53     int header_dirty;
54 };
55
56 struct BFiles_struct {
57     MFile_area commit_area;
58     MFile_area_struct *register_area;
59     char *base;
60     char *cache_fname;
61 };
62
63 BFiles bfs_create (const char *spec, const char *base)
64 {
65     BFiles bfs = (BFiles) xmalloc (sizeof(*bfs));
66     bfs->commit_area = NULL;
67     bfs->base = 0;
68     bfs->cache_fname = 0;
69     if (base)
70         bfs->base = xstrdup (base);
71     bfs->register_area = mf_init("register", spec, base);
72     if (!bfs->register_area)
73     {
74         bfs_destroy(bfs);
75         return 0;
76     }
77     return bfs;
78 }
79
80 void bfs_destroy (BFiles bfs)
81 {
82     if (!bfs)
83         return;
84     xfree (bfs->cache_fname);
85     xfree (bfs->base);
86     mf_destroy (bfs->commit_area);
87     mf_destroy (bfs->register_area);
88     xfree (bfs);
89 }
90
91 static FILE *open_cache (BFiles bfs, const char *flags)
92 {
93     FILE *file;
94
95     file = fopen (bfs->cache_fname, flags);
96     return file;
97 }
98
99 static void unlink_cache (BFiles bfs)
100 {
101     unlink (bfs->cache_fname);
102 }
103
104 ZEBRA_RES bf_cache (BFiles bfs, const char *spec)
105 {
106     if (spec)
107     {
108         yaz_log (YLOG_LOG, "enabling shadow spec=%s", spec);
109         if (!bfs->commit_area)
110             bfs->commit_area = mf_init ("shadow", spec, bfs->base);
111         if (bfs->commit_area)
112         {
113             bfs->cache_fname = xmalloc (strlen(bfs->commit_area->dirs->name)+
114                                        8);
115             strcpy (bfs->cache_fname, bfs->commit_area->dirs->name);
116             strcat (bfs->cache_fname, "/cache");
117             yaz_log (YLOG_LOG, "cache_fname = %s", bfs->cache_fname);
118         }
119         else
120         {
121             yaz_log(YLOG_WARN, "shadow could not be enabled");
122             return ZEBRA_FAIL;
123         }
124     }
125     else
126         bfs->commit_area = NULL;
127     return ZEBRA_OK;
128 }
129
130 int bf_close (BFile bf)
131 {
132     zebra_lock_rdwr_destroy (&bf->rdwr_lock);
133     if (bf->cf)
134         cf_close (bf->cf);
135     mf_close (bf->mf);
136     xfree(bf->alloc_buf);
137     xfree(bf->magic);
138     xfree(bf);
139     return 0;
140 }
141
142 #define HEADER_SIZE 256
143
144 BFile bf_xopen(BFiles bfs, const char *name, int block_size, int wrflag,
145                const char *magic, int *read_version,
146                const char **more_info)
147 {
148     char read_magic[40];
149     int l = 0;
150     int i = 0;
151     char *hbuf;
152     zint pos = 0;
153     BFile bf = bf_open(bfs, name, block_size, wrflag);
154
155     if (!bf)
156         return 0;
157      /* HEADER_SIZE is considered enough for our header */
158     if (bf->alloc_buf_size < HEADER_SIZE)
159         bf->alloc_buf_size = HEADER_SIZE;
160     else
161         bf->alloc_buf_size = bf->block_size;
162         
163     hbuf = bf->alloc_buf = xmalloc(bf->alloc_buf_size);
164
165     /* fill-in default values */
166     bf->free_list = 0;
167     bf->root_block = bf->last_block = HEADER_SIZE / bf->block_size + 1;
168     bf->magic = xstrdup(magic);
169
170     if (!bf_read(bf, pos, 0, 0, hbuf + pos * bf->block_size))
171     {
172         if (wrflag)
173             bf->header_dirty = 1;
174         return bf;
175     }
176     while(hbuf[pos * bf->block_size + i] != '\0')
177     {
178         if (i == bf->block_size)
179         {   /* end of block at pos reached .. */
180             if (bf->alloc_buf_size  < (pos+1) * bf->block_size)
181             {
182                 /* not room for all in alloc_buf_size */
183                 yaz_log(YLOG_WARN, "bad header for %s (3)", magic);
184                 bf_close(bf);
185                 return 0;
186             }
187             /* read next block in header */
188             pos++;
189             if (!bf_read(bf, pos, 0, 0, hbuf + pos * bf->block_size))
190             {
191                 yaz_log(YLOG_WARN, "missing header block %s (4)", magic);
192                 bf_close(bf);
193                 return 0;
194             }
195             i = 0; /* pos within block is reset */
196         }
197         else
198             i++;
199     }
200     if (sscanf(hbuf, "%39s %d " ZINT_FORMAT " " ZINT_FORMAT "%n",
201                read_magic, read_version, &bf->last_block,
202                &bf->free_list, &l) < 4 && l)  /* if %n is counted, it's 5 */
203     {
204         yaz_log(YLOG_WARN, "bad header for %s (1)", magic);
205         bf_close(bf);
206         return 0;
207     }
208     if (strcmp(read_magic, magic))
209     {
210         yaz_log(YLOG_WARN, "bad header for %s (2)", magic);
211         bf_close(bf);
212         return 0;
213     }
214     if (more_info)
215         *more_info = hbuf + l;
216     return bf;
217 }
218
219 int bf_xclose (BFile bf, int version, const char *more_info)
220 {
221     if (bf->header_dirty)
222     {
223         zint pos = 0;
224         assert(bf->alloc_buf);
225         assert(bf->magic);
226         sprintf(bf->alloc_buf, "%s %d " ZINT_FORMAT " " ZINT_FORMAT " ", 
227                 bf->magic, version, bf->last_block, bf->free_list);
228         if (more_info)
229             strcat(bf->alloc_buf, more_info);
230         while (1)
231         {
232             bf_write(bf, pos, 0, 0, bf->alloc_buf + pos * bf->block_size);
233             pos++;
234             if (pos * bf->block_size > strlen(bf->alloc_buf))
235                 break;
236         }
237     }
238     return bf_close(bf);
239 }
240
241 BFile bf_open (BFiles bfs, const char *name, int block_size, int wflag)
242 {
243     BFile bf = (BFile) xmalloc(sizeof(struct BFile_struct));
244
245     bf->alloc_buf = 0;
246     bf->magic = 0;
247     bf->block_size = block_size;
248     bf->header_dirty = 0;
249     if (bfs->commit_area)
250     {
251         int first_time;
252
253         bf->mf = mf_open (bfs->register_area, name, block_size, 0);
254         bf->cf = cf_open (bf->mf, bfs->commit_area, name, block_size,
255                            wflag, &first_time);
256         if (first_time)
257         {
258             FILE *outf;
259
260             outf = open_cache(bfs, "ab");
261             if (!outf)
262             {
263                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", bfs->cache_fname);
264                 exit(1);
265             }
266             fprintf(outf, "%s %d\n", name, block_size);
267             fclose(outf);
268         }
269     }
270     else
271     {
272         bf->mf = mf_open(bfs->register_area, name, block_size, wflag);
273         bf->cf = NULL;
274     }
275     if (!bf->mf)
276     {
277         yaz_log(YLOG_FATAL, "mf_open failed for %s", name);
278         xfree(bf);
279         return 0;
280     }
281     zebra_lock_rdwr_init(&bf->rdwr_lock);
282     return bf;
283 }
284
285 int bf_read (BFile bf, zint no, int offset, int nbytes, void *buf)
286 {
287     int r;
288
289     zebra_lock_rdwr_rlock (&bf->rdwr_lock);
290     if (bf->cf)
291     {
292         if ((r = cf_read (bf->cf, no, offset, nbytes, buf)) == -1)
293             r = mf_read (bf->mf, no, offset, nbytes, buf);
294     }
295     else 
296         r = mf_read (bf->mf, no, offset, nbytes, buf);
297     zebra_lock_rdwr_runlock (&bf->rdwr_lock);
298     return r;
299 }
300
301 int bf_write (BFile bf, zint no, int offset, int nbytes, const void *buf)
302 {
303     int r;
304     zebra_lock_rdwr_wlock (&bf->rdwr_lock);
305     if (bf->cf)
306         r = cf_write (bf->cf, no, offset, nbytes, buf);
307     else
308         r = mf_write (bf->mf, no, offset, nbytes, buf);
309     zebra_lock_rdwr_wunlock (&bf->rdwr_lock);
310     return r;
311 }
312
313 int bf_commitExists (BFiles bfs)
314 {
315     FILE *inf;
316
317     inf = open_cache (bfs, "rb");
318     if (inf)
319     {
320         fclose (inf);
321         return 1;
322     }
323     return 0;
324 }
325
326 void bf_reset (BFiles bfs)
327 {
328     if (!bfs)
329         return;
330     mf_reset (bfs->commit_area);
331     mf_reset (bfs->register_area);
332 }
333
334 void bf_commitExec (BFiles bfs)
335 {
336     FILE *inf;
337     int block_size;
338     char path[256];
339     MFile mf;
340     CFile cf;
341     int first_time;
342
343     assert (bfs->commit_area);
344     if (!(inf = open_cache (bfs, "rb")))
345     {
346         yaz_log (YLOG_LOG, "No commit file");
347         return ;
348     }
349     while (fscanf (inf, "%s %d", path, &block_size) == 2)
350     {
351         mf = mf_open (bfs->register_area, path, block_size, 1);
352         cf = cf_open (mf, bfs->commit_area, path, block_size, 0, &first_time);
353
354         cf_commit (cf);
355
356         cf_close (cf);
357         mf_close (mf);
358     }
359     fclose (inf);
360 }
361
362 void bf_commitClean (BFiles bfs, const char *spec)
363 {
364     FILE *inf;
365     int block_size;
366     char path[256];
367     MFile mf;
368     CFile cf;
369     int mustDisable = 0;
370     int firstTime;
371
372     if (!bfs->commit_area)
373     {
374         bf_cache (bfs, spec);
375         mustDisable = 1;
376     }
377
378     if (!(inf = open_cache (bfs, "rb")))
379         return ;
380     while (fscanf (inf, "%s %d", path, &block_size) == 2)
381     {
382         mf = mf_open (bfs->register_area, path, block_size, 0);
383         cf = cf_open (mf, bfs->commit_area, path, block_size, 1, &firstTime);
384         cf_unlink (cf);
385         cf_close (cf);
386         mf_close (mf);
387     }
388     fclose (inf);
389     unlink_cache (bfs);
390     if (mustDisable)
391         bf_cache (bfs, 0);
392 }
393
394 int bf_alloc(BFile bf, int no, zint *blocks)
395 {
396     int i;
397     assert(bf->alloc_buf);
398     bf->header_dirty = 1;
399     for (i = 0; i < no; i++)
400     {
401         if (!bf->free_list)
402             blocks[i] = bf->last_block++;
403         else
404         {
405             char buf[16];
406             const char *cp = buf;
407             memset(buf, '\0', sizeof(buf));
408
409             blocks[i] = bf->free_list;
410             if (!bf_read(bf, bf->free_list, 0, sizeof(buf), buf))
411             {
412                 yaz_log(YLOG_WARN, "Bad freelist entry " ZINT_FORMAT,
413                         bf->free_list);
414                 exit(1);
415             }
416             zebra_zint_decode(&cp, &bf->free_list);
417         }
418     }
419     return 0;
420 }
421
422 int bf_free(BFile bf, int no, const zint *blocks)
423 {
424     int i;
425     assert(bf->alloc_buf);
426     bf->header_dirty = 1;
427     for (i = 0; i < no; i++)
428     {
429         char buf[16];
430         char *cp = buf;
431         memset(buf, '\0', sizeof(buf));
432         zebra_zint_encode(&cp, bf->free_list);
433         bf->free_list = blocks[i];
434         bf_write(bf, bf->free_list, 0, sizeof(buf), buf);
435     }
436     return 0;
437 }