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