Whamcloud - gitweb
e2fsck: Fix warn_unused_result warnings from gcc
authorTheodore Ts'o <tytso@mit.edu>
Wed, 22 Apr 2009 19:09:41 +0000 (15:09 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Wed, 22 Apr 2009 19:09:41 +0000 (15:09 -0400)
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" <tytso@mit.edu>
e2fsck/e2fsck.h
e2fsck/unix.c
e2fsck/util.c

index a159f2a..9a95cb0 100644 (file)
@@ -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);
index 76baab7..e39afdd 100644 (file)
@@ -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,
index 6101d3c..78c053c 100644 (file)
@@ -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;
+}