aboutsummaryrefslogtreecommitdiff
path: root/src/options.c
blob: 7bcd9b0f7cc389354ce65aa8998db1725a2c96d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* SPDX-License-Identifier: Apache-2.0 */

/* Copyright 2026 Thorsten Töpper
 *
 * vim:ts=4:sw=4:expandtab
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <stdbool.h>
#include <ctype.h>

#include "options.h"
#include "trace_macros.h"


/* === 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 },
    { 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;
bool option_show_non_duplicates = 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 scan|analyze|dump PARAMETERS\n", executable);
    fprintf(stderr, "\nOPTIONS are\n");
    /* long name, short name, optional argument, explanation */
    fprintf(stderr, " %-25s %2s %10s  - %s\n", "--all", "-a", "",
            "Different per command, scan includes hidden files, analyze shows every db entry before analysis");
    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, "\nParameters\n");
    fprintf(stderr, " %-10s %-10s %s\n", "analyze", "", "Currently no parameter");
    fprintf(stderr, " %-10s %-10s %s\n", "dump", "fullpath",  "Print the full stored path of each files entry");
    fprintf(stderr, " %-10s %-10s %s\n", "",     "fileinfos", "Print all entries combined with hashes");
    fprintf(stderr, " %-10s %-10s %s\n", "scan", "", "path in the FS below which files should be scanned");
}


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;
        option_show_non_duplicates = true;
        return;
    }
    if (option_name == NULL)
        return;

    /* options WITHOUT arguments */
    if (strcmp("help", option_name) == 0) {
        usage(exec_name);
        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;
    }


    /* options WITH arguments */
    if (option_argument == NULL || option_argument[0] == '\0') {
        LOGERR("ERROR: option_name %s with missing option_argument\n",
                option_name);
        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);
}


int parse_arguments(int argc, char **argv) {
    int c = 0, index;
    /* exec_name is a file internal global variable for --help in set_option() */
    exec_name = argv[0];

    while(1) {
        index = 0;
        c = getopt_long(argc, argv, "ad:hk:q", long_options, &index);

        if (c == -1) {
            break;
        }

        switch (c) {
            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 '?':
                break;
            default:
                LOGERR("ERROR: unrecognized option 0x%02X '%c'\n", c, c);
                usage(exec_name);
                exit(EXIT_FAILURE);
        }
    }

    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 if (strcmp("dump", argv[optind]) == 0) {
        option_mode = MODE_DUMP;
    } else {
        LOGERR("ERROR: No valid mode given see --help or man.\n");
        exit(EXIT_FAILURE);
    }

    return optind+1;
}