556e0f52e6aa02219feb0203db5f3808f4097c06
[idzebra-moved-to-github.git] / index / dirs.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <fcntl.h>
26
27 #include "index.h"
28
29 #define DIRS_MAX_PATH 1024
30
31 struct dirs_info {
32     Dict dict;
33     int rw;
34     int no_read;
35     int no_cur;
36     int no_max;
37     struct dirs_entry *entries;
38     char nextpath[DIRS_MAX_PATH];
39     char prefix[DIRS_MAX_PATH];
40     int prelen;
41     struct dirs_entry *last_entry;
42     int nextpath_deleted;
43 };
44
45 static int dirs_client_proc(char *name, const char *info, int pos,
46                              void *client)
47 {
48     struct dirs_info *ci = (struct dirs_info *) client;
49     struct dirs_entry *entry;
50
51     if (memcmp(name, ci->prefix, ci->prelen))
52         return 1;
53     if (ci->no_cur < 0)
54     {
55         ci->no_cur = 0;
56         return 0;
57     }
58     assert(ci->no_cur < ci->no_max);
59     entry = ci->entries + ci->no_cur;
60     if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
61     {
62         strcpy(entry->path, name + ci->prelen); 
63         entry->kind = dirs_file;
64         memcpy(&entry->sysno, info+1, sizeof(entry->sysno));
65         memcpy(&entry->mtime, info+1+sizeof(entry->sysno), 
66                 sizeof(entry->mtime));
67         ci->no_cur++;
68     } 
69     else if (info[0] == sizeof(entry->mtime))
70     {
71         strcpy(entry->path, name + ci->prelen);
72         entry->kind = dirs_dir;
73         memcpy(&entry->mtime, info+1, sizeof(entry->mtime));
74         ci->no_cur++;
75     }
76     return 0;
77 }
78
79 struct dirs_info *dirs_open(Dict dict, const char *rep, int rw)
80 {
81     struct dirs_info *p;
82     int before = 0, after;
83
84     yaz_log(YLOG_DEBUG, "dirs_open %s", rep);
85     p = (struct dirs_info *) xmalloc(sizeof(*p));
86     p->dict = dict;
87     p->rw = rw;
88     strcpy(p->prefix, rep);
89     p->prelen = strlen(p->prefix);
90     strcpy(p->nextpath, rep);
91     p->nextpath_deleted = 0;
92     p->no_read = p->no_cur = 0;
93     after = p->no_max = 100;
94     p->entries = (struct dirs_entry *)
95         xmalloc(sizeof(*p->entries) * (p->no_max));
96     yaz_log(YLOG_DEBUG, "dirs_open first scan");
97     dict_scan(p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
98     return p;
99 }
100
101 struct dirs_info *dirs_fopen(Dict dict, const char *path, int rw)
102 {
103     struct dirs_info *p;
104     struct dirs_entry *entry;
105     char *info;
106
107     p = (struct dirs_info *) xmalloc(sizeof(*p));
108     p->dict = dict;
109     p->rw = rw;
110     *p->prefix = '\0';
111     p->entries = (struct dirs_entry *) xmalloc(sizeof(*p->entries));
112     p->no_read = 0;
113     p->no_cur = 0;
114     p->no_max = 2;
115
116     entry = p->entries;
117     info = dict_lookup(dict, path);
118     if (info && info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
119     {
120         strcpy(entry->path, path); 
121         entry->kind = dirs_file;
122         memcpy(&entry->sysno, info+1, sizeof(entry->sysno));
123         memcpy(&entry->mtime, info+1+sizeof(entry->sysno), 
124                 sizeof(entry->mtime));
125         p->no_cur++;
126     }
127     return p;
128 }
129
130 struct dirs_entry *dirs_read(struct dirs_info *p)
131 {
132     int before = 0, after = p->no_max+1;
133
134     if (p->no_read < p->no_cur)
135     {
136         yaz_log(YLOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
137               (p->entries + p->no_read)->path);
138         return p->last_entry = p->entries + (p->no_read++);
139     }
140     if (p->no_cur < p->no_max)
141         return p->last_entry = NULL;
142     if (p->nextpath_deleted)
143     {
144         p->no_cur = 0;
145         after = p->no_max;
146     }
147     else
148     {
149         p->no_cur = -1;
150         after = p->no_max + 1;
151     }
152     p->no_read = 1;
153     p->nextpath_deleted = 0;
154     yaz_log(YLOG_DEBUG, "dirs_read rescan %s", p->nextpath);
155     dict_scan(p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
156     if (p->no_read <= p->no_cur)
157         return p->last_entry = p->entries;
158     return p->last_entry = NULL;
159 }
160
161 struct dirs_entry *dirs_last(struct dirs_info *p)
162 {
163     return p->last_entry;
164 }
165
166 void dirs_mkdir(struct dirs_info *p, const char *src, time_t mtime)
167 {
168     char path[DIRS_MAX_PATH];
169
170     sprintf(path, "%s%s", p->prefix, src);
171     yaz_log(YLOG_DEBUG, "dirs_mkdir %s", path);
172     if (p->rw)
173         dict_insert(p->dict, path, sizeof(mtime), &mtime);
174 }
175
176 void dirs_rmdir(struct dirs_info *p, const char *src)
177 {
178     char path[DIRS_MAX_PATH];
179
180     sprintf(path, "%s%s", p->prefix, src);
181     yaz_log(YLOG_DEBUG, "dirs_rmdir %s", path);
182     if (p->rw)
183         dict_delete(p->dict, path);
184 }
185
186 void dirs_add(struct dirs_info *p, const char *src, zint sysno, time_t mtime)
187 {
188     char path[DIRS_MAX_PATH];
189     char info[16];
190
191     sprintf(path, "%s%s", p->prefix, src);
192     yaz_log(YLOG_DEBUG, "dirs_add %s", path);
193     memcpy(info, &sysno, sizeof(sysno));
194     memcpy(info+sizeof(sysno), &mtime, sizeof(mtime));
195     if (p->rw)
196         dict_insert(p->dict, path, sizeof(sysno)+sizeof(mtime), info);
197 }
198
199 void dirs_del(struct dirs_info *p, const char *src)
200 {
201     char path[DIRS_MAX_PATH];
202
203     sprintf(path, "%s%s", p->prefix, src);
204     yaz_log(YLOG_DEBUG, "dirs_del %s", path);
205     if (p->rw)
206     {
207         if (!strcmp(path, p->nextpath))
208              p->nextpath_deleted = 1;
209         dict_delete(p->dict, path);
210     }
211 }
212
213 void dirs_free(struct dirs_info **pp)
214 {
215     struct dirs_info *p = *pp;
216
217     xfree(p->entries);
218     xfree(p);
219     *pp = NULL;
220 }
221
222 /*
223  * Local variables:
224  * c-basic-offset: 4
225  * indent-tabs-mode: nil
226  * End:
227  * vim: shiftwidth=4 tabstop=8 expandtab
228  */
229