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