From: Allison Karlitskaya Date: Mon, 25 Nov 2024 08:46:30 +0000 (+0100) Subject: mke2fs: factor out 'write_all()' functionality X-Git-Tag: v1.47.3-rc1~28 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=167719dce93fb34cba459298ddfad32cf2c2e73a;p=thirdparty%2Fe2fsprogs.git 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 --- diff --git a/misc/create_inode.c b/misc/create_inode.c index ca03c184..2e58f615 100644 --- a/misc/create_inode.c +++ b/misc/create_inode.c @@ -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: