]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/import/import-tar.c
tree-wide: replace strjoin() with path_join()
[thirdparty/systemd.git] / src / import / import-tar.c
index 09c7654adca567431068ba605242e1ba200d3037..f3eff82a2990b735b6237ef11bf44c7f8cd2fb7f 100644 (file)
@@ -1,22 +1,4 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2015 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
 
 #include <linux/fs.h>
 
@@ -42,6 +24,7 @@
 #include "ratelimit.h"
 #include "rm-rf.h"
 #include "string-util.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 struct TarImport {
@@ -55,7 +38,6 @@ struct TarImport {
         char *local;
         bool force_local;
         bool read_only;
-        bool grow_machine_directory;
 
         char *temp_path;
         char *final_path;
@@ -65,8 +47,6 @@ struct TarImport {
 
         ImportCompress compress;
 
-        uint64_t written_since_last_grow;
-
         sd_event_source *input_event_source;
 
         uint8_t buffer[16*1024];
@@ -119,26 +99,29 @@ int tar_import_new(
                 void *userdata) {
 
         _cleanup_(tar_import_unrefp) TarImport *i = NULL;
+        _cleanup_free_ char *root = NULL;
         int r;
 
         assert(ret);
 
-        i = new0(TarImport, 1);
+        root = strdup(image_root ?: "/var/lib/machines");
+        if (!root)
+                return -ENOMEM;
+
+        i = new(TarImport, 1);
         if (!i)
                 return -ENOMEM;
 
-        i->input_fd = i->tar_fd = -1;
-        i->on_finished = on_finished;
-        i->userdata = userdata;
+        *i = (TarImport) {
+                .input_fd = -1,
+                .tar_fd = -1,
+                .on_finished = on_finished,
+                .userdata = userdata,
+                .last_percent = (unsigned) -1,
+                .image_root = TAKE_PTR(root),
+        };
 
         RATELIMIT_INIT(i->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
-        i->last_percent = (unsigned) -1;
-
-        i->image_root = strdup(image_root ?: "/var/lib/machines");
-        if (!i->image_root)
-                return -ENOMEM;
-
-        i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
 
         if (event)
                 i->event = sd_event_ref(event);
@@ -148,8 +131,7 @@ int tar_import_new(
                         return r;
         }
 
-        *ret = i;
-        i = NULL;
+        *ret = TAKE_PTR(i);
 
         return 0;
 }
@@ -170,7 +152,7 @@ static void tar_import_report_progress(TarImport *i) {
         if (percent == i->last_percent)
                 return;
 
-        if (!ratelimit_test(&i->progress_rate_limit))
+        if (!ratelimit_below(&i->progress_rate_limit))
                 return;
 
         sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
@@ -194,8 +176,14 @@ static int tar_import_finish(TarImport *i) {
                 i->tar_pid = 0;
                 if (r < 0)
                         return r;
+                if (r != EXIT_SUCCESS)
+                        return -EPROTO;
         }
 
+        r = import_mangle_os_tree(i->temp_path);
+        if (r < 0)
+                return r;
+
         if (i->read_only) {
                 r = import_make_read_only(i->temp_path);
                 if (r < 0)
@@ -223,7 +211,7 @@ static int tar_import_fork_tar(TarImport *i) {
         assert(!i->temp_path);
         assert(i->tar_fd < 0);
 
-        i->final_path = strjoin(i->image_root, "/", i->local);
+        i->final_path = path_join(i->image_root, i->local);
         if (!i->final_path)
                 return log_oom();
 
@@ -253,17 +241,11 @@ static int tar_import_write(const void *p, size_t sz, void *userdata) {
         TarImport *i = userdata;
         int r;
 
-        if (i->grow_machine_directory && i->written_since_last_grow >= GROW_INTERVAL_BYTES) {
-                i->written_since_last_grow = 0;
-                grow_machine_directory();
-        }
-
         r = loop_write(i->tar_fd, p, sz, false);
         if (r < 0)
                 return r;
 
         i->written_uncompressed += sz;
-        i->written_since_last_grow += sz;
 
         return 0;
 }