7fb6a8c81ec78c6168d85cbd6f421cb36491e3a6
[idzebra-moved-to-github.git] / bfile / bfile.c
1 /* $Id: bfile.c,v 1.51 2006-11-08 22:08:27 adam Exp $
2    Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
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 = 0;
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 = 0;
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     if (bf->mf)
136         mf_close(bf->mf);
137     xfree(bf->alloc_buf);
138     xfree(bf->magic);
139     xfree(bf);
140     return 0;
141 }
142
143 #define HEADER_SIZE 256
144
145 BFile bf_xopen(BFiles bfs, const char *name, int block_size, int wrflag,
146                const char *magic, int *read_version,
147                const char **more_info)
148 {
149     char read_magic[40];
150     int l = 0;
151     int i = 0;
152     char *hbuf;
153     zint pos = 0;
154     BFile bf = bf_open(bfs, name, block_size, wrflag);
155
156     if (!bf)
157         return 0;
158      /* HEADER_SIZE is considered enough for our header */
159     if (bf->block_size < HEADER_SIZE)
160         bf->alloc_buf_size = HEADER_SIZE;
161     else
162         bf->alloc_buf_size = bf->block_size;
163         
164     hbuf = bf->alloc_buf = xmalloc(bf->alloc_buf_size);
165
166     /* fill-in default values */
167     bf->free_list = 0;
168     bf->root_block = bf->last_block = HEADER_SIZE / bf->block_size + 1;
169     bf->magic = xstrdup(magic);
170
171     if (!bf_read(bf, pos, 0, 0, hbuf + pos * bf->block_size))
172     {
173         if (wrflag)
174             bf->header_dirty = 1;
175         return bf;
176     }
177     while(hbuf[pos * bf->block_size + i] != '\0')
178     {
179         if (i == bf->block_size)
180         {   /* end of block at pos reached .. */
181             if (bf->alloc_buf_size  < (pos+1) * bf->block_size)
182             {
183                 /* not room for all in alloc_buf_size */
184                 yaz_log(YLOG_WARN, "bad header for %s (3)", magic);
185                 bf_close(bf);
186                 return 0;
187             }
188             /* read next block in header */
189             pos++;
190             if (!bf_read(bf, pos, 0, 0, hbuf + pos * bf->block_size))
191             {
192                 yaz_log(YLOG_WARN, "missing header block %s (4)", magic);
193                 bf_close(bf);
194                 return 0;
195             }
196             i = 0; /* pos within block is reset */
197         }
198         else
199             i++;
200     }
201     if (sscanf(hbuf, "%39s %d " ZINT_FORMAT " " ZINT_FORMAT "%n",
202                read_magic, read_version, &bf->last_block,
203                &bf->free_list, &l) < 4 && l)  /* if %n is counted, it's 5 */
204     {
205         yaz_log(YLOG_WARN, "bad header for %s (1)", magic);
206         bf_close(bf);
207         return 0;
208     }
209     if (strcmp(read_magic, magic))
210     {
211         yaz_log(YLOG_WARN, "bad header for %s (2)", magic);
212         bf_close(bf);
213         return 0;
214     }
215     if (hbuf[l] == ' ')
216         l++;
217     if (more_info)
218         *more_info = hbuf + l;
219     return bf;
220 }
221
222 int bf_xclose(BFile bf, int version, const char *more_info)
223 {
224     if (bf->header_dirty)
225     {
226         zint pos = 0;
227         assert(bf->alloc_buf);
228         assert(bf->magic);
229         sprintf(bf->alloc_buf, "%s %d " ZINT_FORMAT " " ZINT_FORMAT " ", 
230                 bf->magic, version, bf->last_block, bf->free_list);
231         if (more_info)
232             strcat(bf->alloc_buf, more_info);
233         while (1)
234         {
235             bf_write(bf, pos, 0, 0, bf->alloc_buf + pos * bf->block_size);
236             pos++;
237             if (pos * bf->block_size > strlen(bf->alloc_buf))
238                 break;
239         }
240     }
241     return bf_close(bf);
242 }
243
244 BFile bf_open(BFiles bfs, const char *name, int block_size, int wflag)
245 {
246     BFile bf = (BFile) xmalloc(sizeof(*bf));
247
248     bf->alloc_buf = 0;
249     bf->magic = 0;
250     bf->block_size = block_size;
251     bf->header_dirty = 0;
252     bf->cf = 0;
253     bf->mf = 0;
254     zebra_lock_rdwr_init(&bf->rdwr_lock);
255
256     if (bfs->commit_area)
257     {
258         int first_time;
259
260         bf->mf = mf_open(bfs->register_area, name, block_size, 0);
261         bf->cf = cf_open(bf->mf, bfs->commit_area, name, block_size,
262                          wflag, &first_time);
263         if (!bf->cf)
264         {
265             yaz_log(YLOG_FATAL, "cf_open failed for %s", name);
266             bf_close(bf);
267             return 0;
268         }
269         if (first_time)
270         {
271             FILE *outf;
272
273             outf = open_cache(bfs, "ab");
274             if (!outf)
275             {
276                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", bfs->cache_fname);
277                 bf_close(bf);
278                 return 0;
279             }
280             fprintf(outf, "%s %d\n", name, block_size);
281             if (fclose(outf))
282             {
283                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fclose %s", bfs->cache_fname);
284                 bf_close(bf);
285                 return 0;
286             }
287         }
288     }
289     else
290     {
291         bf->mf = mf_open(bfs->register_area, name, block_size, wflag);
292     }
293     if (!bf->mf)
294     {
295         yaz_log(YLOG_FATAL, "mf_open failed for %s", name);
296         bf_close(bf);
297         return 0;
298     }
299     return bf;
300 }
301
302 int bf_read(BFile bf, zint no, int offset, int nbytes, void *buf)
303 {
304     int ret = bf_read2(bf, no, offset, nbytes, buf);
305
306     if (ret == -1)
307     {
308         exit(1);
309     }
310     return ret;
311 }
312
313 int bf_read2(BFile bf, zint no, int offset, int nbytes, void *buf)
314 {
315     int ret;
316
317     zebra_lock_rdwr_rlock(&bf->rdwr_lock);
318     if (bf->cf)
319     {
320         if ((ret = cf_read(bf->cf, no, offset, nbytes, buf)) == 0)
321             ret = mf_read(bf->mf, no, offset, nbytes, buf);
322     }
323     else 
324         ret = mf_read(bf->mf, no, offset, nbytes, buf);
325     zebra_lock_rdwr_runlock(&bf->rdwr_lock);
326     return ret;
327 }
328
329 int bf_write(BFile bf, zint no, int offset, int nbytes, const void *buf)
330 {
331     int ret = bf_write2(bf, no, offset, nbytes, buf);
332
333     if (ret == -1)
334     {
335         exit(1);
336     }
337     return ret;
338 }
339
340 int bf_write2(BFile bf, zint no, int offset, int nbytes, const void *buf)
341 {
342     int r;
343     zebra_lock_rdwr_wlock(&bf->rdwr_lock);
344     if (bf->cf)
345         r = cf_write(bf->cf, no, offset, nbytes, buf);
346     else
347         r = mf_write(bf->mf, no, offset, nbytes, buf);
348     zebra_lock_rdwr_wunlock(&bf->rdwr_lock);
349     return r;
350 }
351
352 int bf_commitExists(BFiles bfs)
353 {
354     FILE *inf;
355
356     inf = open_cache(bfs, "rb");
357     if (inf)
358     {
359         fclose(inf);
360         return 1;
361     }
362     return 0;
363 }
364
365 void bf_reset(BFiles bfs)
366 {
367     if (!bfs)
368         return;
369     mf_reset(bfs->commit_area, 1);
370     mf_reset(bfs->register_area, 1);
371     unlink_cache(bfs);
372 }
373
374 void bf_commitExec(BFiles bfs)
375 {
376     FILE *inf;
377     int block_size;
378     char path[256];
379     MFile mf;
380     CFile cf;
381     int first_time;
382
383     assert(bfs->commit_area);
384     if (!(inf = open_cache(bfs, "rb")))
385     {
386         yaz_log(YLOG_LOG, "No commit file");
387         return ;
388     }
389     while (fscanf(inf, "%s %d", path, &block_size) == 2)
390     {
391         mf = mf_open(bfs->register_area, path, block_size, 1);
392         cf = cf_open(mf, bfs->commit_area, path, block_size, 0, &first_time);
393
394         cf_commit(cf);
395
396         cf_close(cf);
397         mf_close(mf);
398     }
399     fclose(inf);
400 }
401
402 void bf_commitClean(BFiles bfs, const char *spec)
403 {
404     int mustDisable = 0;
405
406     if (!bfs->commit_area)
407     {
408         bf_cache(bfs, spec);
409         mustDisable = 1;
410     }
411
412     mf_reset(bfs->commit_area, 1);
413
414     unlink_cache(bfs);
415     if (mustDisable)
416         bf_cache(bfs, 0);
417 }
418
419 int bf_alloc(BFile bf, int no, zint *blocks)
420 {
421     int i;
422     assert(bf->alloc_buf);
423     bf->header_dirty = 1;
424     for (i = 0; i < no; i++)
425     {
426         if (!bf->free_list)
427             blocks[i] = bf->last_block++;
428         else
429         {
430             char buf[16];
431             const char *cp = buf;
432             memset(buf, '\0', sizeof(buf));
433
434             blocks[i] = bf->free_list;
435             if (!bf_read(bf, bf->free_list, 0, sizeof(buf), buf))
436             {
437                 yaz_log(YLOG_WARN, "Bad freelist entry " ZINT_FORMAT,
438                         bf->free_list);
439                 exit(1);
440             }
441             zebra_zint_decode(&cp, &bf->free_list);
442         }
443     }
444     return 0;
445 }
446
447 int bf_free(BFile bf, int no, const zint *blocks)
448 {
449     int i;
450     assert(bf->alloc_buf);
451     bf->header_dirty = 1;
452     for (i = 0; i < no; i++)
453     {
454         char buf[16];
455         char *cp = buf;
456         memset(buf, '\0', sizeof(buf));
457         zebra_zint_encode(&cp, bf->free_list);
458         bf->free_list = blocks[i];
459         bf_write(bf, bf->free_list, 0, sizeof(buf), buf);
460     }
461     return 0;
462 }
463
464 int bfs_register_directory_stat(BFiles bfs, int no, const char **directory,
465                                 double *used_bytes, double *max_bytes)
466 {
467     return mf_area_directory_stat(bfs->register_area, no, directory,
468                                   used_bytes, max_bytes);
469 }
470
471
472 int bfs_shadow_directory_stat(BFiles bfs, int no, const char **directory,
473                               double *used_bytes, double *max_bytes)
474 {
475     if (!bfs->commit_area)
476         return 0;
477     return mf_area_directory_stat(bfs->commit_area, no, directory,
478                                   used_bytes, max_bytes);
479 }
480 /*
481  * Local variables:
482  * c-basic-offset: 4
483  * indent-tabs-mode: nil
484  * End:
485  * vim: shiftwidth=4 tabstop=8 expandtab
486  */
487