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