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