From 9aaafa3a1eb6d1c6c67221fde346a522996d7fab Mon Sep 17 00:00:00 2001 From: Allison Karlitskaya Date: Mon, 25 Nov 2024 09:46:30 +0100 Subject: [PATCH] mke2fs: factor out 'write_all()' functionality 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 --- misc/create_inode.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/misc/create_inode.c b/misc/create_inode.c index f61d41163..2162b9789 100644 --- a/misc/create_inode.c +++ b/misc/create_inode.c @@ -430,6 +430,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) @@ -460,22 +480,9 @@ 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; - } } } fail: