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