[go: up one dir, main page]

Menu

[r369]: / trunk / mergedir.c  Maximize  Restore  History

Download this file

352 lines (299 with data), 7.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* merge a directory, moving files when possible and overwrite/copy when not.
* Do not overwrite files in $APK_LBUDIRS but install files as .apk-new
*
* Copyright (c) 2008 Natanael Copa <natanael.copa@gmail.com>
*
* Distributed under GPL-2
*/
/*
lbu_filter() {
# Ok... I give up. shell is too slow. lets do it in awk.
awk 'BEGIN {
APK_LBUDIRS="'"$APK_LBUDIRS"'";
numdirs = split(APK_LBUDIRS, lbudir, ":");
#precalc lengths to save a few cpu cycles in loop
for (i = 1; i <= numdirs; i++)
len[i] = length(lbudir[i]);
}
# main loop
{
for (i = 1; i <= numdirs; i++) {
if (index($0, lbudir[i]) == 1 && (len[i] == length() || substr($0, len[i] + 1, 1) == "/")) {
print $0;
}
}
}'
}
# merge a temp directory to $ROOT
pkgf_merge() {
local f src dest destdir
ls -a "$1" | while read f ; do
# skip . and ..
if [ "$f" = '.' ] || [ "$f" = '..' ] ; then
continue
fi
# remove leading ./
src="`beautify_path \"${1#./}/$f\"`"
destdir="$ROOT/$1"
dest="$destdir/$f"
# check if the entry exist on dest
if ! [ -e "$ROOT/$src" ] ; then
mv "$src" "$destdir/"
continue
fi
# parse directories recursively
if [ -d "$src" ] && ! [ -L "$src" ] ; then
pkgf_merge "$src"
else
if is_lbu_file "$src" && ! cmp -s "$src" "$dest" 2>/dev/null ; then
# keep existing file and install as apk-new
mv "$src" "$dest.apk-new"
else
# replace it
# try with busybox mv if we fail to handle
# coreutils installs and busybox upgrades
[ -L "$dest" ] && [ -d "$( readlink $dest )" ] && rm -f "$dest"
mv "$1/$f" "$destdir/" 2>/dev/null \
|| /bin/busybox mv "$1/$f" "$destdir/" \
|| "$1/busybox" mv "$1/$f" "$destdir/"
fi
fi
done
}
*/
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
static char *lbudirs;
void usage(const char *prog) {
printf("usage: %s SOURCEDIR DESTDIR\n", prog);
exit(1);
}
void *xmalloc(size_t size)
{
void *buf = malloc(size);
if (buf == NULL)
err(EX_OSERR, "malloc");
return buf;
}
void *xstrdup(const char *s)
{
char *buf = strdup(s);
if (buf == NULL)
err(EX_OSERR, "strdup");
return buf;
}
char *init_lbudirs(void)
{
char *apk_lbudirs = getenv("APK_LBUDIRS");
char *s, *d;
if (apk_lbudirs == NULL) {
lbudirs = xstrdup("etc\0");
return lbudirs;
}
lbudirs = xmalloc(strlen(apk_lbudirs) + 1);
s = apk_lbudirs;
d = lbudirs;
while (*s) {
*d = (*s == ':') ? '\0': *s;
d++;
s++;
}
*d = '\0';
return lbudirs;
}
int is_lbudir(const char *path)
{
char *p = lbudirs;
if (path == NULL || *path == '\0')
return 0;
while (*p) {
int len;
/* strip leading '/' */
while (*p == '/')
p++;
len = strlen(p);
if (strstr(path, p) == path) {
const char *e = path + len;
if (*e == '\0' || *e == '/')
return 1;
}
p += len+1;
}
return 0;
}
int copyfd(int sfd, int dfd, size_t size)
{
size_t count = 0, n;
char *buffer = mmap(NULL, size, PROT_READ, MAP_SHARED, sfd, 0);
if (buffer == MAP_FAILED) {
perror("mmap");
return -1;
}
while (count < size) {
n = write(dfd, buffer + count, size - count);
if (n < 1) {
perror("write");
return -1;
}
count += n;
}
munmap(buffer, size);
return count;
}
int copy_file(const char *src, const char *dest, struct stat *st)
{
int srcfd, destfd;
srcfd = open(src, O_RDONLY);
if (srcfd < 0)
err(EX_OSERR, src);
unlink(dest);
destfd = open(dest, O_WRONLY | O_CREAT, st->st_mode & 07777);
if (destfd < 0)
err(EX_OSERR, dest);
if (st->st_size > 0)
if (copyfd(srcfd, destfd, st->st_size) < 0)
err(EX_OSERR, dest);
close(srcfd);
close(destfd);
return 0;
}
int is_equal(const char *a, const char *b, off_t size)
{
#define BUF_SIZE 4096
int fd1, fd2, ret = 0;
char *buf1, *buf2;
off_t count = 0;
if ((fd1 = open(a, O_RDONLY)) < 0)
return 0;
if ((fd2 = open(b, O_RDONLY)) < 0) {
close(fd1);
return 0;
}
buf1 = xmalloc(BUF_SIZE * 2);
buf2 = &buf1[BUF_SIZE];
while (count < size) {
int n1 = read(fd1, buf1, BUF_SIZE);
int n2;
if (n1 < 1)
goto close_and_return;
n2 = read(fd2, buf2, BUF_SIZE);
if (n2 < 1 || n1 != n2)
goto close_and_return;
if (memcmp(buf1, buf2, n1) != 0)
goto close_and_return;
count += n1;
}
ret++;
close_and_return:
free(buf1);
close(fd1);
close(fd2);
return ret;
#undef BUF_SIZE
}
void merge_recursive(const char *destdir, const char *current, int protected)
{
DIR *dir = opendir(".");
struct dirent *de;
char *destpath, *dest;
int len = 0;
int bufsize;
while (current && *current && *current == '/')
current++;
if (!protected)
protected = current && is_lbudir(current);
if (dir == NULL)
err(EX_OSERR, ".");
// printf("DEBUG: merging %s to %s, protected=%i\n", current, destdir, protected);
destpath = xmalloc(PATH_MAX);
destpath[PATH_MAX - 1] = '\0';
snprintf(destpath, PATH_MAX-1, "%s/%s/", destdir, current ? current : "");
len = strlen(destpath);
/* remove multiple trailing '/' */
while (len > 1 && destpath[len-1] == '/' && destpath[len-2] == '/')
destpath[len-- -1] = '\0';
dest = destpath + len;
bufsize = PATH_MAX - 1 - len;
while ((de = readdir(dir)) != NULL) {
struct stat src_st, dest_st;
int exists = 0;
/* skip . and .. */
if (strcmp(de->d_name, ".") == 0
|| strcmp(de->d_name, "..") == 0)
continue;
strncpy(dest, de->d_name, bufsize);
if (lstat(destpath, &dest_st) < 0) {
if (errno == ENOENT) {
/* try move the file/dir */
if (rename(de->d_name, destpath) == 0) {
//printf("DEBUG: moved %s\n", destpath);
continue;
}
} else
err(EX_OSERR, "lstat");
} else {
exists++;
}
/* dest exists or we failed to move */
if (lstat(de->d_name, &src_st) < 0)
err(EX_OSERR, "lstat");
if (S_ISDIR(src_st.st_mode)) {
char *newcurr;
if (mkdir(destpath, src_st.st_mode) < 0
&& errno != EEXIST)
err(EX_OSERR, destpath);
if (asprintf(&newcurr, "%s/%s", current ? current : "",
de->d_name) < 0)
err(EX_OSERR, "asprintf");
chdir(de->d_name);
merge_recursive(destdir, newcurr, protected);
chdir("..");
rmdir(de->d_name);
free(newcurr);
continue;
}
if (!exists) {
copy_file(de->d_name, destpath, &src_st);
continue;
}
if (protected) {
if (src_st.st_size == dest_st.st_size
&& is_equal(de->d_name, destpath,
src_st.st_size)) {
unlink(de->d_name);
continue;
}
strncat(dest, ".apk-new", len);
} else {
if (unlink(destpath) < 0)
err(EX_OSERR, destpath);
}
if (rename(de->d_name, destpath) == 0)
continue;
/* try to copy the file, as last resort */
copy_file(de->d_name, destpath, &src_st);
}
}
int main(int argc, char *argv[])
{
const char *src, *dest;
if (argc != 3)
usage(argv[0]);
src = argv[1];
dest = argv[2];
init_lbudirs();
if (chdir(src) < 0)
err(EX_OSERR, "chdir");
merge_recursive(dest, NULL, 0);
return 0;
}