Commit files use separate meta file area.
[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.15  1995-12-01 16:24:28  adam
8  * Commit files use separate meta file area.
9  *
10  * Revision 1.14  1995/12/01  11:37:21  adam
11  * Cached/commit files implemented as meta-files.
12  *
13  * Revision 1.13  1995/11/30  17:00:49  adam
14  * Several bug fixes. Commit system runs now.
15  *
16  * Revision 1.12  1995/11/30  08:33:10  adam
17  * Started work on commit facility.
18  *
19  * Revision 1.11  1995/09/04  12:33:21  adam
20  * Various cleanup. YAZ util used instead.
21  *
22  * Revision 1.10  1994/08/25  10:15:54  quinn
23  * Trivial
24  *
25  * Revision 1.9  1994/08/24  08:45:48  quinn
26  * Using mfile.
27  *
28  * Revision 1.8  1994/08/23  15:03:34  quinn
29  * *** empty log message ***
30  *
31  * Revision 1.7  1994/08/23  14:25:45  quinn
32  * Added O_CREAT because some geek wanted it. Sheesh.
33  *
34  * Revision 1.6  1994/08/23  14:21:38  quinn
35  * Fixed call to log
36  *
37  * Revision 1.5  1994/08/18  08:10:08  quinn
38  * Minimal changes
39  *
40  * Revision 1.4  1994/08/17  14:27:32  quinn
41  * last mods
42  *
43  * Revision 1.2  1994/08/17  14:09:32  quinn
44  * Compiles cleanly (still only dummy).
45  *
46  * Revision 1.1  1994/08/17  13:55:08  quinn
47  * New blocksystem. dummy only
48  *
49  */
50
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <assert.h>
54
55 #include <alexutil.h>
56 #include <bfile.h>
57 #include "cfile.h"
58
59 static MFile_area commit_area = NULL;
60
61 void bf_cache (void)
62 {
63     if (!commit_area)
64         if (res_get (common_resource, "commit"))
65             commit_area = mf_init ("commit");
66         else
67         {
68             logf (LOG_FATAL, "Commit area must be defined if commit"
69                   "is to be enabled");
70             exit (1);
71         }
72 }
73
74 int bf_close (BFile bf)
75 {
76     if (bf->cf)
77         cf_close (bf->cf);
78     mf_close (bf->mf);
79     xfree (bf);
80     return 0;
81 }
82
83 BFile bf_open (const char *name, int block_size, int wflag)
84 {
85     BFile tmp = xmalloc(sizeof(BFile_struct));
86
87     if (commit_area)
88     {
89         FILE *outf;
90         int first_time;
91
92         logf (LOG_LOG, "cf,mf_open %s", name);
93         tmp->mf = mf_open (0, name, block_size, 0);
94         tmp->cf = cf_open (tmp->mf, commit_area, name, block_size,
95                            wflag, &first_time);
96
97         if (first_time)
98         {
99             outf = fopen ("cache", "a");
100             fprintf (outf, "%s %d\n", name, block_size);
101             fclose (outf);
102         }
103     }
104     else
105     {
106         tmp->mf = mf_open(0, name, block_size, wflag);
107         tmp->cf = NULL;
108     }
109     if (!tmp->mf)
110     {
111         logf (LOG_FATAL, "mf_open failed for %s", name);
112         xfree (tmp);
113         return 0;
114     }
115     return(tmp);
116 }
117
118 int bf_read (BFile bf, int no, int offset, int num, void *buf)
119 {
120     int r;
121
122     if (bf->cf && (r=cf_read (bf->cf, no, offset, num, buf)) != -1)
123         return r;
124     return mf_read (bf->mf, no, offset, num, buf);
125 }
126
127 int bf_write (BFile bf, int no, int offset, int num, const void *buf)
128 {
129     if (bf->cf)
130         return cf_write (bf->cf, no, offset, num, buf);
131     return mf_write (bf->mf, no, offset, num, buf);
132 }
133
134 void bf_commit (void)
135 {
136     FILE *inf;
137     int block_size;
138     char path[256];
139     MFile mf;
140     CFile cf;
141     int first_time;
142
143     assert (commit_area);
144     if (!(inf = fopen ("cache", "r")))
145     {
146         logf (LOG_FATAL|LOG_ERRNO, "cannot open commit %s", "cache");
147         exit (1);
148     }
149     while (fscanf (inf, "%s %d", path, &block_size) == 2)
150     {
151         mf = mf_open (0, path, block_size, 1);
152         cf = cf_open (mf, commit_area, path, block_size, 0, &first_time);
153
154         cf_commit (cf);
155
156         cf_close (cf);
157         mf_close (mf);
158     }
159     fclose (inf);
160 }