valeriyvdovin / rpms / systemd

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