Debian Codegraph @

delete.c

raw symbols SCIP
1 /*
2 * Deletion routines used in rsync.
3 *
4 * Copyright (C) 1996-2000 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2002 Martin Pool <[email protected]>
7 * Copyright (C) 2003-2024 Wayne Davison
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
21 */
22
23 #include "rsync.h"
24
25 extern int am_root;
26 extern int make_backups;
27 extern int max_delete;
28 extern char *backup_dir;
29 extern char *backup_suffix;
30 extern int backup_suffix_len;
31 extern struct stats stats;
32
33 int ignore_perishable = 0;
34 int non_perishable_cnt = 0;
35 int skipped_deletes = 0;
36
37 /* Held fd of the directory whose contents delete_dir_contents() is currently
38 * removing, so delete_item()'s per-entry rmdir/unlink/chmod go through it
39 * instead of re-resolving the full path for every entry. Set (with save/
40 * restore across the recursion) around the delete loop; -1 outside a recursive
41 * delete or when the secure resolver is gated off (chroot / non-receiver) or
42 * the path doesn't live directly in that dir. */
43 static int del_dirfd = -1;
44 static const char *del_dir_prefix;
45 static int del_dir_prefix_len;
46
47 /* If `path` is a single component directly inside the dir being deleted,
48 * point *leaf at its basename and return the held dir fd; else return -1. */
49 static int del_held_dfd(const char *path, const char **leaf)
50 {
51 if (del_dirfd >= 0
52 && strncmp(path, del_dir_prefix, del_dir_prefix_len) == 0
53 && path[del_dir_prefix_len] == '/'
54 && strchr(path + del_dir_prefix_len + 1, '/') == NULL) {
55 *leaf = path + del_dir_prefix_len + 1;
56 return del_dirfd;
57 }
58 return -1;
59 }
60
61 static void del_chmod(const char *fbuf, mode_t mode)
62 {
63 const char *leaf;
64 int dfd = del_held_dfd(fbuf, &leaf);
65 if (dfd >= 0)
66 do_chmod_atfd(dfd, leaf, mode);
67 else
68 do_chmod_at(fbuf, mode);
69 }
70
71 static int del_unlink(const char *fbuf)
72 {
73 const char *leaf;
74 int dfd = del_held_dfd(fbuf, &leaf);
75 if (dfd >= 0 && do_unlink_atfd(dfd, leaf, 0) == 0)
76 return 0;
77 return robust_unlink(fbuf); /* fall back (ETXTBSY retry, or not held) */
78 }
79
80 static inline int is_backup_file(char *fn)
81 {
82 int k = strlen(fn) - backup_suffix_len;
83 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
84 }
85
86 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
87 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
88 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
89 * buffer is used for recursion, but returned unchanged.)
90 */
91 static enum delret delete_dir_contents(char *fname, uint16 flags)
92 {
93 struct file_list *dirlist;
94 enum delret ret;
95 unsigned remainder;
96 void *save_filters;
97 int j, dlen;
98 char *p;
99
100 if (DEBUG_GTE(DEL, 3)) {
101 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
102 fname, flags);
103 }
104
105 dlen = strlen(fname);
106 save_filters = push_local_filters(fname, dlen);
107
108 non_perishable_cnt = 0;
109 dirlist = get_dirlist(fname, dlen, 0);
110 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
111
112 if (!dirlist->used)
113 goto done;
114
115 if (!(flags & DEL_RECURSE)) {
116 ret = DR_NOT_EMPTY;
117 goto done;
118 }
119
120 p = fname + dlen;
121 if (dlen != 1 || *fname != '/')
122 *p++ = '/';
123 remainder = MAXPATHLEN - (p - fname);
124
125 /* We do our own recursion, so make delete_item() non-recursive. */
126 flags = (flags & ~(DEL_RECURSE|DEL_MAKE_ROOM|DEL_NO_UID_WRITE))
127 | DEL_DIR_IS_EMPTY;
128
129 /* Hold this dir open so the per-entry chmod/rmdir/unlink below (and in
130 * delete_item) become *at() calls against it rather than re-resolving the
131 * full path for every entry. Save/restore around the recursion. */
132 int save_del_dirfd = del_dirfd;
133 const char *save_del_prefix = del_dir_prefix;
134 int save_del_prefix_len = del_dir_prefix_len;
135 fname[dlen] = '\0';
136 del_dirfd = open_dir_secure(fname);
137 fname[dlen] = '/';
138 del_dir_prefix = fname;
139 del_dir_prefix_len = dlen;
140
141 for (j = dirlist->used; j--; ) {
142 struct file_struct *fp = dirlist->files[j];
143
144 if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
145 if (DEBUG_GTE(DEL, 1)) {
146 rprintf(FINFO,
147 "mount point, %s, pins parent directory\n",
148 f_name(fp, NULL));
149 }
150 ret = DR_NOT_EMPTY;
151 continue;
152 }
153
154 strlcpy(p, fp->basename, remainder);
155 if (!(fp->mode & S_IWUSR) && !am_root && fp->flags & FLAG_OWNED_BY_US)
156 del_chmod(fname, fp->mode | S_IWUSR);
157 /* Save stack by recursing to ourself directly. */
158 if (S_ISDIR(fp->mode)) {
159 if (delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
160 ret = DR_NOT_EMPTY;
161 }
162 if (delete_item(fname, fp->mode, flags) != DR_SUCCESS)
163 ret = DR_NOT_EMPTY;
164 }
165
166 if (del_dirfd >= 0)
167 close(del_dirfd);
168 del_dirfd = save_del_dirfd;
169 del_dir_prefix = save_del_prefix;
170 del_dir_prefix_len = save_del_prefix_len;
171
172 fname[dlen] = '\0';
173
174 done:
175 flist_free(dirlist);
176 pop_local_filters(save_filters);
177
178 if (ret == DR_NOT_EMPTY) {
179 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
180 fname);
181 }
182 return ret;
183 }
184
185 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
186 * delete recursively.
187 *
188 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
189 * a directory! (The buffer is used for recursion, but returned unchanged.)
190 */
191 enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
192 {
193 enum delret ret;
194 char *what;
195 int ok;
196
197 if (DEBUG_GTE(DEL, 2)) {
198 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
199 fbuf, (int)mode, (int)flags);
200 }
201
202 if (flags & DEL_NO_UID_WRITE)
203 del_chmod(fbuf, mode | S_IWUSR);
204
205 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
206 /* This only happens on the first call to delete_item() since
207 * delete_dir_contents() always calls us w/DEL_DIR_IS_EMPTY. */
208 ignore_perishable = 1;
209 /* If DEL_RECURSE is not set, this just reports emptiness. */
210 ret = delete_dir_contents(fbuf, flags);
211 ignore_perishable = 0;
212 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
213 goto check_ret;
214 /* OK: try to delete the directory. */
215 }
216
217 if (!(flags & DEL_MAKE_ROOM) && max_delete >= 0 && stats.deleted_files >= max_delete) {
218 skipped_deletes++;
219 return DR_AT_LIMIT;
220 }
221
222 if (S_ISDIR(mode)) {
223 const char *leaf;
224 int dfd = del_held_dfd(fbuf, &leaf);
225 what = "rmdir";
226 ok = (dfd >= 0 ? do_unlink_atfd(dfd, leaf, AT_REMOVEDIR) : do_rmdir_at(fbuf)) == 0;
227 } else {
228 if (make_backups > 0 && !(flags & DEL_FOR_BACKUP) && (backup_dir || !is_backup_file(fbuf))) {
229 what = "make_backup";
230 ok = make_backup(fbuf, True);
231 if (ok == 2) {
232 what = "unlink";
233 ok = del_unlink(fbuf) == 0;
234 }
235 } else {
236 what = "unlink";
237 ok = del_unlink(fbuf) == 0;
238 }
239 }
240
241 if (ok) {
242 if (!(flags & DEL_MAKE_ROOM)) {
243 log_delete(fbuf, mode);
244 stats.deleted_files++;
245 if (S_ISREG(mode)) {
246 /* Nothing more to count */
247 } else if (S_ISDIR(mode))
248 stats.deleted_dirs++;
249 #ifdef SUPPORT_LINKS
250 else if (S_ISLNK(mode))
251 stats.deleted_symlinks++;
252 #endif
253 else if (IS_DEVICE(mode))
254 stats.deleted_devices++;
255 else
256 stats.deleted_specials++;
257 }
258 ret = DR_SUCCESS;
259 } else {
260 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
261 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
262 fbuf);
263 ret = DR_NOT_EMPTY;
264 } else if (errno != ENOENT) {
265 rsyserr(FERROR_XFER, errno, "delete_file: %s(%s) failed",
266 what, fbuf);
267 ret = DR_FAILURE;
268 } else
269 ret = DR_SUCCESS;
270 }
271
272 check_ret:
273 if (ret != DR_SUCCESS && flags & DEL_MAKE_ROOM) {
274 const char *desc;
275 switch (flags & DEL_MAKE_ROOM) {
276 case DEL_FOR_FILE: desc = "regular file"; break;
277 case DEL_FOR_DIR: desc = "directory"; break;
278 case DEL_FOR_SYMLINK: desc = "symlink"; break;
279 case DEL_FOR_DEVICE: desc = "device file"; break;
280 case DEL_FOR_SPECIAL: desc = "special file"; break;
281 default: exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE */
282 }
283 rprintf(FERROR_XFER, "could not make way for %s %s: %s\n",
284 flags & DEL_FOR_BACKUP ? "backup" : "new",
285 desc, fbuf);
286 }
287 return ret;
288 }
289
290 uint16 get_del_for_flag(uint16 mode)
291 {
292 if (S_ISREG(mode))
293 return DEL_FOR_FILE;
294 if (S_ISDIR(mode))
295 return DEL_FOR_DIR;
296 if (S_ISLNK(mode))
297 return DEL_FOR_SYMLINK;
298 if (IS_DEVICE(mode))
299 return DEL_FOR_DEVICE;
300 if (IS_SPECIAL(mode))
301 return DEL_FOR_SPECIAL;
302 exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE */
303 }