IDZEBRA  2.1.2
dirs.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) 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 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
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 {
36  int rw;
37  int no_read;
38  int no_cur;
39  int no_max;
43  int prelen;
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, int rw)
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->rw = rw;
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  yaz_log(YLOG_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  if (p->nextpath_deleted)
146  {
147  p->no_cur = 0;
148  after = p->no_max;
149  }
150  else
151  {
152  p->no_cur = -1;
153  after = p->no_max + 1;
154  }
155  p->no_read = 1;
156  p->nextpath_deleted = 0;
157  yaz_log(YLOG_DEBUG, "dirs_read rescan %s", p->nextpath);
158  dict_scan(p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
159  if (p->no_read <= p->no_cur)
160  return p->last_entry = p->entries;
161  return p->last_entry = NULL;
162 }
163 
164 struct dirs_entry *dirs_last(struct dirs_info *p)
165 {
166  return p->last_entry;
167 }
168 
169 void dirs_mkdir(struct dirs_info *p, const char *src, time_t mtime)
170 {
171  char path[DIRS_MAX_PATH];
172 
173  sprintf(path, "%s%s", p->prefix, src);
174  yaz_log(YLOG_DEBUG, "dirs_mkdir %s", path);
175  if (p->rw)
176  dict_insert(p->dict, path, sizeof(mtime), &mtime);
177 }
178 
179 void dirs_rmdir(struct dirs_info *p, const char *src)
180 {
181  char path[DIRS_MAX_PATH];
182 
183  sprintf(path, "%s%s", p->prefix, src);
184  yaz_log(YLOG_DEBUG, "dirs_rmdir %s", path);
185  if (p->rw)
186  dict_delete(p->dict, path);
187 }
188 
189 void dirs_add(struct dirs_info *p, const char *src, zint sysno, time_t mtime)
190 {
191  char path[DIRS_MAX_PATH];
192  char info[16];
193 
194  sprintf(path, "%s%s", p->prefix, src);
195  yaz_log(YLOG_DEBUG, "dirs_add %s", path);
196  memcpy(info, &sysno, sizeof(sysno));
197  memcpy(info+sizeof(sysno), &mtime, sizeof(mtime));
198  if (p->rw)
199  dict_insert(p->dict, path, sizeof(sysno)+sizeof(mtime), info);
200 }
201 
202 void dirs_del(struct dirs_info *p, const char *src)
203 {
204  char path[DIRS_MAX_PATH];
205 
206  sprintf(path, "%s%s", p->prefix, src);
207  yaz_log(YLOG_DEBUG, "dirs_del %s", path);
208  if (p->rw)
209  {
210  if (!strcmp(path, p->nextpath))
211  p->nextpath_deleted = 1;
212  dict_delete(p->dict, path);
213  }
214 }
215 
216 void dirs_free(struct dirs_info **pp)
217 {
218  struct dirs_info *p = *pp;
219 
220  xfree(p->entries);
221  xfree(p);
222  *pp = NULL;
223 }
224 
225 /*
226  * Local variables:
227  * c-basic-offset: 4
228  * c-file-style: "Stroustrup"
229  * indent-tabs-mode: nil
230  * End:
231  * vim: shiftwidth=4 tabstop=8 expandtab
232  */
233 
char path[256]
Definition: index.h:65
struct dirs_entry * dirs_last(struct dirs_info *p)
Definition: dirs.c:164
int nextpath_deleted
Definition: dirs.c:45
Definition: index.h:55
char prefix[DIRS_MAX_PATH]
Definition: dirs.c:42
struct dirs_info * dirs_open(Dict dict, const char *rep, int rw)
Definition: dirs.c:82
int no_max
Definition: dirs.c:39
void dirs_add(struct dirs_info *p, const char *src, zint sysno, time_t mtime)
Definition: dirs.c:189
zint sysno
Definition: index.h:66
int dict_insert(Dict dict, const char *p, int userlen, void *userinfo)
insert item into dictionary
Definition: insert.c:438
char nextpath[DIRS_MAX_PATH]
Definition: dirs.c:41
Definition: dirs.c:34
struct dirs_info * dirs_fopen(Dict dict, const char *path, int rw)
Definition: dirs.c:104
static int dirs_client_proc(char *name, const char *info, int pos, void *client)
Definition: dirs.c:48
time_t mtime
Definition: index.h:67
int dict_scan(Dict dict, char *str, int *before, int *after, void *client, int(*f)(char *name, const char *info, int pos, void *client))
dictionary scan
Definition: scan.c:242
Definition: index.h:63
#define DIRS_MAX_PATH
Definition: dirs.c:32
void dirs_del(struct dirs_info *p, const char *src)
Definition: dirs.c:202
void dirs_free(struct dirs_info **pp)
Definition: dirs.c:216
char * dict_lookup(Dict dict, const char *p)
lookup item in dictionary
Definition: lookup.c:100
int rw
Definition: dirs.c:36
void dirs_rmdir(struct dirs_info *p, const char *src)
Definition: dirs.c:179
struct dirs_entry * entries
Definition: dirs.c:40
struct dirs_entry * last_entry
Definition: dirs.c:44
enum dirsKind kind
Definition: index.h:64
void dirs_mkdir(struct dirs_info *p, const char *src, time_t mtime)
Definition: dirs.c:169
long zint
Zebra integer.
Definition: util.h:66
struct dirs_entry * dirs_read(struct dirs_info *p)
Definition: dirs.c:133
int prelen
Definition: dirs.c:43
int no_cur
Definition: dirs.c:38
Dict dict
Definition: dirs.c:35
int no_read
Definition: dirs.c:37
int dict_delete(Dict dict, const char *p)
deletes item from dictionary
Definition: delete.c:260