Fixed nasty bug for fileUpdate.
[idzebra-moved-to-github.git] / index / dirs.c
1 /*
2  * Copyright (C) 1994-2001, Index Data
3  * All rights reserved.
4  *
5  * $Log: dirs.c,v $
6  * Revision 1.17  2001-03-29 14:07:14  adam
7  * Fixed nasty bug for fileUpdate.
8  *
9  * Revision 1.16  1999/05/26 07:49:13  adam
10  * C++ compilation.
11  *
12  * Revision 1.15  1999/02/02 14:50:51  adam
13  * Updated WIN32 code specific sections. Changed header.
14  *
15  * Revision 1.14  1998/01/12 15:04:07  adam
16  * The test option (-s) only uses read-lock (and not write lock).
17  *
18  * Revision 1.13  1997/09/09 13:38:06  adam
19  * Partial port to WIN95/NT.
20  *
21  * Revision 1.12  1996/11/08 11:10:13  adam
22  * Buffers used during file match got bigger.
23  * Compressed ISAM support everywhere.
24  * Bug fixes regarding masking characters in queries.
25  * Redesigned Regexp-2 queries.
26  *
27  * Revision 1.11  1996/10/29 14:06:47  adam
28  * Include zebrautl.h instead of alexutil.h.
29  *
30  * Revision 1.10  1996/06/04 10:18:58  adam
31  * Minor changes - removed include of ctype.h.
32  *
33  * Revision 1.9  1996/04/23  12:39:07  adam
34  * Bug fix: In function dirs_del dict_delete is used to remove a file
35  * rather than a bogus dict_insert.
36  *
37  * Revision 1.8  1996/04/12  07:02:21  adam
38  * File update of single files.
39  *
40  * Revision 1.7  1996/03/21 14:50:09  adam
41  * File update uses modify-time instead of change-time.
42  *
43  * Revision 1.6  1996/02/02  13:44:43  adam
44  * The public dictionary functions simply use char instead of Dict_char
45  * to represent search strings. Dict_char is used internally only.
46  *
47  * Revision 1.5  1996/01/17  14:54:44  adam
48  * Function dirs_rmdir uses dict_delete.
49  *
50  * Revision 1.4  1995/11/30  08:34:27  adam
51  * Started work on commit facility.
52  * Changed a few malloc/free to xmalloc/xfree.
53  *
54  * Revision 1.3  1995/11/20  16:59:45  adam
55  * New update method: the 'old' keys are saved for each records.
56  *
57  * Revision 1.2  1995/11/20  11:56:23  adam
58  * Work on new traversal.
59  *
60  * Revision 1.1  1995/11/17  15:54:42  adam
61  * Started work on virtual directory structure.
62  */
63 #include <stdio.h>
64 #include <string.h>
65 #include <assert.h>
66 #include <errno.h>
67 #include <fcntl.h>
68
69 #include "index.h"
70
71 #define DIRS_MAX_PATH 1024
72
73 struct dirs_info {
74     Dict dict;
75     int rw;
76     int no_read;
77     int no_cur;
78     int no_max;
79     struct dirs_entry *entries;
80     char nextpath[DIRS_MAX_PATH];
81     char prefix[DIRS_MAX_PATH];
82     int prelen;
83     struct dirs_entry *last_entry;
84 };
85
86 static int dirs_client_proc (char *name, const char *info, int pos,
87                              void *client)
88 {
89     struct dirs_info *ci = (struct dirs_info *) client;
90     struct dirs_entry *entry;
91
92     if (memcmp (name, ci->prefix, ci->prelen))
93         return 1;
94     if (ci->no_cur < 0)
95     {
96         ci->no_cur = 0;
97         return 0;
98     }
99     if (ci->no_cur == ci->no_max)
100     {
101         assert (0);
102     }
103     entry = ci->entries + ci->no_cur;
104     if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
105     {
106         strcpy (entry->path, name + ci->prelen); 
107         entry->kind = dirs_file;
108         memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
109         memcpy (&entry->mtime, info+1+sizeof(entry->sysno), 
110                 sizeof(entry->mtime));
111         ci->no_cur++;
112     } 
113     else if (info[0] == sizeof(entry->mtime))
114     {
115         strcpy (entry->path, name + ci->prelen);
116         entry->kind = dirs_dir;
117         memcpy (&entry->mtime, info+1, sizeof(entry->mtime));
118         ci->no_cur++;
119     }
120     return 0;
121 }
122
123 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw)
124 {
125     struct dirs_info *p;
126     int before = 0, after;
127
128     logf (LOG_DEBUG, "dirs_open %s", rep);
129     p = (struct dirs_info *) xmalloc (sizeof (*p));
130     p->dict = dict;
131     p->rw = rw;
132     strcpy (p->prefix, rep);
133     p->prelen = strlen(p->prefix);
134     strcpy (p->nextpath, rep);
135     p->no_read = p->no_cur = 0;
136     after = p->no_max = 100;
137     p->entries = (struct dirs_entry *)
138         xmalloc (sizeof(*p->entries) * (p->no_max));
139     logf (LOG_DEBUG, "dirs_open first scan");
140     dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
141     return p;
142 }
143
144 struct dirs_info *dirs_fopen (Dict dict, const char *path)
145 {
146     struct dirs_info *p;
147     struct dirs_entry *entry;
148     char *info;
149
150     p = (struct dirs_info *) xmalloc (sizeof(*p));
151     p->dict = dict;
152     *p->prefix = '\0';
153     p->entries = (struct dirs_entry *) xmalloc (sizeof(*p->entries));
154     p->no_read = 0;
155     p->no_cur = 0;
156     p->no_max = 2;
157
158     entry = p->entries;
159     info = dict_lookup (dict, path);
160     if (info && info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
161     {
162         strcpy (entry->path, path); 
163         entry->kind = dirs_file;
164         memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
165         memcpy (&entry->mtime, info+1+sizeof(entry->sysno), 
166                 sizeof(entry->mtime));
167         p->no_cur++;
168     }
169     return p;
170 }
171
172 struct dirs_entry *dirs_read (struct dirs_info *p)
173 {
174     int before = 0, after = p->no_max+1;
175
176     if (p->no_read < p->no_cur)
177     {
178         logf (LOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
179               (p->entries + p->no_read)->path);
180         return p->last_entry = p->entries + (p->no_read++);
181     }
182     if (p->no_cur < p->no_max)
183         return p->last_entry = NULL;
184     p->no_cur = -1;
185     logf (LOG_DEBUG, "dirs_read rescan");
186     dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
187     p->no_read = 1;
188     if (p->no_read <= p->no_cur)
189         return p->last_entry = p->entries;
190     return p->last_entry = NULL;
191 }
192
193 struct dirs_entry *dirs_last (struct dirs_info *p)
194 {
195     return p->last_entry;
196 }
197
198 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime)
199 {
200     char path[DIRS_MAX_PATH];
201
202     sprintf (path, "%s%s", p->prefix, src);
203     logf (LOG_DEBUG, "dirs_mkdir %s", path);
204     if (p->rw)
205         dict_insert (p->dict, path, sizeof(mtime), &mtime);
206 }
207
208 void dirs_rmdir (struct dirs_info *p, const char *src)
209 {
210     char path[DIRS_MAX_PATH];
211
212     sprintf (path, "%s%s", p->prefix, src);
213     logf (LOG_DEBUG, "dirs_rmdir %s", path);
214     if (p->rw)
215         dict_delete (p->dict, path);
216 }
217
218 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime)
219 {
220     char path[DIRS_MAX_PATH];
221     char info[16];
222
223     sprintf (path, "%s%s", p->prefix, src);
224     logf (LOG_DEBUG, "dirs_add %s", path);
225     memcpy (info, &sysno, sizeof(sysno));
226     memcpy (info+sizeof(sysno), &mtime, sizeof(mtime));
227     if (p->rw)
228         dict_insert (p->dict, path, sizeof(sysno)+sizeof(mtime), info);
229 }
230
231 void dirs_del (struct dirs_info *p, const char *src)
232 {
233     char path[DIRS_MAX_PATH];
234
235     sprintf (path, "%s%s", p->prefix, src);
236     logf (LOG_DEBUG, "dirs_del %s", path);
237     if (p->rw)
238         dict_delete (p->dict, path);
239 }
240
241 void dirs_free (struct dirs_info **pp)
242 {
243     struct dirs_info *p = *pp;
244
245     xfree (p->entries);
246     xfree (p);
247     *pp = NULL;
248 }
249