2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.26 1996-06-04 10:19:01 adam
8 * Minor changes - removed include of ctype.h.
10 * Revision 1.25 1996/05/01 13:46:37 adam
11 * First work on multiple records in one file.
12 * New option, -offset, to the "unread" command in the filter module.
14 * Revision 1.24 1996/04/26 10:00:23 adam
15 * Added option -V to zebraidx to display version information.
16 * Removed stupid warnings from file update.
18 * Revision 1.23 1996/04/12 07:02:25 adam
19 * File update of single files.
21 * Revision 1.22 1996/04/09 06:50:50 adam
22 * Bug fix: bad reference in function fileUpdateR.
24 * Revision 1.21 1996/03/22 15:34:18 quinn
27 * Revision 1.20 1996/03/21 14:50:10 adam
28 * File update uses modify-time instead of change-time.
30 * Revision 1.19 1996/03/20 16:16:55 quinn
31 * Added diagnostic output
33 * Revision 1.18 1996/03/19 12:43:27 adam
34 * Bug fix: File update traversal didn't handle trailing slashes correctly.
35 * Bug fix: Update of sub directory groups wasn't handled correctly.
37 * Revision 1.17 1996/02/12 18:45:17 adam
38 * Changed naming of some functions.
40 * Revision 1.16 1996/02/05 12:30:02 adam
41 * Logging reduced a bit.
42 * The remaining running time is estimated during register merge.
44 * Revision 1.15 1995/12/07 17:38:48 adam
45 * Work locking mechanisms for concurrent updates/commit.
47 * Revision 1.14 1995/12/06 12:41:26 adam
48 * New command 'stat' for the index program.
49 * Filenames can be read from stdin by specifying '-'.
50 * Bug fix/enhancement of the transformation from terms to regular
51 * expressons in the search engine.
53 * Revision 1.13 1995/11/28 09:09:46 adam
54 * Zebra config renamed.
55 * Use setting 'recordId' to identify record now.
56 * Bug fix in recindex.c: rec_release_blocks was invokeded even
57 * though the blocks were already released.
58 * File traversal properly deletes records when needed.
60 * Revision 1.12 1995/11/24 11:31:37 adam
61 * Commands add & del read filenames from stdin if source directory is
63 * Match criteria supports 'constant' strings.
65 * Revision 1.11 1995/11/22 17:19:19 adam
66 * Record management uses the bfile system.
68 * Revision 1.10 1995/11/21 15:01:16 adam
69 * New general match criteria implemented.
70 * New feature: document groups.
72 * Revision 1.9 1995/11/21 09:20:32 adam
73 * Yet more work on record match.
75 * Revision 1.8 1995/11/20 16:59:46 adam
76 * New update method: the 'old' keys are saved for each records.
78 * Revision 1.7 1995/11/20 11:56:28 adam
79 * Work on new traversal.
81 * Revision 1.6 1995/11/17 15:54:42 adam
82 * Started work on virtual directory structure.
84 * Revision 1.5 1995/10/17 18:02:09 adam
85 * New feature: databases. Implemented as prefix to words in dictionary.
87 * Revision 1.4 1995/09/28 09:19:46 adam
88 * xfree/xmalloc used everywhere.
89 * Extract/retrieve method seems to work for text records.
91 * Revision 1.3 1995/09/06 16:11:18 adam
92 * Option: only one word key per file.
94 * Revision 1.2 1995/09/04 12:33:43 adam
95 * Various cleanup. YAZ util used instead.
97 * Revision 1.1 1995/09/01 14:06:36 adam
98 * Split of work into more files.
105 #include <sys/stat.h>
106 #include <sys/types.h>
110 #include <alexutil.h>
113 static int repComp (const char *a, const char *b, size_t len)
117 return memcmp (a, b, len);
120 static void repositoryExtractR (int deleteFlag, char *rep,
121 struct recordGroup *rGroup)
125 size_t rep_len = strlen (rep);
130 logf (LOG_LOG, "Dir: %s", rep);
131 if (rep[rep_len-1] != '/')
135 for (i=0; e[i].name; i++)
137 strcpy (rep +rep_len+1, e[i].name);
141 fileExtract (NULL, rep, rGroup, deleteFlag);
144 repositoryExtractR (deleteFlag, rep, rGroup);
152 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
153 const char *base, char *src,
154 struct recordGroup *rGroup)
157 size_t src_len = strlen (src);
159 while (dst && !repComp (dst->path, src, src_len+1))
164 sprintf (tmppath, "%s%s", base, dst->path);
165 fileExtract (&dst->sysno, tmppath, rGroup, 1);
167 strcpy (tmppath, dst->path);
168 dst = dirs_read (di);
169 dirs_del (di, tmppath);
172 strcpy (tmppath, dst->path);
173 dst = dirs_read (di);
174 dirs_rmdir (di, tmppath);
177 dst = dirs_read (di);
182 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
183 const char *base, char *src,
184 struct recordGroup *rGroup)
186 struct dir_entry *e_src;
188 static char tmppath[1024];
189 size_t src_len = strlen (src);
191 sprintf (tmppath, "%s%s", base, src);
192 e_src = dir_open (tmppath);
193 logf (LOG_LOG, "Dir: %s", tmppath);
196 if (!dst || repComp (dst->path, src, src_len))
198 if (!dst || strcmp (dst->path, src))
204 if (src_len && src[src_len-1] != '/')
207 src[++src_len] = '\0';
209 dirs_mkdir (di, src, 0);
210 if (dst && repComp (dst->path, src, src_len))
215 strcpy (src, dst->path);
216 fileDeleteR (di, dst, base, src, rGroup);
221 if (src_len && src[src_len-1] != '/')
224 src[++src_len] = '\0';
226 dst = dirs_read (di);
234 if (dst && !repComp (dst->path, src, src_len))
236 if (e_src[i_src].name)
238 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
240 sd = strcmp (dst->path + src_len, e_src[i_src].name);
245 else if (e_src[i_src].name)
249 logf (LOG_DEBUG, "trav sd=%d", sd);
252 strcpy (src + src_len, e_src[i_src].name);
253 sprintf (tmppath, "%s%s", base, src);
255 switch (e_src[i_src].kind)
258 if (e_src[i_src].mtime > dst->mtime)
260 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
262 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
264 logf (LOG_LOG, "old: %s", ctime (&dst->mtime));
265 logf (LOG_LOG, "new: %s", ctime (&e_src[i_src].mtime));
267 dst = dirs_read (di);
270 fileUpdateR (di, dst, base, src, rGroup);
271 dst = dirs_last (di);
272 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
275 dst = dirs_read (di);
282 strcpy (src + src_len, e_src[i_src].name);
283 sprintf (tmppath, "%s%s", base, src);
285 switch (e_src[i_src].kind)
288 if (fileExtract (&sysno, tmppath, rGroup, 0))
289 dirs_add (di, src, sysno, e_src[i_src].mtime);
292 fileUpdateR (di, dst, base, src, rGroup);
294 dst = dirs_last (di);
301 strcpy (src, dst->path);
302 sprintf (tmppath, "%s%s", base, dst->path);
307 fileExtract (&dst->sysno, tmppath, rGroup, 1);
308 dirs_del (di, dst->path);
309 dst = dirs_read (di);
312 fileDeleteR (di, dst, base, src, rGroup);
313 dst = dirs_last (di);
320 static void groupRes (struct recordGroup *rGroup)
325 if (!rGroup->groupName || !*rGroup->groupName)
328 sprintf (gPrefix, "%s.", rGroup->groupName);
330 sprintf (resStr, "%srecordId", gPrefix);
331 rGroup->recordId = res_get (common_resource, resStr);
334 void repositoryShow (struct recordGroup *rGroup)
338 struct dirs_entry *dst;
340 struct dirs_info *di;
342 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
344 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
348 assert (rGroup->path);
349 strcpy (src, rGroup->path);
350 src_len = strlen (src);
352 if (src_len && src[src_len-1] != '/')
355 src[++src_len] = '\0';
358 di = dirs_open (dict, src);
360 while ( (dst = dirs_read (di)) )
361 logf (LOG_LOG, "%s", dst->path);
366 static void fileUpdate (Dict dict, struct recordGroup *rGroup,
369 struct dirs_info *di;
377 src_len = strlen (src);
380 if (S_ISREG(sbuf.st_mode))
382 struct dirs_entry *e_dst;
383 di = dirs_fopen (dict, src);
385 e_dst = dirs_read (di);
388 if (sbuf.st_mtime > e_dst->mtime)
389 if (fileExtract (&e_dst->sysno, src, rGroup, 0))
390 dirs_add (di, src, e_dst->sysno, sbuf.st_mtime);
395 if (fileExtract (&sysno, src, rGroup, 0))
396 dirs_add (di, src, sysno, sbuf.st_mtime);
400 else if (S_ISDIR(sbuf.st_mode))
402 if (src_len && src[src_len-1] != '/')
405 src[++src_len] = '\0';
407 di = dirs_open (dict, src);
409 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
414 logf (LOG_WARN, "Cannot handle file %s", src);
419 static void repositoryExtract (int deleteFlag, struct recordGroup *rGroup,
429 if (S_ISREG(sbuf.st_mode))
430 fileExtract (NULL, src, rGroup, deleteFlag);
431 else if (S_ISDIR(sbuf.st_mode))
432 repositoryExtractR (deleteFlag, src, rGroup);
434 logf (LOG_WARN, "Cannot handle file %s", src);
437 static void repositoryExtractG (int deleteFlag, struct recordGroup *rGroup)
439 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
443 while (scanf ("%s", src) == 1)
444 repositoryExtract (deleteFlag, rGroup, src);
447 repositoryExtract (deleteFlag, rGroup, rGroup->path);
450 void repositoryUpdate (struct recordGroup *rGroup)
453 assert (rGroup->path);
454 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
457 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
459 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
462 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
465 while (scanf ("%s", src) == 1)
466 fileUpdate (dict, rGroup, src);
469 fileUpdate (dict, rGroup, rGroup->path);
473 repositoryExtractG (0, rGroup);
476 void repositoryDelete (struct recordGroup *rGroup)
478 repositoryExtractG (1, rGroup);