aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThorsten Töpper <atsutane@freethoughts.de>2026-02-22 22:15:38 +0100
committerThorsten Töpper <atsutane@freethoughts.de>2026-02-22 22:15:38 +0100
commit7ec726e5b4fbddc7b72907fec3fa612a0b1bd570 (patch)
tree5c217bf2fc20273ace7fcb2d2287567f7d532518 /src
parent1a927eb75dee00d9aada43256d6780a53fce46dd (diff)
downloadduplicate_finder-7ec726e5b4fbddc7b72907fec3fa612a0b1bd570.tar.gz
duplicate_finder-7ec726e5b4fbddc7b72907fec3fa612a0b1bd570.tar.bz2
options added
Diffstat (limited to 'src')
-rw-r--r--src/kv_manager.c5
-rw-r--r--src/options.c76
2 files changed, 66 insertions, 15 deletions
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++;
}