2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-10-17 18:02:09 adam
8 * New feature: databases. Implemented as prefix to words in dictionary.
10 * Revision 1.4 1995/09/28 09:19:46 adam
11 * xfree/xmalloc used everywhere.
12 * Extract/retrieve method seems to work for text records.
14 * Revision 1.3 1995/09/06 16:11:18 adam
15 * Option: only one word key per file.
17 * Revision 1.2 1995/09/04 12:33:43 adam
18 * Various cleanup. YAZ util used instead.
20 * Revision 1.1 1995/09/01 14:06:36 adam
21 * Split of work into more files.
29 #include <sys/types.h>
36 static void repository_extract_r (int cmd, char *rep, char *databaseName)
41 size_t rep_len = strlen (rep);
46 if (rep[rep_len-1] != '/')
50 for (i=0; e[i].name; i++)
52 strcpy (rep +rep_len+1, e[i].name);
54 switch (fs.st_mode & S_IFMT)
57 file_extract (cmd, rep, rep, databaseName);
60 repository_extract_r (cmd, rep, databaseName);
67 void copy_file (const char *dst, const char *src)
69 int d_fd = open (dst, O_WRONLY|O_CREAT, 0666);
70 int s_fd = open (src, O_RDONLY);
76 logf (LOG_FATAL|LOG_ERRNO, "Cannot create %s", dst);
81 logf (LOG_FATAL|LOG_ERRNO, "Cannot open %s", src);
85 while ((r=read (s_fd, buf, 4096))>0)
86 for (w = 0; w < r; w += i)
88 i = write (d_fd, buf + w, r - w);
91 logf (LOG_FATAL|LOG_ERRNO, "write");
97 logf (LOG_FATAL|LOG_ERRNO, "read");
105 void del_file (const char *dst)
110 void del_dir (const char *dst)
112 logf (LOG_DEBUG, "rmdir of %s", dst);
113 if (rmdir (dst) == -1)
114 logf (LOG_ERRNO|LOG_WARN, "rmdir");
117 void repository_update_r (int cmd, char *dst, char *src, char *databaseName);
119 void repository_add_tree (int cmd, char *dst, char *src, char *databaseName)
122 repository_update_r (cmd, dst, src, databaseName);
125 void repository_del_tree (int cmd, char *dst, char *src, char *databaseName)
127 size_t dst_len = strlen (dst);
128 size_t src_len = strlen (src);
129 struct dir_entry *e_dst;
133 e_dst = dir_open (dst);
137 if (src[src_len-1] != '/')
141 if (dst[dst_len-1] != '/')
145 while (e_dst[i_dst].name)
147 strcpy (dst +dst_len+1, e_dst[i_dst].name);
148 strcpy (src +src_len+1, e_dst[i_dst].name);
151 switch (fs_dst.st_mode & S_IFMT)
154 file_extract ('d', dst, dst, databaseName);
158 repository_del_tree (cmd, dst, src, databaseName);
171 void repository_update_r (int cmd, char *dst, char *src, char *databaseName)
173 struct dir_entry *e_dst, *e_src;
174 int i_dst = 0, i_src = 0;
175 struct stat fs_dst, fs_src;
176 size_t dst_len = strlen (dst);
177 size_t src_len = strlen (src);
179 e_dst = dir_open (dst);
180 e_src = dir_open (src);
182 if (!e_dst && !e_src)
187 repository_add_tree (cmd, dst, src, databaseName);
193 repository_del_tree (cmd, dst, src, databaseName);
200 if (src[src_len-1] != '/')
204 if (dst[dst_len-1] != '/')
208 while (e_dst[i_dst].name || e_src[i_src].name)
212 if (e_dst[i_dst].name && e_src[i_src].name)
213 sd = strcmp (e_dst[i_dst].name, e_src[i_src].name);
214 else if (e_src[i_src].name)
221 strcpy (dst +dst_len+1, e_dst[i_dst].name);
222 strcpy (src +src_len+1, e_src[i_src].name);
224 /* check type, date, length */
229 switch (fs_dst.st_mode & S_IFMT)
232 if (fs_src.st_ctime > fs_dst.st_ctime)
234 file_extract ('d', dst, dst, databaseName);
235 file_extract ('a', src, dst, databaseName);
236 copy_file (dst, src);
240 repository_update_r (cmd, dst, src, databaseName);
248 strcpy (dst +dst_len+1, e_src[i_src].name);
249 strcpy (src +src_len+1, e_src[i_src].name);
252 switch (fs_src.st_mode & S_IFMT)
255 file_extract ('a', src, dst, databaseName);
256 copy_file (dst, src);
259 repository_add_tree (cmd, dst, src, databaseName);
266 strcpy (dst +dst_len+1, e_dst[i_dst].name);
267 strcpy (src +src_len+1, e_dst[i_dst].name);
270 switch (fs_dst.st_mode & S_IFMT)
273 file_extract ('d', dst, dst, databaseName);
277 repository_del_tree (cmd, dst, src, databaseName);
287 void repository (int cmd, const char *rep, const char *base_path,
293 strcpy (rep_tmp1, rep);
296 strcpy (rep_tmp2, base_path);
297 repository_update_r (cmd, rep_tmp2, rep_tmp1, databaseName);
300 repository_extract_r (cmd, rep_tmp1, databaseName);