1 /* $Id: trav.c,v 1.46 2004-11-19 10:27:03 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include <sys/types.h>
28 #define S_ISREG(x) (x & _S_IFREG)
29 #define S_ISDIR(x) (x & _S_IFDIR)
39 static int repComp (const char *a, const char *b, size_t len)
43 return memcmp (a, b, len);
46 static void repositoryExtractR (ZebraHandle zh, int deleteFlag, char *rep,
51 size_t rep_len = strlen (rep);
53 e = dir_open (rep, zh->path_reg, zh->m_follow_links);
56 yaz_log (YLOG_LOG, "dir %s", rep);
57 if (rep[rep_len-1] != '/')
62 for (i=0; e[i].name; i++)
65 strcpy (rep +rep_len+1, e[i].name);
66 if ((ecp = strrchr (e[i].name, '/')))
72 fileExtract (zh, NULL, rep, deleteFlag);
75 repositoryExtractR (zh, deleteFlag, rep, level+1);
83 static void fileDeleteR (ZebraHandle zh,
84 struct dirs_info *di, struct dirs_entry *dst,
85 const char *base, char *src)
88 size_t src_len = strlen (src);
90 while (dst && !repComp (dst->path, src, src_len+1))
95 sprintf (tmppath, "%s%s", base, dst->path);
96 fileExtract (zh, &dst->sysno, tmppath, 1);
98 strcpy (tmppath, dst->path);
100 dirs_del (di, tmppath);
103 strcpy (tmppath, dst->path);
104 dst = dirs_read (di);
105 dirs_rmdir (di, tmppath);
108 dst = dirs_read (di);
113 static void fileUpdateR (ZebraHandle zh,
114 struct dirs_info *di, struct dirs_entry *dst,
115 const char *base, char *src,
118 struct dir_entry *e_src;
120 static char tmppath[1024];
121 size_t src_len = strlen (src);
123 sprintf (tmppath, "%s%s", base, src);
124 e_src = dir_open (tmppath, zh->path_reg, zh->m_follow_links);
125 yaz_log (YLOG_LOG, "dir %s", tmppath);
128 if (!dst || repComp (dst->path, src, src_len))
130 if (!dst || strcmp (dst->path, src))
136 if (src_len && src[src_len-1] != '/')
139 src[++src_len] = '\0';
141 dirs_mkdir (di, src, 0);
142 if (dst && repComp (dst->path, src, src_len))
147 strcpy (src, dst->path);
148 fileDeleteR (zh, di, dst, base, src);
153 if (src_len && src[src_len-1] != '/')
156 src[++src_len] = '\0';
158 dst = dirs_read (di);
166 if (dst && !repComp (dst->path, src, src_len))
168 if (e_src[i_src].name)
170 yaz_log (YLOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
172 sd = strcmp (dst->path + src_len, e_src[i_src].name);
177 else if (e_src[i_src].name)
181 yaz_log (YLOG_DEBUG, "trav sd=%d", sd);
185 strcpy (src + src_len, e_src[i_src].name);
186 sprintf (tmppath, "%s%s", base, src);
188 switch (e_src[i_src].kind)
191 if (e_src[i_src].mtime > dst->mtime)
193 if (fileExtract (zh, &dst->sysno, tmppath, 0))
195 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
197 yaz_log (YLOG_DEBUG, "old: %s", ctime (&dst->mtime));
198 yaz_log (YLOG_DEBUG, "new: %s", ctime (&e_src[i_src].mtime));
200 dst = dirs_read (di);
203 fileUpdateR (zh, di, dst, base, src, level+1);
204 dst = dirs_last (di);
205 yaz_log (YLOG_DEBUG, "last is %s", dst ? dst->path : "null");
208 dst = dirs_read (di);
215 strcpy (src + src_len, e_src[i_src].name);
216 sprintf (tmppath, "%s%s", base, src);
218 switch (e_src[i_src].kind)
221 if (fileExtract (zh, &sysno, tmppath, 0))
222 dirs_add (di, src, sysno, e_src[i_src].mtime);
225 fileUpdateR (zh, di, dst, base, src, level+1);
227 dst = dirs_last (di);
234 strcpy (src, dst->path);
235 sprintf (tmppath, "%s%s", base, dst->path);
240 fileExtract (zh, &dst->sysno, tmppath, 1);
241 dirs_del (di, dst->path);
242 dst = dirs_read (di);
245 fileDeleteR (zh, di, dst, base, src);
246 dst = dirs_last (di);
253 void repositoryShow (ZebraHandle zh, const char *path)
257 struct dirs_entry *dst;
259 struct dirs_info *di;
261 if (!(dict = dict_open_res (zh->reg->bfs, FMATCH_DICT, 50, 0, 0, zh->res)))
263 yaz_log (YLOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
267 strncpy(src, path, sizeof(src)-1);
268 src[sizeof(src)-1]='\0';
269 src_len = strlen (src);
271 if (src_len && src[src_len-1] != '/')
274 src[++src_len] = '\0';
277 di = dirs_open (dict, src, zh->m_flag_rw);
279 while ( (dst = dirs_read (di)) )
280 yaz_log (YLOG_LOG, "%s", dst->path);
285 static void fileUpdate (ZebraHandle zh, Dict dict, const char *path)
287 struct dirs_info *di;
295 if (zh->path_reg && !yaz_is_abspath(path))
297 strcpy (src, zh->path_reg);
303 ret = zebra_file_stat (src, &sbuf, zh->m_follow_links);
306 src_len = strlen (src);
310 yaz_log (YLOG_WARN|YLOG_ERRNO, "Cannot access path %s", src);
312 else if (S_ISREG(sbuf.st_mode))
314 struct dirs_entry *e_dst;
315 di = dirs_fopen (dict, src);
317 e_dst = dirs_read (di);
320 if (sbuf.st_mtime > e_dst->mtime)
321 if (fileExtract (zh, &e_dst->sysno, src, 0))
322 dirs_add (di, src, e_dst->sysno, sbuf.st_mtime);
327 if (fileExtract (zh, &sysno, src, 0))
328 dirs_add (di, src, sysno, sbuf.st_mtime);
332 else if (S_ISDIR(sbuf.st_mode))
334 if (src_len && src[src_len-1] != '/')
337 src[++src_len] = '\0';
339 di = dirs_open (dict, src, zh->m_flag_rw);
341 fileUpdateR (zh, di, dirs_read (di), src, dst, 0);
346 yaz_log (YLOG_WARN, "Skipping path %s", src);
350 static void repositoryExtract (ZebraHandle zh,
351 int deleteFlag, const char *path)
359 if (zh->path_reg && !yaz_is_abspath(path))
361 strcpy (src, zh->path_reg);
367 ret = zebra_file_stat (src, &sbuf, zh->m_follow_links);
372 yaz_log (YLOG_WARN|YLOG_ERRNO, "Cannot access path %s", src);
373 else if (S_ISREG(sbuf.st_mode))
374 fileExtract (zh, NULL, src, deleteFlag);
375 else if (S_ISDIR(sbuf.st_mode))
376 repositoryExtractR (zh, deleteFlag, src, 0);
378 yaz_log (YLOG_WARN, "Skipping path %s", src);
381 static void repositoryExtractG (ZebraHandle zh, const char *path,
384 if (!strcmp(path, "") || !strcmp(path, "-"))
388 while (scanf ("%1020s", src) == 1)
389 repositoryExtract (zh, deleteFlag, src);
392 repositoryExtract (zh, deleteFlag, path);
395 void repositoryUpdate (ZebraHandle zh, const char *path)
398 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
401 if (!(dict = dict_open_res (zh->reg->bfs, FMATCH_DICT, 50,
402 zh->m_flag_rw, 0, zh->res)))
404 yaz_log (YLOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
407 if (!strcmp(path, "") || !strcmp(path, "-"))
410 while (scanf ("%s", src) == 1)
411 fileUpdate (zh, dict, src);
414 fileUpdate (zh, dict, path);
418 repositoryExtractG (zh, path, 0);
421 void repositoryDelete (ZebraHandle zh, const char *path)
424 repositoryExtractG (zh, path, 1);