2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-09-28 09:19:46 adam
8 * xfree/xmalloc used everywhere.
9 * Extract/retrieve method seems to work for text records.
11 * Revision 1.3 1995/09/06 16:11:18 adam
12 * Option: only one word key per file.
14 * Revision 1.2 1995/09/04 12:33:43 adam
15 * Various cleanup. YAZ util used instead.
17 * Revision 1.1 1995/09/01 14:06:36 adam
18 * Split of work into more files.
26 #include <sys/types.h>
33 static void repository_extract_r (int cmd, char *rep)
38 size_t rep_len = strlen (rep);
43 if (rep[rep_len-1] != '/')
47 for (i=0; e[i].name; i++)
49 strcpy (rep +rep_len+1, e[i].name);
51 switch (fs.st_mode & S_IFMT)
54 file_extract (cmd, rep, rep);
57 repository_extract_r (cmd, rep);
64 void copy_file (const char *dst, const char *src)
66 int d_fd = open (dst, O_WRONLY|O_CREAT, 0666);
67 int s_fd = open (src, O_RDONLY);
73 logf (LOG_FATAL|LOG_ERRNO, "Cannot create %s", dst);
78 logf (LOG_FATAL|LOG_ERRNO, "Cannot open %s", src);
82 while ((r=read (s_fd, buf, 4096))>0)
83 for (w = 0; w < r; w += i)
85 i = write (d_fd, buf + w, r - w);
88 logf (LOG_FATAL|LOG_ERRNO, "write");
94 logf (LOG_FATAL|LOG_ERRNO, "read");
102 void del_file (const char *dst)
107 void del_dir (const char *dst)
109 logf (LOG_DEBUG, "rmdir of %s", dst);
110 if (rmdir (dst) == -1)
111 logf (LOG_ERRNO|LOG_WARN, "rmdir");
114 void repository_update_r (int cmd, char *dst, char *src);
116 void repository_add_tree (int cmd, char *dst, char *src)
119 repository_update_r (cmd, dst, src);
122 void repository_del_tree (int cmd, char *dst, char *src)
124 size_t dst_len = strlen (dst);
125 size_t src_len = strlen (src);
126 struct dir_entry *e_dst;
130 e_dst = dir_open (dst);
134 if (src[src_len-1] != '/')
138 if (dst[dst_len-1] != '/')
142 while (e_dst[i_dst].name)
144 strcpy (dst +dst_len+1, e_dst[i_dst].name);
145 strcpy (src +src_len+1, e_dst[i_dst].name);
148 switch (fs_dst.st_mode & S_IFMT)
151 file_extract ('d', dst, dst);
155 repository_del_tree (cmd, dst, src);
168 void repository_update_r (int cmd, char *dst, char *src)
170 struct dir_entry *e_dst, *e_src;
171 int i_dst = 0, i_src = 0;
172 struct stat fs_dst, fs_src;
173 size_t dst_len = strlen (dst);
174 size_t src_len = strlen (src);
176 e_dst = dir_open (dst);
177 e_src = dir_open (src);
179 if (!e_dst && !e_src)
184 repository_add_tree (cmd, dst, src);
190 repository_del_tree (cmd, dst, src);
197 if (src[src_len-1] != '/')
201 if (dst[dst_len-1] != '/')
205 while (e_dst[i_dst].name || e_src[i_src].name)
209 if (e_dst[i_dst].name && e_src[i_src].name)
210 sd = strcmp (e_dst[i_dst].name, e_src[i_src].name);
211 else if (e_src[i_src].name)
218 strcpy (dst +dst_len+1, e_dst[i_dst].name);
219 strcpy (src +src_len+1, e_src[i_src].name);
221 /* check type, date, length */
226 switch (fs_dst.st_mode & S_IFMT)
229 if (fs_src.st_ctime > fs_dst.st_ctime)
231 file_extract ('d', dst, dst);
232 file_extract ('a', src, dst);
233 copy_file (dst, src);
237 repository_update_r (cmd, dst, src);
245 strcpy (dst +dst_len+1, e_src[i_src].name);
246 strcpy (src +src_len+1, e_src[i_src].name);
249 switch (fs_src.st_mode & S_IFMT)
252 file_extract ('a', src, dst);
253 copy_file (dst, src);
256 repository_add_tree (cmd, dst, src);
263 strcpy (dst +dst_len+1, e_dst[i_dst].name);
264 strcpy (src +src_len+1, e_dst[i_dst].name);
267 switch (fs_dst.st_mode & S_IFMT)
270 file_extract ('d', dst, dst);
274 repository_del_tree (cmd, dst, src);
284 void repository (int cmd, const char *rep, const char *base_path)
289 strcpy (rep_tmp1, rep);
292 strcpy (rep_tmp2, base_path);
293 repository_update_r (cmd, rep_tmp2, rep_tmp1);
296 repository_extract_r (cmd, rep_tmp1);