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