1 /*
2   Copyright 2012-2018 Jyri J. Virkki <jyri@virkki.com>
3 
4   This file is part of dupd.
5 
6   dupd is free software: you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10 
11   dupd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with dupd.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef _DUPD_MAIN_H
21 #define _DUPD_MAIN_H
22 
23 #include <pthread.h>
24 #include <stdint.h>
25 #include <sys/types.h>
26 
27 #define K512 (1024 * 512)
28 #define MB1 (1024 * 1024)
29 #define MB2 (1024 * 1024 * 2)
30 #define MB8 (1024 * 1024 * 8)
31 #define MB16 (1024 * 1024 * 16)
32 #define MB32 (1024 * 1024 * 32)
33 #define GB1 (1024 * 1024 * 1024)
34 
35 
36 /** ***************************************************************************
37  * Verbosity is 1 by default, increased by one for every -v command
38  * line argument. Higher values produce more diagnostic noise:
39  *
40  *  L_NONE = No output (-q option)
41  *  L_BASE = (Default) Animated progress (brief)
42  *  L_MORE = More animated progress data and summary stats
43  *
44  *  (Levels 3 and higher skip the animation to show lines of info.)
45  *
46  *  L_PROGRESS = Basic progress lines, non-fatal errors and show stats
47  *  L_INFO = State, defaults, other low-volume info pre- and post-run
48  *  L_MORE_INFO = Like info but more annoyingly verbose
49  *  L_RESOURCES = Allocations, resize, other low-volume memory management
50  *  L_THREADS = Producer/consumer thread activity
51  *  L_SKIPPED = Files not read or processed
52  *  L_MORE_THREADS = Noisier thread activity
53  *  L_TRACE = Lots of output on everything that is going on
54  *  L_FILES = Print every file read
55  *  L_MORE_TRACE = Data structure dumps and such, too much noise
56  *
57  */
58 extern int log_level;
59 extern int log_only;
60 extern char * log_level_name[];
61 extern pthread_mutex_t logger_lock;
62 
63 #define L_NONE 0
64 #define L_BASE 1
65 #define L_MORE 2
66 #define L_PROGRESS 3
67 #define L_INFO 4
68 #define L_MORE_INFO 5
69 #define L_RESOURCES 6
70 #define L_THREADS 7
71 #define L_SKIPPED 8
72 #define L_MORE_THREADS 9
73 #define L_TRACE 10
74 #define L_FILES 11
75 #define L_MORE_TRACE 12
76 #define L_EVEN_MORE_TRACE 13
77 #define L_MAX_LOG_LEVEL 13
78 
79 #define LOG(level, ...) if ( (log_only && level == log_level) ||        \
80                              (!log_only && level <= log_level )) {      \
81     pthread_mutex_lock(&logger_lock);                                   \
82     printf("%s", get_thread_name());                                    \
83     printf(__VA_ARGS__);                                                \
84     fflush(stdout);                                                     \
85     pthread_mutex_unlock(&logger_lock);                                 \
86   }
87 
88 #define LOG_BASE if ( (log_only && log_level == L_BASE) ||      \
89                       (!log_only && log_level >= L_BASE) )
90 #define LOG_MORE if ( (log_only && log_level == L_MORE) ||      \
91                       (!log_only && log_level >= L_MORE) )
92 #define LOG_PROGRESS if ( (log_only && log_level == L_PROGRESS) ||      \
93                           (!log_only && log_level >= L_PROGRESS) )
94 #define LOG_INFO if ( (log_only && log_level == L_INFO) ||      \
95                       (!log_only && log_level >= L_INFO) )
96 #define LOG_MORE_INFO if ( (log_only && log_level == L_MORE_INFO) ||    \
97                            (!log_only && log_level >= L_MORE_INFO) )
98 #define LOG_RESOURCES if ( (log_only && log_level == L_RESOURCES) ||    \
99                            (!log_only && log_level >= L_RESOURCES) )
100 #define LOG_THREADS if ( (log_only && log_level == L_THREADS) ||        \
101                          (!log_only && log_level >= L_THREADS) )
102 #define LOG_SKIPPED if ( (log_only && log_level == L_SKIPPED) ||        \
103                          (!log_only && log_level >= L_SKIPPED) )
104 #define LOG_MORE_THREADS if ( (log_only && log_level == L_MORE_THREADS) || \
105                               (!log_only && log_level >= L_MORE_THREADS) )
106 #define LOG_TRACE if ( (log_only && log_level == L_TRACE) ||    \
107                        (!log_only && log_level >= L_TRACE) )
108 #define LOG_MORE_TRACE if ( (log_only && log_level == L_MORE_TRACE) ||  \
109                             (!log_only && log_level >= L_MORE_TRACE) )
110 #define LOG_EVEN_MORE_TRACE if ( (log_only && log_level == L_EVEN_MORE_TRACE) ||  \
111                                  (!log_only && log_level >= L_EVEN_MORE_TRACE) )
112 
113 
114 /** ***************************************************************************
115  * Scanning starts here.
116  *
117  */
118 extern char * start_path[];
119 
120 
121 /** ***************************************************************************
122  * A file specified by the user.
123  *
124  */
125 extern char * file_path;
126 
127 
128 /** ***************************************************************************
129  * Duplicate info will be saved in the sqlite database unless this is false.
130  *
131  */
132 extern int write_db;
133 
134 
135 /** ***************************************************************************
136  * Path to the sqlite database.
137  *
138  */
139 extern char * db_path;
140 
141 
142 /** ***************************************************************************
143  * This cut_path is used by report operation to optionally remove matching
144  * path components to reduce output size.
145  *
146  */
147 extern char * cut_path;
148 
149 
150 /** ***************************************************************************
151  * When reporting duplicates, if exclude_path is defined, any duplicates
152  * contained within this tree are ignored (not considered duplicates).
153  *
154  */
155 extern char * exclude_path;
156 extern int exclude_path_len;
157 
158 
159 /** ***************************************************************************
160  * This minimum_file_size is the smallest size handled by scan or report.
161  *
162  */
163 extern uint32_t minimum_file_size;
164 
165 
166 /** ***************************************************************************
167  * If true, in sets with only two files the files are compared directly
168  * skipping the hash list processing entirely.
169  *
170  */
171 extern int opt_compare_two;
172 
173 
174 /** ***************************************************************************
175  * If true, in sets with only three files the files are compared directly
176  * skipping the hash list processing entirely.
177  *
178  */
179 extern int opt_compare_three;
180 
181 
182 /** ***************************************************************************
183  * Maximum number of blocks read by the first pass.
184  *
185  */
186 extern int hash_one_max_blocks;
187 
188 
189 /** ***************************************************************************
190  * Size of blocks to read from disk during first pass.
191  *
192  */
193 extern uint32_t hash_one_block_size;
194 
195 
196 /** ***************************************************************************
197  * Max bytes to read into first buffer read.
198  *
199  */
200 extern uint32_t round1_max_bytes;
201 
202 
203 /** ***************************************************************************
204  * Size of blocks to read from disk during subsequent passes.
205  *
206  */
207 extern int hash_block_size;
208 
209 
210 /** ***************************************************************************
211  * Size of blocks to read from disk during direct file comparisons.
212  *
213  */
214 extern int filecmp_block_size;
215 
216 
217 /** ***************************************************************************
218  * If true, save files found to be unique during a scan in the database.
219  *
220  */
221 extern int save_uniques;
222 
223 
224 /** ***************************************************************************
225  * If true, current database has info on unique files.
226  *
227  */
228 extern int have_uniques;
229 
230 
231 /** ***************************************************************************
232  * If true, ignore unique table info even if have_uniques is true.
233  *
234  */
235 extern int no_unique;
236 
237 
238 /** ***************************************************************************
239  * Save stats to this file if defined.
240  *
241  */
242 extern char * stats_file;
243 
244 
245 /** ***************************************************************************
246  * Character used as pathname separator when saving multiple paths to db.
247  * path_sep_string contains same in null-terminated string form.
248  *
249  */
250 extern int path_separator;
251 extern char * path_sep_string;
252 
253 
254 /** ***************************************************************************
255  * If true, include hidden files and directories in the scan.
256  *
257  */
258 extern int scan_hidden;
259 
260 
261 /** ***************************************************************************
262  * If true, use smaller defaults for memory buffers. This is useful only
263  * for testing in order to force reallocations earlier.
264  *
265  */
266 extern int x_small_buffers;
267 
268 
269 /** ***************************************************************************
270  * If true, enable behavior(s) that only make sense while testing.
271  *
272  */
273 extern int only_testing;
274 
275 
276 /** ***************************************************************************
277  * If true, add to sizetree (while scanning) in a separate thread.
278  *
279  */
280 extern int threaded_sizetree;
281 
282 
283 /** ***************************************************************************
284  * If database is older than this, show a warning.
285  *
286  */
287 extern long db_warn_age_seconds;
288 
289 
290 /** ***************************************************************************
291  * If true, generate links in rmsh operation.
292  *
293  */
294 #define RMSH_LINK_SOFT 1
295 #define RMSH_LINK_HARD 2
296 extern int rmsh_link;
297 
298 
299 /** ***************************************************************************
300  * If true, hard links are considered unique files.
301  *
302  */
303 extern int hardlink_is_unique;
304 
305 
306 /** ***************************************************************************
307  * Hash function to use.
308  *
309  */
310 extern int hash_function;
311 
312 
313 /** ***************************************************************************
314  * Output size of hash_function.
315  *
316  */
317 extern int hash_bufsize;
318 
319 
320 /** ***************************************************************************
321  * True if we'll be using fiemap info.
322  *
323  */
324 extern int using_fiemap;
325 
326 
327 /** ***************************************************************************
328  * Forced sort bypass. Not used normally.
329  *
330  */
331 extern int sort_bypass;
332 #define SORT_BY_NONE 11
333 #define SORT_BY_BLOCK 13
334 #define SORT_BY_INODE 15
335 
336 
337 /** ***************************************************************************
338  * Report output format.
339  *
340  */
341 #define REPORT_FORMAT_TEXT 1
342 #define REPORT_FORMAT_CSV 2
343 #define REPORT_FORMAT_JSON 3
344 extern int report_format;
345 
346 
347 /** ***************************************************************************
348  * Thread name used for logging (at L_THREADS and higher).
349  *
350  */
351 extern pthread_key_t thread_name;
352 
353 
354 /** ***************************************************************************
355  * Size limit (bytes) used for data buffers when reading files.
356  *
357  */
358 extern uint64_t buffer_limit;
359 
360 
361 /** ***************************************************************************
362  * If true, do not cross into a different filesystem while scanning.
363  *
364  */
365 extern int one_file_system;
366 
367 
368 /** ***************************************************************************
369  * Limit of open files.
370  *
371  */
372 extern int max_open_files;
373 
374 
375 /** ***************************************************************************
376  * Used as the max path+filename length.
377  *
378  */
379 #define DUPD_PATH_MAX 4096
380 #define DUPD_FILENAME_MAX 256
381 
382 #endif
383