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