Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ff9a53d
Use explicit types for some integer constants
mfwitten Oct 1, 2018
d085572
lib/cmdline.c: Fix error message
mfwitten Oct 15, 2018
63bc4be
Miscellaneous improvements
mfwitten Oct 15, 2018
a3e9d5c
lib/cfg.*: Clean up `rm_cfg_free_paths()'
mfwitten Oct 3, 2018
93541fc
lib/cfg.*: `rm_cfg_add_path()' now returns `bool'
mfwitten Sep 29, 2018
9c51e9d
lib/cmdline.c: return the status of `rm_cfg_add_path()'
mfwitten Sep 30, 2018
f8629f1
lib/cfg.*: Refactor `rm_cfg_add_path()'
mfwitten Oct 1, 2018
3539636
lib/cfg.*: Refactor `rm_cfg_add_path()'
mfwitten Oct 1, 2018
fb1ba2d
lib/cmdline.c: Move `rm_cmd_read_paths_from_stdin()'
mfwitten Oct 4, 2018
ee9b9a0
lib/cmdline.c: Simplify size in call to `malloc()'
mfwitten Oct 16, 2018
f8cea9f
lib/cmdline.c: Replace `RmSession' with `RmCfg' in `rm_cmd_set_paths*()'
mfwitten Sep 29, 2018
25db8f6
lib/cmdline.c: Abstract out struct `rm_cmd_set_paths_vars'
mfwitten Oct 16, 2018
1e81e41
lib/cmdline.c: Add error handling to `rm_cmd_set_paths_from_stdin()'
mfwitten Oct 15, 2018
fb31ce0
lib/cfg.h: Remove member `read_stdin'
mfwitten Sep 29, 2018
d4f7805
lib/cmdline.c: Consolidate the logic around whether to read paths fro…
mfwitten Sep 29, 2018
13304f7
lib/cmdline.c: Move constant loop condition (`paths') out of the loop
mfwitten Oct 3, 2018
a9f4de0
lib/cmdline.c: Indent code to reflect the fact that it's in a block
mfwitten Oct 3, 2018
05191ee
lib/cmdline.c: Use a pointer rather than an index integer for looping
mfwitten Oct 3, 2018
4f14e8a
lib/cmdline.c: Free each command-line path as it is processed.
mfwitten Oct 3, 2018
5dafe3a
lib/cfg-funcs.h: Move `cfg->replay' and `cfg->path_count++'
mfwitten Oct 4, 2018
a5698fd
lib/path.h: s/idx/index/ in `RmPath'
mfwitten Oct 14, 2018
322d7cc
lib/cmdline.c: Move `cfg->replay' and `cfg->path_count++' out of loops
mfwitten Oct 4, 2018
67e0738
lib/path.h: s/treat_as_single_vol/single_volume/ in `RmPath'
mfwitten Oct 14, 2018
e39fa29
lib/cfg.h: Use `RmCfg::path_count' more purposefully
mfwitten Oct 15, 2018
2f72e34
lib/cfg.c: git mv lib/cfg.c lib/cfg-funcs.h
mfwitten Oct 4, 2018
eb09ea9
lib/cfg.*: Rename `rm_cfg_add_path' to `rm_cfg_prepend_path()'
mfwitten Oct 3, 2018
bc825f3
lib/cmdline.c: Abstract out `rm_cmd_set_paths_from_cmdline()'
mfwitten Oct 4, 2018
74daf3b
lib/cfg-funcs.h: Complete `git mv lib/cfg.c lib/cfg-funcs.h'
mfwitten Oct 4, 2018
6dcbfde
lib/cfg.*: rm_cfg_prepend_path(): swap parameters `preferred' and `path'
mfwitten Oct 3, 2018
4182c59
lib/cmdline.c: Replace `strcmp()' with direct character comparisons
mfwitten Oct 7, 2018
605b5a9
lib/cfg.c -> lib/cfg-funcs.h: Make functions inlinable
mfwitten Oct 4, 2018
a4e4b88
lib/cmdline.c: Tell compiler to optimize for the value of `replay'
mfwitten Oct 4, 2018
30e7156
lib/cfg-funcs.h: Insert `g_assert(cfg);' into `rm_cfg_set_default()'
mfwitten Oct 15, 2018
2c24f77
lib/cmdline.c: INLINE `rm_cmd_set_paths()'
mfwitten Oct 16, 2018
3b1098c
lib/cfg.c -> lib/cfg-funcs.h: Refactor functions
mfwitten Oct 6, 2018
c0ba1b5
lib/cmdline.c: Refactor `rm_cmd_set_paths*()'
mfwitten Oct 6, 2018
0d332c9
Merge 'develop' into 'mfwitten/0007/prepend-paths'
mfwitten Jan 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <glib.h>

#include "cfg.h"
#include "cfg-funcs.h"
#include "cmdline.h"
#include "config.h"
#include "session.h"
109 changes: 66 additions & 43 deletions lib/cfg.c → lib/cfg-funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@
* Hosted on http://github.com/sahib/rmlint
**/

#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#ifndef RM_CFG_FUNCS_H
#define RM_CFG_FUNCS_H

#include "cfg.h"
#include <string.h> // memset
#include <stdbool.h> // bool, true, false
#include <limits.h> // PATH_MAX (maybe)
#include <glib.h> // G_MAXUINT64, g_strdup, G_LOG_LEVEL_INFO, g_slist_free_full

static void rm_path_free(RmPath *rmpath) {
free(rmpath->path);
g_slice_free(RmPath, rmpath);
}
#include "config.h" // RM_DEFAULT_DIGEST, PATH_MAX (maybe), RmOff
#include "cfg.h" // RmCfg
#include "pathtricia.h" // rm_trie_init
#include "path-funcs.h" // rm_path_free, rm_path_is_valid, rm_path_is_json, rm_path_prepend

/* Options not specified by commandline get a default option -
* this is usually called before rm_cmd_parse_args */
static INLINE
void rm_cfg_set_default(RmCfg *cfg) {
g_assert(cfg);

/* Set everything to 0 at first,
* only non-null options are listed below.
*/
Expand Down Expand Up @@ -87,8 +92,8 @@ void rm_cfg_set_default(RmCfg *cfg) {
* 32k => 15.8 seconds */
cfg->read_buf_len = 16 * 1024;

cfg->total_mem = (RmOff)1024 * 1024 * 1024;
cfg->sweep_size = 1024 * 1024 * 1024;
cfg->total_mem = 1024L * 1024 * 1024;
cfg->sweep_size = 1024L * 1024 * 1024;
cfg->sweep_count = 1024 * 16;

cfg->skip_start_factor = 0.0;
Expand All @@ -103,46 +108,64 @@ void rm_cfg_set_default(RmCfg *cfg) {
rm_trie_init(&cfg->file_trie);
}

guint rm_cfg_add_path(RmCfg *cfg, bool is_prefd, const char *path) {
int rc = 0;

#if HAVE_FACCESSAT
rc = faccessat(AT_FDCWD, path, R_OK, AT_EACCESS);
#else
rc = access(path, R_OK);
#endif

if(rc != 0) {
rm_log_warning_line(_("Can't open directory or file \"%s\": %s"), path,
strerror(errno));
return 0;
}

char *real_path = realpath(path, NULL);
if(real_path == NULL) {
rm_log_warning_line(_("Can't get real path for directory or file \"%s\": %s"),
path, strerror(errno));
return 0;
static INLINE
bool rm_cfg_prepend_json(
RmCfg *const cfg,
const char *const path
) {
g_assert(cfg);
char *real_path;
if(rm_path_is_valid(path, &real_path) && rm_path_is_json(real_path)) {
rm_path_prepend(
&cfg->json_paths,
real_path,
cfg->path_count++,
false /* not preferred */
);
return true;
}
return false;
}

RmPath *rmpath = g_slice_new(RmPath);
rmpath->path = real_path;
rmpath->is_prefd = is_prefd;
rmpath->idx = cfg->path_count++;
rmpath->treat_as_single_vol = strncmp(path, "//", 2) == 0;

if(cfg->replay && g_str_has_suffix(rmpath->path, ".json")) {
cfg->json_paths = g_slist_prepend(cfg->json_paths, rmpath);
return 1;
#define PREPEND_TO(list) \
rm_path_prepend( \
&(list), \
real_path, \
index, \
preferred \
)

static INLINE
bool rm_cfg_prepend_path(
RmCfg *const cfg,
const char *const path,
const unsigned int index,
const bool replay,
const bool preferred
) {
g_assert(cfg);
char *real_path;
if(rm_path_is_valid(path, &real_path)) {
if(replay && rm_path_is_json(path)) {
PREPEND_TO(cfg->json_paths);
} else {
PREPEND_TO(cfg->paths);
++(cfg->path_count);
}
return true;
}

cfg->paths = g_slist_prepend(cfg->paths, rmpath);
return 1;
return false;
}

void rm_cfg_free_paths(RmCfg *cfg) {
#undef PREPEND_TO

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 = NULL;
g_slist_free_full(cfg->json_paths, (GDestroyNotify)rm_path_free);
cfg->json_paths = NULL;
}

#endif /* end of include guard */
59 changes: 13 additions & 46 deletions lib/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,15 @@
* Hosted on http://github.com/sahib/rmlint
**/

#ifndef RM_SETTINGS_H
#define RM_SETTINGS_H
#ifndef RM_CFG_H
#define RM_CFG_H

#include <stdio.h>
#include <stdbool.h> // bool
#include <glib.h> // gboolean, gdouble, gint, GSList, guint

#include "checksum.h"
#include "pathtricia.h"
#include "utilities.h"

/* Struct for paths passed to rmlint from command line (or stdin) */
typedef struct RmPath {
/* the RealPath of the passed string */
char *path;

/* index number (command line order) */
guint idx;

/* whether path was tagged as preferred path */
bool is_prefd;

/* whether to treat all files under path as one filesystem */
bool treat_as_single_vol;
} RmPath;
#include "config.h" // RmOff
#include "pathtricia.h" // RmTrie
#include "checksum.h" // RmDigestType

/* Storage struct for all options settable in cmdline. */
typedef struct RmCfg {
Expand Down Expand Up @@ -87,7 +73,6 @@ typedef struct RmCfg {
gboolean progress_enabled;
gboolean list_mounts;
gboolean replay;
gboolean read_stdin;
gboolean read_stdin0;
gboolean no_backup;

Expand Down Expand Up @@ -115,16 +100,13 @@ typedef struct RmCfg {
*
* + To record a unique index for each path supplied by the
* user; a path's index represents the number of paths that
* were already processed. This is always the case.
* were already processed. This is the case DURING the
* processing of user-input options (such as '--replay').
*
* + To provide quick access to the length of its associated
* RmCfg::paths list. This is only the case when NOT running
* in "--replay" mode; when running in "--replay" mode, it
* just represents the total number of paths that have been
* supplied by the user, i.e., the sums of the lengths of
* the associated lists RmCfg::{paths,json_paths}, which is
* not meant to be a useful number to know, and is simply a
* byproduct of calculating path indicies.
* + To provide quick access to the length of its associated
* RmCfg::paths list. This is the case AFTER the processing
* of user-input options (including during the processing of
* non-option paths from the command line or stdin).
*/
guint path_count;

Expand Down Expand Up @@ -186,19 +168,4 @@ typedef struct RmCfg {

} RmCfg;

/**
* @brief Reset RmCfg to default cfg and all other vars to 0.
*/
void rm_cfg_set_default(RmCfg *cfg);

/**
* @brief check and add a path to cfg->paths.
*/
guint rm_cfg_add_path(RmCfg *cfg, bool is_prefd, const char *path);

/**
* @brief free all data associated with cfg->paths.
*/
void rm_cfg_free_paths(RmCfg *cfg);

#endif /* end of include guard */
Loading