From b0258cbcd89862b927707258c67f2eeac769adc2 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Wed, 22 Apr 2009 15:09:41 -0400 Subject: [PATCH] e2fsck: Fix warn_unused_result warnings from gcc Fixed a potential bug where by partial returns from the write(2) system call could lost characters to be sent to external progress bar display program. Signed-off-by: "Theodore Ts'o" --- e2fsck/e2fsck.h | 1 + e2fsck/unix.c | 2 +- e2fsck/util.c | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/e2fsck/e2fsck.h b/e2fsck/e2fsck.h index a159f2a..9a95cb0 100644 --- a/e2fsck/e2fsck.h +++ b/e2fsck/e2fsck.h @@ -509,6 +509,7 @@ extern void mtrace_print(char *mesg); extern blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name, io_manager manager); extern int ext2_file_type(unsigned int mode); +extern int write_all(int fd, char *buf, size_t count); /* unix.c */ extern void e2fsck_clear_progbar(e2fsck_t ctx); diff --git a/e2fsck/unix.c b/e2fsck/unix.c index 76baab7..e39afdd 100644 --- a/e2fsck/unix.c +++ b/e2fsck/unix.c @@ -488,7 +488,7 @@ static int e2fsck_update_progress(e2fsck_t ctx, int pass, if (ctx->progress_fd) { snprintf(buf, sizeof(buf), "%d %lu %lu %s\n", pass, cur, max, ctx->device_name); - write(ctx->progress_fd, buf, strlen(buf)); + write_all(ctx->progress_fd, buf, strlen(buf)); } else { percent = calc_percent(&e2fsck_tbl, pass, cur, max); e2fsck_simple_progress(ctx, ctx->device_name, diff --git a/e2fsck/util.c b/e2fsck/util.c index 6101d3c..78c053c 100644 --- a/e2fsck/util.c +++ b/e2fsck/util.c @@ -680,3 +680,26 @@ int check_for_modules(const char *fs_name) #endif /* __linux__ */ return (0); } + +/* + * Helper function that does the right thing if write returns a + * partial write, or an EGAIN/EINTR error. + */ +int write_all(int fd, char *buf, size_t count) +{ + ssize_t ret; + int c = 0; + + while (count > 0) { + ret = write(fd, buf, count); + if (ret < 0) { + if ((errno == EAGAIN) || (errno == EINTR)) + continue; + return -1; + } + count -= ret; + buf += ret; + c += ret; + } + return c; +} -- 1.8.3.1