2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.32 1997-09-25 14:56:51 adam
8 * Windows NT interface code to the stat call.
10 * Revision 1.31 1997/09/17 12:19:17 adam
11 * Zebra version corresponds to YAZ version 1.4.
12 * Changed Zebra server so that it doesn't depend on global common_resource.
14 * Revision 1.30 1997/09/09 13:38:09 adam
15 * Partial port to WIN95/NT.
17 * Revision 1.29 1997/02/12 20:39:47 adam
18 * Implemented options -f <n> that limits the log to the first <n>
20 * Changed some log messages also.
22 * Revision 1.28 1996/11/01 08:58:44 adam
23 * Interface to isamc system now includes update and delete.
25 * Revision 1.27 1996/10/29 14:06:56 adam
26 * Include zebrautl.h instead of alexutil.h.
28 * Revision 1.26 1996/06/04 10:19:01 adam
29 * Minor changes - removed include of ctype.h.
31 * Revision 1.25 1996/05/01 13:46:37 adam
32 * First work on multiple records in one file.
33 * New option, -offset, to the "unread" command in the filter module.
35 * Revision 1.24 1996/04/26 10:00:23 adam
36 * Added option -V to zebraidx to display version information.
37 * Removed stupid warnings from file update.
39 * Revision 1.23 1996/04/12 07:02:25 adam
40 * File update of single files.
42 * Revision 1.22 1996/04/09 06:50:50 adam
43 * Bug fix: bad reference in function fileUpdateR.
45 * Revision 1.21 1996/03/22 15:34:18 quinn
48 * Revision 1.20 1996/03/21 14:50:10 adam
49 * File update uses modify-time instead of change-time.
51 * Revision 1.19 1996/03/20 16:16:55 quinn
52 * Added diagnostic output
54 * Revision 1.18 1996/03/19 12:43:27 adam
55 * Bug fix: File update traversal didn't handle trailing slashes correctly.
56 * Bug fix: Update of sub directory groups wasn't handled correctly.
58 * Revision 1.17 1996/02/12 18:45:17 adam
59 * Changed naming of some functions.
61 * Revision 1.16 1996/02/05 12:30:02 adam
62 * Logging reduced a bit.
63 * The remaining running time is estimated during register merge.
65 * Revision 1.15 1995/12/07 17:38:48 adam
66 * Work locking mechanisms for concurrent updates/commit.
68 * Revision 1.14 1995/12/06 12:41:26 adam
69 * New command 'stat' for the index program.
70 * Filenames can be read from stdin by specifying '-'.
71 * Bug fix/enhancement of the transformation from terms to regular
72 * expressons in the search engine.
74 * Revision 1.13 1995/11/28 09:09:46 adam
75 * Zebra config renamed.
76 * Use setting 'recordId' to identify record now.
77 * Bug fix in recindex.c: rec_release_blocks was invokeded even
78 * though the blocks were already released.
79 * File traversal properly deletes records when needed.
81 * Revision 1.12 1995/11/24 11:31:37 adam
82 * Commands add & del read filenames from stdin if source directory is
84 * Match criteria supports 'constant' strings.
86 * Revision 1.11 1995/11/22 17:19:19 adam
87 * Record management uses the bfile system.
89 * Revision 1.10 1995/11/21 15:01:16 adam
90 * New general match criteria implemented.
91 * New feature: document groups.
93 * Revision 1.9 1995/11/21 09:20:32 adam
94 * Yet more work on record match.
96 * Revision 1.8 1995/11/20 16:59:46 adam
97 * New update method: the 'old' keys are saved for each records.
99 * Revision 1.7 1995/11/20 11:56:28 adam
100 * Work on new traversal.
102 * Revision 1.6 1995/11/17 15:54:42 adam
103 * Started work on virtual directory structure.
105 * Revision 1.5 1995/10/17 18:02:09 adam
106 * New feature: databases. Implemented as prefix to words in dictionary.
108 * Revision 1.4 1995/09/28 09:19:46 adam
109 * xfree/xmalloc used everywhere.
110 * Extract/retrieve method seems to work for text records.
112 * Revision 1.3 1995/09/06 16:11:18 adam
113 * Option: only one word key per file.
115 * Revision 1.2 1995/09/04 12:33:43 adam
116 * Various cleanup. YAZ util used instead.
118 * Revision 1.1 1995/09/01 14:06:36 adam
119 * Split of work into more files.
126 #include <sys/types.h>
127 #include <sys/stat.h>
130 #define S_ISREG(x) (x & _S_IFREG)
131 #define S_ISDIR(x) (x & _S_IFDIR)
141 static int repComp (const char *a, const char *b, size_t len)
145 return memcmp (a, b, len);
148 static void repositoryExtractR (int deleteFlag, char *rep,
149 struct recordGroup *rGroup)
153 size_t rep_len = strlen (rep);
158 logf (LOG_LOG, "dir %s", rep);
159 if (rep[rep_len-1] != '/')
163 for (i=0; e[i].name; i++)
165 strcpy (rep +rep_len+1, e[i].name);
169 fileExtract (NULL, rep, rGroup, deleteFlag);
172 repositoryExtractR (deleteFlag, rep, rGroup);
180 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
181 const char *base, char *src,
182 struct recordGroup *rGroup)
185 size_t src_len = strlen (src);
187 while (dst && !repComp (dst->path, src, src_len+1))
192 sprintf (tmppath, "%s%s", base, dst->path);
193 fileExtract (&dst->sysno, tmppath, rGroup, 1);
195 strcpy (tmppath, dst->path);
196 dst = dirs_read (di);
197 dirs_del (di, tmppath);
200 strcpy (tmppath, dst->path);
201 dst = dirs_read (di);
202 dirs_rmdir (di, tmppath);
205 dst = dirs_read (di);
210 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
211 const char *base, char *src,
212 struct recordGroup *rGroup)
214 struct dir_entry *e_src;
216 static char tmppath[1024];
217 size_t src_len = strlen (src);
219 sprintf (tmppath, "%s%s", base, src);
220 e_src = dir_open (tmppath);
221 logf (LOG_LOG, "dir %s", tmppath);
224 if (!dst || repComp (dst->path, src, src_len))
226 if (!dst || strcmp (dst->path, src))
232 if (src_len && src[src_len-1] != '/')
235 src[++src_len] = '\0';
237 dirs_mkdir (di, src, 0);
238 if (dst && repComp (dst->path, src, src_len))
243 strcpy (src, dst->path);
244 fileDeleteR (di, dst, base, src, rGroup);
249 if (src_len && src[src_len-1] != '/')
252 src[++src_len] = '\0';
254 dst = dirs_read (di);
262 if (dst && !repComp (dst->path, src, src_len))
264 if (e_src[i_src].name)
266 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
268 sd = strcmp (dst->path + src_len, e_src[i_src].name);
273 else if (e_src[i_src].name)
277 logf (LOG_DEBUG, "trav sd=%d", sd);
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 (e_src[i_src].mtime > dst->mtime)
288 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
290 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
292 logf (LOG_DEBUG, "old: %s", ctime (&dst->mtime));
293 logf (LOG_DEBUG, "new: %s", ctime (&e_src[i_src].mtime));
295 dst = dirs_read (di);
298 fileUpdateR (di, dst, base, src, rGroup);
299 dst = dirs_last (di);
300 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
303 dst = dirs_read (di);
310 strcpy (src + src_len, e_src[i_src].name);
311 sprintf (tmppath, "%s%s", base, src);
313 switch (e_src[i_src].kind)
316 if (fileExtract (&sysno, tmppath, rGroup, 0))
317 dirs_add (di, src, sysno, e_src[i_src].mtime);
320 fileUpdateR (di, dst, base, src, rGroup);
322 dst = dirs_last (di);
329 strcpy (src, dst->path);
330 sprintf (tmppath, "%s%s", base, dst->path);
335 fileExtract (&dst->sysno, tmppath, rGroup, 1);
336 dirs_del (di, dst->path);
337 dst = dirs_read (di);
340 fileDeleteR (di, dst, base, src, rGroup);
341 dst = dirs_last (di);
348 static void groupRes (struct recordGroup *rGroup)
353 if (!rGroup->groupName || !*rGroup->groupName)
356 sprintf (gPrefix, "%s.", rGroup->groupName);
358 sprintf (resStr, "%srecordId", gPrefix);
359 rGroup->recordId = res_get (common_resource, resStr);
362 void repositoryShow (struct recordGroup *rGroup)
366 struct dirs_entry *dst;
368 struct dirs_info *di;
370 if (!(dict = dict_open (rGroup->bfs, FMATCH_DICT, 50, 1)))
372 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
376 assert (rGroup->path);
377 strcpy (src, rGroup->path);
378 src_len = strlen (src);
380 if (src_len && src[src_len-1] != '/')
383 src[++src_len] = '\0';
386 di = dirs_open (dict, src);
388 while ( (dst = dirs_read (di)) )
389 logf (LOG_LOG, "%s", dst->path);
394 static void fileUpdate (Dict dict, struct recordGroup *rGroup,
397 struct dirs_info *di;
405 src_len = strlen (src);
408 if (S_ISREG(sbuf.st_mode))
410 struct dirs_entry *e_dst;
411 di = dirs_fopen (dict, src);
413 e_dst = dirs_read (di);
416 if (sbuf.st_mtime > e_dst->mtime)
417 if (fileExtract (&e_dst->sysno, src, rGroup, 0))
418 dirs_add (di, src, e_dst->sysno, sbuf.st_mtime);
423 if (fileExtract (&sysno, src, rGroup, 0))
424 dirs_add (di, src, sysno, sbuf.st_mtime);
428 else if (S_ISDIR(sbuf.st_mode))
430 if (src_len && src[src_len-1] != '/')
433 src[++src_len] = '\0';
435 di = dirs_open (dict, src);
437 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
442 logf (LOG_WARN, "Cannot handle file %s", src);
447 static void repositoryExtract (int deleteFlag, struct recordGroup *rGroup,
457 if (S_ISREG(sbuf.st_mode))
458 fileExtract (NULL, src, rGroup, deleteFlag);
459 else if (S_ISDIR(sbuf.st_mode))
460 repositoryExtractR (deleteFlag, src, rGroup);
462 logf (LOG_WARN, "Cannot handle file %s", src);
465 static void repositoryExtractG (int deleteFlag, struct recordGroup *rGroup)
467 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
471 while (scanf ("%s", src) == 1)
472 repositoryExtract (deleteFlag, rGroup, src);
475 repositoryExtract (deleteFlag, rGroup, rGroup->path);
478 void repositoryUpdate (struct recordGroup *rGroup)
481 assert (rGroup->path);
482 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
485 if (!(dict = dict_open (rGroup->bfs, FMATCH_DICT, 50, 1)))
487 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
490 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
493 while (scanf ("%s", src) == 1)
494 fileUpdate (dict, rGroup, src);
497 fileUpdate (dict, rGroup, rGroup->path);
501 repositoryExtractG (0, rGroup);
504 void repositoryDelete (struct recordGroup *rGroup)
506 repositoryExtractG (1, rGroup);