2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
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.
11 * Revision 1.22 1997/09/09 13:37:52 adam
12 * Partial port to WIN95/NT.
14 * Revision 1.21 1996/10/29 13:56:13 adam
15 * Include of zebrautl.h instead of alexutil.h.
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
21 * Revision 1.19 1996/02/05 12:28:58 adam
22 * Removed a LOG_LOG message.
24 * Revision 1.18 1996/01/02 08:59:06 quinn
25 * Changed "commit" setting to "shadow".
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.
31 * Revision 1.16 1995/12/08 16:21:13 adam
32 * Work on commit/update.
34 * Revision 1.15 1995/12/01 16:24:28 adam
35 * Commit files use separate meta file area.
37 * Revision 1.14 1995/12/01 11:37:21 adam
38 * Cached/commit files implemented as meta-files.
40 * Revision 1.13 1995/11/30 17:00:49 adam
41 * Several bug fixes. Commit system runs now.
43 * Revision 1.12 1995/11/30 08:33:10 adam
44 * Started work on commit facility.
46 * Revision 1.11 1995/09/04 12:33:21 adam
47 * Various cleanup. YAZ util used instead.
49 * Revision 1.10 1994/08/25 10:15:54 quinn
52 * Revision 1.9 1994/08/24 08:45:48 quinn
55 * Revision 1.8 1994/08/23 15:03:34 quinn
56 * *** empty log message ***
58 * Revision 1.7 1994/08/23 14:25:45 quinn
59 * Added O_CREAT because some geek wanted it. Sheesh.
61 * Revision 1.6 1994/08/23 14:21:38 quinn
64 * Revision 1.5 1994/08/18 08:10:08 quinn
67 * Revision 1.4 1994/08/17 14:27:32 quinn
70 * Revision 1.2 1994/08/17 14:09:32 quinn
71 * Compiles cleanly (still only dummy).
73 * Revision 1.1 1994/08/17 13:55:08 quinn
74 * New blocksystem. dummy only
92 struct BFiles_struct {
93 MFile_area commit_area;
94 MFile_area_struct *register_area;
98 BFiles bfs_create (const char *spec)
100 BFiles bfs = xmalloc (sizeof(*bfs));
101 bfs->commit_area = NULL;
102 bfs->register_area = mf_init("register", spec);
107 void bfs_destroy (BFiles bfs)
112 static FILE *open_cache (BFiles bfs, const char *flags)
114 char cacheFilename[1024];
117 sprintf (cacheFilename, "%scache",
118 bfs->lockDir ? bfs->lockDir : "");
119 file = fopen (cacheFilename, flags);
123 static void unlink_cache (BFiles bfs)
125 char cacheFilename[1024];
127 sprintf (cacheFilename, "%scache",
128 bfs->lockDir ? bfs->lockDir : "");
129 unlink (cacheFilename);
132 void bf_lockDir (BFiles bfs, const char *lockDir)
136 xfree (bfs->lockDir);
139 len = strlen(lockDir);
140 bfs->lockDir = xmalloc (len+2);
141 strcpy (bfs->lockDir, lockDir);
143 if (len > 0 && bfs->lockDir[len-1] != '/')
144 strcpy (bfs->lockDir + len, "/");
147 void bf_cache (BFiles bfs, const char *spec)
151 if (!bfs->commit_area)
152 bfs->commit_area = mf_init ("shadow", spec);
155 bfs->commit_area = NULL;
158 int bf_close (BFile bf)
167 BFile bf_open (BFiles bfs, const char *name, int block_size, int wflag)
169 BFile tmp = xmalloc(sizeof(BFile_struct));
171 if (bfs->commit_area)
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,
182 outf = open_cache (bfs, "a");
185 logf (LOG_FATAL|LOG_ERRNO, "open %scache",
186 bfs->lockDir ? bfs->lockDir : "");
189 fprintf (outf, "%s %d\n", name, block_size);
195 tmp->mf = mf_open(bfs->register_area, name, block_size, wflag);
200 logf (LOG_FATAL, "mf_open failed for %s", name);
207 int bf_read (BFile bf, int no, int offset, int num, void *buf)
211 if (bf->cf && (r=cf_read (bf->cf, no, offset, num, buf)) != -1)
213 return mf_read (bf->mf, no, offset, num, buf);
216 int bf_write (BFile bf, int no, int offset, int num, const void *buf)
219 return cf_write (bf->cf, no, offset, num, buf);
220 return mf_write (bf->mf, no, offset, num, buf);
223 int bf_commitExists (BFiles bfs)
227 inf = open_cache (bfs, "r");
236 void bf_commitExec (BFiles bfs)
245 assert (bfs->commit_area);
246 if (!(inf = open_cache (bfs, "r")))
248 logf (LOG_LOG, "No commit file");
251 while (fscanf (inf, "%s %d", path, &block_size) == 2)
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);
264 void bf_commitClean (BFiles bfs, const char *spec)
274 if (!bfs->commit_area)
276 bf_cache (bfs, spec);
280 if (!(inf = open_cache (bfs, "r")))
282 while (fscanf (inf, "%s %d", path, &block_size) == 2)
284 mf = mf_open (bfs->register_area, path, block_size, 0);
285 cf = cf_open (mf, bfs->commit_area, path, block_size, 1, &firstTime);