2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1996-11-08 11:10:13 adam
8 * Buffers used during file match got bigger.
9 * Compressed ISAM support everywhere.
10 * Bug fixes regarding masking characters in queries.
11 * Redesigned Regexp-2 queries.
13 * Revision 1.11 1996/10/29 14:06:47 adam
14 * Include zebrautl.h instead of alexutil.h.
16 * Revision 1.10 1996/06/04 10:18:58 adam
17 * Minor changes - removed include of ctype.h.
19 * Revision 1.9 1996/04/23 12:39:07 adam
20 * Bug fix: In function dirs_del dict_delete is used to remove a file
21 * rather than a bogus dict_insert.
23 * Revision 1.8 1996/04/12 07:02:21 adam
24 * File update of single files.
26 * Revision 1.7 1996/03/21 14:50:09 adam
27 * File update uses modify-time instead of change-time.
29 * Revision 1.6 1996/02/02 13:44:43 adam
30 * The public dictionary functions simply use char instead of Dict_char
31 * to represent search strings. Dict_char is used internally only.
33 * Revision 1.5 1996/01/17 14:54:44 adam
34 * Function dirs_rmdir uses dict_delete.
36 * Revision 1.4 1995/11/30 08:34:27 adam
37 * Started work on commit facility.
38 * Changed a few malloc/free to xmalloc/xfree.
40 * Revision 1.3 1995/11/20 16:59:45 adam
41 * New update method: the 'old' keys are saved for each records.
43 * Revision 1.2 1995/11/20 11:56:23 adam
44 * Work on new traversal.
46 * Revision 1.1 1995/11/17 15:54:42 adam
47 * Started work on virtual directory structure.
56 #define DIRS_MAX_PATH 1024
63 struct dirs_entry *entries;
64 char nextpath[DIRS_MAX_PATH];
65 char prefix[DIRS_MAX_PATH];
67 struct dirs_entry *last_entry;
70 static int dirs_client_proc (char *name, const char *info, int pos,
73 struct dirs_info *ci = client;
74 struct dirs_entry *entry;
76 if (memcmp (name, ci->prefix, ci->prelen))
83 if (ci->no_cur == ci->no_max)
87 entry = ci->entries + ci->no_cur;
88 if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
90 strcpy (entry->path, name + ci->prelen);
91 entry->kind = dirs_file;
92 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
93 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
94 sizeof(entry->mtime));
97 else if (info[0] == sizeof(entry->mtime))
99 strcpy (entry->path, name + ci->prelen);
100 entry->kind = dirs_dir;
101 memcpy (&entry->mtime, info+1, sizeof(entry->mtime));
107 struct dirs_info *dirs_open (Dict dict, const char *rep)
110 int before = 0, after;
112 logf (LOG_DEBUG, "dirs_open %s", rep);
113 p = xmalloc (sizeof (*p));
115 strcpy (p->prefix, rep);
116 p->prelen = strlen(p->prefix);
117 strcpy (p->nextpath, rep);
118 p->no_read = p->no_cur = 0;
119 after = p->no_max = 100;
120 p->entries = xmalloc (sizeof(*p->entries) * (p->no_max));
121 logf (LOG_DEBUG, "dirs_open first scan");
122 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
126 struct dirs_info *dirs_fopen (Dict dict, const char *path)
129 struct dirs_entry *entry;
132 p = xmalloc (sizeof(*p));
135 p->entries = xmalloc (sizeof(*p->entries));
141 info = dict_lookup (dict, path);
142 if (info && info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
144 strcpy (entry->path, path);
145 entry->kind = dirs_file;
146 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
147 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
148 sizeof(entry->mtime));
154 struct dirs_entry *dirs_read (struct dirs_info *p)
156 int before = 0, after = p->no_max+1;
158 if (p->no_read < p->no_cur)
160 logf (LOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
161 (p->entries + p->no_read)->path);
162 return p->last_entry = p->entries + (p->no_read++);
164 if (p->no_cur < p->no_max)
165 return p->last_entry = NULL;
167 logf (LOG_DEBUG, "dirs_read rescan");
168 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
170 if (p->no_read < p->no_cur)
171 return p->last_entry = p->entries;
172 return p->last_entry = NULL;
175 struct dirs_entry *dirs_last (struct dirs_info *p)
177 return p->last_entry;
180 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime)
182 char path[DIRS_MAX_PATH];
184 sprintf (path, "%s%s", p->prefix, src);
185 logf (LOG_DEBUG, "dirs_mkdir %s", path);
186 dict_insert (p->dict, path, sizeof(mtime), &mtime);
189 void dirs_rmdir (struct dirs_info *p, const char *src)
191 char path[DIRS_MAX_PATH];
193 sprintf (path, "%s%s", p->prefix, src);
194 logf (LOG_DEBUG, "dirs_rmdir %s", path);
195 dict_delete (p->dict, path);
198 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime)
200 char path[DIRS_MAX_PATH];
203 sprintf (path, "%s%s", p->prefix, src);
204 logf (LOG_DEBUG, "dirs_add %s", path);
205 memcpy (info, &sysno, sizeof(sysno));
206 memcpy (info+sizeof(sysno), &mtime, sizeof(mtime));
207 dict_insert (p->dict, path, sizeof(sysno)+sizeof(mtime), info);
210 void dirs_del (struct dirs_info *p, const char *src)
212 char path[DIRS_MAX_PATH];
214 sprintf (path, "%s%s", p->prefix, src);
215 logf (LOG_DEBUG, "dirs_del %s", path);
216 dict_delete (p->dict, path);
219 void dirs_free (struct dirs_info **pp)
221 struct dirs_info *p = *pp;