2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.25 1996-05-01 13:46:37 adam
8 * First work on multiple records in one file.
9 * New option, -offset, to the "unread" command in the filter module.
11 * Revision 1.24 1996/04/26 10:00:23 adam
12 * Added option -V to zebraidx to display version information.
13 * Removed stupid warnings from file update.
15 * Revision 1.23 1996/04/12 07:02:25 adam
16 * File update of single files.
18 * Revision 1.22 1996/04/09 06:50:50 adam
19 * Bug fix: bad reference in function fileUpdateR.
21 * Revision 1.21 1996/03/22 15:34:18 quinn
24 * Revision 1.20 1996/03/21 14:50:10 adam
25 * File update uses modify-time instead of change-time.
27 * Revision 1.19 1996/03/20 16:16:55 quinn
28 * Added diagnostic output
30 * Revision 1.18 1996/03/19 12:43:27 adam
31 * Bug fix: File update traversal didn't handle trailing slashes correctly.
32 * Bug fix: Update of sub directory groups wasn't handled correctly.
34 * Revision 1.17 1996/02/12 18:45:17 adam
35 * Changed naming of some functions.
37 * Revision 1.16 1996/02/05 12:30:02 adam
38 * Logging reduced a bit.
39 * The remaining running time is estimated during register merge.
41 * Revision 1.15 1995/12/07 17:38:48 adam
42 * Work locking mechanisms for concurrent updates/commit.
44 * Revision 1.14 1995/12/06 12:41:26 adam
45 * New command 'stat' for the index program.
46 * Filenames can be read from stdin by specifying '-'.
47 * Bug fix/enhancement of the transformation from terms to regular
48 * expressons in the search engine.
50 * Revision 1.13 1995/11/28 09:09:46 adam
51 * Zebra config renamed.
52 * Use setting 'recordId' to identify record now.
53 * Bug fix in recindex.c: rec_release_blocks was invokeded even
54 * though the blocks were already released.
55 * File traversal properly deletes records when needed.
57 * Revision 1.12 1995/11/24 11:31:37 adam
58 * Commands add & del read filenames from stdin if source directory is
60 * Match criteria supports 'constant' strings.
62 * Revision 1.11 1995/11/22 17:19:19 adam
63 * Record management uses the bfile system.
65 * Revision 1.10 1995/11/21 15:01:16 adam
66 * New general match criteria implemented.
67 * New feature: document groups.
69 * Revision 1.9 1995/11/21 09:20:32 adam
70 * Yet more work on record match.
72 * Revision 1.8 1995/11/20 16:59:46 adam
73 * New update method: the 'old' keys are saved for each records.
75 * Revision 1.7 1995/11/20 11:56:28 adam
76 * Work on new traversal.
78 * Revision 1.6 1995/11/17 15:54:42 adam
79 * Started work on virtual directory structure.
81 * Revision 1.5 1995/10/17 18:02:09 adam
82 * New feature: databases. Implemented as prefix to words in dictionary.
84 * Revision 1.4 1995/09/28 09:19:46 adam
85 * xfree/xmalloc used everywhere.
86 * Extract/retrieve method seems to work for text records.
88 * Revision 1.3 1995/09/06 16:11:18 adam
89 * Option: only one word key per file.
91 * Revision 1.2 1995/09/04 12:33:43 adam
92 * Various cleanup. YAZ util used instead.
94 * Revision 1.1 1995/09/01 14:06:36 adam
95 * Split of work into more files.
102 #include <sys/stat.h>
103 #include <sys/types.h>
108 #include <alexutil.h>
111 static int repComp (const char *a, const char *b, size_t len)
115 return memcmp (a, b, len);
118 static void repositoryExtractR (int deleteFlag, char *rep,
119 struct recordGroup *rGroup)
123 size_t rep_len = strlen (rep);
128 logf (LOG_LOG, "Dir: %s", rep);
129 if (rep[rep_len-1] != '/')
133 for (i=0; e[i].name; i++)
135 strcpy (rep +rep_len+1, e[i].name);
139 fileExtract (NULL, rep, rGroup, deleteFlag);
142 repositoryExtractR (deleteFlag, rep, rGroup);
150 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
151 const char *base, char *src,
152 struct recordGroup *rGroup)
155 size_t src_len = strlen (src);
157 while (dst && !repComp (dst->path, src, src_len+1))
162 sprintf (tmppath, "%s%s", base, dst->path);
163 fileExtract (&dst->sysno, tmppath, rGroup, 1);
165 strcpy (tmppath, dst->path);
166 dst = dirs_read (di);
167 dirs_del (di, tmppath);
170 strcpy (tmppath, dst->path);
171 dst = dirs_read (di);
172 dirs_rmdir (di, tmppath);
175 dst = dirs_read (di);
180 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
181 const char *base, char *src,
182 struct recordGroup *rGroup)
184 struct dir_entry *e_src;
186 static char tmppath[1024];
187 size_t src_len = strlen (src);
189 sprintf (tmppath, "%s%s", base, src);
190 e_src = dir_open (tmppath);
191 logf (LOG_LOG, "Dir: %s", tmppath);
194 if (!dst || repComp (dst->path, src, src_len))
196 if (!dst || strcmp (dst->path, src))
202 if (src_len && src[src_len-1] != '/')
205 src[++src_len] = '\0';
207 dirs_mkdir (di, src, 0);
208 if (dst && repComp (dst->path, src, src_len))
213 strcpy (src, dst->path);
214 fileDeleteR (di, dst, base, src, rGroup);
219 if (src_len && src[src_len-1] != '/')
222 src[++src_len] = '\0';
224 dst = dirs_read (di);
232 if (dst && !repComp (dst->path, src, src_len))
234 if (e_src[i_src].name)
236 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
238 sd = strcmp (dst->path + src_len, e_src[i_src].name);
243 else if (e_src[i_src].name)
247 logf (LOG_DEBUG, "trav sd=%d", sd);
250 strcpy (src + src_len, e_src[i_src].name);
251 sprintf (tmppath, "%s%s", base, src);
253 switch (e_src[i_src].kind)
256 if (e_src[i_src].mtime > dst->mtime)
258 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
260 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
262 logf (LOG_LOG, "old: %s", ctime (&dst->mtime));
263 logf (LOG_LOG, "new: %s", ctime (&e_src[i_src].mtime));
265 dst = dirs_read (di);
268 fileUpdateR (di, dst, base, src, rGroup);
269 dst = dirs_last (di);
270 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
273 dst = dirs_read (di);
280 strcpy (src + src_len, e_src[i_src].name);
281 sprintf (tmppath, "%s%s", base, src);
283 switch (e_src[i_src].kind)
286 if (fileExtract (&sysno, tmppath, rGroup, 0))
287 dirs_add (di, src, sysno, e_src[i_src].mtime);
290 fileUpdateR (di, dst, base, src, rGroup);
292 dst = dirs_last (di);
299 strcpy (src, dst->path);
300 sprintf (tmppath, "%s%s", base, dst->path);
305 fileExtract (&dst->sysno, tmppath, rGroup, 1);
306 dirs_del (di, dst->path);
307 dst = dirs_read (di);
310 fileDeleteR (di, dst, base, src, rGroup);
311 dst = dirs_last (di);
318 static void groupRes (struct recordGroup *rGroup)
323 if (!rGroup->groupName || !*rGroup->groupName)
326 sprintf (gPrefix, "%s.", rGroup->groupName);
328 sprintf (resStr, "%srecordId", gPrefix);
329 rGroup->recordId = res_get (common_resource, resStr);
332 void repositoryShow (struct recordGroup *rGroup)
336 struct dirs_entry *dst;
338 struct dirs_info *di;
340 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
342 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
346 assert (rGroup->path);
347 strcpy (src, rGroup->path);
348 src_len = strlen (src);
350 if (src_len && src[src_len-1] != '/')
353 src[++src_len] = '\0';
356 di = dirs_open (dict, src);
358 while ( (dst = dirs_read (di)) )
359 logf (LOG_LOG, "%s", dst->path);
364 static void fileUpdate (Dict dict, struct recordGroup *rGroup,
367 struct dirs_info *di;
375 src_len = strlen (src);
378 if (S_ISREG(sbuf.st_mode))
380 struct dirs_entry *e_dst;
381 di = dirs_fopen (dict, src);
383 e_dst = dirs_read (di);
386 if (sbuf.st_mtime > e_dst->mtime)
387 if (fileExtract (&e_dst->sysno, src, rGroup, 0))
388 dirs_add (di, src, e_dst->sysno, sbuf.st_mtime);
393 if (fileExtract (&sysno, src, rGroup, 0))
394 dirs_add (di, src, sysno, sbuf.st_mtime);
398 else if (S_ISDIR(sbuf.st_mode))
400 if (src_len && src[src_len-1] != '/')
403 src[++src_len] = '\0';
405 di = dirs_open (dict, src);
407 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
412 logf (LOG_WARN, "Cannot handle file %s", src);
417 static void repositoryExtract (int deleteFlag, struct recordGroup *rGroup,
427 if (S_ISREG(sbuf.st_mode))
428 fileExtract (NULL, src, rGroup, deleteFlag);
429 else if (S_ISDIR(sbuf.st_mode))
430 repositoryExtractR (deleteFlag, src, rGroup);
432 logf (LOG_WARN, "Cannot handle file %s", src);
435 static void repositoryExtractG (int deleteFlag, struct recordGroup *rGroup)
437 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
441 while (scanf ("%s", src) == 1)
442 repositoryExtract (deleteFlag, rGroup, src);
445 repositoryExtract (deleteFlag, rGroup, rGroup->path);
448 void repositoryUpdate (struct recordGroup *rGroup)
451 assert (rGroup->path);
452 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
455 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
457 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
460 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
463 while (scanf ("%s", src) == 1)
464 fileUpdate (dict, rGroup, src);
467 fileUpdate (dict, rGroup, rGroup->path);
471 repositoryExtractG (0, rGroup);
474 void repositoryDelete (struct recordGroup *rGroup)
476 repositoryExtractG (1, rGroup);