2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.30 1997-09-09 13:38:09 adam
8 * Partial port to WIN95/NT.
10 * Revision 1.29 1997/02/12 20:39:47 adam
11 * Implemented options -f <n> that limits the log to the first <n>
13 * Changed some log messages also.
15 * Revision 1.28 1996/11/01 08:58:44 adam
16 * Interface to isamc system now includes update and delete.
18 * Revision 1.27 1996/10/29 14:06:56 adam
19 * Include zebrautl.h instead of alexutil.h.
21 * Revision 1.26 1996/06/04 10:19:01 adam
22 * Minor changes - removed include of ctype.h.
24 * Revision 1.25 1996/05/01 13:46:37 adam
25 * First work on multiple records in one file.
26 * New option, -offset, to the "unread" command in the filter module.
28 * Revision 1.24 1996/04/26 10:00:23 adam
29 * Added option -V to zebraidx to display version information.
30 * Removed stupid warnings from file update.
32 * Revision 1.23 1996/04/12 07:02:25 adam
33 * File update of single files.
35 * Revision 1.22 1996/04/09 06:50:50 adam
36 * Bug fix: bad reference in function fileUpdateR.
38 * Revision 1.21 1996/03/22 15:34:18 quinn
41 * Revision 1.20 1996/03/21 14:50:10 adam
42 * File update uses modify-time instead of change-time.
44 * Revision 1.19 1996/03/20 16:16:55 quinn
45 * Added diagnostic output
47 * Revision 1.18 1996/03/19 12:43:27 adam
48 * Bug fix: File update traversal didn't handle trailing slashes correctly.
49 * Bug fix: Update of sub directory groups wasn't handled correctly.
51 * Revision 1.17 1996/02/12 18:45:17 adam
52 * Changed naming of some functions.
54 * Revision 1.16 1996/02/05 12:30:02 adam
55 * Logging reduced a bit.
56 * The remaining running time is estimated during register merge.
58 * Revision 1.15 1995/12/07 17:38:48 adam
59 * Work locking mechanisms for concurrent updates/commit.
61 * Revision 1.14 1995/12/06 12:41:26 adam
62 * New command 'stat' for the index program.
63 * Filenames can be read from stdin by specifying '-'.
64 * Bug fix/enhancement of the transformation from terms to regular
65 * expressons in the search engine.
67 * Revision 1.13 1995/11/28 09:09:46 adam
68 * Zebra config renamed.
69 * Use setting 'recordId' to identify record now.
70 * Bug fix in recindex.c: rec_release_blocks was invokeded even
71 * though the blocks were already released.
72 * File traversal properly deletes records when needed.
74 * Revision 1.12 1995/11/24 11:31:37 adam
75 * Commands add & del read filenames from stdin if source directory is
77 * Match criteria supports 'constant' strings.
79 * Revision 1.11 1995/11/22 17:19:19 adam
80 * Record management uses the bfile system.
82 * Revision 1.10 1995/11/21 15:01:16 adam
83 * New general match criteria implemented.
84 * New feature: document groups.
86 * Revision 1.9 1995/11/21 09:20:32 adam
87 * Yet more work on record match.
89 * Revision 1.8 1995/11/20 16:59:46 adam
90 * New update method: the 'old' keys are saved for each records.
92 * Revision 1.7 1995/11/20 11:56:28 adam
93 * Work on new traversal.
95 * Revision 1.6 1995/11/17 15:54:42 adam
96 * Started work on virtual directory structure.
98 * Revision 1.5 1995/10/17 18:02:09 adam
99 * New feature: databases. Implemented as prefix to words in dictionary.
101 * Revision 1.4 1995/09/28 09:19:46 adam
102 * xfree/xmalloc used everywhere.
103 * Extract/retrieve method seems to work for text records.
105 * Revision 1.3 1995/09/06 16:11:18 adam
106 * Option: only one word key per file.
108 * Revision 1.2 1995/09/04 12:33:43 adam
109 * Various cleanup. YAZ util used instead.
111 * Revision 1.1 1995/09/01 14:06:36 adam
112 * Split of work into more files.
123 #include <sys/types.h>
124 #include <sys/stat.h>
130 static int repComp (const char *a, const char *b, size_t len)
134 return memcmp (a, b, len);
137 static void repositoryExtractR (int deleteFlag, char *rep,
138 struct recordGroup *rGroup)
142 size_t rep_len = strlen (rep);
147 logf (LOG_LOG, "dir %s", rep);
148 if (rep[rep_len-1] != '/')
152 for (i=0; e[i].name; i++)
154 strcpy (rep +rep_len+1, e[i].name);
158 fileExtract (NULL, rep, rGroup, deleteFlag);
161 repositoryExtractR (deleteFlag, rep, rGroup);
169 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
170 const char *base, char *src,
171 struct recordGroup *rGroup)
174 size_t src_len = strlen (src);
176 while (dst && !repComp (dst->path, src, src_len+1))
181 sprintf (tmppath, "%s%s", base, dst->path);
182 fileExtract (&dst->sysno, tmppath, rGroup, 1);
184 strcpy (tmppath, dst->path);
185 dst = dirs_read (di);
186 dirs_del (di, tmppath);
189 strcpy (tmppath, dst->path);
190 dst = dirs_read (di);
191 dirs_rmdir (di, tmppath);
194 dst = dirs_read (di);
199 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
200 const char *base, char *src,
201 struct recordGroup *rGroup)
203 struct dir_entry *e_src;
205 static char tmppath[1024];
206 size_t src_len = strlen (src);
208 sprintf (tmppath, "%s%s", base, src);
209 e_src = dir_open (tmppath);
210 logf (LOG_LOG, "dir %s", tmppath);
213 if (!dst || repComp (dst->path, src, src_len))
215 if (!dst || strcmp (dst->path, src))
221 if (src_len && src[src_len-1] != '/')
224 src[++src_len] = '\0';
226 dirs_mkdir (di, src, 0);
227 if (dst && repComp (dst->path, src, src_len))
232 strcpy (src, dst->path);
233 fileDeleteR (di, dst, base, src, rGroup);
238 if (src_len && src[src_len-1] != '/')
241 src[++src_len] = '\0';
243 dst = dirs_read (di);
251 if (dst && !repComp (dst->path, src, src_len))
253 if (e_src[i_src].name)
255 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
257 sd = strcmp (dst->path + src_len, e_src[i_src].name);
262 else if (e_src[i_src].name)
266 logf (LOG_DEBUG, "trav sd=%d", sd);
269 strcpy (src + src_len, e_src[i_src].name);
270 sprintf (tmppath, "%s%s", base, src);
272 switch (e_src[i_src].kind)
275 if (e_src[i_src].mtime > dst->mtime)
277 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
279 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
281 logf (LOG_DEBUG, "old: %s", ctime (&dst->mtime));
282 logf (LOG_DEBUG, "new: %s", ctime (&e_src[i_src].mtime));
284 dst = dirs_read (di);
287 fileUpdateR (di, dst, base, src, rGroup);
288 dst = dirs_last (di);
289 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
292 dst = dirs_read (di);
299 strcpy (src + src_len, e_src[i_src].name);
300 sprintf (tmppath, "%s%s", base, src);
302 switch (e_src[i_src].kind)
305 if (fileExtract (&sysno, tmppath, rGroup, 0))
306 dirs_add (di, src, sysno, e_src[i_src].mtime);
309 fileUpdateR (di, dst, base, src, rGroup);
311 dst = dirs_last (di);
318 strcpy (src, dst->path);
319 sprintf (tmppath, "%s%s", base, dst->path);
324 fileExtract (&dst->sysno, tmppath, rGroup, 1);
325 dirs_del (di, dst->path);
326 dst = dirs_read (di);
329 fileDeleteR (di, dst, base, src, rGroup);
330 dst = dirs_last (di);
337 static void groupRes (struct recordGroup *rGroup)
342 if (!rGroup->groupName || !*rGroup->groupName)
345 sprintf (gPrefix, "%s.", rGroup->groupName);
347 sprintf (resStr, "%srecordId", gPrefix);
348 rGroup->recordId = res_get (common_resource, resStr);
351 void repositoryShow (struct recordGroup *rGroup)
355 struct dirs_entry *dst;
357 struct dirs_info *di;
359 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
361 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
365 assert (rGroup->path);
366 strcpy (src, rGroup->path);
367 src_len = strlen (src);
369 if (src_len && src[src_len-1] != '/')
372 src[++src_len] = '\0';
375 di = dirs_open (dict, src);
377 while ( (dst = dirs_read (di)) )
378 logf (LOG_LOG, "%s", dst->path);
383 static void fileUpdate (Dict dict, struct recordGroup *rGroup,
386 struct dirs_info *di;
394 src_len = strlen (src);
397 if (S_ISREG(sbuf.st_mode))
399 struct dirs_entry *e_dst;
400 di = dirs_fopen (dict, src);
402 e_dst = dirs_read (di);
405 if (sbuf.st_mtime > e_dst->mtime)
406 if (fileExtract (&e_dst->sysno, src, rGroup, 0))
407 dirs_add (di, src, e_dst->sysno, sbuf.st_mtime);
412 if (fileExtract (&sysno, src, rGroup, 0))
413 dirs_add (di, src, sysno, sbuf.st_mtime);
417 else if (S_ISDIR(sbuf.st_mode))
419 if (src_len && src[src_len-1] != '/')
422 src[++src_len] = '\0';
424 di = dirs_open (dict, src);
426 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
431 logf (LOG_WARN, "Cannot handle file %s", src);
436 static void repositoryExtract (int deleteFlag, struct recordGroup *rGroup,
446 if (S_ISREG(sbuf.st_mode))
447 fileExtract (NULL, src, rGroup, deleteFlag);
448 else if (S_ISDIR(sbuf.st_mode))
449 repositoryExtractR (deleteFlag, src, rGroup);
451 logf (LOG_WARN, "Cannot handle file %s", src);
454 static void repositoryExtractG (int deleteFlag, struct recordGroup *rGroup)
456 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
460 while (scanf ("%s", src) == 1)
461 repositoryExtract (deleteFlag, rGroup, src);
464 repositoryExtract (deleteFlag, rGroup, rGroup->path);
467 void repositoryUpdate (struct recordGroup *rGroup)
470 assert (rGroup->path);
471 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
474 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
476 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
479 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
482 while (scanf ("%s", src) == 1)
483 fileUpdate (dict, rGroup, src);
486 fileUpdate (dict, rGroup, rGroup->path);
490 repositoryExtractG (0, rGroup);
493 void repositoryDelete (struct recordGroup *rGroup)
495 repositoryExtractG (1, rGroup);