Skip to content

Commit

Permalink
mke2fs: factor out 'write_all()' functionality
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
allisonkarlitskaya committed Nov 25, 2024
1 parent a21b0bc commit 9aaafa3
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions misc/create_inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 9aaafa3

Please sign in to comment.