Whamcloud - gitweb
mke2fs: factor out 'write_all()' functionality
authorAllison Karlitskaya <allison.karlitskaya@redhat.com>
Mon, 25 Nov 2024 08:46:30 +0000 (09:46 +0100)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 23 May 2025 17:53:16 +0000 (13:53 -0400)
When writing data to an inode (with mke2fs -d) we need to do the typical
loop to handle partial writes to make sure all of the data gets written.

Move that code to its own function.  This function also takes an offset
parameter, which makes it feel a bit like pwrite() (except that it does
modify the file offset).

Signed-off-by: Allison Karlitskaya <allison.karlitskaya@redhat.com>
misc/create_inode.c

index ca03c18..2e58f61 100644 (file)
@@ -431,6 +431,26 @@ static ssize_t my_pread(int fd, void *buf, size_t count, off_t offset)
 }
 #endif /* !defined HAVE_PREAD64 && !defined HAVE_PREAD */
 
+static errcode_t write_all(ext2_file_t e2_file, ext2_off_t off, const char *buf, unsigned int n_bytes) {
+       errcode_t err = ext2fs_file_llseek(e2_file, off, EXT2_SEEK_SET, NULL);
+       if (err)
+               return err;
+
+       const char *ptr = buf;
+       while (n_bytes) {
+               unsigned int written;
+               err = ext2fs_file_write(e2_file, ptr, n_bytes, &written);
+               if (err)
+                       return err;
+               if (written == 0)
+                       return EIO;
+               n_bytes -= written;
+               ptr += written;
+       }
+
+       return 0;
+}
+
 static errcode_t copy_file_chunk(ext2_filsys fs, int fd, ext2_file_t e2_file,
                                 off_t start, off_t end, char *buf,
                                 char *zerobuf)
@@ -461,22 +481,10 @@ static errcode_t copy_file_chunk(ext2_filsys fs, int fd, ext2_file_t e2_file,
                                ptr += blen;
                                continue;
                        }
-                       err = ext2fs_file_llseek(e2_file, off + bpos,
-                                                EXT2_SEEK_SET, NULL);
+                       err = write_all(e2_file, off + bpos, ptr, blen);
                        if (err)
                                goto fail;
-                       while (blen > 0) {
-                               err = ext2fs_file_write(e2_file, ptr, blen,
-                                                       &written);
-                               if (err)
-                                       goto fail;
-                               if (written == 0) {
-                                       err = EIO;
-                                       goto fail;
-                               }
-                               blen -= written;
-                               ptr += written;
-                       }
+                       ptr += blen;
                }
        }
 fail: