Towards GPL
[idzebra-moved-to-github.git] / index / dirs.c
1 /* $Id: dirs.c,v 1.18 2002-08-02 19:26:55 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 };
46
47 static int dirs_client_proc (char *name, const char *info, int pos,
48                              void *client)
49 {
50     struct dirs_info *ci = (struct dirs_info *) client;
51     struct dirs_entry *entry;
52
53     if (memcmp (name, ci->prefix, ci->prelen))
54         return 1;
55     if (ci->no_cur < 0)
56     {
57         ci->no_cur = 0;
58         return 0;
59     }
60     if (ci->no_cur == ci->no_max)
61     {
62         assert (0);
63     }
64     entry = ci->entries + ci->no_cur;
65     if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
66     {
67         strcpy (entry->path, name + ci->prelen); 
68         entry->kind = dirs_file;
69         memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
70         memcpy (&entry->mtime, info+1+sizeof(entry->sysno), 
71                 sizeof(entry->mtime));
72         ci->no_cur++;
73     } 
74     else if (info[0] == sizeof(entry->mtime))
75     {
76         strcpy (entry->path, name + ci->prelen);
77         entry->kind = dirs_dir;
78         memcpy (&entry->mtime, info+1, sizeof(entry->mtime));
79         ci->no_cur++;
80     }
81     return 0;
82 }
83
84 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw)
85 {
86     struct dirs_info *p;
87     int before = 0, after;
88
89     logf (LOG_DEBUG, "dirs_open %s", rep);
90     p = (struct dirs_info *) xmalloc (sizeof (*p));
91     p->dict = dict;
92     p->rw = rw;
93     strcpy (p->prefix, rep);
94     p->prelen = strlen(p->prefix);
95     strcpy (p->nextpath, rep);
96     p->no_read = p->no_cur = 0;
97     after = p->no_max = 100;
98     p->entries = (struct dirs_entry *)
99         xmalloc (sizeof(*p->entries) * (p->no_max));
100     logf (LOG_DEBUG, "dirs_open first scan");
101     dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
102     return p;
103 }
104
105 struct dirs_info *dirs_fopen (Dict dict, const char *path)
106 {
107     struct dirs_info *p;
108     struct dirs_entry *entry;
109     char *info;
110
111     p = (struct dirs_info *) xmalloc (sizeof(*p));
112     p->dict = dict;
113     *p->prefix = '\0';
114     p->entries = (struct dirs_entry *) xmalloc (sizeof(*p->entries));
115     p->no_read = 0;
116     p->no_cur = 0;
117     p->no_max = 2;
118
119     entry = p->entries;
120     info = dict_lookup (dict, path);
121     if (info && info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
122     {
123         strcpy (entry->path, path); 
124         entry->kind = dirs_file;
125         memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
126         memcpy (&entry->mtime, info+1+sizeof(entry->sysno), 
127                 sizeof(entry->mtime));
128         p->no_cur++;
129     }
130     return p;
131 }
132
133 struct dirs_entry *dirs_read (struct dirs_info *p)
134 {
135     int before = 0, after = p->no_max+1;
136
137     if (p->no_read < p->no_cur)
138     {
139         logf (LOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
140               (p->entries + p->no_read)->path);
141         return p->last_entry = p->entries + (p->no_read++);
142     }
143     if (p->no_cur < p->no_max)
144         return p->last_entry = NULL;
145     p->no_cur = -1;
146     logf (LOG_DEBUG, "dirs_read rescan");
147     dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
148     p->no_read = 1;
149     if (p->no_read <= p->no_cur)
150         return p->last_entry = p->entries;
151     return p->last_entry = NULL;
152 }
153
154 struct dirs_entry *dirs_last (struct dirs_info *p)
155 {
156     return p->last_entry;
157 }
158
159 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime)
160 {
161     char path[DIRS_MAX_PATH];
162
163     sprintf (path, "%s%s", p->prefix, src);
164     logf (LOG_DEBUG, "dirs_mkdir %s", path);
165     if (p->rw)
166         dict_insert (p->dict, path, sizeof(mtime), &mtime);
167 }
168
169 void dirs_rmdir (struct dirs_info *p, const char *src)
170 {
171     char path[DIRS_MAX_PATH];
172
173     sprintf (path, "%s%s", p->prefix, src);
174     logf (LOG_DEBUG, "dirs_rmdir %s", path);
175     if (p->rw)
176         dict_delete (p->dict, path);
177 }
178
179 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime)
180 {
181     char path[DIRS_MAX_PATH];
182     char info[16];
183
184     sprintf (path, "%s%s", p->prefix, src);
185     logf (LOG_DEBUG, "dirs_add %s", path);
186     memcpy (info, &sysno, sizeof(sysno));
187     memcpy (info+sizeof(sysno), &mtime, sizeof(mtime));
188     if (p->rw)
189         dict_insert (p->dict, path, sizeof(sysno)+sizeof(mtime), info);
190 }
191
192 void dirs_del (struct dirs_info *p, const char *src)
193 {
194     char path[DIRS_MAX_PATH];
195
196     sprintf (path, "%s%s", p->prefix, src);
197     logf (LOG_DEBUG, "dirs_del %s", path);
198     if (p->rw)
199         dict_delete (p->dict, path);
200 }
201
202 void dirs_free (struct dirs_info **pp)
203 {
204     struct dirs_info *p = *pp;
205
206     xfree (p->entries);
207     xfree (p);
208     *pp = NULL;
209 }
210