10000 hardlink: Use macro for verbose output · util-linux/util-linux@5a2600c · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a2600c

Browse files
committed
hardlink: Use macro for verbose output
Now that jlog() is a function, all gettext() arguments are evaluated in all cases, including when output is unnecessary due to --quiet or low verbose mode. Fixes: #3548 Signed-off-by: Karel Zak <kzak@redhat.com>
1 parent 0e4f4f6 commit 5a2600c

File tree

1 file changed

+65
-67
lines changed

1 file changed

+65
-67
lines changed

misc-utils/hardlink.c

Lines changed: 65 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,16 @@ struct file {
118118

119119
/**
120120
* enum log_level - Logging levels
121-
* @JLOG_SUMMARY: Default log level
122-
* @JLOG_INFO: Verbose logging (verbose == 1)
123-
* @JLOG_VERBOSE1: Verbosity 2
124-
* @JLOG_VERBOSE2: Verbosity 3
121+
* @HDL_LOG_SUMMARY: Default log level
122+
* @HDL_LOG_INFO: Verbose logging (verbose == 1)
123+
* @HDL_LOG_VERBOSE1: Verbosity 2
124+
* @HDL_LOG_VERBOSE2: Verbosity 3
125125
*/
126-
enum log_level {
127-
JLOG_SUMMARY,
128-
JLOG_INFO,
129-
JLOG_VERBOSE1,
130-
JLOG_VERBOSE2
126+
enum hdl_log_level {
127+
HDL_LOG_SUMMARY,
128+
HDL_LOG_INFO,
129+
HDL_LOG_VERBOSE1,
130+
HDL_LOG_VERBOSE2
131131
};
132132

133133
/**
@@ -183,7 +183,8 @@ static struct options {
183183
struct hdl_regex *exclude_subtree;
184184

185185
const char *method;
186-
signed int verbosity;
186+
short int verbosity;
187+
187188
bool respect_mode;
188189
bool respect_owner;
189190
bool respect_name;
@@ -238,26 +239,20 @@ static void *files_by_ino;
238239
static volatile sig_atomic_t last_signal;
239240

240241

241-
#define is_log_enabled(_level) (quiet == 0 && (_level) <= (unsigned int)opts.verbosity)
242+
#define is_log_enabled( l ) (quiet == 0 && opts.verbosity >= HDL_LOG_ ## l)
242243

243244
/**
244-
* jlog - Logging for hardlink
245-
* @level: The log level
246-
* @format: A format string for printf()
245+
* hdl_log - Logging for hardlink
246+
* @l: The log level (without HDL_LOG_ prefix)
247+
* @x: function to print output
247248
*/
248-
__attribute__((format(printf, 2, 3)))
249-
static void jlog(enum log_level level, const char *format, ...)
250-
{
251-
va_list args;
252-
253-
if (!is_log_enabled(level))
254-
return;
255-
256-
va_start(args, format);
257-
vfprintf(stdout, format, args);
258-
va_end(args);
259-
fputc('\n', stdout);
260-
}
249+
#define jlog(l, x) \
250+
do { \
251+
if (is_log_enabled( l )) { \
252+
x; \
253+
fputc('\n', stdout); \
254+
} \
255+
} while (0)
261256

262257
/**
263258
* CMP - Compare two numerical values, return 1, 0, or -1
@@ -405,32 +400,34 @@ static void print_stats(void)
405400
gettime_monotonic(&end);
406401
timersub(&end, &stats.start_time, &delta);
407402

408-
jlog(JLOG_SUMMARY, "%-25s %s", _("Mode:"),
409-
opts.dry_run ? _("dry-run") : _("real"));
410-
jlog(JLOG_SUMMARY, "%-25s %s", _("Method:"), opts.method);
411-
jlog(JLOG_SUMMARY, "%-25s %zu", _("Files:"), stats.files);
412-
jlog(JLOG_SUMMARY, _("%-25s %zu files"), _("Linked:"), stats.linked);
403+
jlog(SUMMARY, printf("%-25s %s", _("Mode:"),
404+
opts.dry_run ? _("dry-run") : _("real")));
405+
jlog(SUMMARY, printf("%-25s %s", _("Method:"), opts.method));
406+
jlog(SUMMARY, printf("%-25s %zu", _("Files:"), stats.files));
407+
jlog(SUMMARY, printf(_("%-25s %zu files"), _("Linked:"), stats.linked));
413408

414409
#ifdef USE_XATTR
415-
jlog(JLOG_SUMMARY, _("%-25s %zu xattrs"), _("Compared:"),
416-
stats.xattr_comparisons);
410+
jlog(SUMMARY, printf(_("%-25s %zu xattrs"), _("Compared:"),
411+
stats.xattr_comparisons));
417412
#endif
418-
jlog(JLOG_SUMMARY, _("%-25s %zu files"), _("Compared:"),
419-
stats.comparisons);
413+
jlog(SUMMARY, printf(_("%-25s %zu files"), _("Compared:"),
414+
stats.comparisons));
420415
#ifdef USE_REFLINK
421416
if (reflinks_skip)
422-
jlog(JLOG_SUMMARY, _("%-25s %zu files"), _("Skipped reflinks:"),
423-
stats.ignored_reflinks);
417+
jlog(SUMMARY, printf(_("%-25s %zu files"), _("Skipped reflinks:"),
418+
stats.ignored_reflinks));
424419
#endif
425420
ssz = size_to_human_string(SIZE_SUFFIX_3LETTER |
426421
SIZE_SUFFIX_SPACE |
427422
SIZE_DECIMAL_2DIGITS, stats.saved);
428423

429-
jlog(JLOG_SUMMARY, "%-25s %s", _("Saved:"), ssz);
424+
jlog(SUMMARY, printf("%-25s %s", _("Saved:"), ssz));
430425
free(ssz);
431426

432-
jlog(JLOG_SUMMARY, _("%-25s %"PRId64".%06"PRId64" seconds"), _("Duration:"),
433-
(int64_t)delta.tv_sec, (int64_t)delta.tv_usec);
427+
jlog(SUMMARY, printf(_("%-25s %"PRId64".%06"PRId64" seconds"),
428+
_("Duration:"),
429+
(int64_t) delta.tv_sec,
430+
(int64_t) delta.tv_usec));
434431
}
435432

436433
/**
@@ -560,8 +557,8 @@ static int file_xattrs_equal(const struct file *a, const struct file *b)
560557
assert(a->links != NULL);
561558
assert(b->links != NULL);
562559

563-
jlog(JLOG_VERBOSE1, _("Comparing xattrs of %s to %s"), a->links->path,
564-
b->links->path);
560+
jlog(VERBOSE1, printf(_("Comparing xattrs of %s to %s"),
561+
a->links->path, b->links->path));
565562

566563
stats.xattr_comparisons++;
567564

@@ -727,7 +724,7 @@ static inline int do_link(struct file *a, struct file *b,
727724

728725
if (reflink_mode == REFLINK_ALWAYS)
729726
return -errno;
730-
jlog(JLOG_VERBOSE2,_("Reflinking failed, fallback to hardlinking"));
727+
jlog(VERBOSE2, printf(_("Reflinking failed, fallback to hardlinking")));
731728
}
732729

733730
return link(a->links->path, new_name);
@@ -758,15 +755,15 @@ static int file_link(struct file *a, struct file *b, int reflink)
758755
assert(a->links != NULL);
759756
assert(b->links != NULL);
760757

761-
if (is_log_enabled(JLOG_INFO)) {
758+
if (is_log_enabled(INFO)) {
762759
char *ssz = size_to_human_string(SIZE_SUFFIX_3LETTER |
763760
SIZE_SUFFIX_SPACE |
764761
SIZE_DECIMAL_2DIGITS, a->st.st_size);
765-
jlog(JLOG_INFO, _("%s%sLinking %s to %s (-%s)"),
762+
jlog(INFO, printf(_("%s%sLinking %s to %s (-%s)"),
766763
opts.dry_run ? _("[DryRun] ") : "",
767764
reflink ? "Ref" : "",
768765
a->links->path, b->links->path,
769-
ssz);
766+
ssz));
770767
free(ssz);
771768
}
772769

@@ -855,8 +852,8 @@ static int inserter(const char *fpath, const struct stat *sb,
855852
if (opts.exclude_subtree
856853
&& typeflag == FTW_D
857854
&& match_any_regex(opts.exclude_subtree, fpath)) {
858-
jlog(JLOG_VERBOSE1,
859-
_("Skipped (excluded subtree) %s"), fpath);
855+
jlog(VERBOSE1,
856+
printf(_("Skipped (excluded subtree) %s"), fpath));
860857
return FTW_SKIP_SUBTREE;
861858
}
862859
#endif
@@ -868,26 +865,26 @@ static int inserter(const char *fpath, const struct stat *sb,
868865

869866
if ((opts.exclude && excluded && !included) ||
870867
(!opts.exclude && opts.include && !included)) {
871-
jlog(JLOG_VERBOSE1,
872-
_("Skipped (excluded) %s"), fpath);
868+
jlog(VERBOSE1,
869+
printf(_("Skipped (excluded) %s"), fpath));
873870
return 0;
874871
}
875872

876873
stats.files++;
877874

878875
if ((uintmax_t) sb->st_size < opts.min_size) {
879-
jlog(JLOG_VERBOSE1,
880-
_("Skipped (smaller than configured size) %s"), fpath);
876+
jlog(VERBOSE1,
877+
printf(_("Skipped (smaller than configured size) %s"), fpath));
881878
return 0;
882879
}
883880

884-
jlog(JLOG_VERBOSE2, " %5zu: [%" PRIu64 "/%" PRIu64 "/%zu] %s",
881+
jlog(VERBOSE2, printf(" %5zu: [%" PRIu64 "/%" PRIu64 "/%zu] %s",
885882
stats.files, sb->st_dev, sb->st_ino,
886-
(size_t) sb->st_nlink, fpath);
883+
(size_t) sb->st_nlink, fpath));
887884

888885
if ((opts.max_size > 0) && ((uintmax_t) sb->st_size > opts.max_size)) {
889-
jlog(JLOG_VERBOSE1,
890-
_("Skipped (greater than configured size) %s"), fpath);
886+
jlog(VERBOSE1,
887+
printf(_("Skipped (greater than configured size) %s"), fpath));
891888
return 0;
892889
}
893890

@@ -915,8 +912,8 @@ static int inserter(const char *fpath, const struct stat *sb,
915912
assert((*node)->st.st_ino == sb->st_ino);
916913

917914
if (has_fpath(*node, fpath)) {
918-
jlog(JLOG_VERBOSE1,
919-
_("Skipped (specified more than once) %s"), fpath);
915+
jlog(VERBOSE1,
916+
printf(_("Skipped (specified more than once) %s"), fpath));
920917
free(fil->links);
921918
} else {
922919
fil->links->next = (*node)->links;
@@ -1121,14 +1118,14 @@ static void visitor(const void *nodep, const VISIT which, const int depth)
11211118

11221119
/* check file attributes, etc. */
11231120
if (!file_may_link_to(master, other)) {
1124-
jlog(JLOG_VERBOSE2,
1125-
_("Skipped (attributes mismatch) %s"), other->links->path);
1121+
jlog(VERBOSE2,
1122+
printf(_("Skipped (attributes mismatch) %s"), other->links->path));
11261123
continue;
11271124
}
11281125
#ifdef USE_REFLINK
11291126
if (may_reflink && reflinks_skip && is_reflink(master, other)) {
1130-
jlog(JLOG_VERBOSE2,
1131-
_("Skipped (already reflink) %s"), other->links->path);
1127+
jlog(VERBOSE2,
1128+
printf(_("Skipped (already reflink) %s"), other->links->path));
11321129
stats.ignored_reflinks++;
11331130
continue;
11341131
}
@@ -1148,8 +1145,8 @@ static void visitor(const void *nodep, const VISIT which, const int depth)
11481145
stats.comparisons++;
11491146

11501147
if (!eq) {
1151-
jlog(JLOG_VERBOSE2,
1152-
_("Skipped (content mismatch) %s"), other->links->path);
1148+
jlog(VERBOSE2,
1149+
printf(_("Skipped (content mismatch) %s"), other->links->path));
11531150
continue;
11541151
}
11551152

@@ -1479,7 +1476,8 @@ int main(int argc, char *argv[])
14791476

14801477
rc = ul_fileeq_init(&fileeq, opts.method);
14811478
if (rc != 0 && strcmp(opts.method, "memcmp") != 0) {
1482-
jlog(JLOG_INFO, _("cannot initialize %s method, use 'memcmp' fallback"), opts.method);
1479+
jlog(INFO, printf(_("cannot initialize %s method, use 'memcmp' fallback"),
1480+
opts.method));
14831481
opts.method = "memcmp";
14841482
rc = ul_fileeq_init(&fileeq, opts.method);
14851483
}
@@ -1505,7 +1503,7 @@ int main(int argc, char *argv[])
15051503
if (opts.exclude_subtree)
15061504
ftw_flags |= FTW_ACTIONRETVAL;
15071505
#endif
1508-
jlog(JLOG_VERBOSE2, _("Scanning [device/inode/links]:"));
1506+
jlog(VERBOSE2, printf(_("Scanning [device/inode/links]:")));
15091507
for (; optind < argc; optind++) {
15101508
char *path = realpath(argv[optind], NULL);
15111509

0 commit comments

Comments
 (0)
0