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