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