]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fsck: introduce string tables for fsck.mode= and fsck.repair=
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 12 Jul 2025 19:07:53 +0000 (04:07 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 15 Jul 2025 20:47:38 +0000 (05:47 +0900)
No functional change, just refactoring and preparation for later change.

src/fsck/fsck.c

index 9e4f7c66464a372f2ee7b3463a4596cdc3207329..7f2b0f5d36e2e362c4bb1fa860e2446fe68d1bdb 100644 (file)
 #include "socket-util.h"
 #include "special.h"
 #include "stdio-util.h"
+#include "string-table.h"
 #include "string-util.h"
 #include "time-util.h"
 
-static bool arg_skip = false;
-static bool arg_force = false;
-static const char *arg_repair = "-a";
+typedef enum FSCKMode {
+        FSCK_AUTO,
+        FSCK_FORCE,
+        FSCK_SKIP,
+        _FSCK_MODE_MAX,
+        _FSCK_MODE_INVALID = -EINVAL,
+} FSCKMode;
+
+typedef enum FSCKRepair {
+        FSCK_REPAIR_NO,
+        FSCK_REPAIR_YES,
+        FSCK_REPAIR_PREEN,
+        _FSCK_REPAIR_MAX,
+        _FSCK_REPAIR_INVALID = -EINVAL,
+} FSCKRepair;
+
+static FSCKMode arg_mode = FSCK_AUTO;
+static FSCKRepair arg_repair = FSCK_REPAIR_PREEN;
+
+static const char * const fsck_mode_table[_FSCK_MODE_MAX] = {
+        [FSCK_AUTO]  = "auto",
+        [FSCK_FORCE] = "force",
+        [FSCK_SKIP]  = "skip",
+};
+
+DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(fsck_mode, FSCKMode);
+
+static const char * const fsck_repair_table[_FSCK_REPAIR_MAX] = {
+        [FSCK_REPAIR_NO]    = "no",
+        [FSCK_REPAIR_YES]   = "yes",
+        [FSCK_REPAIR_PREEN] = "preen",
+};
+
+DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(fsck_repair, FSCKRepair, FSCK_REPAIR_YES);
+
+static const char * const fsck_repair_option_table[_FSCK_REPAIR_MAX] = {
+        [FSCK_REPAIR_NO]    = "-n",
+        [FSCK_REPAIR_YES]   = "-y",
+        [FSCK_REPAIR_PREEN] = "-a",
+};
+
+DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(fsck_repair_option, FSCKRepair);
 
 static void start_target(const char *target, const char *mode) {
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
@@ -59,8 +99,6 @@ static void start_target(const char *target, const char *mode) {
 }
 
 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
-        int r;
-
         assert(key);
 
         if (streq(key, "fsck.mode")) {
@@ -68,38 +106,25 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
                 if (proc_cmdline_value_missing(key, value))
                         return 0;
 
-                if (streq(value, "auto"))
-                        arg_force = arg_skip = false;
-                else if (streq(value, "force"))
-                        arg_force = true;
-                else if (streq(value, "skip"))
-                        arg_skip = true;
-                else
-                        log_warning("Invalid fsck.mode= parameter '%s'. Ignoring.", value);
+                arg_mode = fsck_mode_from_string(value);
+                if (arg_mode < 0)
+                        log_warning_errno(arg_mode, "Invalid fsck.mode= parameter, ignoring: %s", value);
 
         } else if (streq(key, "fsck.repair")) {
 
                 if (proc_cmdline_value_missing(key, value))
                         return 0;
 
-                if (streq(value, "preen"))
-                        arg_repair = "-a";
-                else {
-                        r = parse_boolean(value);
-                        if (r > 0)
-                                arg_repair = "-y";
-                        else if (r == 0)
-                                arg_repair = "-n";
-                        else
-                                log_warning("Invalid fsck.repair= parameter '%s'. Ignoring.", value);
-                }
+                arg_repair = fsck_repair_from_string(value);
+                if (arg_repair < 0)
+                        log_warning_errno(arg_repair, "Invalid fsck.repair= parameter, ignoring: %s", value);
         }
 
         else if (streq(key, "fastboot") && !value)
-                arg_skip = true;
+                arg_mode = FSCK_SKIP;
 
         else if (streq(key, "forcefsck") && !value)
-                arg_force = true;
+                arg_mode = FSCK_FORCE;
 
         return 0;
 }
@@ -233,7 +258,7 @@ static int run(int argc, char *argv[]) {
 
         bool show_progress = access("/run/systemd/show-status", F_OK) >= 0;
 
-        if (!arg_force && arg_skip)
+        if (arg_mode == FSCK_SKIP)
                 return 0;
 
         if (argc > 1) {
@@ -341,7 +366,7 @@ static int run(int argc, char *argv[]) {
                         dash_c[0] = 0;
 
                 cmdline[i++] = "fsck";
-                cmdline[i++] =  arg_repair;
+                cmdline[i++] = fsck_repair_option_to_string(arg_repair);
                 cmdline[i++] = "-T";
 
                 /*
@@ -354,7 +379,7 @@ static int run(int argc, char *argv[]) {
                 if (!root_directory)
                         cmdline[i++] = "-M";
 
-                if (arg_force)
+                if (arg_mode == FSCK_FORCE)
                         cmdline[i++] = "-f";
 
                 if (!isempty(dash_c))