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