2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 1996-04-09 06:50:50 adam
8 * Bug fix: bad reference in function fileUpdateR.
10 * Revision 1.21 1996/03/22 15:34:18 quinn
13 * Revision 1.20 1996/03/21 14:50:10 adam
14 * File update uses modify-time instead of change-time.
16 * Revision 1.19 1996/03/20 16:16:55 quinn
17 * Added diagnostic output
19 * Revision 1.18 1996/03/19 12:43:27 adam
20 * Bug fix: File update traversal didn't handle trailing slashes correctly.
21 * Bug fix: Update of sub directory groups wasn't handled correctly.
23 * Revision 1.17 1996/02/12 18:45:17 adam
24 * Changed naming of some functions.
26 * Revision 1.16 1996/02/05 12:30:02 adam
27 * Logging reduced a bit.
28 * The remaining running time is estimated during register merge.
30 * Revision 1.15 1995/12/07 17:38:48 adam
31 * Work locking mechanisms for concurrent updates/commit.
33 * Revision 1.14 1995/12/06 12:41:26 adam
34 * New command 'stat' for the index program.
35 * Filenames can be read from stdin by specifying '-'.
36 * Bug fix/enhancement of the transformation from terms to regular
37 * expressons in the search engine.
39 * Revision 1.13 1995/11/28 09:09:46 adam
40 * Zebra config renamed.
41 * Use setting 'recordId' to identify record now.
42 * Bug fix in recindex.c: rec_release_blocks was invokeded even
43 * though the blocks were already released.
44 * File traversal properly deletes records when needed.
46 * Revision 1.12 1995/11/24 11:31:37 adam
47 * Commands add & del read filenames from stdin if source directory is
49 * Match criteria supports 'constant' strings.
51 * Revision 1.11 1995/11/22 17:19:19 adam
52 * Record management uses the bfile system.
54 * Revision 1.10 1995/11/21 15:01:16 adam
55 * New general match criteria implemented.
56 * New feature: document groups.
58 * Revision 1.9 1995/11/21 09:20:32 adam
59 * Yet more work on record match.
61 * Revision 1.8 1995/11/20 16:59:46 adam
62 * New update method: the 'old' keys are saved for each records.
64 * Revision 1.7 1995/11/20 11:56:28 adam
65 * Work on new traversal.
67 * Revision 1.6 1995/11/17 15:54:42 adam
68 * Started work on virtual directory structure.
70 * Revision 1.5 1995/10/17 18:02:09 adam
71 * New feature: databases. Implemented as prefix to words in dictionary.
73 * Revision 1.4 1995/09/28 09:19:46 adam
74 * xfree/xmalloc used everywhere.
75 * Extract/retrieve method seems to work for text records.
77 * Revision 1.3 1995/09/06 16:11:18 adam
78 * Option: only one word key per file.
80 * Revision 1.2 1995/09/04 12:33:43 adam
81 * Various cleanup. YAZ util used instead.
83 * Revision 1.1 1995/09/01 14:06:36 adam
84 * Split of work into more files.
92 #include <sys/types.h>
100 static int repComp (const char *a, const char *b, size_t len)
104 return memcmp (a, b, len);
107 static void repositoryExtractR (int deleteFlag, char *rep,
108 struct recordGroup *rGroup)
112 size_t rep_len = strlen (rep);
117 logf (LOG_LOG, "Dir: %s", rep);
118 if (rep[rep_len-1] != '/')
122 for (i=0; e[i].name; i++)
124 strcpy (rep +rep_len+1, e[i].name);
128 fileExtract (NULL, rep, rGroup, deleteFlag);
131 repositoryExtractR (deleteFlag, rep, rGroup);
139 static void stdinExtractR (int deleteFlag, struct recordGroup *rGroup)
143 logf (LOG_LOG, "stdinExtractR");
144 while (scanf ("%s", tmppath) == 1)
145 fileExtract (NULL, tmppath, rGroup, deleteFlag);
148 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
149 const char *base, char *src,
150 struct recordGroup *rGroup)
153 size_t src_len = strlen (src);
155 while (dst && !repComp (dst->path, src, src_len+1))
160 sprintf (tmppath, "%s%s", base, dst->path);
161 fileExtract (&dst->sysno, tmppath, rGroup, 1);
163 strcpy (tmppath, dst->path);
164 dst = dirs_read (di);
165 dirs_del (di, tmppath);
168 strcpy (tmppath, dst->path);
169 dst = dirs_read (di);
170 dirs_rmdir (di, tmppath);
173 dst = dirs_read (di);
178 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
179 const char *base, char *src,
180 struct recordGroup *rGroup)
182 struct dir_entry *e_src;
184 static char tmppath[1024];
185 size_t src_len = strlen (src);
187 sprintf (tmppath, "%s%s", base, src);
188 e_src = dir_open (tmppath);
189 logf (LOG_LOG, "Dir: %s", tmppath);
192 if (!dst || repComp (dst->path, src, src_len))
194 if (!dst || strcmp (dst->path, src))
200 if (src_len && src[src_len-1] != '/')
203 src[++src_len] = '\0';
205 dirs_mkdir (di, src, 0);
206 if (dst && repComp (dst->path, src, src_len))
211 strcpy (src, dst->path);
212 fileDeleteR (di, dst, base, src, rGroup);
217 if (src_len && src[src_len-1] != '/')
220 src[++src_len] = '\0';
222 dst = dirs_read (di);
230 if (dst && !repComp (dst->path, src, src_len))
232 if (e_src[i_src].name)
234 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
236 sd = strcmp (dst->path + src_len, e_src[i_src].name);
241 else if (e_src[i_src].name)
245 logf (LOG_DEBUG, "trav sd=%d", sd);
248 strcpy (src + src_len, e_src[i_src].name);
249 sprintf (tmppath, "%s%s", base, src);
251 switch (e_src[i_src].kind)
254 if (e_src[i_src].mtime > dst->mtime)
256 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
258 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
260 logf (LOG_LOG, "old: %s", ctime (&dst->mtime));
261 logf (LOG_LOG, "new: %s", ctime (&e_src[i_src].mtime));
263 dst = dirs_read (di);
266 fileUpdateR (di, dst, base, src, rGroup);
267 dst = dirs_last (di);
268 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
271 dst = dirs_read (di);
278 strcpy (src + src_len, e_src[i_src].name);
279 sprintf (tmppath, "%s%s", base, src);
281 switch (e_src[i_src].kind)
284 if (fileExtract (&sysno, tmppath, rGroup, 0))
285 dirs_add (di, src, sysno, e_src[i_src].mtime);
288 fileUpdateR (di, dst, base, src, rGroup);
290 dst = dirs_last (di);
297 strcpy (src, dst->path);
298 sprintf (tmppath, "%s%s", base, dst->path);
303 fileExtract (&dst->sysno, tmppath, rGroup, 1);
304 dirs_del (di, dst->path);
305 dst = dirs_read (di);
308 fileDeleteR (di, dst, base, src, rGroup);
309 dst = dirs_last (di);
316 static void groupRes (struct recordGroup *rGroup)
321 if (!rGroup->groupName || !*rGroup->groupName)
324 sprintf (gPrefix, "%s.", rGroup->groupName);
326 sprintf (resStr, "%srecordId", gPrefix);
327 rGroup->recordId = res_get (common_resource, resStr);
331 void repositoryShow (struct recordGroup *rGroup)
335 struct dirs_entry *dst;
337 struct dirs_info *di;
339 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
341 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
345 assert (rGroup->path);
346 strcpy (src, rGroup->path);
347 src_len = strlen (src);
349 if (src_len && src[src_len-1] != '/')
352 src[++src_len] = '\0';
355 di = dirs_open (dict, src);
357 while ( (dst = dirs_read (di)) )
358 logf (LOG_LOG, "%s", dst->path);
363 void repositoryUpdate (struct recordGroup *rGroup)
370 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
373 struct dirs_info *di;
375 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
377 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
380 assert (rGroup->path);
382 strcpy (src, rGroup->path);
383 src_len = strlen (src);
385 if (src_len && src[src_len-1] != '/')
388 src[++src_len] = '\0';
391 di = dirs_open (dict, src);
394 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
400 strcpy (src, rGroup->path);
401 if (*src == '\0' || !strcmp (src, "-"))
402 stdinExtractR (0, rGroup);
404 repositoryExtractR (0, src, rGroup);
408 void repositoryDelete (struct recordGroup *rGroup)
412 assert (rGroup->path);
414 strcpy (src, rGroup->path);
415 if (*src == '\0' || !strcmp(src, "-"))
416 stdinExtractR (1, rGroup);
418 repositoryExtractR (1, src, rGroup);