2018-06-12 12:32:33 -05:00
|
|
|
/*
|
|
|
|
* hardlink - consolidate duplicate files via hardlinks
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Red Hat, Inc. All rights reserved.
|
|
|
|
* Written by Jakub Jelinek <jakub@redhat.com>
|
|
|
|
*
|
2019-02-11 06:18:35 -06:00
|
|
|
* Copyright (C) 2019 Karel Zak <kzak@redhat.com>
|
|
|
|
*
|
2018-06-12 12:32:33 -05:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it would be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2005-01-06 23:58:05 -06:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
2018-06-13 18:13:56 -05:00
|
|
|
#include <getopt.h>
|
2005-01-06 23:58:05 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
2016-07-09 21:03:41 -05:00
|
|
|
#include <errno.h>
|
2018-06-12 07:04:06 -05:00
|
|
|
#ifdef HAVE_PCRE
|
|
|
|
# define PCRE2_CODE_UNIT_WIDTH 8
|
|
|
|
# include <pcre2.h>
|
|
|
|
#endif
|
2005-01-06 23:58:05 -06:00
|
|
|
|
2018-06-13 18:13:56 -05:00
|
|
|
#include "c.h"
|
2019-02-06 10:21:36 -06:00
|
|
|
#include "xalloc.h"
|
2018-06-13 18:13:56 -05:00
|
|
|
#include "nls.h"
|
|
|
|
#include "closestream.h"
|
|
|
|
|
2018-06-12 12:32:33 -05:00
|
|
|
#define NHASH (1<<17) /* Must be a power of 2! */
|
|
|
|
#define NIOBUF (1<<12)
|
|
|
|
#define NBUF 64
|
2005-01-06 23:58:05 -06:00
|
|
|
|
2018-06-12 07:04:06 -05:00
|
|
|
#ifdef HAVE_PCRE
|
2017-03-28 14:16:56 -05:00
|
|
|
pcre2_code *re;
|
|
|
|
PCRE2_SPTR exclude_pattern;
|
|
|
|
pcre2_match_data *match_data;
|
2018-06-12 07:04:06 -05:00
|
|
|
#endif
|
2017-03-28 14:16:56 -05:00
|
|
|
|
2019-02-11 06:00:18 -06:00
|
|
|
struct hardlink_file;
|
|
|
|
|
|
|
|
struct hardlink_hash {
|
|
|
|
struct hardlink_hash *next;
|
|
|
|
struct hardlink_file *chain;
|
2018-06-12 12:32:33 -05:00
|
|
|
off_t size;
|
|
|
|
time_t mtime;
|
2019-02-11 06:00:18 -06:00
|
|
|
};
|
2005-01-06 23:58:05 -06:00
|
|
|
|
2019-02-11 06:00:18 -06:00
|
|
|
struct hardlink_dir {
|
|
|
|
struct hardlink_dir *next;
|
2018-06-12 12:32:33 -05:00
|
|
|
char name[0];
|
2019-02-11 06:00:18 -06:00
|
|
|
};
|
2005-01-06 23:58:05 -06:00
|
|
|
|
2019-02-11 06:00:18 -06:00
|
|
|
struct hardlink_file {
|
|
|
|
struct hardlink_file *next;
|
|
|
|
ino_t ino;
|
|
|
|
dev_t dev;
|
|
|
|
unsigned int cksum;
|
|
|
|
char name[0];
|
|
|
|
};
|
2005-01-06 23:58:05 -06:00
|
|
|
|
2019-02-11 06:00:18 -06:00
|
|
|
struct hardlink_dynstr {
|
|
|
|
char *buf;
|
|
|
|
size_t alloc;
|
|
|
|
};
|
|
|
|
|
2019-02-11 06:18:35 -06:00
|
|
|
static struct hardlink_dir *dirs;
|
|
|
|
static struct hardlink_hash *hps[NHASH];
|
|
|
|
|
|
|
|
static int no_link = 0;
|
|
|
|
static int verbose = 0;
|
|
|
|
static int content_only = 0;
|
|
|
|
static int force = 0;
|
|
|
|
static dev_t dev = 0;
|
2005-01-06 23:58:05 -06:00
|
|
|
|
2019-02-11 06:18:35 -06:00
|
|
|
/* summary counters */
|
|
|
|
static unsigned long long ndirs, nobjects, nregfiles, ncomp, nlinks, nsaved;
|
|
|
|
|
|
|
|
static unsigned int buf[NBUF];
|
|
|
|
static char iobuf1[NIOBUF], iobuf2[NIOBUF];
|
2005-01-06 23:58:05 -06:00
|
|
|
|
2018-06-12 12:32:33 -05:00
|
|
|
__attribute__ ((always_inline))
|
2018-06-12 13:19:21 -05:00
|
|
|
static inline unsigned int hash(off_t size, time_t mtime)
|
2005-01-06 23:58:05 -06:00
|
|
|
{
|
2018-06-12 12:32:33 -05:00
|
|
|
return (size ^ mtime) & (NHASH - 1);
|
2005-01-06 23:58:05 -06:00
|
|
|
}
|
|
|
|
|
2018-06-12 12:32:33 -05:00
|
|
|
__attribute__ ((always_inline))
|
2018-06-12 13:19:21 -05:00
|
|
|
static inline int stcmp(struct stat *st1, struct stat *st2, int content_only)
|
2005-01-06 23:58:05 -06:00
|
|
|
{
|
2018-06-12 12:32:33 -05:00
|
|
|
if (content_only)
|
|
|
|
return st1->st_size != st2->st_size;
|
2019-02-11 06:18:35 -06:00
|
|
|
|
2018-06-12 12:32:33 -05:00
|
|
|
return st1->st_mode != st2->st_mode || st1->st_uid != st2->st_uid ||
|
|
|
|
st1->st_gid != st2->st_gid || st1->st_size != st2->st_size ||
|
|
|
|
st1->st_mtime != st2->st_mtime;
|
2005-01-06 23:58:05 -06:00
|
|
|
}
|
|
|
|
|
2019-02-06 10:21:36 -06:00
|
|
|
static void print_summary(void)
|
2005-01-06 23:58:05 -06:00
|
|
|
{
|
2019-02-06 10:21:36 -06:00
|
|
|
if (!verbose)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (verbose > 1 && nlinks)
|
|
|
|
fputc('\n', stdout);
|
|
|
|
printf(_("Directories: %9lld\n"), ndirs);
|
|
|
|
printf(_("Objects: %9lld\n"), nobjects);
|
|
|
|
printf(_("Regular files: %9lld\n"), nregfiles);
|
|
|
|
printf(_("Comparisons: %9lld\n"), ncomp);
|
|
|
|
printf("%s%9lld\n", (no_link ?
|
|
|
|
_("Would link: ") :
|
|
|
|
_("Linked: ")), nlinks);
|
|
|
|
printf("%s %9lld\n", (no_link ?
|
|
|
|
_("Would save: ") :
|
|
|
|
_("Saved: ")), nsaved);
|
2005-01-06 23:58:05 -06:00
|
|
|
}
|
|
|
|
|
2018-06-13 18:13:56 -05:00
|
|
|
static void __attribute__((__noreturn__)) usage(void)
|
2005-01-06 23:58:05 -06:00
|
|
|
{
|
2018-06-13 18:13:56 -05:00
|
|
|
fputs(USAGE_HEADER, stdout);
|
|
|
|
printf(_(" %s [options] directory...\n"), program_invocation_short_name);
|
|
|
|
|
|
|
|
fputs(USAGE_SEPARATOR, stdout);
|
|
|
|
puts(_("Consolidate duplicate files using hardlinks."));
|
|
|
|
|
|
|
|
fputs(USAGE_OPTIONS, stdout);
|
|
|
|
puts(_(" -c when finding candidates for linking, compare only file contents"));
|
|
|
|
puts(_(" -n don't actually link anything, just report what would be done"));
|
|
|
|
puts(_(" -v print summary after hardlinking"));
|
|
|
|
puts(_(" -vv print every hardlinked file and bytes saved + summary"));
|
|
|
|
puts(_(" -f force hardlinking across filesystems"));
|
|
|
|
puts(_(" -x <regex> exclude files matching pattern"));
|
2019-02-11 06:18:35 -06:00
|
|
|
|
2018-06-13 18:13:56 -05:00
|
|
|
fputs(USAGE_SEPARATOR, stdout);
|
|
|
|
printf(USAGE_HELP_OPTIONS(16)); /* char offset to align option descriptions */
|
|
|
|
printf(USAGE_MAN_TAIL("hardlink(1)"));
|
|
|
|
exit(EXIT_SUCCESS);
|
2005-01-06 23:58:05 -06:00
|
|
|
}
|
|
|
|
|
2018-06-12 12:32:33 -05:00
|
|
|
__attribute__ ((always_inline))
|
2018-06-12 13:19:21 -05:00
|
|
|
static inline size_t add2(size_t a, size_t b)
|
2011-10-20 19:49:08 -05:00
|
|
|
{
|
2018-06-12 12:32:33 -05:00
|
|
|
size_t sum = a + b;
|
2019-02-06 10:21:36 -06:00
|
|
|
if (sum < a)
|
|
|
|
errx(EXIT_FAILURE, _("integer overflow"));
|
2018-06-12 12:32:33 -05:00
|
|
|
return sum;
|
2011-10-20 19:49:08 -05:00
|
|
|
}
|
|
|
|
|
2018-06-12 12:32:33 -05:00
|
|
|
__attribute__ ((always_inline))
|
2018-06-12 13:19:21 -05:00
|
|
|
static inline size_t add3(size_t a, size_t b, size_t c)
|
2011-10-20 19:49:08 -05:00
|
|
|
{
|
2018-06-12 12:32:33 -05:00
|
|
|
return add2(add2(a, b), c);
|
2011-10-20 19:49:08 -05:00
|
|
|
}
|
|
|
|
|
2019-02-11 06:00:18 -06:00
|
|
|
static void growstr(struct hardlink_dynstr *str, size_t newlen)
|
2011-10-20 19:49:08 -05:00
|
|
|
{
|
2018-06-12 12:32:33 -05:00
|
|
|
if (newlen < str->alloc)
|
|
|
|
return;
|
2019-02-06 10:21:36 -06:00
|
|
|
str->buf = xrealloc(str->buf, str->alloc = add2(newlen, 1));
|
2011-10-20 19:49:08 -05:00
|
|
|
}
|
2018-06-12 12:32:33 -05:00
|
|
|
|
2018-06-12 13:19:21 -05:00
|
|
|
static void rf(const char *name)
|
2005-01-06 23:58:05 -06:00
|
|
|
{
|
2018-06-12 12:32:33 -05:00
|
|
|
struct stat st, st2, st3;
|
|
|
|
const size_t namelen = strlen(name);
|
2019-02-11 06:18:35 -06:00
|
|
|
|
2018-06-12 12:32:33 -05:00
|
|
|
nobjects++;
|
|
|
|
if (lstat(name, &st))
|
|
|
|
return;
|
|
|
|
if (st.st_dev != dev && !force) {
|
2019-02-06 10:21:36 -06:00
|
|
|
if (dev)
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
_("%s is on different filesystem than the rest "
|
|
|
|
"(use -f option to override)."), name);
|
2018-06-12 12:32:33 -05:00
|
|
|
dev = st.st_dev;
|
2011-03-03 05:15:33 -06:00
|
|
|
}
|
2018-06-12 12:32:33 -05:00
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2019-02-11 06:00:18 -06:00
|
|
|
struct hardlink_dir *dp = xmalloc(add3(sizeof(*dp), namelen, 1));
|
2018-06-12 12:32:33 -05:00
|
|
|
memcpy(dp->name, name, namelen + 1);
|
|
|
|
dp->next = dirs;
|
|
|
|
dirs = dp;
|
|
|
|
} else if (S_ISREG(st.st_mode)) {
|
|
|
|
int fd, i;
|
2019-02-11 06:00:18 -06:00
|
|
|
struct hardlink_file *fp, *fp2;
|
|
|
|
struct hardlink_hash *hp;
|
2018-06-12 12:32:33 -05:00
|
|
|
const char *n1, *n2;
|
|
|
|
int cksumsize = sizeof(buf);
|
|
|
|
unsigned int cksum;
|
|
|
|
time_t mtime = content_only ? 0 : st.st_mtime;
|
|
|
|
unsigned int hsh = hash(st.st_size, mtime);
|
|
|
|
off_t fsize;
|
2019-02-11 06:00:18 -06:00
|
|
|
|
2018-06-12 12:32:33 -05:00
|
|
|
nregfiles++;
|
|
|
|
if (verbose > 1)
|
2019-02-06 10:13:08 -06:00
|
|
|
printf(" %s", name);
|
2018-06-12 12:32:33 -05:00
|
|
|
fd = open(name, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
2018-06-12 13:19:21 -05:00
|
|
|
if ((size_t)st.st_size < sizeof(buf)) {
|
2018-06-12 12:32:33 -05:00
|
|
|
cksumsize = st.st_size;
|
|
|
|
memset(((char *)buf) + cksumsize, 0,
|
|
|
|
(sizeof(buf) - cksumsize) % sizeof(buf[0]));
|
|
|
|
}
|
|
|
|
if (read(fd, buf, cksumsize) != cksumsize) {
|
|
|
|
close(fd);
|
2019-02-11 06:18:35 -06:00
|
|
|
if (verbose > 1 && namelen <= PATH_MAX)
|
2019-02-06 10:13:08 -06:00
|
|
|
printf("\r%*s\r", (int)(namelen + 2), "");
|
2018-06-12 12:32:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
cksumsize = (cksumsize + sizeof(buf[0]) - 1) / sizeof(buf[0]);
|
|
|
|
for (i = 0, cksum = 0; i < cksumsize; i++) {
|
|
|
|
if (cksum + buf[i] < cksum)
|
|
|
|
cksum += buf[i] + 1;
|
|
|
|
else
|
|
|
|
cksum += buf[i];
|
|
|
|
}
|
|
|
|
for (hp = hps[hsh]; hp; hp = hp->next)
|
|
|
|
if (hp->size == st.st_size && hp->mtime == mtime)
|
|
|
|
break;
|
|
|
|
if (!hp) {
|
2019-02-11 06:00:18 -06:00
|
|
|
hp = xmalloc(sizeof(*hp));
|
2018-06-12 12:32:33 -05:00
|
|
|
hp->size = st.st_size;
|
|
|
|
hp->mtime = mtime;
|
|
|
|
hp->chain = NULL;
|
|
|
|
hp->next = hps[hsh];
|
|
|
|
hps[hsh] = hp;
|
|
|
|
}
|
|
|
|
for (fp = hp->chain; fp; fp = fp->next)
|
|
|
|
if (fp->cksum == cksum)
|
|
|
|
break;
|
|
|
|
for (fp2 = fp; fp2 && fp2->cksum == cksum; fp2 = fp2->next)
|
|
|
|
if (fp2->ino == st.st_ino && fp2->dev == st.st_dev) {
|
|
|
|
close(fd);
|
2019-02-11 06:18:35 -06:00
|
|
|
if (verbose > 1 && namelen <= PATH_MAX)
|
2019-02-06 10:13:08 -06:00
|
|
|
printf("\r%*s\r", (int)(namelen + 2), "");
|
2018-06-12 12:32:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (fp2 = fp; fp2 && fp2->cksum == cksum; fp2 = fp2->next)
|
|
|
|
if (!lstat(fp2->name, &st2) && S_ISREG(st2.st_mode) &&
|
|
|
|
!stcmp(&st, &st2, content_only) &&
|
|
|
|
st2.st_ino != st.st_ino &&
|
|
|
|
st2.st_dev == st.st_dev) {
|
|
|
|
int fd2 = open(fp2->name, O_RDONLY);
|
|
|
|
if (fd2 < 0)
|
|
|
|
continue;
|
|
|
|
if (fstat(fd2, &st2) || !S_ISREG(st2.st_mode)
|
|
|
|
|| st2.st_size == 0) {
|
|
|
|
close(fd2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ncomp++;
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
for (fsize = st.st_size; fsize > 0;
|
|
|
|
fsize -= NIOBUF) {
|
2019-02-06 10:13:08 -06:00
|
|
|
ssize_t xsz;
|
|
|
|
off_t rsize = fsize >= NIOBUF ? NIOBUF : fsize;
|
|
|
|
|
|
|
|
if ((xsz = read(fd, iobuf1, rsize)) != rsize)
|
|
|
|
warn(_("cannot read %s"), name);
|
|
|
|
else if ((xsz = read(fd2, iobuf2, rsize)) != rsize)
|
|
|
|
warn(_("cannot read %s"), fp2->name);
|
|
|
|
|
|
|
|
if (xsz != rsize) {
|
2018-06-12 12:32:33 -05:00
|
|
|
close(fd);
|
|
|
|
close(fd2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (memcmp(iobuf1, iobuf2, rsize))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd2);
|
|
|
|
if (fsize > 0)
|
|
|
|
continue;
|
|
|
|
if (lstat(name, &st3)) {
|
2019-02-06 10:13:08 -06:00
|
|
|
warn(_("cannot stat %s"), name);
|
2018-06-12 12:32:33 -05:00
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
st3.st_atime = st.st_atime;
|
|
|
|
if (stcmp(&st, &st3, 0)) {
|
2019-02-06 10:13:08 -06:00
|
|
|
warnx(_("file %s changed underneath us"), name);
|
2018-06-12 12:32:33 -05:00
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
n1 = fp2->name;
|
|
|
|
n2 = name;
|
|
|
|
if (!no_link) {
|
|
|
|
const char *suffix =
|
|
|
|
".$$$___cleanit___$$$";
|
|
|
|
const size_t suffixlen = strlen(suffix);
|
|
|
|
size_t n2len = strlen(n2);
|
2019-02-11 06:00:18 -06:00
|
|
|
struct hardlink_dynstr nam2 = { NULL, 0 };
|
2018-06-12 12:32:33 -05:00
|
|
|
growstr(&nam2, add2(n2len, suffixlen));
|
|
|
|
memcpy(nam2.buf, n2, n2len);
|
|
|
|
memcpy(&nam2.buf[n2len], suffix,
|
|
|
|
suffixlen + 1);
|
|
|
|
/* First create a temporary link to n1 under a new name */
|
|
|
|
if (link(n1, nam2.buf)) {
|
2019-02-06 10:13:08 -06:00
|
|
|
warn(_("failed to hardlink %s to %s (create temporary link as %s failed)"),
|
|
|
|
n1, n2, nam2.buf);
|
2018-06-12 12:32:33 -05:00
|
|
|
free(nam2.buf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Then rename into place over the existing n2 */
|
|
|
|
if (rename(nam2.buf, n2)) {
|
2019-02-06 10:13:08 -06:00
|
|
|
warn(_("failed to hardlink %s to %s (rename temporary link to %s failed)"),
|
|
|
|
n1, n2, n2);
|
2018-06-12 12:32:33 -05:00
|
|
|
/* Something went wrong, try to remove the now redundant temporary link */
|
2019-02-06 10:13:08 -06:00
|
|
|
if (unlink(nam2.buf))
|
|
|
|
warn(_("failed to remove temporary link %s"), nam2.buf);
|
2018-06-12 12:32:33 -05:00
|
|
|
free(nam2.buf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
free(nam2.buf);
|
|
|
|
}
|
|
|
|
nlinks++;
|
|
|
|
if (st3.st_nlink > 1) {
|
|
|
|
/* We actually did not save anything this time, since the link second argument
|
|
|
|
had some other links as well. */
|
|
|
|
if (verbose > 1)
|
2019-02-06 10:13:08 -06:00
|
|
|
printf(_("\r%*s\r%s %s to %s\n"),
|
2019-02-11 06:18:35 -06:00
|
|
|
(int)(((namelen > PATH_MAX) ? 0 : namelen) + 2),
|
2018-06-12 12:32:33 -05:00
|
|
|
"",
|
2019-02-06 10:13:08 -06:00
|
|
|
(no_link ? _("Would link") : _("Linked")),
|
|
|
|
n1, n2);
|
2018-06-12 12:32:33 -05:00
|
|
|
} else {
|
|
|
|
nsaved +=
|
|
|
|
((st.st_size + 4095) / 4096) * 4096;
|
|
|
|
if (verbose > 1)
|
2019-02-06 10:13:08 -06:00
|
|
|
printf(_("\r%*s\r%s %s to %s, %s %jd\n"),
|
2019-02-11 06:18:35 -06:00
|
|
|
(int)(((namelen > PATH_MAX) ? 0 : namelen) + 2),
|
2018-06-12 12:32:33 -05:00
|
|
|
"",
|
2019-02-06 10:13:08 -06:00
|
|
|
(no_link ? _("Would link") : _("Linked")),
|
|
|
|
n1, n2,
|
|
|
|
(no_link ? _("would save") : _("saved")),
|
2018-06-12 13:19:21 -05:00
|
|
|
(intmax_t)st.st_size);
|
2018-06-12 12:32:33 -05:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
2019-02-11 06:00:18 -06:00
|
|
|
fp2 = xmalloc(add3(sizeof(*fp2), namelen, 1));
|
2018-06-12 12:32:33 -05:00
|
|
|
close(fd);
|
|
|
|
fp2->ino = st.st_ino;
|
|
|
|
fp2->dev = st.st_dev;
|
|
|
|
fp2->cksum = cksum;
|
|
|
|
memcpy(fp2->name, name, namelen + 1);
|
|
|
|
if (fp) {
|
|
|
|
fp2->next = fp->next;
|
|
|
|
fp->next = fp2;
|
|
|
|
} else {
|
|
|
|
fp2->next = hp->chain;
|
|
|
|
hp->chain = fp2;
|
|
|
|
}
|
2019-02-11 06:18:35 -06:00
|
|
|
if (verbose > 1 && namelen <= PATH_MAX)
|
2019-02-06 10:13:08 -06:00
|
|
|
printf("\r%*s\r", (int)(namelen + 2), "");
|
2018-06-12 12:32:33 -05:00
|
|
|
return;
|
2005-01-06 23:58:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2018-06-12 12:32:33 -05:00
|
|
|
int ch;
|
|
|
|
int i;
|
2018-06-12 07:04:06 -05:00
|
|
|
#ifdef HAVE_PCRE
|
2018-06-12 12:32:33 -05:00
|
|
|
int errornumber;
|
|
|
|
PCRE2_SIZE erroroffset;
|
2018-06-12 07:04:06 -05:00
|
|
|
#endif
|
2019-02-11 06:00:18 -06:00
|
|
|
struct hardlink_dynstr nam1 = { NULL, 0 };
|
2018-06-13 18:13:56 -05:00
|
|
|
|
|
|
|
static const struct option longopts[] = {
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ NULL, 0, NULL, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
atexit(close_stdout);
|
|
|
|
|
|
|
|
while ((ch = getopt_long(argc, argv, "cnvfx:Vh", longopts, NULL)) != -1) {
|
2018-06-12 12:32:33 -05:00
|
|
|
switch (ch) {
|
|
|
|
case 'n':
|
|
|
|
no_link++;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
content_only++;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
force = 1;
|
|
|
|
break;
|
|
|
|
case 'x':
|
2018-06-12 07:04:06 -05:00
|
|
|
#ifdef HAVE_PCRE
|
2018-06-12 12:32:33 -05:00
|
|
|
exclude_pattern = (PCRE2_SPTR) optarg;
|
2018-06-12 07:04:06 -05:00
|
|
|
#else
|
2018-06-13 18:13:56 -05:00
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
_("option -x not supported (built without pcre2)"));
|
2018-06-12 07:04:06 -05:00
|
|
|
#endif
|
2018-06-12 12:32:33 -05:00
|
|
|
break;
|
2018-06-13 18:13:56 -05:00
|
|
|
case 'V':
|
|
|
|
printf(UTIL_LINUX_VERSION);
|
|
|
|
return EXIT_SUCCESS;
|
2018-06-12 12:32:33 -05:00
|
|
|
case 'h':
|
2018-06-13 18:13:56 -05:00
|
|
|
usage();
|
2018-06-12 12:32:33 -05:00
|
|
|
default:
|
2018-06-13 18:13:56 -05:00
|
|
|
errtryhelp(EXIT_FAILURE);
|
2018-06-12 12:32:33 -05:00
|
|
|
}
|
|
|
|
}
|
2018-06-13 18:13:56 -05:00
|
|
|
|
|
|
|
if (optind == argc) {
|
|
|
|
warnx(_("no directory specified"));
|
|
|
|
errtryhelp(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2018-06-12 07:04:06 -05:00
|
|
|
#ifdef HAVE_PCRE
|
2018-06-12 12:32:33 -05:00
|
|
|
if (exclude_pattern) {
|
|
|
|
re = pcre2_compile(exclude_pattern, /* the pattern */
|
|
|
|
PCRE2_ZERO_TERMINATED, /* indicates pattern is zero-terminate */
|
|
|
|
0, /* default options */
|
|
|
|
&errornumber, &erroroffset, NULL); /* use default compile context */
|
|
|
|
if (!re) {
|
|
|
|
PCRE2_UCHAR buffer[256];
|
|
|
|
pcre2_get_error_message(errornumber, buffer,
|
|
|
|
sizeof(buffer));
|
2018-06-13 18:13:56 -05:00
|
|
|
errx(EXIT_FAILURE, _("pattern error at offset %d: %s"),
|
2018-06-12 12:32:33 -05:00
|
|
|
(int)erroroffset, buffer);
|
|
|
|
}
|
|
|
|
match_data = pcre2_match_data_create_from_pattern(re, NULL);
|
|
|
|
}
|
2018-06-12 07:04:06 -05:00
|
|
|
#endif
|
2019-02-06 10:21:36 -06:00
|
|
|
atexit(print_summary);
|
|
|
|
|
2018-06-12 12:32:33 -05:00
|
|
|
for (i = optind; i < argc; i++)
|
|
|
|
rf(argv[i]);
|
|
|
|
while (dirs) {
|
|
|
|
DIR *dh;
|
|
|
|
struct dirent *di;
|
2019-02-11 06:00:18 -06:00
|
|
|
struct hardlink_dir *dp = dirs;
|
2018-06-12 12:32:33 -05:00
|
|
|
size_t nam1baselen = strlen(dp->name);
|
|
|
|
dirs = dp->next;
|
|
|
|
growstr(&nam1, add2(nam1baselen, 1));
|
|
|
|
memcpy(nam1.buf, dp->name, nam1baselen);
|
|
|
|
free(dp);
|
|
|
|
nam1.buf[nam1baselen++] = '/';
|
|
|
|
nam1.buf[nam1baselen] = 0;
|
|
|
|
dh = opendir(nam1.buf);
|
|
|
|
if (dh == NULL)
|
|
|
|
continue;
|
|
|
|
ndirs++;
|
|
|
|
while ((di = readdir(dh)) != NULL) {
|
|
|
|
if (!di->d_name[0])
|
|
|
|
continue;
|
|
|
|
if (di->d_name[0] == '.') {
|
|
|
|
if (!di->d_name[1] || !strcmp(di->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-12 07:04:06 -05:00
|
|
|
#ifdef HAVE_PCRE
|
2018-06-12 12:32:33 -05:00
|
|
|
if (re && pcre2_match(re, /* compiled regex */
|
|
|
|
(PCRE2_SPTR) di->d_name, strlen(di->d_name), 0, /* start at offset 0 */
|
|
|
|
0, /* default options */
|
|
|
|
match_data, /* block for storing the result */
|
|
|
|
NULL) /* use default match context */
|
|
|
|
>=0) {
|
|
|
|
if (verbose) {
|
|
|
|
nam1.buf[nam1baselen] = 0;
|
2019-02-06 10:13:08 -06:00
|
|
|
printf(_("Skipping %s%s\n"), nam1.buf, di->d_name);
|
2018-06-12 12:32:33 -05:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-12 07:04:06 -05:00
|
|
|
#endif
|
2018-06-12 12:32:33 -05:00
|
|
|
{
|
|
|
|
size_t subdirlen;
|
|
|
|
growstr(&nam1,
|
|
|
|
add2(nam1baselen, subdirlen =
|
|
|
|
strlen(di->d_name)));
|
|
|
|
memcpy(&nam1.buf[nam1baselen], di->d_name,
|
|
|
|
add2(subdirlen, 1));
|
|
|
|
}
|
|
|
|
rf(nam1.buf);
|
|
|
|
}
|
|
|
|
closedir(dh);
|
|
|
|
}
|
2019-02-06 10:21:36 -06:00
|
|
|
|
2018-06-12 12:32:33 -05:00
|
|
|
return 0;
|
2005-01-06 23:58:05 -06:00
|
|
|
}
|