2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1996-02-05 12:30:02 adam
8 * Logging reduced a bit.
9 * The remaining running time is estimated during register merge.
11 * Revision 1.15 1995/12/07 17:38:48 adam
12 * Work locking mechanisms for concurrent updates/commit.
14 * Revision 1.14 1995/12/06 12:41:26 adam
15 * New command 'stat' for the index program.
16 * Filenames can be read from stdin by specifying '-'.
17 * Bug fix/enhancement of the transformation from terms to regular
18 * expressons in the search engine.
20 * Revision 1.13 1995/11/28 09:09:46 adam
21 * Zebra config renamed.
22 * Use setting 'recordId' to identify record now.
23 * Bug fix in recindex.c: rec_release_blocks was invokeded even
24 * though the blocks were already released.
25 * File traversal properly deletes records when needed.
27 * Revision 1.12 1995/11/24 11:31:37 adam
28 * Commands add & del read filenames from stdin if source directory is
30 * Match criteria supports 'constant' strings.
32 * Revision 1.11 1995/11/22 17:19:19 adam
33 * Record management uses the bfile system.
35 * Revision 1.10 1995/11/21 15:01:16 adam
36 * New general match criteria implemented.
37 * New feature: document groups.
39 * Revision 1.9 1995/11/21 09:20:32 adam
40 * Yet more work on record match.
42 * Revision 1.8 1995/11/20 16:59:46 adam
43 * New update method: the 'old' keys are saved for each records.
45 * Revision 1.7 1995/11/20 11:56:28 adam
46 * Work on new traversal.
48 * Revision 1.6 1995/11/17 15:54:42 adam
49 * Started work on virtual directory structure.
51 * Revision 1.5 1995/10/17 18:02:09 adam
52 * New feature: databases. Implemented as prefix to words in dictionary.
54 * Revision 1.4 1995/09/28 09:19:46 adam
55 * xfree/xmalloc used everywhere.
56 * Extract/retrieve method seems to work for text records.
58 * Revision 1.3 1995/09/06 16:11:18 adam
59 * Option: only one word key per file.
61 * Revision 1.2 1995/09/04 12:33:43 adam
62 * Various cleanup. YAZ util used instead.
64 * Revision 1.1 1995/09/01 14:06:36 adam
65 * Split of work into more files.
73 #include <sys/types.h>
80 static int repComp (const char *a, const char *b, size_t len)
84 return memcmp (a, b, len);
87 static void repositoryExtractR (int deleteFlag, char *rep,
88 struct recordGroup *rGroup)
92 size_t rep_len = strlen (rep);
97 logf (LOG_LOG, "Dir: %s", rep);
98 if (rep[rep_len-1] != '/')
102 for (i=0; e[i].name; i++)
104 strcpy (rep +rep_len+1, e[i].name);
108 fileExtract (NULL, rep, rGroup, deleteFlag);
111 repositoryExtractR (deleteFlag, rep, rGroup);
119 static void stdinExtractR (int deleteFlag, struct recordGroup *rGroup)
123 logf (LOG_LOG, "stdinExtractR");
124 while (scanf ("%s", tmppath) == 1)
125 fileExtract (NULL, tmppath, rGroup, deleteFlag);
128 static void repositoryDeleteR (struct dirs_info *di, struct dirs_entry *dst,
129 const char *base, char *src,
130 struct recordGroup *rGroup)
133 size_t src_len = strlen (src);
135 while (dst && !repComp (dst->path, src, src_len+1))
140 sprintf (tmppath, "%s%s", base, dst->path);
141 fileExtract (&dst->sysno, tmppath, rGroup, 1);
143 strcpy (tmppath, dst->path);
144 dst = dirs_read (di);
145 dirs_del (di, tmppath);
148 strcpy (tmppath, dst->path);
149 dst = dirs_read (di);
150 dirs_rmdir (di, tmppath);
153 dst = dirs_read (di);
158 static void repositoryUpdateR (struct dirs_info *di, struct dirs_entry *dst,
159 const char *base, char *src,
160 struct recordGroup *rGroup)
162 struct dir_entry *e_src;
164 static char tmppath[1024];
165 size_t src_len = strlen (src);
167 sprintf (tmppath, "%s%s", base, src);
168 e_src = dir_open (tmppath);
169 logf (LOG_LOG, "Dir: %s", tmppath);
171 if (!dst || repComp (dst->path, src, src_len))
173 if (!dst || strcmp (dst->path, src))
178 if (src_len && src[src_len-1] == '/')
182 src[src_len+1] = '\0';
183 dirs_mkdir (di, src, 0);
188 strcpy (src, dst->path);
189 repositoryDeleteR (di, dst, base, src, rGroup);
194 if (src_len && src[src_len-1] == '/')
198 src[src_len+1] = '\0';
199 dst = dirs_read (di);
207 if (dst && !repComp (dst->path, src, src_len+1))
209 if (e_src[i_src].name)
211 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len+1,
213 sd = strcmp (dst->path + src_len+1, e_src[i_src].name);
218 else if (e_src[i_src].name)
222 logf (LOG_DEBUG, "trav sd=%d", sd);
225 strcpy (src + src_len+1, e_src[i_src].name);
226 sprintf (tmppath, "%s%s", base, src);
228 switch (e_src[i_src].kind)
231 if (e_src[i_src].ctime > dst->ctime)
233 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
235 dirs_add (di, src, dst->sysno, e_src[i_src].ctime);
238 dst = dirs_read (di);
241 repositoryUpdateR (di, dst, base, src, rGroup);
242 dst = dirs_last (di);
243 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
246 dst = dirs_read (di);
253 strcpy (src + src_len+1, e_src[i_src].name);
254 sprintf (tmppath, "%s%s", base, src);
256 switch (e_src[i_src].kind)
259 if (fileExtract (&sysno, tmppath, rGroup, 0))
260 dirs_add (di, src, sysno, e_src[i_src].ctime);
263 repositoryUpdateR (di, dst, base, src, rGroup);
265 dst = dirs_last (di);
272 strcpy (src, dst->path);
273 sprintf (tmppath, "%s%s", base, dst->path);
278 fileExtract (&dst->sysno, tmppath, rGroup, 1);
279 dirs_del (di, dst->path);
280 dst = dirs_read (di);
283 repositoryDeleteR (di, dst, base, src, rGroup);
284 dst = dirs_last (di);
291 static void groupRes (struct recordGroup *rGroup)
296 if (!rGroup->groupName || !*rGroup->groupName)
299 sprintf (gPrefix, "%s.", rGroup->groupName);
301 sprintf (resStr, "%srecordId", gPrefix);
302 rGroup->recordId = res_get (common_resource, resStr);
305 void repositoryUpdate (struct recordGroup *rGroup)
310 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
313 struct dirs_info *di;
315 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
317 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
320 assert (rGroup->path);
321 di = dirs_open (dict, rGroup->path);
323 repositoryUpdateR (di, dirs_read (di), rGroup->path, src, rGroup);
329 strcpy (src, rGroup->path);
330 if (*src == '\0' || !strcmp (src, "-"))
331 stdinExtractR (0, rGroup);
333 repositoryExtractR (0, src, rGroup);
337 void repositoryDelete (struct recordGroup *rGroup)
341 assert (rGroup->path);
343 strcpy (src, rGroup->path);
344 if (*src == '\0' || !strcmp(src, "-"))
345 stdinExtractR (1, rGroup);
347 repositoryExtractR (1, src, rGroup);