chiark / gitweb /
import: properly remove pre-existing images if --force is used
[elogind.git] / src / import / import-dkr.c
index e7dbe0f6626e75c9f3ee4b8cb47a31b825aef651..fbd64122f9eff5aabd863ff90f5531e157ebebe4 100644 (file)
@@ -411,17 +411,17 @@ static void dkr_import_name_maybe_finish(DkrImportName *name) {
 
                 if (name->force_local) {
                         (void) btrfs_subvol_remove(p);
-                        (void) rm_rf(p, false, true, false);
+                        (void) rm_rf_dangerous(p, false, true, false);
                 }
 
                 r = btrfs_subvol_snapshot(q, p, false, false);
                 if (r < 0) {
-                        log_error_errno(r, "Failed to snapshot final image: %m");
+                        log_error_errno(r, "Failed to snapshot local image: %m");
                         dkr_import_finish(name->import, r);
                         return;
                 }
 
-                log_info("Created new image %s.", p);
+                log_info("Created new local image %s.", p);
         }
 
         dkr_import_finish(name->import, 0);
@@ -718,7 +718,7 @@ static void dkr_import_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result
         if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, &job) != CURLE_OK)
                 return;
 
-        if (!job)
+        if (!job || job->done)
                 return;
 
         job->done = true;
@@ -780,7 +780,7 @@ static void dkr_import_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result
                         goto fail;
                 }
 
-                r = btrfs_subvol_read_only(job->temp_path, true);
+                r = btrfs_subvol_set_read_only(job->temp_path, true);
                 if (r < 0) {
                         log_error_errno(r, "Failed to mark snapshot read-only: %m");
                         goto fail;
@@ -817,12 +817,17 @@ static size_t dkr_import_job_write_callback(void *contents, size_t size, size_t
         assert(contents);
         assert(j);
 
+        if (j->done) {
+                r = -ESTALE;
+                goto fail;
+        }
+
         if (j->tar_stream) {
                 size_t l;
 
                 l = fwrite(contents, size, nmemb, j->tar_stream);
                 if (l != nmemb) {
-                        r = -errno;
+                        r = log_error_errno(errno, "Failed to write to tar: %m");
                         goto fail;
                 }
 
@@ -830,13 +835,14 @@ static size_t dkr_import_job_write_callback(void *contents, size_t size, size_t
         }
 
         if (j->payload_size + sz > PAYLOAD_MAX) {
+                log_error("Payload too large.");
                 r = -EFBIG;
                 goto fail;
         }
 
         p = realloc(j->payload, j->payload_size + sz);
         if (!p) {
-                r = -ENOMEM;
+                r = log_oom();
                 goto fail;
         }
 
@@ -865,6 +871,11 @@ static size_t dkr_import_job_header_callback(void *contents, size_t size, size_t
         assert(contents);
         assert(j);
 
+        if (j->done) {
+                r = -ESTALE;
+                goto fail;
+        }
+
         r = curl_header_strdup(contents, sz, HEADER_TOKEN, &token);
         if (r < 0) {
                 log_oom();
@@ -1130,7 +1141,7 @@ int dkr_import_pull(DkrImport *import, const char *name, const char *tag, const
                 n->force_local = force_local;
         }
 
-        r = hashmap_put(import->names, name, n);
+        r = hashmap_put(import->names, n->name, n);
         if (r < 0)
                 return r;