2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1996-02-12 18:45:17 adam
8 * Changed naming of some functions.
10 * Revision 1.16 1996/02/05 12:30:02 adam
11 * Logging reduced a bit.
12 * The remaining running time is estimated during register merge.
14 * Revision 1.15 1995/12/07 17:38:48 adam
15 * Work locking mechanisms for concurrent updates/commit.
17 * Revision 1.14 1995/12/06 12:41:26 adam
18 * New command 'stat' for the index program.
19 * Filenames can be read from stdin by specifying '-'.
20 * Bug fix/enhancement of the transformation from terms to regular
21 * expressons in the search engine.
23 * Revision 1.13 1995/11/28 09:09:46 adam
24 * Zebra config renamed.
25 * Use setting 'recordId' to identify record now.
26 * Bug fix in recindex.c: rec_release_blocks was invokeded even
27 * though the blocks were already released.
28 * File traversal properly deletes records when needed.
30 * Revision 1.12 1995/11/24 11:31:37 adam
31 * Commands add & del read filenames from stdin if source directory is
33 * Match criteria supports 'constant' strings.
35 * Revision 1.11 1995/11/22 17:19:19 adam
36 * Record management uses the bfile system.
38 * Revision 1.10 1995/11/21 15:01:16 adam
39 * New general match criteria implemented.
40 * New feature: document groups.
42 * Revision 1.9 1995/11/21 09:20:32 adam
43 * Yet more work on record match.
45 * Revision 1.8 1995/11/20 16:59:46 adam
46 * New update method: the 'old' keys are saved for each records.
48 * Revision 1.7 1995/11/20 11:56:28 adam
49 * Work on new traversal.
51 * Revision 1.6 1995/11/17 15:54:42 adam
52 * Started work on virtual directory structure.
54 * Revision 1.5 1995/10/17 18:02:09 adam
55 * New feature: databases. Implemented as prefix to words in dictionary.
57 * Revision 1.4 1995/09/28 09:19:46 adam
58 * xfree/xmalloc used everywhere.
59 * Extract/retrieve method seems to work for text records.
61 * Revision 1.3 1995/09/06 16:11:18 adam
62 * Option: only one word key per file.
64 * Revision 1.2 1995/09/04 12:33:43 adam
65 * Various cleanup. YAZ util used instead.
67 * Revision 1.1 1995/09/01 14:06:36 adam
68 * Split of work into more files.
76 #include <sys/types.h>
83 static int repComp (const char *a, const char *b, size_t len)
87 return memcmp (a, b, len);
90 static void repositoryExtractR (int deleteFlag, char *rep,
91 struct recordGroup *rGroup)
95 size_t rep_len = strlen (rep);
100 logf (LOG_LOG, "Dir: %s", rep);
101 if (rep[rep_len-1] != '/')
105 for (i=0; e[i].name; i++)
107 strcpy (rep +rep_len+1, e[i].name);
111 fileExtract (NULL, rep, rGroup, deleteFlag);
114 repositoryExtractR (deleteFlag, rep, rGroup);
122 static void stdinExtractR (int deleteFlag, struct recordGroup *rGroup)
126 logf (LOG_LOG, "stdinExtractR");
127 while (scanf ("%s", tmppath) == 1)
128 fileExtract (NULL, tmppath, rGroup, deleteFlag);
131 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
132 const char *base, char *src,
133 struct recordGroup *rGroup)
136 size_t src_len = strlen (src);
138 while (dst && !repComp (dst->path, src, src_len+1))
143 sprintf (tmppath, "%s%s", base, dst->path);
144 fileExtract (&dst->sysno, tmppath, rGroup, 1);
146 strcpy (tmppath, dst->path);
147 dst = dirs_read (di);
148 dirs_del (di, tmppath);
151 strcpy (tmppath, dst->path);
152 dst = dirs_read (di);
153 dirs_rmdir (di, tmppath);
156 dst = dirs_read (di);
161 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
162 const char *base, char *src,
163 struct recordGroup *rGroup)
165 struct dir_entry *e_src;
167 static char tmppath[1024];
168 size_t src_len = strlen (src);
170 sprintf (tmppath, "%s%s", base, src);
171 e_src = dir_open (tmppath);
172 logf (LOG_LOG, "Dir: %s", tmppath);
174 if (!dst || repComp (dst->path, src, src_len))
176 if (!dst || strcmp (dst->path, src))
181 if (src_len && src[src_len-1] == '/')
185 src[src_len+1] = '\0';
186 dirs_mkdir (di, src, 0);
191 strcpy (src, dst->path);
192 fileDeleteR (di, dst, base, src, rGroup);
197 if (src_len && src[src_len-1] == '/')
201 src[src_len+1] = '\0';
202 dst = dirs_read (di);
210 if (dst && !repComp (dst->path, src, src_len+1))
212 if (e_src[i_src].name)
214 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len+1,
216 sd = strcmp (dst->path + src_len+1, e_src[i_src].name);
221 else if (e_src[i_src].name)
225 logf (LOG_DEBUG, "trav sd=%d", sd);
228 strcpy (src + src_len+1, e_src[i_src].name);
229 sprintf (tmppath, "%s%s", base, src);
231 switch (e_src[i_src].kind)
234 if (e_src[i_src].ctime > dst->ctime)
236 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
238 dirs_add (di, src, dst->sysno, e_src[i_src].ctime);
241 dst = dirs_read (di);
244 fileUpdateR (di, dst, base, src, rGroup);
245 dst = dirs_last (di);
246 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
249 dst = dirs_read (di);
256 strcpy (src + src_len+1, e_src[i_src].name);
257 sprintf (tmppath, "%s%s", base, src);
259 switch (e_src[i_src].kind)
262 if (fileExtract (&sysno, tmppath, rGroup, 0))
263 dirs_add (di, src, sysno, e_src[i_src].ctime);
266 fileUpdateR (di, dst, base, src, rGroup);
268 dst = dirs_last (di);
275 strcpy (src, dst->path);
276 sprintf (tmppath, "%s%s", base, dst->path);
281 fileExtract (&dst->sysno, tmppath, rGroup, 1);
282 dirs_del (di, dst->path);
283 dst = dirs_read (di);
286 fileDeleteR (di, dst, base, src, rGroup);
287 dst = dirs_last (di);
294 static void groupRes (struct recordGroup *rGroup)
299 if (!rGroup->groupName || !*rGroup->groupName)
302 sprintf (gPrefix, "%s.", rGroup->groupName);
304 sprintf (resStr, "%srecordId", gPrefix);
305 rGroup->recordId = res_get (common_resource, resStr);
308 void repositoryUpdate (struct recordGroup *rGroup)
313 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
316 struct dirs_info *di;
318 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
320 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
323 assert (rGroup->path);
324 di = dirs_open (dict, rGroup->path);
326 fileUpdateR (di, dirs_read (di), rGroup->path, src, rGroup);
332 strcpy (src, rGroup->path);
333 if (*src == '\0' || !strcmp (src, "-"))
334 stdinExtractR (0, rGroup);
336 repositoryExtractR (0, src, rGroup);
340 void repositoryDelete (struct recordGroup *rGroup)
344 assert (rGroup->path);
346 strcpy (src, rGroup->path);
347 if (*src == '\0' || !strcmp(src, "-"))
348 stdinExtractR (1, rGroup);
350 repositoryExtractR (1, src, rGroup);