chiark / gitweb /
import: support downloading bzip2-encoded images
[elogind.git] / src / import / import.c
index c0fc22427e38bfb3756eeb85f5021f9d1312807e..b3ebb353570fa09608878ff84e5859171c290037 100644 (file)
 #include "import-tar.h"
 #include "import-raw.h"
 #include "import-dkr.h"
+#include "import-util.h"
 
 static bool arg_force = false;
 static const char *arg_image_root = "/var/lib/machines";
-
+static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE;
 static const char* arg_dkr_index_url = DEFAULT_DKR_INDEX_URL;
 
 static void on_tar_finished(TarImport *import, int error, void *userdata) {
@@ -41,34 +42,8 @@ static void on_tar_finished(TarImport *import, int error, void *userdata) {
 
         if (error == 0)
                 log_info("Operation completed successfully.");
-        else
-                log_error_errno(error, "Operation failed: %m");
 
-        sd_event_exit(event, error);
-}
-
-static int url_final_component(const char *url, char **ret) {
-        const char *e, *p;
-        char *s;
-
-        e = strchrnul(url, '?');
-
-        while (e > url && e[-1] == '/')
-                        e--;
-
-        p = e;
-        while (p > url && p[-1] != '/')
-                p--;
-
-        if (e <= p)
-                return -EINVAL;
-
-        s = strndup(p, e - p);
-        if (!s)
-                return -ENOMEM;
-
-        *ret = s;
-        return 0;
+        sd_event_exit(event, EXIT_FAILURE);
 }
 
 static int strip_tar_suffixes(const char *name, char **ret) {
@@ -76,10 +51,12 @@ static int strip_tar_suffixes(const char *name, char **ret) {
         char *s;
 
         e = endswith(name, ".tar");
+        if (!e)
+                e = endswith(name, ".tar.xz");
         if (!e)
                 e = endswith(name, ".tar.gz");
         if (!e)
-                e = endswith(name, ".tar.xz");
+                e = endswith(name, ".tar.bz2");
         if (!e)
                 e = endswith(name, ".tgz");
         if (!e)
@@ -112,7 +89,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
         if (argc >= 3)
                 local = argv[2];
         else {
-                r = url_final_component(url, &l);
+                r = import_url_last_component(url, &l);
                 if (r < 0)
                         return log_error_errno(r, "Failed get final component of URL: %m");
 
@@ -160,7 +137,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return log_error_errno(r, "Failed to allocate importer: %m");
 
-        r = tar_import_pull(import, url, local, arg_force);
+        r = tar_import_pull(import, url, local, arg_force, arg_verify);
         if (r < 0)
                 return log_error_errno(r, "Failed to pull image: %m");
 
@@ -170,7 +147,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
 
         log_info("Exiting.");
 
-        return 0;
+        return r;
 }
 
 static void on_raw_finished(RawImport *import, int error, void *userdata) {
@@ -179,18 +156,19 @@ static void on_raw_finished(RawImport *import, int error, void *userdata) {
 
         if (error == 0)
                 log_info("Operation completed successfully.");
-        else
-                log_error_errno(error, "Operation failed: %m");
 
-        sd_event_exit(event, error);
+        sd_event_exit(event, EXIT_FAILURE);
 }
 
 static int strip_raw_suffixes(const char *p, char **ret) {
         static const char suffixes[] =
                 ".xz\0"
+                ".gz\0"
+                ".bz2\0"
                 ".raw\0"
                 ".qcow2\0"
-                ".img\0";
+                ".img\0"
+                ".bin\0";
 
         _cleanup_free_ char *q = NULL;
 
@@ -226,7 +204,7 @@ 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;
-        _cleanup_free_ char *l = NULL;
+        _cleanup_free_ char *l = NULL, *ll = NULL;
         int r;
 
         url = argv[1];
@@ -238,44 +216,37 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
         if (argc >= 3)
                 local = argv[2];
         else {
-                const char *e, *p;
-
-                e = url + strlen(url);
-                while (e > url && e[-1] == '/')
-                        e--;
-
-                p = e;
-                while (p > url && p[-1] != '/')
-                        p--;
+                r = import_url_last_component(url, &l);
+                if (r < 0)
+                        return log_error_errno(r, "Failed get final component of URL: %m");
 
-                local = strndupa(p, e - p);
+                local = l;
         }
 
         if (isempty(local) || streq(local, "-"))
                 local = NULL;
 
         if (local) {
-                const char *p;
-
-                r = strip_raw_suffixes(local, &l);
+                r = strip_raw_suffixes(local, &ll);
                 if (r < 0)
                         return log_oom();
 
-                local = l;
+                local = ll;
 
                 if (!machine_name_is_valid(local)) {
                         log_error("Local image name '%s' is not valid.", local);
                         return -EINVAL;
                 }
 
-                p = strappenda(arg_image_root, "/", local, ".raw");
-                if (laccess(p, F_OK) >= 0) {
-                        if (!arg_force) {
-                                log_info("Image '%s' already exists.", local);
-                                return 0;
+                if (!arg_force) {
+                        r = image_find(local, NULL);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
+                        else if (r > 0) {
+                                log_error_errno(EEXIST, "Image '%s' already exists.", local);
+                                return -EEXIST;
                         }
-                } else if (errno != ENOENT)
-                        return log_error_errno(errno, "Can't check if image '%s' already exists: %m", local);
+                }
 
                 log_info("Pulling '%s', saving as '%s'.", url, local);
         } else
@@ -293,7 +264,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return log_error_errno(r, "Failed to allocate importer: %m");
 
-        r = raw_import_pull(import, url, local, arg_force);
+        r = raw_import_pull(import, url, local, arg_force, arg_verify);
         if (r < 0)
                 return log_error_errno(r, "Failed to pull image: %m");
 
@@ -303,7 +274,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
 
         log_info("Exiting.");
 
-        return 0;
+        return r;
 }
 
 static void on_dkr_finished(DkrImport *import, int error, void *userdata) {
@@ -329,6 +300,11 @@ static int pull_dkr(int argc, char *argv[], void *userdata) {
                 return -EINVAL;
         }
 
+        if (arg_verify != IMPORT_VERIFY_NO) {
+                log_error("Imports from dkr do not support image verification, please pass --verify=no.");
+                return -EINVAL;
+        }
+
         tag = strchr(argv[1], ':');
         if (tag) {
                 name = strndupa(argv[1], tag - argv[1]);
@@ -414,10 +390,12 @@ static int help(int argc, char *argv[], void *userdata) {
                "  -h --help                   Show this help\n"
                "     --version                Show package version\n"
                "     --force                  Force creation of image\n"
+               "     --verify=                Verify downloaded image, one of: 'no', 'sum'\n"
+               "                              'signature'.\n"
                "     --image-root=            Image root directory\n"
                "     --dkr-index-url=URL      Specify index URL to use for downloads\n\n"
                "Commands:\n"
-               "  pull-tar URL                Download a TAR image\n"
+               "  pull-tar URL [NAME]         Download a TAR image\n"
                "  pull-raw URL [NAME]         Download a RAW image\n"
                "  pull-dkr REMOTE [NAME]      Download a DKR image\n",
                program_invocation_short_name);
@@ -432,6 +410,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_FORCE,
                 ARG_DKR_INDEX_URL,
                 ARG_IMAGE_ROOT,
+                ARG_VERIFY,
         };
 
         static const struct option options[] = {
@@ -440,6 +419,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "force",           no_argument,       NULL, ARG_FORCE           },
                 { "dkr-index-url",   required_argument, NULL, ARG_DKR_INDEX_URL   },
                 { "image-root",      required_argument, NULL, ARG_IMAGE_ROOT      },
+                { "verify",          required_argument, NULL, ARG_VERIFY          },
                 {}
         };
 
@@ -477,6 +457,15 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_image_root = optarg;
                         break;
 
+                case ARG_VERIFY:
+                        arg_verify = import_verify_from_string(optarg);
+                        if (arg_verify < 0) {
+                                log_error("Invalid verification setting '%s'", optarg);
+                                return -EINVAL;
+                        }
+
+                        break;
+
                 case '?':
                         return -EINVAL;