forked from miekg/rdup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrm.c
117 lines (104 loc) · 2.7 KB
/
rm.c
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
/*
* Copyright (c) 2009 - 2011 Miek Gieben
* See LICENSE for the license
* rm.c remove an fs object (recursively)
*/
#include "rdup-up.h"
extern gboolean opt_dry;
extern gboolean opt_verbose;
/* ENOENT */
/* errno */
gboolean
rm(gchar *p)
{
int ret;
gchar *dirp, *q;
gchar *parent;
GDir *d;
struct stat st;
struct stat *st2;
if (opt_dry || !p)
return TRUE; /* noop */
if (lstat(p, &st) == -1) {
if (opt_verbose > 0 && errno != ENOENT)
msgd(__func__, __LINE__,_("Failed to remove `%s\': %s"), p, strerror(errno));
return TRUE; /* noop */
}
if (S_ISDIR(st.st_mode)) {
ret = remove(p);
if (ret == -1) {
switch(errno) {
case ENOTEMPTY:
/* recursive into this dir and do our bidding */
if (!(d = g_dir_open(p, 0, NULL))) {
msgd(__func__, __LINE__,_("Failed to open directory `%s\': %s"), p, strerror(errno));
return FALSE;
}
while ( (dirp = (gchar*)g_dir_read_name(d))) {
dirp = g_strdup_printf("%s/%s", p, dirp);
rm(dirp);
g_free(dirp);
}
g_dir_close(d);
/* dir should be empty by now */
if ((ret = remove(p)) == -1)
msgd(__func__, __LINE__,_("Failed to remove directory `%s\': %s"),
p, strerror(errno));
return TRUE;
case EACCES:
/* no write to dir, make writable */
parent = dir_parent(p);
st2 = dir_write(parent);
if (remove(p) == -1) {
msgd(__func__, __LINE__,_("Failed to remove `%s\': %s"),
p, strerror(errno));
dir_restore(parent, st2);
g_free(parent);
return FALSE;
}
dir_restore(parent, st2);
g_free(parent);
return TRUE;
default:
/* not ENOEMPTY */
msgd(__func__, __LINE__,_("Failed to remove directory `%s\': %s"), p, strerror(errno));
return FALSE;
}
}
return TRUE;
}
/* dirs are now handled */
if (remove(p) == -1) {
switch(errno) {
case EACCES:
/* we have no access, ok ... */
q = g_strdup(p);
parent = dirname(q);
st2 = dir_write(parent);
if (remove(p) == -1) {
msgd(__func__, __LINE__,_("Failed to remove `%s\': %s"), p, strerror(errno));
dir_restore(parent, st2);
g_free(q);
return FALSE;
}
dir_restore(parent, st2);
g_free(q);
return TRUE;
case EPERM:
/* no write on file, reuse st - and why is this needed again? */
/* this is dead code ... */
stat(p, &st);
chmod(p, st.st_mode | S_IWUSR);
if (remove(p) == -1) {
msgd(__func__, __LINE__,_("Failed to remove `%s\': %s"),
p, strerror(errno));
chmod(p, st.st_mode); /* is this usefull then? */
return FALSE;
}
return TRUE;
}
msgd(__func__, __LINE__,_("Failed to remove `%s\': %s"), p, strerror(errno));
return FALSE;
}
return TRUE;
}