diff options
| -rw-r--r-- | include/kv_manager.h | 2 | ||||
| -rw-r--r-- | include/options.h | 11 | ||||
| -rw-r--r-- | src/kv_manager.c | 5 | ||||
| -rw-r--r-- | src/options.c | 76 |
4 files changed, 77 insertions, 17 deletions
diff --git a/include/kv_manager.h b/include/kv_manager.h index 068841b..9a8875a 100644 --- a/include/kv_manager.h +++ b/include/kv_manager.h @@ -8,7 +8,7 @@ #define KV_MANAGER_H /*=========== FUNCTIONS ===========*/ -int kv_open_storage(char *fname); +int kv_open_storage(const char *fname); bool kv_close_storage(); bool kv_add_bool_type(char *key, bool value, char type); bool kv_set_bool(char *key, bool value); diff --git a/include/options.h b/include/options.h index 5a7747e..7e8ec38 100644 --- a/include/options.h +++ b/include/options.h @@ -19,9 +19,18 @@ /* === GLOBAL VARIABLES === */ +enum operation_modes { + MODE_SCAN, + MODE_ANALYZE_DB, + MODE_DEV_MESSED_UP +}; + extern bool option_quiet; extern bool option_show_hidden_entries; - +extern bool option_clean_kv; +extern char *option_gdbm_db_name; +extern char *option_sqlite_db_name; +extern enum operation_modes option_mode; int parse_arguments(int argc, char **argv); void set_option(const char *option_name, char *option_argument); diff --git a/src/kv_manager.c b/src/kv_manager.c index 8d2e79f..2307876 100644 --- a/src/kv_manager.c +++ b/src/kv_manager.c @@ -31,6 +31,7 @@ #include "kv_manager.h" #include "trace_macros.h" +#include "options.h" /*=========== DEFINES, CONSTANTS AND TYPES ===========*/ @@ -51,7 +52,7 @@ bool add_b_t_wrapped(char *key, bool value, char type, bool keep_original_type); * -2 on already open gdbm file * -3 on failure when opening or creating the db file */ -int kv_open_storage(char *fname) { +int kv_open_storage(const char *fname) { if (fname == NULL || fname[0] == '\0') { LOGERR("ERROR: No valid filename\n"); return false; @@ -63,7 +64,7 @@ int kv_open_storage(char *fname) { } /* Currently CLOEXEC is obsolete, as no exec calls are planned */ - gdbf = gdbm_open(fname, 0, GDBM_WRCREAT | GDBM_CLOEXEC | GDBM_XVERIFY, 0644, NULL); + gdbf = gdbm_open(fname, 0, ((option_clean_kv) ? GDBM_NEWDB : GDBM_WRCREAT) | GDBM_CLOEXEC | GDBM_XVERIFY, 0644, NULL); if (gdbf == NULL) { LOGERR("ERROR: Failed to open gdbm db: %s\n", gdbm_strerror(gdbm_errno)); diff --git a/src/options.c b/src/options.c index 214a72c..749ee90 100644 --- a/src/options.c +++ b/src/options.c @@ -17,29 +17,42 @@ /* === GLOBAL VARIABLES === */ struct option long_options[] = { + { "all", no_argument, 0, 0 }, + { "database", required_argument, 0 ,0 }, { "help", no_argument, 0, 0 }, + { "kv-storage", required_argument, 0, 0}, + { "new-kv", no_argument, 0, 0 }, { "quiet", no_argument, 0, 0 }, - { "show-hidden-entries", no_argument, 0, 0 }, { 0, 0, 0, 0 } }; +enum operation_modes option_mode = MODE_DEV_MESSED_UP; +bool option_clean_kv = false; bool option_quiet = false; bool option_show_hidden_entries = false; +char *option_gdbm_db_name = "/tmp/duplicate_finder.gdbm"; +char *option_sqlite_db_name = "duplicate_finder.sqlite"; char *exec_name; /* === IMPLEMENTATION === */ void usage(char *executable) { - fprintf(stderr, "Call: %s OPTIONS path_to_open\n", executable); + fprintf(stderr, "Call: %s OPTIONS scan|analyze [path]\n", executable); fprintf(stderr, "\nOPTIONS are\n"); /* long name, short name, optional argument, explanation */ + fprintf(stderr, " %-25s %2s %10s - %s\n", "--all", "-a", "", + "Also process and show files hidden on the filesystem"); + fprintf(stderr, " %-25s %2s %10s - %s\n", "--database", "-d", "", + "fullname of the sqlite db to be used"); fprintf(stderr, " %-25s %2s %10s - %s\n", "--help", "-h", "", "Show this message and exit"); + fprintf(stderr, " %-25s %2s %10s - %s\n", "--kv-storage", "-k", "", + "fullname of the gdbm storage file used during filesystem scans"); + fprintf(stderr, " %-25s %2s %10s - %s\n", "--new-kv", "", "", + "If the kv storage file already exists replace it with a new one"); fprintf(stderr, " %-25s %2s %10s - %s\n", "--quiet", "-q", "", "Don't print error messages or warnings"); - fprintf(stderr, " %-25s %2s %10s - %s\n", "--show-hidden-entries", "-a", "", - "Show hidden entries in the directory"); } @@ -48,6 +61,10 @@ void set_option(const char *option_name, char *option_argument) { DBGTRC("DEBUG: called with option_name '%s' and option_argument '%s'\n", option_name, option_argument); + if (strcmp("all", option_name) == 0) { + option_show_hidden_entries = true; + return; + } if (option_name == NULL) return; @@ -57,15 +74,15 @@ void set_option(const char *option_name, char *option_argument) { exit(EXIT_SUCCESS); } + if (strcmp("new-kv", option_name) == 0) { + option_clean_kv = true; + return; + } if (strcmp("quiet", option_name) == 0) { option_quiet = true; return; } - if (strcmp("show-hidden-entries", option_name) == 0) { - option_show_hidden_entries = true; - return; - } /* options WITH arguments */ if (option_argument == NULL || option_argument[0] == '\0') { @@ -74,6 +91,18 @@ void set_option(const char *option_name, char *option_argument) { exit(EXIT_FAILURE); } + /* No checks regarding validity of the paths here, if the lib can't + * work with it, it will react with a error which will be handled. */ + if (strcmp("database", option_name) == 0) { + option_sqlite_db_name = option_argument; + return; + } + + if (strcmp("kv-storage", option_name) == 0) { + option_gdbm_db_name = option_argument; + return; + } + LOGERR("ERROR: Option '%s' not recognized\n.", option_name); } @@ -85,7 +114,7 @@ int parse_arguments(int argc, char **argv) { while(1) { index = 0; - c = getopt_long(argc, argv, "hqs", long_options, &index); + c = getopt_long(argc, argv, "ad:hk:q", long_options, &index); if (c == -1) { break; @@ -95,15 +124,21 @@ int parse_arguments(int argc, char **argv) { case 0: set_option(long_options[index].name, optarg); break; + case 'a': + option_show_hidden_entries = true; + break; + case 'd': + option_sqlite_db_name = optarg; + break; case 'h': usage(exec_name); exit(EXIT_SUCCESS); + case 'k': + option_gdbm_db_name = optarg; + break; case 'q': option_quiet = true; break; - case 's': - option_show_hidden_entries = true; - break; case '?': break; default: @@ -113,6 +148,21 @@ int parse_arguments(int argc, char **argv) { } } - return optind; + if (argc == optind) { + LOGERR("ERROR: Too few arguments, at least mode is required, see --help or man\n"); + /* No external stuff touched so far, so just quit. */ + exit(EXIT_FAILURE); + } + + if (strcmp("scan", argv[optind]) == 0) { + option_mode = MODE_SCAN; + } else if (strcmp("analyze", argv[optind]) == 0) { + option_mode = MODE_ANALYZE_DB; + } else { + LOGERR("ERROR: No valid mode given see --help or man.\n"); + exit(EXIT_FAILURE); + } + + return optind++; } |
