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