]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/import-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 Lennart Poettering
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.
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.
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/>.
22 #include "btrfs-util.h"
24 #include "import-util.h"
26 int import_url_last_component(const char *url
, char **ret
) {
30 e
= strchrnul(url
, '?');
32 while (e
> url
&& e
[-1] == '/')
36 while (p
> url
&& p
[-1] != '/')
42 s
= strndup(p
, e
- p
);
51 int import_url_change_last_component(const char *url
, const char *suffix
, char **ret
) {
58 e
= strchrnul(url
, '?');
60 while (e
> url
&& e
[-1] == '/')
63 while (e
> url
&& e
[-1] != '/')
69 s
= new(char, (e
- url
) + strlen(suffix
) + 1);
73 strcpy(mempcpy(s
, url
, e
- url
), suffix
);
78 static const char* const import_verify_table
[_IMPORT_VERIFY_MAX
] = {
79 [IMPORT_VERIFY_NO
] = "no",
80 [IMPORT_VERIFY_CHECKSUM
] = "checksum",
81 [IMPORT_VERIFY_SIGNATURE
] = "signature",
84 DEFINE_STRING_TABLE_LOOKUP(import_verify
, ImportVerify
);
86 int tar_strip_suffixes(const char *name
, char **ret
) {
90 e
= endswith(name
, ".tar");
92 e
= endswith(name
, ".tar.xz");
94 e
= endswith(name
, ".tar.gz");
96 e
= endswith(name
, ".tar.bz2");
98 e
= endswith(name
, ".tgz");
105 s
= strndup(name
, e
- name
);
113 int raw_strip_suffixes(const char *p
, char **ret
) {
115 static const char suffixes
[] =
124 _cleanup_free_
char *q
= NULL
;
132 bool changed
= false;
134 NULSTR_FOREACH(sfx
, suffixes
) {
137 e
= endswith(q
, sfx
);
154 bool dkr_digest_is_valid(const char *digest
) {
155 /* 7 chars for prefix, 64 chars for the digest itself */
156 if (strlen(digest
) != 71)
159 return startswith(digest
, "sha256:") && in_charset(digest
+ 7, "0123456789abcdef");
162 bool dkr_ref_is_valid(const char *ref
) {
168 colon
= strchr(ref
, ':');
170 return filename_is_valid(ref
);
172 return dkr_digest_is_valid(ref
);
175 bool dkr_name_is_valid(const char *name
) {
176 const char *slash
, *p
;
181 slash
= strchr(name
, '/');
185 if (!filename_is_valid(slash
+ 1))
188 p
= strndupa(name
, slash
- name
);
189 if (!filename_is_valid(p
))
195 bool dkr_id_is_valid(const char *id
) {
197 if (!filename_is_valid(id
))
200 if (!in_charset(id
, "0123456789abcdef"))
206 int import_assign_pool_quota_and_warn(const char *path
) {
209 r
= btrfs_subvol_auto_qgroup("/var/lib/machines", 0, true);
211 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.");
215 return log_error_errno(r
, "Failed to set up default quota hierarchy for /var/lib/machines: %m");
217 log_info("Set up default quota hierarchy for /var/lib/machines.");
219 r
= btrfs_subvol_auto_qgroup(path
, 0, true);
221 log_debug_errno(r
, "Failed to set up quota hierarchy for %s, as directory is not on btrfs or not a subvolume. Ignoring.", path
);
225 return log_error_errno(r
, "Failed to set up default quota hierarchy for %s: %m", path
);
227 log_info("Set up default quota hierarchy for %s.", path
);