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