hardlink: fix indention

The original Julian's hardlink.c code is pretty readable, but for
util-linux we use a little different indention (aka Linux kernel
coding style).

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2021-02-17 11:48:03 +01:00
parent 4bf9ebca2b
commit 5c7cac85c9
1 changed files with 591 additions and 574 deletions

View File

@ -139,6 +139,7 @@ static struct options {
regex_t preg;
struct regex_link *next;
} *include, *exclude;
signed int verbosity;
unsigned int respect_mode:1;
unsigned int respect_owner:1;
@ -182,12 +183,12 @@ static int last_signal;
* @level: The log level
* @format: A format string for printf()
*/
__attribute__ ((format(printf, 2, 3)))
__attribute__((format(printf, 2, 3)))
static void jlog(enum log_level level, const char *format, ...)
{
va_list args;
if (quiet || level > (unsigned int) opts.verbosity)
if (quiet || level > (unsigned int)opts.verbosity)
return;
va_start(args, format);
@ -215,9 +216,10 @@ static void jlog(enum log_level level, const char *format, ...)
*/
static int regexec_any(struct regex_link *pregs, const char *what)
{
for (; pregs != NULL; pregs = pregs->next)
for (; pregs != NULL; pregs = pregs->next) {
if (regexec(&pregs->preg, what, 0, NULL, 0) == 0)
return TRUE;
}
return FALSE;
}
@ -285,13 +287,16 @@ static void print_stats(void)
opts.dry_run ? _("dry-run") : _("real"));
jlog(JLOG_SUMMARY, "%-15s %zu", _("Files:"), stats.files);
jlog(JLOG_SUMMARY, _("%-15s %zu files"), _("Linked:"), stats.linked);
#ifdef HAVE_SYS_XATTR_H
jlog(JLOG_SUMMARY, _("%-15s %zu xattrs"), _("Compared:"), stats.xattr_comparisons);
#endif
jlog(JLOG_SUMMARY, _("%-15s %zu files"), _("Compared:"), stats.comparisons);
ssz = size_to_human_string(SIZE_SUFFIX_3LETTER|
SIZE_SUFFIX_SPACE|
#ifdef HAVE_SYS_XATTR_H
jlog(JLOG_SUMMARY, _("%-15s %zu xattrs"), _("Compared:"),
stats.xattr_comparisons);
#endif
jlog(JLOG_SUMMARY, _("%-15s %zu files"), _("Compared:"),
stats.comparisons);
ssz = size_to_human_string(SIZE_SUFFIX_3LETTER |
SIZE_SUFFIX_SPACE |
SIZE_DECIMAL_2DIGITS, stats.saved);
jlog(JLOG_SUMMARY, "%-15s %s", _("Saved:"), ssz);
@ -345,13 +350,14 @@ static ssize_t llistxattr_or_die(const char *path, char *list, size_t size)
*
* This does the same thing as lgetxattr() except that it aborts upon error.
*/
static ssize_t lgetxattr_or_die(const char *path, const char *name, void *value,
size_t size)
static ssize_t lgetxattr_or_die(const char *path,
const char *name, void *value, size_t size)
{
ssize_t len = lgetxattr(path, name, value, size);
if (len < 0)
err(EXIT_FAILURE, _("cannot get xattr value of %s for %s"), name, path);
err(EXIT_FAILURE, _("cannot get xattr value of %s for %s"),
name, path);
return len;
}
@ -380,7 +386,7 @@ static int get_xattr_name_count(const char *const names, ssize_t len)
*/
static int cmp_xattr_name_ptrs(const void *ptr1, const void *ptr2)
{
return strcmp(*(char *const *) ptr1, *(char *const *) ptr2);
return strcmp(*(char *const *)ptr1, *(char *const *)ptr2);
}
/**
@ -469,8 +475,10 @@ static int file_xattrs_equal(const struct file *a, const struct file *b)
if (strcmp(name_ptrs_a[i], name_ptrs_b[i]) != 0)
goto exit; // names at same slot differ
len_a = lgetxattr_or_die(a->links->path, name_ptrs_a[i], NULL, 0);
len_b = lgetxattr_or_die(b->links->path, name_ptrs_b[i], NULL, 0);
len_a =
lgetxattr_or_die(a->links->path, name_ptrs_a[i], NULL, 0);
len_b =
lgetxattr_or_die(b->links->path, name_ptrs_b[i], NULL, 0);
if (len_a != len_b)
goto exit; // xattrs with same name, different value lengths
@ -504,12 +512,12 @@ static int file_xattrs_equal(const struct file *a, const struct file *b)
free(value_b);
return ret;
}
#else
#else /* !HAVE_SYS_XATTR_H */
static int file_xattrs_equal(const struct file *a, const struct file *b)
{
return TRUE;
}
#endif
#endif /* HAVE_SYS_XATTR_H */
/**
* file_contents_equal - Compare contents of two files for equality
@ -530,7 +538,8 @@ static int file_contents_equal(const struct file *a, const struct file *b)
assert(a->links != NULL);
assert(b->links != NULL);
jlog(JLOG_VERBOSE1, _("Comparing %s to %s"), a->links->path, b->links->path);
jlog(JLOG_VERBOSE1, _("Comparing %s to %s"), a->links->path,
b->links->path);
stats.comparisons++;
@ -574,7 +583,8 @@ static int file_contents_equal(const struct file *a, const struct file *b)
if (fa == NULL || fb == NULL)
warn(_("cannot open %s"), fa ? b->links->path : a->links->path);
else
warn(_("cannot read %s"), ferror(fa) ? a->links->path : b->links->path);
warn(_("cannot read %s"),
ferror(fa) ? a->links->path : b->links->path);
cmp = 1;
goto out;
}
@ -651,25 +661,30 @@ static int file_link(struct file *a, struct file *b)
assert(a->links != NULL);
assert(b->links != NULL);
ssz = size_to_human_string(SIZE_SUFFIX_3LETTER|
SIZE_SUFFIX_SPACE|
ssz = size_to_human_string(SIZE_SUFFIX_3LETTER |
SIZE_SUFFIX_SPACE |
SIZE_DECIMAL_2DIGITS, a->st.st_size);
jlog(JLOG_INFO, _("%sLinking %s to %s (-%s)"),
opts.dry_run ? _("[DryRun] ") : "", a->links->path, b->links->path, ssz);
opts.dry_run ? _("[DryRun] ") : "", a->links->path, b->links->path,
ssz);
free(ssz);
if (!opts.dry_run) {
size_t len = strlen(b->links->path) + strlen(".hardlink-temporary") + 1;
size_t len =
strlen(b->links->path) + strlen(".hardlink-temporary") + 1;
char *new_path = xmalloc(len);
snprintf(new_path, len, "%s.hardlink-temporary", b->links->path);
snprintf(new_path, len, "%s.hardlink-temporary",
b->links->path);
if (link(a->links->path, new_path) != 0) {
warn(_("cannot link %s to %s"), a->links->path, new_path);
warn(_("cannot link %s to %s"), a->links->path,
new_path);
free(new_path);
return FALSE;
} else if (rename(new_path, b->links->path) != 0) {
warn(_("cannot rename %s to %s"), a->links->path, new_path);
warn(_("cannot rename %s to %s"), a->links->path,
new_path);
unlink(new_path); /* cleanup failed rename */
free(new_path);
return FALSE;
@ -696,7 +711,7 @@ static int file_link(struct file *a, struct file *b)
a->links->next = new_link;
}
// Do it again
/* Do it again */
if (b->links)
goto file_link;
@ -713,8 +728,8 @@ static int file_link(struct file *a, struct file *b)
* Called by nftw() for the files. See the manual page for nftw() for
* further information.
*/
static int inserter(const char *fpath, const struct stat *sb, int typeflag,
struct FTW *ftwbuf)
static int inserter(const char *fpath, const struct stat *sb,
int typeflag, struct FTW *ftwbuf)
{
struct file *fil;
struct file **node;
@ -739,7 +754,8 @@ static int inserter(const char *fpath, const struct stat *sb, int typeflag,
stats.files++;
if ((uintmax_t) sb->st_size < opts.min_size) {
jlog(JLOG_VERBOSE1, _("Skipped %s (smaller than configured size)"), fpath);
jlog(JLOG_VERBOSE1,
_("Skipped %s (smaller than configured size)"), fpath);
return 0;
}
@ -785,7 +801,8 @@ static int inserter(const char *fpath, const struct stat *sb, int typeflag,
*node = fil;
} else {
for (l = *node; l != NULL; l = l->next) {
if (l->next != NULL && file_compare(fil, l->next) < 0)
if (l->next != NULL
&& file_compare(fil, l->next) < 0)
continue;
fil->next = l->next;
@ -799,7 +816,7 @@ static int inserter(const char *fpath, const struct stat *sb, int typeflag,
return 0;
fail:
fail:
warn(_("cannot continue")); /* probably ENOMEM */
return 0;
}
@ -816,10 +833,10 @@ fail:
*/
static void visitor(const void *nodep, const VISIT which, const int depth)
{
struct file *master = *(struct file **) nodep;
struct file *master = *(struct file **)nodep;
struct file *other;
(void) depth;
(void)depth;
if (which != leaf && which != endorder)
return;
@ -837,7 +854,8 @@ static void visitor(const void *nodep, const VISIT which, const int depth)
assert(other != other->next);
assert(other->st.st_size == master->st.st_size);
if (other->links == NULL || !file_may_link_to(master, other))
if (other->links == NULL
|| !file_may_link_to(master, other))
continue;
if (!file_link(master, other) && errno == EMLINK)
@ -854,14 +872,15 @@ static void __attribute__((__noreturn__)) usage(void)
FILE *out = stdout;
fputs(USAGE_HEADER, out);
fprintf(out, _(" %s [options] <directory>|<file> ...\n"), program_invocation_short_name);
fprintf(out, _(" %s [options] <directory>|<file> ...\n"),
program_invocation_short_name);
fputs(USAGE_SEPARATOR, out);
fputs(_("Consolidate duplicate files using hardlinks.\n"), out);
fputs(USAGE_OPTIONS, out);
fputs(_(" -v, --verbose verbose output (repeat for more verbosity)\n"), out);
fputs(_(" -q, --quiet quiet mode - don't print anything\n"
fputs(_(" -q, --quiet quiet mode - don't print anything\n"), out);
fputs(_(" -n, --dry-run don't actually link anything\n"), out);
fputs(_(" -f, --respect-name filenames have to be identical\n"), out);
fputs(_(" -p, --ignore-mode ignore changes of file mode\n"), out);
@ -882,7 +901,6 @@ static void __attribute__((__noreturn__)) usage(void)
fputs(USAGE_SEPARATOR, out);
printf(USAGE_HELP_OPTIONS(28));
printf(USAGE_MAN_TAIL("hardlink(1)"));
exit(EXIT_SUCCESS);
@ -895,9 +913,11 @@ static void __attribute__((__noreturn__)) usage(void)
*/
static void register_regex(struct regex_link **pregs, const char *regex)
{
struct regex_link *link = xmalloc(sizeof(*link));
struct regex_link *link;
int err;
link = xmalloc(sizeof(*link));
if ((err = regcomp(&link->preg, regex, REG_NOSUB | REG_EXTENDED)) != 0) {
size_t size = regerror(err, &link->preg, NULL, 0);
char *buf = xmalloc(size + 1);
@ -907,9 +927,7 @@ static void register_regex(struct regex_link **pregs, const char *regex)
errx(EXIT_FAILURE, _("could not compile regular expression %s: %s"),
regex, buf);
}
link->next = *pregs;
*pregs = link;
link->next = *pregs; *pregs = link;
}
/**
@ -941,8 +959,8 @@ static int parse_options(int argc, char *argv[])
{NULL, 0, NULL, 0}
};
static const ul_excl_t excl[] = {
{ 'q','v' },
{ 0 }
{'q', 'v'},
{0}
};
int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
int c;
@ -1001,21 +1019,20 @@ static int parse_options(int argc, char *argv[])
case 's':
opts.min_size = strtosize_or_err(optarg, _("failed to parse size"));
break;
case 'h':
usage();
case 'V':
print_version(EXIT_SUCCESS);
default:
errtryhelp(EXIT_FAILURE);
}
errtryhelp(EXIT_FAILURE);}
}
return 0;
}
/**
* to_be_called_atexit - Cleanup handler, also prints statistics.
*/
* to_be_called_atexit - Cleanup handler, also prints statistics.
*/
static void to_be_called_atexit(void)
{
if (stats.started)
@ -1023,9 +1040,9 @@ static void to_be_called_atexit(void)
}
/**
* sighandler - Signal handler, sets the global last_signal variable
* @i: The signal number
*/
* sighandler - Signal handler, sets the global last_signal variable
* @i: The signal number
*/
static void sighandler(int i)
{
if (last_signal != SIGINT)
@ -1060,11 +1077,11 @@ int main(int argc, char *argv[])
gettime_monotonic(&stats.start_time);
stats.started = TRUE;
for (; optind < argc; optind++)
for (; optind < argc; optind++) {
if (nftw(argv[optind], inserter, 20, FTW_PHYS) == -1)
warn(_("cannot process %s"), argv[optind]);
}
twalk(files, visitor);
return 0;
}