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