Skip to content

Commit

Permalink
hide error messages behind a macro
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Bruyelles <[email protected]>
  • Loading branch information
JackSlateur committed Dec 6, 2023
1 parent 1b1b9fc commit ed80c8b
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 114 deletions.
30 changes: 14 additions & 16 deletions dbfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ static GMutex io_mutex; /* Locks db writes */

#if (SQLITE_VERSION_NUMBER < 3007015)
#define perror_sqlite(_err, _why) \
fprintf(stderr, "%s(): Database error %d while %s: %s\n", \
eprintf("%s(): Database error %d while %s: %s\n", \
__FUNCTION__, _err, _why, "[sqlite3_errstr() unavailable]")
#else
#define perror_sqlite(_err, _why) \
fprintf(stderr, "%s()/%ld: Database error %d while %s: %s\n", \
eprintf("%s()/%ld: Database error %d while %s: %s\n", \
__FUNCTION__, syscall(SYS_gettid), _err, _why, sqlite3_errstr(_err))
#endif

#define perror_sqlite_open(_ptr, _filename) \
fprintf(stderr, "Error opening db \"%s\": %s\n", _filename, \
eprintf("Error opening db \"%s\": %s\n", _filename, \
sqlite3_errmsg(_ptr))

struct dbhandle *dbfile_get_handle(void)
Expand Down Expand Up @@ -104,17 +104,15 @@ static int dbfile_check(sqlite3 *db, struct dbfile_config *cfg)
char path[PATH_MAX + 1];

if (cfg->major != DB_FILE_MAJOR || cfg->minor != DB_FILE_MINOR) {
fprintf(stderr,
"Hash db version mismatch (mine: %d.%d, file: %d.%d)\n",
eprintf("Hash db version mismatch (mine: %d.%d, file: %d.%d)\n",
DB_FILE_MAJOR, DB_FILE_MINOR, cfg->major, cfg->minor);
return EIO;
}

dbfile_get_dbpath(db, path);

if (strncasecmp(cfg->hash_type, HASH_TYPE, 8)) {
fprintf(stderr,
"Error: Hashfile %s uses \"%.*s\" for checksums "
eprintf("Error: Hashfile %s uses \"%.*s\" for checksums "
"but we are using %.*s.\nYou are probably "
"using a hashfile generated from an old version, "
"which cannot be read anymore.\n", path, 8,
Expand Down Expand Up @@ -285,12 +283,12 @@ static int dbfile_prepare(sqlite3 *db)

ret = dbfile_check(db, &cfg);
if (ret && strcmp("(null)", dbpath) != 0) {
fprintf(stderr, "Recreating hashfile ..\n");
eprintf("Recreating hashfile ..\n");
sqlite3_close(db);
ret = unlink(dbpath);
if ( ret && errno != ENOENT) {
ret = errno;
fprintf(stderr, "Error %d while unlinking old "
eprintf("Error %d while unlinking old "
"db file \"%s\" : %s\n", ret, dbpath,
strerror(ret));
return ret;
Expand Down Expand Up @@ -580,14 +578,14 @@ uint64_t count_file_by_digest(struct dbhandle *db, unsigned char *digest,

ret = sqlite3_bind_blob(stmt, 1, digest, DIGEST_LEN, SQLITE_STATIC);
if (ret) {
fprintf(stderr, "Error %d binding digest: %s\n", ret,
eprintf("Error %d binding digest: %s\n", ret,
sqlite3_errstr(ret));
return 0;
}

ret = sqlite3_step(stmt);
if (ret != SQLITE_ROW && ret != SQLITE_DONE) {
fprintf(stderr, "error %d, file count search: %s\n",
eprintf("error %d, file count search: %s\n",
ret, sqlite3_errstr(ret));
return 0;
}
Expand Down Expand Up @@ -1166,7 +1164,7 @@ int dbfile_load_block_hashes(struct dbhandle *db, struct hash_tree *hash_tree,
if (!file) {
ret = dbfile_load_one_filerec(db, fileid, &file);
if (ret) {
fprintf(stderr, "Error loading filerec (%"
eprintf("Error loading filerec (%"
PRIu64") from db\n",
fileid);
return ret;
Expand Down Expand Up @@ -1214,7 +1212,7 @@ int dbfile_load_extent_hashes(struct dbhandle *db, struct results_tree *res,
if (!file) {
ret = dbfile_load_one_filerec(db, fileid, &file);
if (ret) {
fprintf(stderr, "Error loading filerec (%"
eprintf("Error loading filerec (%"
PRIu64") from db\n",
fileid);
return ret;
Expand Down Expand Up @@ -1370,7 +1368,7 @@ void dbfile_list_files(struct dbhandle *db, int (*callback)(void*, int, char**,

ret = sqlite3_exec(db->db, LIST_FILERECS, callback, NULL, &err);
if (ret) {
fprintf(stderr, "error %d, executing file search: %s\n", ret,
eprintf("error %d, executing file search: %s\n", ret,
err);
return;
}
Expand Down Expand Up @@ -1458,7 +1456,7 @@ int dbfile_load_same_files(struct dbhandle *db, struct results_tree *res,
if (!file) {
ret = dbfile_load_one_filerec(db, fileid, &file);
if (ret) {
fprintf(stderr, "Error loading filerec (%"
eprintf("Error loading filerec (%"
PRIu64") from db\n",
fileid);
return ret;
Expand Down Expand Up @@ -1514,7 +1512,7 @@ unsigned int get_max_dedupe_seq(struct dbhandle *db)

int ret = sqlite3_step(stmt);
if (ret != SQLITE_ROW) {
fprintf(stderr, "error %d, get max dedupe seq: %s\n",
eprintf("error %d, get max dedupe seq: %s\n",
ret, sqlite3_errstr(ret));
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extern int quiet;
#define dprintf(args...) if (debug) printf(args)
#define vprintf(args...) if (verbose) printf(args)
#define qprintf(args...) if (!quiet) printf(args)
#define eprintf(args...) fprintf(stderr, args)
void print_stack_trace(void);/* defined in util.c */
#define abort_lineno() do { \
printf("ERROR: %s:%d\n", __FILE__, __LINE__); \
Expand Down
5 changes: 2 additions & 3 deletions dedupe.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ static void free_dedupe_req(struct dedupe_req *req)
if (!list_empty(&req->req_list)) {
struct filerec *file = req->req_file;

fprintf(stderr,
"%s: freeing request with nonempty list\n",
eprintf("%s: freeing request with nonempty list\n",
file ? file->filename : "(null)");
list_del(&req->req_list);
}
Expand Down Expand Up @@ -149,7 +148,7 @@ static unsigned int get_fs_blocksize(struct filerec *file)
ret = fstatfs(file->fd, &fs);
if (ret) {
if (!bsize_warned) {
fprintf(stderr, "Error %d (\"%s\") while getting fs "
eprintf("Error %d (\"%s\") while getting fs "
"blocksize, defaulting to 4096 bytes for this "
"dedupe.\n", errno, strerror(errno));
bsize_warned = 1;
Expand Down
51 changes: 22 additions & 29 deletions duperemove.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include "dbfile.h"
#include "memstats.h"
#include "debug.h"

#include "file_scan.h"
#include "find_dupes.h"
#include "run_dedupe.h"
Expand Down Expand Up @@ -74,7 +73,7 @@ static int list_db_files(char *filename)

_cleanup_(sqlite3_close_cleanup) struct dbhandle *db = dbfile_open_handle(filename);
if (!db) {
fprintf(stderr, "Error: Could not open \"%s\"\n", filename);
eprintf("Error: Could not open \"%s\"\n", filename);
return -1;
}

Expand All @@ -97,7 +96,7 @@ static void rm_db_files_from_stdin(struct dbhandle *db)
}

if (readlen > PATH_MAX - 1) {
fprintf(stderr, "Path max exceeded: %s\n", path);
eprintf("Path max exceeded: %s\n", path);
continue;
}

Expand All @@ -110,7 +109,7 @@ static int rm_db_files(int numfiles, char **files)
int i, ret;
_cleanup_(sqlite3_close_cleanup) struct dbhandle *db = dbfile_open_handle(options.hashfile);
if (!db) {
fprintf(stderr, "Error: Could not open \"%s\"\n", options.hashfile);
eprintf("Error: Could not open \"%s\"\n", options.hashfile);
return -1;
}

Expand Down Expand Up @@ -178,7 +177,7 @@ static int parse_dedupe_opts(const char *raw_opts)
}

if (print_usage) {
fprintf(stderr, "Bad dedupe options specified. Valid dedupe "
eprintf("Bad dedupe options specified. Valid dedupe "
"options are:\n"
"\t[no]same\n"
"\t[no]only_whole_files\n"
Expand Down Expand Up @@ -230,7 +229,7 @@ static int process_fdupes()
}

if (readlen > PATH_MAX - 1) {
fprintf(stderr, "Path max exceeded: %s\n", path);
eprintf("Path max exceeded: %s\n", path);
continue;
}

Expand All @@ -255,13 +254,12 @@ static int add_files_from_stdin(struct dbhandle *db)
}

if (readlen > PATH_MAX - 1) {
fprintf(stderr, "Path max exceeded: %s\n", path);
eprintf("Path max exceeded: %s\n", path);
continue;
}

if (scan_file(path, db)) {
fprintf(stderr,
"Error: cannot add %s into the lookup list\n",
eprintf("Error: cannot add %s into the lookup list\n",
path);
return 1;
}
Expand All @@ -278,9 +276,7 @@ static int scan_files_from_cmdline(int numfiles, char **files, struct dbhandle *
char *name = files[i];

if (scan_file(name, db)) {
fprintf(stderr,
"Error: cannot scan %s\n",
name);
eprintf("Error: cannot scan %s\n", name);
return 1;
}
}
Expand Down Expand Up @@ -333,7 +329,7 @@ static int parse_options(int argc, char **argv, int *filelist_idx)
blocksize = parse_size(optarg);
if (blocksize < MIN_BLOCKSIZE ||
blocksize > MAX_BLOCKSIZE){
fprintf(stderr, "Error: Blocksize is bounded by %u and %u, %u found\n",
eprintf("Error: Blocksize is bounded by %u and %u, %u found\n",
MIN_BLOCKSIZE, MAX_BLOCKSIZE, blocksize);
return EINVAL;
}
Expand Down Expand Up @@ -373,15 +369,15 @@ static int parse_options(int argc, char **argv, int *filelist_idx)
case IO_THREADS_OPTION:
options.io_threads = strtoul(optarg, NULL, 10);
if (!options.io_threads){
fprintf(stderr, "Error: --io-threads must be "
eprintf("Error: --io-threads must be "
"an integer, %s found\n", optarg);
return EINVAL;
}
break;
case CPU_THREADS_OPTION:
options.cpu_threads = strtoul(optarg, NULL, 10);
if (!options.cpu_threads){
fprintf(stderr, "Error: --cpu-threads must be "
eprintf("Error: --cpu-threads must be "
"an integer, %s found\n", optarg);
return EINVAL;
}
Expand All @@ -408,7 +404,7 @@ static int parse_options(int argc, char **argv, int *filelist_idx)
break;
case EXCLUDE_OPTION:
if (add_exclude_pattern(optarg))
fprintf(stderr, "Error: cannot exclude %s\n", optarg);
eprintf("Error: cannot exclude %s\n", optarg);
break;
case BATCH_SIZE_OPTION:
case 'B':
Expand All @@ -426,14 +422,14 @@ static int parse_options(int argc, char **argv, int *filelist_idx)
numfiles = argc - optind;

if (options.only_whole_files && options.do_block_hash) {
fprintf(stderr, "Error: using both only_whole_files and partial "
eprintf("Error: using both only_whole_files and partial "
"options have no meaning\n");
return 1;
}

/* Filter out option combinations that don't make sense. */
if ((write_hashes + read_hashes + update_hashes) > 1) {
fprintf(stderr, "Error: Specify only one hashfile option.\n");
eprintf("Error: Specify only one hashfile option.\n");
return 1;
}

Expand All @@ -446,8 +442,7 @@ static int parse_options(int argc, char **argv, int *filelist_idx)

if (read_hashes) {
if (numfiles) {
fprintf(stderr,
"Error: --read-hashes option does not take a "
eprintf("Error: --read-hashes option does not take a "
"file list argument\n");
return 1;
}
Expand All @@ -456,15 +451,13 @@ static int parse_options(int argc, char **argv, int *filelist_idx)

if (options.fdupes_mode) {
if (read_hashes || write_hashes || update_hashes) {
fprintf(stderr,
"Error: cannot mix hashfile option with "
eprintf("Error: cannot mix hashfile option with "
"--fdupes option\n");
return 1;
}

if (numfiles) {
fprintf(stderr,
"Error: fdupes option does not take a file "
eprintf("Error: fdupes option does not take a file "
"list argument\n");
return 1;
}
Expand All @@ -477,27 +470,27 @@ static int parse_options(int argc, char **argv, int *filelist_idx)
stdin_filelist = 1;

if (list_only_opt && rm_only_opt) {
fprintf(stderr, "Error: Can not mix '-L' and '-R' options.\n");
eprintf("Error: Can not mix '-L' and '-R' options.\n");
return 1;
}

if (list_only_opt || rm_only_opt) {
if (!options.hashfile || use_hashfile == H_WRITE) {
fprintf(stderr, "Error: --hashfile= option is required "
eprintf("Error: --hashfile= option is required "
"with '-L' or -R.\n");
return 1;
}

if (list_only_opt && numfiles) {
fprintf(stderr, "Error: -L option do not take "
eprintf("Error: -L option do not take "
"a file list argument\n");
return 1;
}
}

if (!(options.fdupes_mode || list_only_opt)
&& numfiles == 0) {
fprintf(stderr, "Error: a file list argument is required.\n");
eprintf("Error: a file list argument is required.\n");
return 1;
}

Expand Down Expand Up @@ -675,7 +668,7 @@ int main(int argc, char **argv)
if (use_hashfile == H_WRITE || use_hashfile == H_UPDATE) {
ret = dbfile_prune_unscanned_files(db);
if (ret) {
fprintf(stderr, "Unable to prune unscanned files\n");
eprintf("Unable to prune unscanned files\n");
goto out;
}

Expand Down
Loading

0 comments on commit ed80c8b

Please sign in to comment.