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