bf73dc3a39f0506687f4c19c457ee7b3f5804a81
[idzebra-moved-to-github.git] / bfile / bfile.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: bfile.c,v $
7  * Revision 1.12  1995-11-30 08:33:10  adam
8  * Started work on commit facility.
9  *
10  * Revision 1.11  1995/09/04  12:33:21  adam
11  * Various cleanup. YAZ util used instead.
12  *
13  * Revision 1.10  1994/08/25  10:15:54  quinn
14  * Trivial
15  *
16  * Revision 1.9  1994/08/24  08:45:48  quinn
17  * Using mfile.
18  *
19  * Revision 1.8  1994/08/23  15:03:34  quinn
20  * *** empty log message ***
21  *
22  * Revision 1.7  1994/08/23  14:25:45  quinn
23  * Added O_CREAT because some geek wanted it. Sheesh.
24  *
25  * Revision 1.6  1994/08/23  14:21:38  quinn
26  * Fixed call to log
27  *
28  * Revision 1.5  1994/08/18  08:10:08  quinn
29  * Minimal changes
30  *
31  * Revision 1.4  1994/08/17  14:27:32  quinn
32  * last mods
33  *
34  * Revision 1.2  1994/08/17  14:09:32  quinn
35  * Compiles cleanly (still only dummy).
36  *
37  * Revision 1.1  1994/08/17  13:55:08  quinn
38  * New blocksystem. dummy only
39  *
40  */
41
42 #include <stdio.h>
43 #include <sys/types.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47
48 #include <alexutil.h>
49 #include <bfile.h>
50 #include "cfile.h"
51
52 static const char *cache_name = NULL;
53
54 void bf_cache (const char *name)
55 {
56     cache_name = name;
57 }
58
59 int bf_close (BFile bf)
60 {
61     if (bf->cf)
62         cf_close (bf->cf);
63     mf_close (bf->mf);
64     xfree (bf);
65     return 0;
66 }
67
68 BFile bf_open (const char *name, int block_size, int wflag)
69 {
70     BFile tmp = xmalloc(sizeof(BFile_struct));
71
72     if (cache_name)
73     {
74         FILE *outf;
75         int first_time;
76
77         logf (LOG_LOG, "cf,mf_open %s, cache_name=%s", name, cache_name);
78         tmp->mf = mf_open(0, name, block_size, wflag);
79         tmp->cf = cf_open(tmp->mf, cache_name, name, block_size, wflag,
80                           &first_time);
81
82         if (first_time)
83         {
84             outf = fopen (cache_name, "a");
85             fprintf (outf, "%s %d\n", name, block_size);
86             fclose (outf);
87         }
88     }
89     else
90     {
91         tmp->mf = mf_open(0, name, block_size, wflag);
92         tmp->cf = NULL;
93     }
94     if (!tmp->mf)
95     {
96         logf (LOG_FATAL, "mf_open failed for %s", name);
97         xfree (tmp);
98         return 0;
99     }
100     return(tmp);
101 }
102
103 int bf_read (BFile bf, int no, int offset, int num, void *buf)
104 {
105     int r;
106
107     if (bf->cf && (r=cf_read (bf->cf, no, offset, num, buf)) != -1)
108         return r;
109     return mf_read (bf->mf, no, offset, num, buf);
110 }
111
112 int bf_write (BFile bf, int no, int offset, int num, const void *buf)
113 {
114     if (bf->cf)
115         return cf_write (bf->cf, no, offset, num, buf);
116     return mf_write (bf->mf, no, offset, num, buf);
117 }
118
119 void bf_commit (const char *name)
120 {
121     FILE *inf;
122     int block_size;
123     char path[256];
124     MFile mf;
125     CFile cf;
126     int first_time;
127
128     if (!(inf = fopen (name, "r")))
129     {
130         logf (LOG_FATAL|LOG_ERRNO, "cannot open commit %s", name);
131         exit (1);
132     }
133     while (fscanf (inf, "%s %d", path, &block_size) == 1)
134     {
135         mf = mf_open(0, path, block_size, 1);
136         cf = cf_open(mf, name, path, block_size, 0, &first_time);
137
138         cf_commit (cf);
139
140         cf_close (cf);
141         mf_close (mf);
142     }
143     fclose (inf);
144 }