rmlint icon indicating copy to clipboard operation
rmlint copied to clipboard

Clean up path processing

Open mfwitten opened this issue 6 years ago • 1 comments

This series entails cleanup and refactoring, which clarify the code and improve the handling of errors in user input and runtime. Here are the main commits:

4 [HEAD] lib/cmdline.c: Refactor `rm_cmd_set_paths*()'
3 lib/cfg.c -> lib/cfg-funcs.h: Refactor functions
2 Miscellaneous improvements
1 [develop]

Each of those is composed of finer-grained commits:

4 [HEAD] lib/cmdline.c: Refactor `rm_cmd_set_paths*()'
|\  
| o lib/cmdline.c: INLINE `rm_cmd_set_paths()'
| o lib/cmdline.c: Tell compiler to optimize for the value of `replay'
| o lib/cmdline.c: Replace `strcmp()' with direct character comparisons
| o lib/cmdline.c: Abstract out `rm_cmd_set_paths_from_cmdline()'
| o lib/cfg.h: Use `RmCfg::path_count' more purposefully
| o lib/cmdline.c: Move `cfg->replay' and `cfg->path_count++' out of loops
| o lib/cfg-funcs.h: Move `cfg->replay' and `cfg->path_count++'
| o lib/cmdline.c: Free each command-line path as it is processed.
| o lib/cmdline.c: Use a pointer rather than an index integer for looping
| o lib/cmdline.c: Indent code to reflect the fact that it's in a block
| o lib/cmdline.c: Move constant loop condition (`paths') out of the loop
| o lib/cmdline.c: Consolidate the logic around whether to read paths from `stdin'
| o lib/cfg.h: Remove member `read_stdin'
| o lib/cmdline.c: Add error handling to `rm_cmd_set_paths_from_stdin()'
| o lib/cmdline.c: Abstract out struct `rm_cmd_set_paths_vars'
| o lib/cmdline.c: Replace `RmSession' with `RmCfg' in `rm_cmd_set_paths*()'
| o lib/cmdline.c: Simplify size in call to `malloc()'
| o lib/cmdline.c: Move `rm_cmd_read_paths_from_stdin()'
|/  
3 lib/cfg.c -> lib/cfg-funcs.h: Refactor functions
|\  
| o lib/cfg-funcs.h: Insert `g_assert(cfg);' into `rm_cfg_set_default()'
| o lib/cfg.c -> lib/cfg-funcs.h: Make functions inlinable
| |\  
| | o lib/cfg-funcs.h: Complete `git mv lib/cfg.c lib/cfg-funcs.h'
| | o lib/cfg.c: git mv lib/cfg.c lib/cfg-funcs.h
| |/  
| o lib/cfg.*: rm_cfg_prepend_path(): swap parameters `preferred' and `path'
| o lib/cfg.*: Rename `rm_cfg_add_path' to `rm_cfg_prepend_path()'
| o lib/path.h: s/treat_as_single_vol/single_volume/ in `RmPath'
| o lib/path.h: s/idx/index/ in `RmPath'
| o lib/cfg.*: Refactor `rm_cfg_add_path()'
| o lib/cmdline.c: Test parameters of `rm_cmd_parse_replay()' with `g_assert()'
| o lib/cmdline.c: return the status of `rm_cfg_add_path()'
| o lib/cfg.*: `rm_cfg_add_path()' now returns `bool'
| o lib/cfg.*: Clean up `rm_cfg_free_paths()'
|/  
2 Miscellaneous improvements
|\  
| o lib/cmdline.c: Fix error message
| o Use explicit types for some integer constants
|/  
1 [develop]

mfwitten avatar Oct 16 '18 19:10 mfwitten

The original patch series has essentially been updated as per the following diff:

diff --git a/lib/cfg-funcs.h b/lib/cfg-funcs.h
index 9b218c31..c2e4f000 100644
--- a/lib/cfg-funcs.h
+++ b/lib/cfg-funcs.h
@@ -121,8 +121,10 @@ bool rm_cfg_prepend_json(
             real_path,
             cfg->path_count++,
             false /* not preferred */
-        ); return true;
-    } return false;
+        );
+        return true;
+    }
+    return false;
 }
 
 #define PREPEND_TO(list)    \
@@ -149,8 +151,10 @@ bool rm_cfg_prepend_path(
         } else {
             PREPEND_TO(cfg->paths);
             ++(cfg->path_count);
-        } return true;
-    } return false;
+        }
+        return true;
+    }
+    return false;
 }
 
 #undef PREPEND_TO
@@ -159,9 +163,9 @@ static INLINE
 void rm_cfg_free_paths(RmCfg *const cfg) {
     g_assert(cfg);
     g_slist_free_full(cfg->paths, (GDestroyNotify)rm_path_free);
-    cfg->paths = 0;
+    cfg->paths = NULL;
     g_slist_free_full(cfg->json_paths, (GDestroyNotify)rm_path_free);
-    cfg->json_paths = 0;
+    cfg->json_paths = NULL;
 }
 
 #endif /* end of include guard */
diff --git a/lib/cmdline.c b/lib/cmdline.c
index 56d07a8e..a3ab75f9 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -1074,12 +1074,25 @@ static gboolean rm_cmd_parse_rankby(_UNUSED const char *option_name,
 
 static gboolean rm_cmd_parse_replay(_UNUSED const char *option_name,
                                     const gchar *path, RmSession *session,
-                                    _UNUSED GError **error) {
+                                    GError **error) {
     g_assert(session);
     g_assert(session->cfg);
     session->cfg->replay = true;
     session->cfg->cache_file_structs = true;
-    return rm_cfg_prepend_json(session->cfg, path);
+
+    if(rm_cfg_prepend_json(session->cfg, path)) {
+        return true;
+    }
+
+    g_assert(error);
+    g_assert(*error == NULL);
+
+    g_set_error(
+        error, RM_ERROR_QUARK, 0,
+        _("Failed to include this replay file: %s"), path
+    );
+
+    return false;
 }
 
 static gboolean rm_cmd_parse_equal(_UNUSED const char *option_name,
@@ -1145,7 +1158,7 @@ static bool rm_cmd_set_cmdline(RmCfg *cfg, int argc, char **argv) {
     return true;
 }
 
-typedef struct rm_cmd_set_paths_vars {
+typedef struct RmCmdSetPathVars {
     RmCfg *const cfg;
     char **const paths;
     unsigned int index;
@@ -1154,23 +1167,21 @@ typedef struct rm_cmd_set_paths_vars {
     bool stdin_paths_preferred;
     const bool null_separated;
     bool all_paths_valid;
-} rm_cmd_set_paths_vars;
+} RmCmdSetPathVars;
 
 static INLINE
 void rm_cmd_set_paths_from_cmdline(
-    rm_cmd_set_paths_vars *const v,
+    RmCmdSetPathVars *const v,
     const bool replay
 ) {
     g_assert(v);
     g_assert(v->paths);
 
     for(char *path, **list = v->paths; (path = *list); ++list) {
-        if(path[0] == '-' && path[1] == 0) {
+        if(/* path is "-" */ path[0] == '-' && path[1] == 0) {
             v->read_stdin = true;
-            /* remember whether to treat stdin paths as preferred paths */
             v->stdin_paths_preferred = v->is_prefd;
-        } else if(path[0] == '/' && path[1] == '/' && path[2] == 0) {
-            /* the '//' separator separates non-preferred paths from preferred */
+        } else if(/* path is "//" */ path[0] == '/' && path[1] == '/' && path[2] == 0) {
             v->is_prefd = !v->is_prefd;
         } else {
             v->all_paths_valid &= rm_cfg_prepend_path(
@@ -1184,7 +1195,7 @@ void rm_cmd_set_paths_from_cmdline(
 
 static INLINE
 bool rm_cmd_set_paths_from_stdin(
-    rm_cmd_set_paths_vars *const v,
+    RmCmdSetPathVars *const v,
     const bool replay
 ) {
     g_assert(v);
@@ -1195,8 +1206,8 @@ bool rm_cmd_set_paths_from_stdin(
 
     char delim = v->null_separated ? 0 : '\n';
 
-    size_t buf_len;
-    char *path_buf = malloc(buf_len = PATH_MAX);
+    size_t buf_len = PATH_MAX;
+    char *path_buf = malloc(buf_len);
     if(!path_buf) {
         rm_log_perror(_("Failed to allocate memory"));
         return false;
@@ -1243,7 +1254,7 @@ bool rm_cmd_set_paths(RmCfg *const cfg, char **const paths) {
 
     const bool replay = cfg->replay;
     const bool read_stdin0 = cfg->read_stdin0;
-    rm_cmd_set_paths_vars v = {
+    RmCmdSetPathVars v = {
         .cfg = cfg,
         .paths = paths,
         .index = cfg->path_count,
diff --git a/lib/path-funcs.h b/lib/path-funcs.h
index 5599387b..1f46ac0b 100644
--- a/lib/path-funcs.h
+++ b/lib/path-funcs.h
@@ -39,7 +39,7 @@
 #endif
 
 #include "path.h"       // RmPath
-#include "config.h"     // _, rm_log_warning_line, rm_log_perrorf
+#include "config.h"     // _, rm_log_warning_line
 
 static INLINE
 void rm_path_free(RmPath *const p) {
@@ -63,7 +63,9 @@ bool rm_path_is_real(
     rm_log_warning_line(
         _("Can't get real path for directory or file \"%s\": %s"),
         path, strerror(errno)
-    ); return false;
+    );
+
+    return false;
 }
 
 #if HAVE_FACCESSAT
@@ -79,8 +81,10 @@ bool rm_path_is_accessible(const char *const path) {
         rm_log_warning_line(
             _("Can't open directory or file \"%s\": %s"),
             path, strerror(errno)
-        ); return false;
-    } return true;
+        );
+        return false;
+    }
+    return true;
 }
 
 #undef NOT_ACCESSIBLE
@@ -109,8 +113,10 @@ bool rm_path_is_file(const char *const path) {
         rm_log_warning_line(
             _("Could not get metadata for path \"%s\": %s"),
             path, strerror(errno)
-        ); return false;
-    } return S_ISREG(s.st_mode);
+        );
+        return false;
+    }
+    return S_ISREG(s.st_mode);
 }
 
 static INLINE
diff --git a/po/de.po b/po/de.po
index feb59eb5..e4d23497 100644
--- a/po/de.po
+++ b/po/de.po
@@ -7,7 +7,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: rmlint 2.0.0\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2018-10-15 04:46+0000\n"
+"POT-Creation-Date: 2019-01-21 21:40+0000\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <[email protected]>\n"
@@ -1028,6 +1028,11 @@ msgstr ""
 msgid "No stamp file at `%s`, will create one after this run."
 msgstr ""
 
+#: lib/cmdline.c
+#, c-format
+msgid "Failed to include this replay file: %s"
+msgstr ""
+
 #: lib/path-funcs.h
 #, c-format
 msgid "Invalid path \"%s\""
diff --git a/po/es.po b/po/es.po
index d0a4c46e..fd81c42d 100644
--- a/po/es.po
+++ b/po/es.po
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: rmlint 2.4.0\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2018-10-15 04:46+0000\n"
+"POT-Creation-Date: 2019-01-21 21:40+0000\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <[email protected]>\n"
@@ -1005,6 +1005,11 @@ msgstr ""
 msgid "No stamp file at `%s`, will create one after this run."
 msgstr ""
 
+#: lib/cmdline.c
+#, c-format
+msgid "Failed to include this replay file: %s"
+msgstr ""
+
 #: lib/path-funcs.h
 #, c-format
 msgid "Invalid path \"%s\""
diff --git a/po/fr.po b/po/fr.po
index 2b2c58c1..ea5b2db4 100644
--- a/po/fr.po
+++ b/po/fr.po
@@ -7,7 +7,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: rmlint 2.0.0\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2018-10-15 04:46+0000\n"
+"POT-Creation-Date: 2019-01-21 21:40+0000\n"
 "PO-Revision-Date: 2014-12-02 13:37+0100\n"
 "Last-Translator: F. <[email protected]>\n"
 "Language-Team: none\n"
@@ -1001,6 +1001,11 @@ msgstr ""
 msgid "No stamp file at `%s`, will create one after this run."
 msgstr ""
 
+#: lib/cmdline.c
+#, c-format
+msgid "Failed to include this replay file: %s"
+msgstr ""
+
 #: lib/path-funcs.h
 #, c-format
 msgid "Invalid path \"%s\""
diff --git a/po/rmlint.pot b/po/rmlint.pot
index 94b92658..348f86fa 100644
--- a/po/rmlint.pot
+++ b/po/rmlint.pot
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: rmlint 2.7.0\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2018-10-15 04:46+0000\n"
+"POT-Creation-Date: 2019-01-21 21:40+0000\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <[email protected]>\n"
@@ -950,6 +950,11 @@ msgstr ""
 msgid "No stamp file at `%s`, will create one after this run."
 msgstr ""
 
+#: lib/cmdline.c
+#, c-format
+msgid "Failed to include this replay file: %s"
+msgstr ""
+
 #: lib/path-funcs.h
 #, c-format
 msgid "Invalid path \"%s\""

mfwitten avatar Jan 24 '19 00:01 mfwitten