84b277
From 64de3526cce9ef980b37fcb5c412a2734132ad47 Mon Sep 17 00:00:00 2001
84b277
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
84b277
Date: Mon, 11 Nov 2013 23:32:31 -0500
84b277
Subject: [PATCH] fsck: modernization
84b277
84b277
(cherry picked from commit e375825da0cbdbf3be755c277f9c0dec35b41a09)
84b277
84b277
Related: #1098310
84b277
---
84b277
 src/fsck/fsck.c | 43 ++++++++++++++++++-------------------------
84b277
 1 file changed, 18 insertions(+), 25 deletions(-)
84b277
84b277
diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c
84b277
index 96a79dd..9b4e555 100644
84b277
--- a/src/fsck/fsck.c
84b277
+++ b/src/fsck/fsck.c
84b277
@@ -176,7 +176,7 @@ static double percent(int pass, unsigned long cur, unsigned long max) {
84b277
 }
84b277
 
84b277
 static int process_progress(int fd) {
84b277
-        FILE *f, *console;
84b277
+        _cleanup_fclose_ FILE *console = NULL, *f = NULL;
84b277
         usec_t last = 0;
84b277
         bool locked = false;
84b277
         int clear = 0;
84b277
@@ -188,15 +188,13 @@ static int process_progress(int fd) {
84b277
         }
84b277
 
84b277
         console = fopen("/dev/console", "w");
84b277
-        if (!console) {
84b277
-                fclose(f);
84b277
+        if (!console)
84b277
                 return -ENOMEM;
84b277
-        }
84b277
 
84b277
         while (!feof(f)) {
84b277
                 int pass, m;
84b277
                 unsigned long cur, max;
84b277
-                char *device;
84b277
+                _cleanup_free_ char *device = NULL;
84b277
                 double p;
84b277
                 usec_t t;
84b277
 
84b277
@@ -205,20 +203,16 @@ static int process_progress(int fd) {
84b277
 
84b277
                 /* Only show one progress counter at max */
84b277
                 if (!locked) {
84b277
-                        if (flock(fileno(console), LOCK_EX|LOCK_NB) < 0) {
84b277
-                                free(device);
84b277
+                        if (flock(fileno(console), LOCK_EX|LOCK_NB) < 0)
84b277
                                 continue;
84b277
-                        }
84b277
 
84b277
                         locked = true;
84b277
                 }
84b277
 
84b277
                 /* Only update once every 50ms */
84b277
                 t = now(CLOCK_MONOTONIC);
84b277
-                if (last + 50 * USEC_PER_MSEC > t)  {
84b277
-                        free(device);
84b277
+                if (last + 50 * USEC_PER_MSEC > t)
84b277
                         continue;
84b277
-                }
84b277
 
84b277
                 last = t;
84b277
 
84b277
@@ -226,8 +220,6 @@ static int process_progress(int fd) {
84b277
                 fprintf(console, "\r%s: fsck %3.1f%% complete...\r%n", device, p, &m);
84b277
                 fflush(console);
84b277
 
84b277
-                free(device);
84b277
-
84b277
                 if (m > clear)
84b277
                         clear = m;
84b277
         }
84b277
@@ -242,8 +234,6 @@ static int process_progress(int fd) {
84b277
                 fflush(console);
84b277
         }
84b277
 
84b277
-        fclose(f);
84b277
-        fclose(console);
84b277
         return 0;
84b277
 }
84b277
 
84b277
@@ -287,34 +277,37 @@ int main(int argc, char *argv[]) {
84b277
 
84b277
                 if (stat("/", &st) < 0) {
84b277
                         log_error("Failed to stat() the root directory: %m");
84b277
-                        goto finish;
84b277
+                        return EXIT_FAILURE;
84b277
                 }
84b277
 
84b277
                 /* Virtual root devices don't need an fsck */
84b277
                 if (major(st.st_dev) == 0)
84b277
-                        return 0;
84b277
+                        return EXIT_SUCCESS;
84b277
 
84b277
                 /* check if we are already writable */
84b277
                 times[0] = st.st_atim;
84b277
                 times[1] = st.st_mtim;
84b277
                 if (utimensat(AT_FDCWD, "/", times, 0) == 0) {
84b277
                         log_info("Root directory is writable, skipping check.");
84b277
-                        return 0;
84b277
+                        return EXIT_SUCCESS;
84b277
                 }
84b277
 
84b277
-                if (!(udev = udev_new())) {
84b277
+                udev = udev_new();
84b277
+                if (!udev) {
84b277
                         log_oom();
84b277
-                        goto finish;
84b277
+                        return EXIT_FAILURE;
84b277
                 }
84b277
 
84b277
-                if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev))) {
84b277
+                udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev);
84b277
+                if (!udev_device) {
84b277
                         log_error("Failed to detect root device.");
84b277
-                        goto finish;
84b277
+                        return EXIT_FAILURE;
84b277
                 }
84b277
 
84b277
-                if (!(device = udev_device_get_devnode(udev_device))) {
84b277
+                device = udev_device_get_devnode(udev_device);
84b277
+                if (!device) {
84b277
                         log_error("Failed to detect device node of root directory.");
84b277
-                        goto finish;
84b277
+                        return EXIT_FAILURE;
84b277
                 }
84b277
 
84b277
                 root_directory = true;
84b277
@@ -323,7 +316,7 @@ int main(int argc, char *argv[]) {
84b277
         if (arg_show_progress)
84b277
                 if (pipe(progress_pipe) < 0) {
84b277
                         log_error("pipe(): %m");
84b277
-                        goto finish;
84b277
+                        return EXIT_FAILURE;
84b277
                 }
84b277
 
84b277
         cmdline[i++] = "/sbin/fsck";