Fixed bfx_open so that more_info is set to correct value
[idzebra-moved-to-github.git] / bfile / bfile.c
1 /* $Id: bfile.c,v 1.45 2005-09-19 09:37:31 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->block_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 (hbuf[l] == ' ')
215         l++;
216     if (more_info)
217         *more_info = hbuf + l;
218     return bf;
219 }
220
221 int bf_xclose (BFile bf, int version, const char *more_info)
222 {
223     if (bf->header_dirty)
224     {
225         zint pos = 0;
226         assert(bf->alloc_buf);
227         assert(bf->magic);
228         sprintf(bf->alloc_buf, "%s %d " ZINT_FORMAT " " ZINT_FORMAT " ", 
229                 bf->magic, version, bf->last_block, bf->free_list);
230         if (more_info)
231             strcat(bf->alloc_buf, more_info);
232         while (1)
233         {
234             bf_write(bf, pos, 0, 0, bf->alloc_buf + pos * bf->block_size);
235             pos++;
236             if (pos * bf->block_size > strlen(bf->alloc_buf))
237                 break;
238         }
239     }
240     return bf_close(bf);
241 }
242
243 BFile bf_open (BFiles bfs, const char *name, int block_size, int wflag)
244 {
245     BFile bf = (BFile) xmalloc(sizeof(struct BFile_struct));
246
247     bf->alloc_buf = 0;
248     bf->magic = 0;
249     bf->block_size = block_size;
250     bf->header_dirty = 0;
251     if (bfs->commit_area)
252     {
253         int first_time;
254
255         bf->mf = mf_open (bfs->register_area, name, block_size, 0);
256         bf->cf = cf_open (bf->mf, bfs->commit_area, name, block_size,
257                            wflag, &first_time);
258         if (first_time)
259         {
260             FILE *outf;
261
262             outf = open_cache(bfs, "ab");
263             if (!outf)
264             {
265                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", bfs->cache_fname);
266                 exit(1);
267             }
268             fprintf(outf, "%s %d\n", name, block_size);
269             fclose(outf);
270         }
271     }
272     else
273     {
274         bf->mf = mf_open(bfs->register_area, name, block_size, wflag);
275         bf->cf = NULL;
276     }
277     if (!bf->mf)
278     {
279         yaz_log(YLOG_FATAL, "mf_open failed for %s", name);
280         xfree(bf);
281         return 0;
282     }
283     zebra_lock_rdwr_init(&bf->rdwr_lock);
284     return bf;
285 }
286
287 int bf_read (BFile bf, zint no, int offset, int nbytes, void *buf)
288 {
289     int r;
290
291     zebra_lock_rdwr_rlock (&bf->rdwr_lock);
292     if (bf->cf)
293     {
294         if ((r = cf_read (bf->cf, no, offset, nbytes, buf)) == -1)
295             r = mf_read (bf->mf, no, offset, nbytes, buf);
296     }
297     else 
298         r = mf_read (bf->mf, no, offset, nbytes, buf);
299     zebra_lock_rdwr_runlock (&bf->rdwr_lock);
300     return r;
301 }
302
303 int bf_write (BFile bf, zint no, int offset, int nbytes, const void *buf)
304 {
305     int r;
306     zebra_lock_rdwr_wlock (&bf->rdwr_lock);
307     if (bf->cf)
308         r = cf_write (bf->cf, no, offset, nbytes, buf);
309     else
310         r = mf_write (bf->mf, no, offset, nbytes, buf);
311     zebra_lock_rdwr_wunlock (&bf->rdwr_lock);
312     return r;
313 }
314
315 int bf_commitExists (BFiles bfs)
316 {
317     FILE *inf;
318
319     inf = open_cache (bfs, "rb");
320     if (inf)
321     {
322         fclose (inf);
323         return 1;
324     }
325     return 0;
326 }
327
328 void bf_reset (BFiles bfs)
329 {
330     if (!bfs)
331         return;
332     mf_reset (bfs->commit_area);
333     mf_reset (bfs->register_area);
334 }
335
336 void bf_commitExec (BFiles bfs)
337 {
338     FILE *inf;
339     int block_size;
340     char path[256];
341     MFile mf;
342     CFile cf;
343     int first_time;
344
345     assert (bfs->commit_area);
346     if (!(inf = open_cache (bfs, "rb")))
347     {
348         yaz_log (YLOG_LOG, "No commit file");
349         return ;
350     }
351     while (fscanf (inf, "%s %d", path, &block_size) == 2)
352     {
353         mf = mf_open (bfs->register_area, path, block_size, 1);
354         cf = cf_open (mf, bfs->commit_area, path, block_size, 0, &first_time);
355
356         cf_commit (cf);
357
358         cf_close (cf);
359         mf_close (mf);
360     }
361     fclose (inf);
362 }
363
364 void bf_commitClean (BFiles bfs, const char *spec)
365 {
366     FILE *inf;
367     int block_size;
368     char path[256];
369     MFile mf;
370     CFile cf;
371     int mustDisable = 0;
372     int firstTime;
373
374     if (!bfs->commit_area)
375     {
376         bf_cache (bfs, spec);
377         mustDisable = 1;
378     }
379
380     if (!(inf = open_cache (bfs, "rb")))
381         return ;
382     while (fscanf (inf, "%s %d", path, &block_size) == 2)
383     {
384         mf = mf_open (bfs->register_area, path, block_size, 0);
385         cf = cf_open (mf, bfs->commit_area, path, block_size, 1, &firstTime);
386         cf_unlink (cf);
387         cf_close (cf);
388         mf_close (mf);
389     }
390     fclose (inf);
391     unlink_cache (bfs);
392     if (mustDisable)
393         bf_cache (bfs, 0);
394 }
395
396 int bf_alloc(BFile bf, int no, zint *blocks)
397 {
398     int i;
399     assert(bf->alloc_buf);
400     bf->header_dirty = 1;
401     for (i = 0; i < no; i++)
402     {
403         if (!bf->free_list)
404             blocks[i] = bf->last_block++;
405         else
406         {
407             char buf[16];
408             const char *cp = buf;
409             memset(buf, '\0', sizeof(buf));
410
411             blocks[i] = bf->free_list;
412             if (!bf_read(bf, bf->free_list, 0, sizeof(buf), buf))
413             {
414                 yaz_log(YLOG_WARN, "Bad freelist entry " ZINT_FORMAT,
415                         bf->free_list);
416                 exit(1);
417             }
418             zebra_zint_decode(&cp, &bf->free_list);
419         }
420     }
421     return 0;
422 }
423
424 int bf_free(BFile bf, int no, const zint *blocks)
425 {
426     int i;
427     assert(bf->alloc_buf);
428     bf->header_dirty = 1;
429     for (i = 0; i < no; i++)
430     {
431         char buf[16];
432         char *cp = buf;
433         memset(buf, '\0', sizeof(buf));
434         zebra_zint_encode(&cp, bf->free_list);
435         bf->free_list = blocks[i];
436         bf_write(bf, bf->free_list, 0, sizeof(buf), buf);
437     }
438     return 0;
439 }