06bca1557e4e91eb08e5164a0d02adc6eb978c3c
[idzebra-moved-to-github.git] / bfile / bfile.c
1 /*
2  * Copyright (C) 1994-1997, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: bfile.c,v $
7  * Revision 1.23  1997-09-17 12:19:06  adam
8  * Zebra version corresponds to YAZ version 1.4.
9  * Changed Zebra server so that it doesn't depend on global common_resource.
10  *
11  * Revision 1.22  1997/09/09 13:37:52  adam
12  * Partial port to WIN95/NT.
13  *
14  * Revision 1.21  1996/10/29 13:56:13  adam
15  * Include of zebrautl.h instead of alexutil.h.
16  *
17  * Revision 1.20  1996/03/26 15:59:04  adam
18  * The directory of the shadow table file can be specified by the new
19  * bf_lockDir call.
20  *
21  * Revision 1.19  1996/02/05  12:28:58  adam
22  * Removed a LOG_LOG message.
23  *
24  * Revision 1.18  1996/01/02  08:59:06  quinn
25  * Changed "commit" setting to "shadow".
26  *
27  * Revision 1.17  1995/12/11  09:03:51  adam
28  * New function: cf_unlink.
29  * New member of commit file head: state (0) deleted, (1) hash file.
30  *
31  * Revision 1.16  1995/12/08  16:21:13  adam
32  * Work on commit/update.
33  *
34  * Revision 1.15  1995/12/01  16:24:28  adam
35  * Commit files use separate meta file area.
36  *
37  * Revision 1.14  1995/12/01  11:37:21  adam
38  * Cached/commit files implemented as meta-files.
39  *
40  * Revision 1.13  1995/11/30  17:00:49  adam
41  * Several bug fixes. Commit system runs now.
42  *
43  * Revision 1.12  1995/11/30  08:33:10  adam
44  * Started work on commit facility.
45  *
46  * Revision 1.11  1995/09/04  12:33:21  adam
47  * Various cleanup. YAZ util used instead.
48  *
49  * Revision 1.10  1994/08/25  10:15:54  quinn
50  * Trivial
51  *
52  * Revision 1.9  1994/08/24  08:45:48  quinn
53  * Using mfile.
54  *
55  * Revision 1.8  1994/08/23  15:03:34  quinn
56  * *** empty log message ***
57  *
58  * Revision 1.7  1994/08/23  14:25:45  quinn
59  * Added O_CREAT because some geek wanted it. Sheesh.
60  *
61  * Revision 1.6  1994/08/23  14:21:38  quinn
62  * Fixed call to log
63  *
64  * Revision 1.5  1994/08/18  08:10:08  quinn
65  * Minimal changes
66  *
67  * Revision 1.4  1994/08/17  14:27:32  quinn
68  * last mods
69  *
70  * Revision 1.2  1994/08/17  14:09:32  quinn
71  * Compiles cleanly (still only dummy).
72  *
73  * Revision 1.1  1994/08/17  13:55:08  quinn
74  * New blocksystem. dummy only
75  *
76  */
77
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <assert.h>
82 #ifdef WINDOWS
83 #include <io.h>
84 #else
85 #include <unistd.h>
86 #endif
87
88 #include <zebrautl.h>
89 #include <bfile.h>
90 #include "cfile.h"
91
92 struct BFiles_struct {
93     MFile_area commit_area;
94     MFile_area_struct *register_area;
95     char *lockDir;
96 };
97
98 BFiles bfs_create (const char *spec)
99 {
100     BFiles bfs = xmalloc (sizeof(*bfs));
101     bfs->commit_area = NULL;
102     bfs->register_area = mf_init("register", spec);
103     bfs->lockDir = NULL;
104     return bfs;
105 }
106
107 void bfs_destroy (BFiles bfs)
108 {
109     xfree (bfs);
110 }
111
112 static FILE *open_cache (BFiles bfs, const char *flags)
113 {
114     char cacheFilename[1024];
115     FILE *file;
116
117     sprintf (cacheFilename, "%scache",
118              bfs->lockDir ? bfs->lockDir : "");
119     file = fopen (cacheFilename, flags);
120     return file;
121 }
122
123 static void unlink_cache (BFiles bfs)
124 {
125     char cacheFilename[1024];
126
127     sprintf (cacheFilename, "%scache",
128              bfs->lockDir ? bfs->lockDir : "");
129     unlink (cacheFilename);
130 }
131
132 void bf_lockDir (BFiles bfs, const char *lockDir)
133 {
134     size_t len;
135     
136     xfree (bfs->lockDir);
137     if (lockDir == NULL)
138         lockDir = "";
139     len = strlen(lockDir);
140     bfs->lockDir = xmalloc (len+2);
141     strcpy (bfs->lockDir, lockDir);
142     
143     if (len > 0 && bfs->lockDir[len-1] != '/')
144         strcpy (bfs->lockDir + len, "/");
145 }
146
147 void bf_cache (BFiles bfs, const char *spec)
148 {
149     if (spec)
150     {
151         if (!bfs->commit_area)
152             bfs->commit_area = mf_init ("shadow", spec);
153     }
154     else
155         bfs->commit_area = NULL;
156 }
157
158 int bf_close (BFile bf)
159 {
160     if (bf->cf)
161         cf_close (bf->cf);
162     mf_close (bf->mf);
163     xfree (bf);
164     return 0;
165 }
166
167 BFile bf_open (BFiles bfs, const char *name, int block_size, int wflag)
168 {
169     BFile tmp = xmalloc(sizeof(BFile_struct));
170
171     if (bfs->commit_area)
172     {
173         int first_time;
174
175         tmp->mf = mf_open (bfs->register_area, name, block_size, 0);
176         tmp->cf = cf_open (tmp->mf, bfs->commit_area, name, block_size,
177                            wflag, &first_time);
178         if (first_time)
179         {
180             FILE *outf;
181
182             outf = open_cache (bfs, "a");
183             if (!outf)
184             {
185                 logf (LOG_FATAL|LOG_ERRNO, "open %scache",
186                       bfs->lockDir ? bfs->lockDir : "");
187                 exit (1);
188             }
189             fprintf (outf, "%s %d\n", name, block_size);
190             fclose (outf);
191         }
192     }
193     else
194     {
195         tmp->mf = mf_open(bfs->register_area, name, block_size, wflag);
196         tmp->cf = NULL;
197     }
198     if (!tmp->mf)
199     {
200         logf (LOG_FATAL, "mf_open failed for %s", name);
201         xfree (tmp);
202         return 0;
203     }
204     return(tmp);
205 }
206
207 int bf_read (BFile bf, int no, int offset, int num, void *buf)
208 {
209     int r;
210
211     if (bf->cf && (r=cf_read (bf->cf, no, offset, num, buf)) != -1)
212         return r;
213     return mf_read (bf->mf, no, offset, num, buf);
214 }
215
216 int bf_write (BFile bf, int no, int offset, int num, const void *buf)
217 {
218     if (bf->cf)
219         return cf_write (bf->cf, no, offset, num, buf);
220     return mf_write (bf->mf, no, offset, num, buf);
221 }
222
223 int bf_commitExists (BFiles bfs)
224 {
225     FILE *inf;
226
227     inf = open_cache (bfs, "r");
228     if (inf)
229     {
230         fclose (inf);
231         return 1;
232     }
233     return 0;
234 }
235
236 void bf_commitExec (BFiles bfs)
237 {
238     FILE *inf;
239     int block_size;
240     char path[256];
241     MFile mf;
242     CFile cf;
243     int first_time;
244
245     assert (bfs->commit_area);
246     if (!(inf = open_cache (bfs, "r")))
247     {
248         logf (LOG_LOG, "No commit file");
249         return ;
250     }
251     while (fscanf (inf, "%s %d", path, &block_size) == 2)
252     {
253         mf = mf_open (bfs->register_area, path, block_size, 1);
254         cf = cf_open (mf, bfs->commit_area, path, block_size, 0, &first_time);
255
256         cf_commit (cf);
257
258         cf_close (cf);
259         mf_close (mf);
260     }
261     fclose (inf);
262 }
263
264 void bf_commitClean (BFiles bfs, const char *spec)
265 {
266     FILE *inf;
267     int block_size;
268     char path[256];
269     MFile mf;
270     CFile cf;
271     int mustDisable = 0;
272     int firstTime;
273
274     if (!bfs->commit_area)
275     {
276         bf_cache (bfs, spec);
277         mustDisable = 1;
278     }
279
280     if (!(inf = open_cache (bfs, "r")))
281         return ;
282     while (fscanf (inf, "%s %d", path, &block_size) == 2)
283     {
284         mf = mf_open (bfs->register_area, path, block_size, 0);
285         cf = cf_open (mf, bfs->commit_area, path, block_size, 1, &firstTime);
286         cf_unlink (cf);
287         cf_close (cf);
288         mf_close (mf);
289     }
290     fclose (inf);
291     unlink_cache (bfs);
292     if (mustDisable)
293         bf_cache (bfs, 0);
294 }