]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
repart: Open files in context_minimize()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 28 Jul 2023 15:44:03 +0000 (17:44 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 1 Aug 2023 05:53:34 +0000 (07:53 +0200)
Instead of relying on context_open_copy_blocks_paths() to do the
opening, let's just do it in context_minimize() itself as it's trivial.

src/partition/repart.c

index 6c39ee804c200734c82e9d22e49a54f1d2cb785d..dadaae545f21489deed5c09cdddc2132282c2910 100644 (file)
@@ -5760,6 +5760,12 @@ static int context_minimize(Context *context) {
                 if (fstype_is_ro(p->format)) {
                         struct stat st;
 
+                        if (fd < 0) {
+                                fd = open(temp, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
+                                if (fd < 0)
+                                        return log_error_errno(errno, "Failed to open temporary file %s: %m", temp);
+                        }
+
                         if (stat(temp, &st) < 0)
                                 return log_error_errno(errno, "Failed to stat temporary file: %m");
 
@@ -5768,6 +5774,8 @@ static int context_minimize(Context *context) {
 
                         p->copy_blocks_path = TAKE_PTR(temp);
                         p->copy_blocks_path_is_our_file = true;
+                        p->copy_blocks_fd = TAKE_FD(fd);
+                        p->copy_blocks_size = st.st_size;
                         continue;
                 }
 
@@ -5832,6 +5840,8 @@ static int context_minimize(Context *context) {
 
                 p->copy_blocks_path = TAKE_PTR(temp);
                 p->copy_blocks_path_is_our_file = true;
+                p->copy_blocks_fd = TAKE_FD(fd);
+                p->copy_blocks_size = fsz;
         }
 
         /* Now that we've done the data partitions, do the verity hash partitions. We do these in a separate
@@ -5840,6 +5850,7 @@ static int context_minimize(Context *context) {
         LIST_FOREACH(partitions, p, context->partitions) {
                 _cleanup_(unlink_and_freep) char *temp = NULL;
                 _cleanup_free_ char *hint = NULL;
+                _cleanup_close_ int fd = -EBADF;
                 struct stat st;
                 Partition *dp;
 
@@ -5882,7 +5893,11 @@ static int context_minimize(Context *context) {
                 if (r < 0)
                         return r;
 
-                if (stat(temp, &st) < 0)
+                fd = open(temp, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
+                if (fd < 0)
+                        return log_error_errno(errno, "Failed to open temporary file %s: %m", temp);
+
+                if (fstat(fd, &st) < 0)
                         return log_error_errno(r, "Failed to stat temporary file: %m");
 
                 log_info("Minimal partition size of verity hash partition %s is %s",
@@ -5890,6 +5905,8 @@ static int context_minimize(Context *context) {
 
                 p->copy_blocks_path = TAKE_PTR(temp);
                 p->copy_blocks_path_is_our_file = true;
+                p->copy_blocks_fd = TAKE_FD(fd);
+                p->copy_blocks_size = st.st_size;
         }
 
         return 0;
@@ -7004,12 +7021,6 @@ static int run(int argc, char *argv[]) {
         if (r < 0)
                 return r;
 
-        /* We might have gotten more copy blocks paths to open during the minimize process, so let's make
-         * sure we open those as well. These should all be regular files, so don't allow any block devices. */
-        r = context_open_copy_block_paths(context, 0);
-        if (r < 0)
-                return r;
-
         if (arg_size_auto) {
                 r = determine_auto_size(context);
                 if (r < 0)