]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/import/aufs-util.c
util: rework rm_rf() logic
[thirdparty/systemd.git] / src / import / aufs-util.c
CommitLineData
72648326
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <ftw.h>
23
24#include "util.h"
c6878637 25#include "rm-rf.h"
72648326
LP
26#include "aufs-util.h"
27
28static int nftw_cb(
29 const char *fpath,
30 const struct stat *sb,
31 int flag,
32 struct FTW *ftwbuf) {
33
34 const char *fn, *original;
35 char *p;
36 int r;
37
38 fn = fpath + ftwbuf->base;
39
40 /* We remove all whiteout files, and all whiteouts */
41
42 original = startswith(fn, ".wh.");
43 if (!original)
44 return FTW_CONTINUE;
45
46 log_debug("Removing whiteout indicator %s.", fpath);
c6878637 47 r = rm_rf(fpath, REMOVE_ROOT|REMOVE_PHYSICAL);
72648326
LP
48 if (r < 0)
49 return FTW_STOP;
50
51 if (!startswith(fn, ".wh..wh.")) {
52
53 p = alloca(ftwbuf->base + strlen(original));
54 strcpy(mempcpy(p, fpath, ftwbuf->base), original);
55
56 log_debug("Removing deleted file %s.", p);
c6878637 57 r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
72648326
LP
58 if (r < 0)
59 return FTW_STOP;
60 }
61
62 return FTW_CONTINUE;
63}
64
65int aufs_resolve(const char *path) {
66 int r;
67
68 errno = 0;
69 r = nftw(path, nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL);
70 if (r == FTW_STOP)
71 return errno ? -errno : -EIO;
72
73 return 0;
74}