Debian Codegraph @

src/bootctl/bootctl-cleanup.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "alloc-util.h"
6 #include "bootctl.h"
7 #include "bootctl-cleanup.h"
8 #include "bootctl-unlink.h"
9 #include "bootctl-util.h"
10 #include "bootspec.h"
11 #include "bootspec-util.h"
12 #include "chase.h"
13 #include "errno-util.h"
14 #include "fd-util.h"
15 #include "hashmap.h"
16 #include "log.h"
17 #include "path-util.h"
18 #include "recurse-dir.h"
19
20 static int list_remove_orphaned_file(
21 RecurseDirEvent event,
22 const char *path,
23 int dir_fd,
24 int inode_fd,
25 const struct dirent *de,
26 const struct statx *sx,
27 void *userdata) {
28
29 Hashmap *known_files = userdata;
30
31 assert(path);
32
33 if (event != RECURSE_DIR_ENTRY)
34 return RECURSE_DIR_CONTINUE;
35
36 if (hashmap_contains(known_files, path))
37 return RECURSE_DIR_CONTINUE; /* keep! */
38
39 if (arg_dry_run)
40 log_info("Would remove %s", path);
41 else if (unlinkat(dir_fd, de->d_name, 0) < 0)
42 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
43 "Failed to remove \"%s\", ignoring: %m", path);
44 else
45 log_info("Removed %s", path);
46
47 return RECURSE_DIR_CONTINUE;
48 }
49
50 static int cleanup_orphaned_files(
51 const BootConfig *config,
52 BootEntrySource source,
53 const char *root) {
54
55 _cleanup_hashmap_free_ Hashmap *known_files = NULL;
56 _cleanup_free_ char *full = NULL, *p = NULL;
57 _cleanup_close_ int dir_fd = -EBADF;
58 int r;
59
60 assert(config);
61 assert(root);
62
63 log_info("Cleaning %s", root);
64
65 r = settle_entry_token();
66 if (r < 0)
67 return r;
68
69 r = boot_config_count_known_files(config, source, &known_files);
70 if (r < 0)
71 return log_error_errno(r, "Failed to count files in %s: %m", root);
72
73 dir_fd = chase_and_open(arg_entry_token, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_TRIGGER_AUTOFS,
74 O_DIRECTORY|O_CLOEXEC, &full);
75 if (dir_fd == -ENOENT)
76 return 0;
77 if (dir_fd < 0)
78 return log_error_errno(dir_fd, "Failed to open '%s/%s': %m", root, skip_leading_slash(arg_entry_token));
79
80 p = path_join("/", arg_entry_token);
81 if (!p)
82 return log_oom();
83
84 r = recurse_dir(dir_fd, p, 0, UINT_MAX, RECURSE_DIR_SORT, list_remove_orphaned_file, known_files);
85 if (r < 0)
86 return log_error_errno(r, "Failed to cleanup %s: %m", full);
87
88 return r;
89 }
90
91 int verb_cleanup(int argc, char *argv[], uintptr_t _data, void *userdata) {
92 dev_t esp_devid = 0, xbootldr_devid = 0;
93 int r;
94
95 r = acquire_esp(/* unprivileged_mode= */ false,
96 /* graceful= */ false,
97 /* ret_fd= */ NULL,
98 /* ret_part= */ NULL,
99 /* ret_pstart= */ NULL,
100 /* ret_psize= */ NULL,
101 /* ret_uuid= */ NULL,
102 &esp_devid);
103 if (r < 0)
104 return r;
105
106 r = acquire_xbootldr(
107 /* unprivileged_mode= */ false,
108 /* ret_fd= */ NULL,
109 /* ret_uuid= */ NULL,
110 &xbootldr_devid);
111 if (r < 0)
112 return r;
113
114 _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL;
115 r = boot_config_load_and_select(&config, arg_root, arg_esp_path, esp_devid, arg_xbootldr_path, xbootldr_devid);
116 if (r < 0)
117 return r;
118
119 r = 0;
120 RET_GATHER(r, cleanup_orphaned_files(&config, BOOT_ENTRY_ESP, arg_esp_path));
121
122 if (arg_xbootldr_path && xbootldr_devid != esp_devid)
123 RET_GATHER(r, cleanup_orphaned_files(&config, BOOT_ENTRY_XBOOTLDR, arg_xbootldr_path));
124
125 return r;
126 }