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