]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/import-util.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / shared / import-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
3d7415f4 2
a8fbdf54 3#include <errno.h>
a8fbdf54 4
b5efdb8a 5#include "alloc-util.h"
8c9cfc28 6#include "btrfs-util.h"
137c6c6b
LP
7#include "chattr-util.h"
8#include "errno-util.h"
bb15fafe 9#include "import-util.h"
a8fbdf54
TA
10#include "log.h"
11#include "macro.h"
d8b4d14d 12#include "nulstr-util.h"
bb15fafe 13#include "path-util.h"
8b43440b 14#include "string-table.h"
07630cea 15#include "string-util.h"
3d7415f4
LP
16
17int import_url_last_component(const char *url, char **ret) {
18 const char *e, *p;
19 char *s;
20
21 e = strchrnul(url, '?');
22
23 while (e > url && e[-1] == '/')
24 e--;
25
26 p = e;
27 while (p > url && p[-1] != '/')
28 p--;
29
30 if (e <= p)
31 return -EINVAL;
32
33 s = strndup(p, e - p);
34 if (!s)
35 return -ENOMEM;
36
37 *ret = s;
38 return 0;
39}
40
3d7415f4
LP
41int import_url_change_last_component(const char *url, const char *suffix, char **ret) {
42 const char *e;
43 char *s;
44
45 assert(url);
46 assert(ret);
47
48 e = strchrnul(url, '?');
49
50 while (e > url && e[-1] == '/')
51 e--;
52
53 while (e > url && e[-1] != '/')
54 e--;
55
56 if (e <= url)
57 return -EINVAL;
58
59 s = new(char, (e - url) + strlen(suffix) + 1);
60 if (!s)
61 return -ENOMEM;
62
63 strcpy(mempcpy(s, url, e - url), suffix);
64 *ret = s;
65 return 0;
66}
67
68static const char* const import_verify_table[_IMPORT_VERIFY_MAX] = {
69 [IMPORT_VERIFY_NO] = "no",
7f444afa 70 [IMPORT_VERIFY_CHECKSUM] = "checksum",
3d7415f4
LP
71 [IMPORT_VERIFY_SIGNATURE] = "signature",
72};
73
74DEFINE_STRING_TABLE_LOOKUP(import_verify, ImportVerify);
75
76int tar_strip_suffixes(const char *name, char **ret) {
77 const char *e;
78 char *s;
79
80 e = endswith(name, ".tar");
81 if (!e)
82 e = endswith(name, ".tar.xz");
83 if (!e)
84 e = endswith(name, ".tar.gz");
85 if (!e)
86 e = endswith(name, ".tar.bz2");
87 if (!e)
88 e = endswith(name, ".tgz");
89 if (!e)
90 e = strchr(name, 0);
91
92 if (e <= name)
93 return -EINVAL;
94
95 s = strndup(name, e - name);
96 if (!s)
97 return -ENOMEM;
98
99 *ret = s;
100 return 0;
101}
102
103int raw_strip_suffixes(const char *p, char **ret) {
104
105 static const char suffixes[] =
106 ".xz\0"
107 ".gz\0"
108 ".bz2\0"
109 ".raw\0"
110 ".qcow2\0"
111 ".img\0"
112 ".bin\0";
113
114 _cleanup_free_ char *q = NULL;
115
116 q = strdup(p);
117 if (!q)
118 return -ENOMEM;
119
120 for (;;) {
121 const char *sfx;
122 bool changed = false;
123
124 NULSTR_FOREACH(sfx, suffixes) {
125 char *e;
126
127 e = endswith(q, sfx);
128 if (e) {
129 *e = 0;
130 changed = true;
131 }
132 }
133
134 if (!changed)
135 break;
136 }
137
ae2a15bc 138 *ret = TAKE_PTR(q);
3d7415f4
LP
139
140 return 0;
141}
142
8c9cfc28
LP
143int import_assign_pool_quota_and_warn(const char *path) {
144 int r;
145
146 r = btrfs_subvol_auto_qgroup("/var/lib/machines", 0, true);
147 if (r == -ENOTTY) {
148 log_debug_errno(r, "Failed to set up default quota hierarchy for /var/lib/machines, as directory is not on btrfs or not a subvolume. Ignoring.");
149 return 0;
150 }
151 if (r < 0)
152 return log_error_errno(r, "Failed to set up default quota hierarchy for /var/lib/machines: %m");
153 if (r > 0)
154 log_info("Set up default quota hierarchy for /var/lib/machines.");
155
156 r = btrfs_subvol_auto_qgroup(path, 0, true);
157 if (r == -ENOTTY) {
158 log_debug_errno(r, "Failed to set up quota hierarchy for %s, as directory is not on btrfs or not a subvolume. Ignoring.", path);
159 return 0;
160 }
161 if (r < 0)
162 return log_error_errno(r, "Failed to set up default quota hierarchy for %s: %m", path);
163 if (r > 0)
b11591af 164 log_debug("Set up default quota hierarchy for %s.", path);
8c9cfc28
LP
165
166 return 0;
167}
137c6c6b
LP
168
169int import_set_nocow_and_log(int fd, const char *path) {
170 int r;
171
172 r = chattr_fd(fd, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
173 if (r < 0)
174 return log_full_errno(
175 ERRNO_IS_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING,
176 r, "Failed to set file attributes on %s: %m", path);
177
178 return 0;
179}