2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1995-09-01 14:06:36 adam
8 * Split of work into more files.
16 #include <sys/types.h>
23 static void repository_extract_r (int cmd, char *rep)
28 size_t rep_len = strlen (rep);
33 if (rep[rep_len-1] != '/')
37 for (i=0; e[i].name; i++)
39 strcpy (rep +rep_len+1, e[i].name);
41 switch (fs.st_mode & S_IFMT)
44 file_extract (cmd, rep, rep);
47 repository_extract_r (cmd, rep);
54 void copy_file (const char *dst, const char *src)
56 int d_fd = open (dst, O_WRONLY|O_CREAT, 0666);
57 int s_fd = open (src, O_RDONLY);
63 log (LOG_FATAL|LOG_ERRNO, "Cannot create %s", dst);
68 log (LOG_FATAL|LOG_ERRNO, "Cannot open %s", src);
71 if (!(buf = malloc (4096)))
73 log (LOG_FATAL|LOG_ERRNO, "malloc");
76 while ((r=read (s_fd, buf, 4096))>0)
77 for (w = 0; w < r; w += i)
79 i = write (d_fd, buf + w, r - w);
82 log (LOG_FATAL|LOG_ERRNO, "write");
88 log (LOG_FATAL|LOG_ERRNO, "read");
96 void del_file (const char *dst)
101 void del_dir (const char *dst)
103 log (LOG_DEBUG, "rmdir of %s", dst);
104 if (rmdir (dst) == -1)
105 log (LOG_ERRNO|LOG_WARN, "rmdir");
108 void repository_update_r (int cmd, char *dst, char *src);
110 void repository_add_tree (int cmd, char *dst, char *src)
113 repository_update_r (cmd, dst, src);
116 void repository_del_tree (int cmd, char *dst, char *src)
118 size_t dst_len = strlen (dst);
119 size_t src_len = strlen (src);
120 struct dir_entry *e_dst;
124 e_dst = dir_open (dst);
128 if (src[src_len-1] != '/')
132 if (dst[dst_len-1] != '/')
136 while (e_dst[i_dst].name)
138 strcpy (dst +dst_len+1, e_dst[i_dst].name);
139 strcpy (src +src_len+1, e_dst[i_dst].name);
142 switch (fs_dst.st_mode & S_IFMT)
145 file_extract ('d', dst, dst);
149 repository_del_tree (cmd, dst, src);
162 void repository_update_r (int cmd, char *dst, char *src)
164 struct dir_entry *e_dst, *e_src;
165 int i_dst = 0, i_src = 0;
166 struct stat fs_dst, fs_src;
167 size_t dst_len = strlen (dst);
168 size_t src_len = strlen (src);
170 e_dst = dir_open (dst);
171 e_src = dir_open (src);
173 if (!e_dst && !e_src)
178 repository_add_tree (cmd, dst, src);
184 repository_del_tree (cmd, dst, src);
191 if (src[src_len-1] != '/')
195 if (dst[dst_len-1] != '/')
199 while (e_dst[i_dst].name || e_src[i_src].name)
203 if (e_dst[i_dst].name && e_src[i_src].name)
204 sd = strcmp (e_dst[i_dst].name, e_src[i_src].name);
205 else if (e_src[i_src].name)
212 strcpy (dst +dst_len+1, e_dst[i_dst].name);
213 strcpy (src +src_len+1, e_src[i_src].name);
215 /* check type, date, length */
220 switch (fs_dst.st_mode & S_IFMT)
223 if (fs_src.st_ctime > fs_dst.st_ctime)
225 file_extract ('d', dst, dst);
226 file_extract ('a', src, dst);
227 copy_file (dst, src);
231 repository_update_r (cmd, dst, src);
239 strcpy (dst +dst_len+1, e_src[i_src].name);
240 strcpy (src +src_len+1, e_src[i_src].name);
243 switch (fs_src.st_mode & S_IFMT)
246 file_extract ('a', src, dst);
247 copy_file (dst, src);
250 repository_add_tree (cmd, dst, src);
257 strcpy (dst +dst_len+1, e_dst[i_dst].name);
258 strcpy (src +src_len+1, e_dst[i_dst].name);
261 switch (fs_dst.st_mode & S_IFMT)
264 file_extract ('d', dst, dst);
268 repository_del_tree (cmd, dst, src);
278 void repository (int cmd, const char *rep, const char *base_path)
283 strcpy (rep_tmp1, rep);
286 strcpy (rep_tmp2, base_path);
287 repository_update_r (cmd, rep_tmp2, rep_tmp1);
290 repository_extract_r (cmd, rep_tmp1);