]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/import-util.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / shared / import-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
3d7415f4
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2015 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
a8fbdf54
TA
21#include <errno.h>
22#include <string.h>
23
b5efdb8a 24#include "alloc-util.h"
8c9cfc28 25#include "btrfs-util.h"
bb15fafe 26#include "import-util.h"
a8fbdf54
TA
27#include "log.h"
28#include "macro.h"
bb15fafe 29#include "path-util.h"
8b43440b 30#include "string-table.h"
07630cea 31#include "string-util.h"
3d7415f4 32#include "util.h"
3d7415f4
LP
33
34int import_url_last_component(const char *url, char **ret) {
35 const char *e, *p;
36 char *s;
37
38 e = strchrnul(url, '?');
39
40 while (e > url && e[-1] == '/')
41 e--;
42
43 p = e;
44 while (p > url && p[-1] != '/')
45 p--;
46
47 if (e <= p)
48 return -EINVAL;
49
50 s = strndup(p, e - p);
51 if (!s)
52 return -ENOMEM;
53
54 *ret = s;
55 return 0;
56}
57
58
59int import_url_change_last_component(const char *url, const char *suffix, char **ret) {
60 const char *e;
61 char *s;
62
63 assert(url);
64 assert(ret);
65
66 e = strchrnul(url, '?');
67
68 while (e > url && e[-1] == '/')
69 e--;
70
71 while (e > url && e[-1] != '/')
72 e--;
73
74 if (e <= url)
75 return -EINVAL;
76
77 s = new(char, (e - url) + strlen(suffix) + 1);
78 if (!s)
79 return -ENOMEM;
80
81 strcpy(mempcpy(s, url, e - url), suffix);
82 *ret = s;
83 return 0;
84}
85
86static const char* const import_verify_table[_IMPORT_VERIFY_MAX] = {
87 [IMPORT_VERIFY_NO] = "no",
7f444afa 88 [IMPORT_VERIFY_CHECKSUM] = "checksum",
3d7415f4
LP
89 [IMPORT_VERIFY_SIGNATURE] = "signature",
90};
91
92DEFINE_STRING_TABLE_LOOKUP(import_verify, ImportVerify);
93
94int tar_strip_suffixes(const char *name, char **ret) {
95 const char *e;
96 char *s;
97
98 e = endswith(name, ".tar");
99 if (!e)
100 e = endswith(name, ".tar.xz");
101 if (!e)
102 e = endswith(name, ".tar.gz");
103 if (!e)
104 e = endswith(name, ".tar.bz2");
105 if (!e)
106 e = endswith(name, ".tgz");
107 if (!e)
108 e = strchr(name, 0);
109
110 if (e <= name)
111 return -EINVAL;
112
113 s = strndup(name, e - name);
114 if (!s)
115 return -ENOMEM;
116
117 *ret = s;
118 return 0;
119}
120
121int raw_strip_suffixes(const char *p, char **ret) {
122
123 static const char suffixes[] =
124 ".xz\0"
125 ".gz\0"
126 ".bz2\0"
127 ".raw\0"
128 ".qcow2\0"
129 ".img\0"
130 ".bin\0";
131
132 _cleanup_free_ char *q = NULL;
133
134 q = strdup(p);
135 if (!q)
136 return -ENOMEM;
137
138 for (;;) {
139 const char *sfx;
140 bool changed = false;
141
142 NULSTR_FOREACH(sfx, suffixes) {
143 char *e;
144
145 e = endswith(q, sfx);
146 if (e) {
147 *e = 0;
148 changed = true;
149 }
150 }
151
152 if (!changed)
153 break;
154 }
155
156 *ret = q;
157 q = NULL;
158
159 return 0;
160}
161
8c9cfc28
LP
162int import_assign_pool_quota_and_warn(const char *path) {
163 int r;
164
165 r = btrfs_subvol_auto_qgroup("/var/lib/machines", 0, true);
166 if (r == -ENOTTY) {
167 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.");
168 return 0;
169 }
170 if (r < 0)
171 return log_error_errno(r, "Failed to set up default quota hierarchy for /var/lib/machines: %m");
172 if (r > 0)
173 log_info("Set up default quota hierarchy for /var/lib/machines.");
174
175 r = btrfs_subvol_auto_qgroup(path, 0, true);
176 if (r == -ENOTTY) {
177 log_debug_errno(r, "Failed to set up quota hierarchy for %s, as directory is not on btrfs or not a subvolume. Ignoring.", path);
178 return 0;
179 }
180 if (r < 0)
181 return log_error_errno(r, "Failed to set up default quota hierarchy for %s: %m", path);
182 if (r > 0)
183 log_info("Set up default quota hierarchy for %s.", path);
184
185 return 0;
186}