2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1998-01-12 15:04:07 adam
8 * The test option (-s) only uses read-lock (and not write lock).
10 * Revision 1.13 1997/09/09 13:38:06 adam
11 * Partial port to WIN95/NT.
13 * Revision 1.12 1996/11/08 11:10:13 adam
14 * Buffers used during file match got bigger.
15 * Compressed ISAM support everywhere.
16 * Bug fixes regarding masking characters in queries.
17 * Redesigned Regexp-2 queries.
19 * Revision 1.11 1996/10/29 14:06:47 adam
20 * Include zebrautl.h instead of alexutil.h.
22 * Revision 1.10 1996/06/04 10:18:58 adam
23 * Minor changes - removed include of ctype.h.
25 * Revision 1.9 1996/04/23 12:39:07 adam
26 * Bug fix: In function dirs_del dict_delete is used to remove a file
27 * rather than a bogus dict_insert.
29 * Revision 1.8 1996/04/12 07:02:21 adam
30 * File update of single files.
32 * Revision 1.7 1996/03/21 14:50:09 adam
33 * File update uses modify-time instead of change-time.
35 * Revision 1.6 1996/02/02 13:44:43 adam
36 * The public dictionary functions simply use char instead of Dict_char
37 * to represent search strings. Dict_char is used internally only.
39 * Revision 1.5 1996/01/17 14:54:44 adam
40 * Function dirs_rmdir uses dict_delete.
42 * Revision 1.4 1995/11/30 08:34:27 adam
43 * Started work on commit facility.
44 * Changed a few malloc/free to xmalloc/xfree.
46 * Revision 1.3 1995/11/20 16:59:45 adam
47 * New update method: the 'old' keys are saved for each records.
49 * Revision 1.2 1995/11/20 11:56:23 adam
50 * Work on new traversal.
52 * Revision 1.1 1995/11/17 15:54:42 adam
53 * Started work on virtual directory structure.
63 #define DIRS_MAX_PATH 1024
71 struct dirs_entry *entries;
72 char nextpath[DIRS_MAX_PATH];
73 char prefix[DIRS_MAX_PATH];
75 struct dirs_entry *last_entry;
78 static int dirs_client_proc (char *name, const char *info, int pos,
81 struct dirs_info *ci = client;
82 struct dirs_entry *entry;
84 if (memcmp (name, ci->prefix, ci->prelen))
91 if (ci->no_cur == ci->no_max)
95 entry = ci->entries + ci->no_cur;
96 if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
98 strcpy (entry->path, name + ci->prelen);
99 entry->kind = dirs_file;
100 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
101 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
102 sizeof(entry->mtime));
105 else if (info[0] == sizeof(entry->mtime))
107 strcpy (entry->path, name + ci->prelen);
108 entry->kind = dirs_dir;
109 memcpy (&entry->mtime, info+1, sizeof(entry->mtime));
115 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw)
118 int before = 0, after;
120 logf (LOG_DEBUG, "dirs_open %s", rep);
121 p = xmalloc (sizeof (*p));
124 strcpy (p->prefix, rep);
125 p->prelen = strlen(p->prefix);
126 strcpy (p->nextpath, rep);
127 p->no_read = p->no_cur = 0;
128 after = p->no_max = 100;
129 p->entries = xmalloc (sizeof(*p->entries) * (p->no_max));
130 logf (LOG_DEBUG, "dirs_open first scan");
131 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
135 struct dirs_info *dirs_fopen (Dict dict, const char *path)
138 struct dirs_entry *entry;
141 p = xmalloc (sizeof(*p));
144 p->entries = xmalloc (sizeof(*p->entries));
150 info = dict_lookup (dict, path);
151 if (info && info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
153 strcpy (entry->path, path);
154 entry->kind = dirs_file;
155 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
156 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
157 sizeof(entry->mtime));
163 struct dirs_entry *dirs_read (struct dirs_info *p)
165 int before = 0, after = p->no_max+1;
167 if (p->no_read < p->no_cur)
169 logf (LOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
170 (p->entries + p->no_read)->path);
171 return p->last_entry = p->entries + (p->no_read++);
173 if (p->no_cur < p->no_max)
174 return p->last_entry = NULL;
176 logf (LOG_DEBUG, "dirs_read rescan");
177 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
179 if (p->no_read < p->no_cur)
180 return p->last_entry = p->entries;
181 return p->last_entry = NULL;
184 struct dirs_entry *dirs_last (struct dirs_info *p)
186 return p->last_entry;
189 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime)
191 char path[DIRS_MAX_PATH];
193 sprintf (path, "%s%s", p->prefix, src);
194 logf (LOG_DEBUG, "dirs_mkdir %s", path);
196 dict_insert (p->dict, path, sizeof(mtime), &mtime);
199 void dirs_rmdir (struct dirs_info *p, const char *src)
201 char path[DIRS_MAX_PATH];
203 sprintf (path, "%s%s", p->prefix, src);
204 logf (LOG_DEBUG, "dirs_rmdir %s", path);
206 dict_delete (p->dict, path);
209 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime)
211 char path[DIRS_MAX_PATH];
214 sprintf (path, "%s%s", p->prefix, src);
215 logf (LOG_DEBUG, "dirs_add %s", path);
216 memcpy (info, &sysno, sizeof(sysno));
217 memcpy (info+sizeof(sysno), &mtime, sizeof(mtime));
219 dict_insert (p->dict, path, sizeof(sysno)+sizeof(mtime), info);
222 void dirs_del (struct dirs_info *p, const char *src)
224 char path[DIRS_MAX_PATH];
226 sprintf (path, "%s%s", p->prefix, src);
227 logf (LOG_DEBUG, "dirs_del %s", path);
229 dict_delete (p->dict, path);
232 void dirs_free (struct dirs_info **pp)
234 struct dirs_info *p = *pp;