Blob Blame History Raw
From 64de3526cce9ef980b37fcb5c412a2734132ad47 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Mon, 11 Nov 2013 23:32:31 -0500
Subject: [PATCH] fsck: modernization

(cherry picked from commit e375825da0cbdbf3be755c277f9c0dec35b41a09)

Related: #1098310
---
 src/fsck/fsck.c | 43 ++++++++++++++++++-------------------------
 1 file changed, 18 insertions(+), 25 deletions(-)

diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c
index 96a79dd..9b4e555 100644
--- a/src/fsck/fsck.c
+++ b/src/fsck/fsck.c
@@ -176,7 +176,7 @@ static double percent(int pass, unsigned long cur, unsigned long max) {
 }
 
 static int process_progress(int fd) {
-        FILE *f, *console;
+        _cleanup_fclose_ FILE *console = NULL, *f = NULL;
         usec_t last = 0;
         bool locked = false;
         int clear = 0;
@@ -188,15 +188,13 @@ static int process_progress(int fd) {
         }
 
         console = fopen("/dev/console", "w");
-        if (!console) {
-                fclose(f);
+        if (!console)
                 return -ENOMEM;
-        }
 
         while (!feof(f)) {
                 int pass, m;
                 unsigned long cur, max;
-                char *device;
+                _cleanup_free_ char *device = NULL;
                 double p;
                 usec_t t;
 
@@ -205,20 +203,16 @@ static int process_progress(int fd) {
 
                 /* Only show one progress counter at max */
                 if (!locked) {
-                        if (flock(fileno(console), LOCK_EX|LOCK_NB) < 0) {
-                                free(device);
+                        if (flock(fileno(console), LOCK_EX|LOCK_NB) < 0)
                                 continue;
-                        }
 
                         locked = true;
                 }
 
                 /* Only update once every 50ms */
                 t = now(CLOCK_MONOTONIC);
-                if (last + 50 * USEC_PER_MSEC > t)  {
-                        free(device);
+                if (last + 50 * USEC_PER_MSEC > t)
                         continue;
-                }
 
                 last = t;
 
@@ -226,8 +220,6 @@ static int process_progress(int fd) {
                 fprintf(console, "\r%s: fsck %3.1f%% complete...\r%n", device, p, &m);
                 fflush(console);
 
-                free(device);
-
                 if (m > clear)
                         clear = m;
         }
@@ -242,8 +234,6 @@ static int process_progress(int fd) {
                 fflush(console);
         }
 
-        fclose(f);
-        fclose(console);
         return 0;
 }
 
@@ -287,34 +277,37 @@ int main(int argc, char *argv[]) {
 
                 if (stat("/", &st) < 0) {
                         log_error("Failed to stat() the root directory: %m");
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
                 /* Virtual root devices don't need an fsck */
                 if (major(st.st_dev) == 0)
-                        return 0;
+                        return EXIT_SUCCESS;
 
                 /* check if we are already writable */
                 times[0] = st.st_atim;
                 times[1] = st.st_mtim;
                 if (utimensat(AT_FDCWD, "/", times, 0) == 0) {
                         log_info("Root directory is writable, skipping check.");
-                        return 0;
+                        return EXIT_SUCCESS;
                 }
 
-                if (!(udev = udev_new())) {
+                udev = udev_new();
+                if (!udev) {
                         log_oom();
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
-                if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev))) {
+                udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev);
+                if (!udev_device) {
                         log_error("Failed to detect root device.");
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
-                if (!(device = udev_device_get_devnode(udev_device))) {
+                device = udev_device_get_devnode(udev_device);
+                if (!device) {
                         log_error("Failed to detect device node of root directory.");
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
                 root_directory = true;
@@ -323,7 +316,7 @@ int main(int argc, char *argv[]) {
         if (arg_show_progress)
                 if (pipe(progress_pipe) < 0) {
                         log_error("pipe(): %m");
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
         cmdline[i++] = "/sbin/fsck";