chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
util: make http url validity checks more generic, and move them to util.c
[elogind.git]
/
src
/
import
/
import.c
diff --git
a/src/import/import.c
b/src/import/import.c
index e457adf6294f640f24d49ed296011ecddf621572..9b10de55599abb54f16d9bbf9bdbb29891a5784c 100644
(file)
--- a/
src/import/import.c
+++ b/
src/import/import.c
@@
-45,14
+45,52
@@
static void on_raw_finished(RawImport *import, int error, void *userdata) {
sd_event_exit(event, error);
}
sd_event_exit(event, error);
}
+static int strip_raw_suffixes(const char *p, char **ret) {
+ static const char suffixes[] =
+ ".xz\0"
+ ".raw\0"
+ ".qcow2\0"
+ ".img\0";
+
+ _cleanup_free_ char *q = NULL;
+
+ q = strdup(p);
+ if (!q)
+ return -ENOMEM;
+
+ for (;;) {
+ const char *sfx;
+ bool changed = false;
+
+ NULSTR_FOREACH(sfx, suffixes) {
+ char *e;
+
+ e = endswith(q, sfx);
+ if (e) {
+ *e = 0;
+ changed = true;
+ }
+ }
+
+ if (!changed)
+ break;
+ }
+
+ *ret = q;
+ q = NULL;
+
+ return 0;
+}
+
static int pull_raw(int argc, char *argv[], void *userdata) {
_cleanup_(raw_import_unrefp) RawImport *import = NULL;
_cleanup_event_unref_ sd_event *event = NULL;
static int pull_raw(int argc, char *argv[], void *userdata) {
_cleanup_(raw_import_unrefp) RawImport *import = NULL;
_cleanup_event_unref_ sd_event *event = NULL;
- const char *url, *local, *suffix;
+ const char *url, *local;
+ _cleanup_free_ char *l = NULL;
int r;
url = argv[1];
int r;
url = argv[1];
- if (!
raw
_url_is_valid(url)) {
+ if (!
http
_url_is_valid(url)) {
log_error("URL '%s' is not valid.", url);
return -EINVAL;
}
log_error("URL '%s' is not valid.", url);
return -EINVAL;
}
@@
-79,13
+117,11
@@
static int pull_raw(int argc, char *argv[], void *userdata) {
if (local) {
const char *p;
if (local) {
const char *p;
- suffix = endswith(local, ".raw.xz");
- if (!suffix)
- suffix = endswith(local, ".raw");
- if (!suffix)
- suffix = endswith(local, ".xz");
- if (suffix)
- local = strndupa(local, suffix - local);
+ r = strip_raw_suffixes(local, &l);
+ if (r < 0)
+ return log_oom();
+
+ local = l;
if (!machine_name_is_valid(local)) {
log_error("Local image name '%s' is not valid.", local);
if (!machine_name_is_valid(local)) {
log_error("Local image name '%s' is not valid.", local);