1 |
vapier 11/02/23 08:59:45 |
2 |
|
3 |
Modified: libq.c |
4 |
Added: xmkdir.c |
5 |
Log: |
6 |
replace `rm -rf` shells with an internal rm_rf(), and add support for running pkg_{pre,post}inst ourselves |
7 |
|
8 |
Revision Changes Path |
9 |
1.22 portage-utils/libq/libq.c |
10 |
|
11 |
file : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/libq/libq.c?rev=1.22&view=markup |
12 |
plain: http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/libq/libq.c?rev=1.22&content-type=text/plain |
13 |
diff : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/libq/libq.c?r1=1.21&r2=1.22 |
14 |
|
15 |
Index: libq.c |
16 |
=================================================================== |
17 |
RCS file: /var/cvsroot/gentoo-projects/portage-utils/libq/libq.c,v |
18 |
retrieving revision 1.21 |
19 |
retrieving revision 1.22 |
20 |
diff -u -r1.21 -r1.22 |
21 |
--- libq.c 19 Jul 2010 00:25:13 -0000 1.21 |
22 |
+++ libq.c 23 Feb 2011 08:59:45 -0000 1.22 |
23 |
@@ -23,6 +23,7 @@ |
24 |
#include "safe_io.c" |
25 |
#include "xchdir.c" |
26 |
#include "xgetcwd.c" |
27 |
+#include "xmkdir.c" |
28 |
#include "xreadlink.c" |
29 |
#include "xsystem.c" |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
1.1 portage-utils/libq/xmkdir.c |
35 |
|
36 |
file : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/libq/xmkdir.c?rev=1.1&view=markup |
37 |
plain: http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/libq/xmkdir.c?rev=1.1&content-type=text/plain |
38 |
|
39 |
Index: xmkdir.c |
40 |
=================================================================== |
41 |
/* Emulate `mkdir -p -m MODE PATH` */ |
42 |
static int mkdir_p(const char *path, mode_t mode) |
43 |
{ |
44 |
char *_p, *p, *s; |
45 |
|
46 |
_p = p = xstrdup(path); |
47 |
|
48 |
while (1) { |
49 |
/* Skip duplicate slashes */ |
50 |
while (*p == '/') |
51 |
++p; |
52 |
|
53 |
/* Find the next path element */ |
54 |
s = strchr(p, '/'); |
55 |
if (!s) |
56 |
break; |
57 |
|
58 |
/* Make it */ |
59 |
*s = '\0'; |
60 |
mkdir(_p, mode); |
61 |
*s = '/'; |
62 |
|
63 |
p = s; |
64 |
} |
65 |
|
66 |
free(_p); |
67 |
|
68 |
return 0; |
69 |
} |
70 |
|
71 |
/* Emulate `rm -rf PATH` */ |
72 |
static int _rm_rf_subdir(int dfd, const char *path) |
73 |
{ |
74 |
int subdfd; |
75 |
DIR *dir; |
76 |
struct dirent *de; |
77 |
|
78 |
subdfd = openat(dfd, path, O_RDONLY|O_CLOEXEC|O_NOFOLLOW); |
79 |
if (subdfd < 0) |
80 |
return -1; |
81 |
|
82 |
dir = fdopendir(subdfd); |
83 |
if (!dir) { |
84 |
close(subdfd); |
85 |
return -1; |
86 |
} |
87 |
|
88 |
while ((de = readdir(dir)) != NULL) { |
89 |
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) |
90 |
continue; |
91 |
if (unlinkat(subdfd, de->d_name, 0) == -1) { |
92 |
if (errno != EISDIR) |
93 |
errp("could not unlink %s", de->d_name); |
94 |
_rm_rf_subdir(subdfd, de->d_name); |
95 |
unlinkat(subdfd, de->d_name, AT_REMOVEDIR); |
96 |
} |
97 |
} |
98 |
|
99 |
/* this also does close(subdfd); */ |
100 |
closedir(dir); |
101 |
|
102 |
return 0; |
103 |
} |
104 |
|
105 |
static int rm_rf(const char *path) |
106 |
{ |
107 |
_rm_rf_subdir(AT_FDCWD, path); |
108 |
|
109 |
if (rmdir(path) == 0) |
110 |
return 0; |
111 |
|
112 |
/* if path is a symlink, unlink it */ |
113 |
if (unlink(path) == 0) |
114 |
return 0; |
115 |
|
116 |
/* XXX: we don't handle: |
117 |
* trailing slashes: `rm -rf a/b/c/` -> need to change to a/b/c */ |
118 |
return -1; |
119 |
} |