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