aa76c5
/* Copyright (C) 2001 Red Hat, Inc.
aa76c5
aa76c5
   Written by Jakub Jelinek <jakub@redhat.com>.
7f38a6
aa76c5
   This program is free software; you can redistribute it and/or
aa76c5
   modify it under the terms of the GNU General Public License as
aa76c5
   published by the Free Software Foundation; either version 2 of the
aa76c5
   License, or (at your option) any later version.
7f38a6
aa76c5
   This program is distributed in the hope that it will be useful,
aa76c5
   but WITHOUT ANY WARRANTY; without even the implied warranty of
aa76c5
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
aa76c5
   General Public License for more details.
7f38a6
aa76c5
   You should have received a copy of the GNU General Public
aa76c5
   License along with this program; see the file COPYING.  If not,
aa76c5
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
aa76c5
   Boston, MA 02111-1307, USA.  */
7f38a6
1ada99
/*  Changes by Rémy Card to use constants and add option -n.  */
7f38a6
/*  Changes by Jindrich Novy to add option -h, replace mmap(2), fix overflows */
aa76c5
aa76c5
#define _GNU_SOURCE
aa76c5
#include <sys types.h="">
aa76c5
#include <stdlib.h>
aa76c5
#include <stdio.h>
aa76c5
#include <unistd.h>
aa76c5
#include <sys stat.h="">
aa76c5
#include <sys mman.h="">
aa76c5
#include <string.h>
aa76c5
#include <dirent.h>
aa76c5
#include <fcntl.h>
aa76c5
838510
#define NHASH	(1<<17)	/* Must be a power of 2! */
838510
#define NIOBUF	(1<<12)
aa76c5
#define NAMELEN	4096
aa76c5
#define NBUF	64
aa76c5
aa76c5
struct _f;
aa76c5
typedef struct _h {
aa76c5
  struct _h *next;
aa76c5
  struct _f *chain;
aa76c5
  off_t size;
aa76c5
  time_t mtime;
aa76c5
} h;
aa76c5
aa76c5
typedef struct _d {
aa76c5
  struct _d *next;
aa76c5
  char name[0];
aa76c5
} d;
aa76c5
aa76c5
d *dirs;
aa76c5
aa76c5
h *hps[NHASH];
aa76c5
aa76c5
int no_link = 0;
aa76c5
int verbose = 0;
aa76c5
int content_only = 0;
aa76c5
aa76c5
typedef struct _f {
aa76c5
  struct _f *next;
aa76c5
  ino_t ino;
aa76c5
  dev_t dev;
aa76c5
  unsigned int cksum;
aa76c5
  char name[0];
aa76c5
} f;
aa76c5
aa76c5
inline unsigned int hash(off_t size, time_t mtime)
aa76c5
{
aa76c5
  return (size ^ mtime) & (NHASH - 1);
aa76c5
}
aa76c5
aa76c5
inline int stcmp(struct stat *st1, struct stat *st2, int content_only)
aa76c5
{
aa76c5
  if (content_only)
aa76c5
    return st1->st_size != st2->st_size;
aa76c5
  return st1->st_mode != st2->st_mode || st1->st_uid != st2->st_uid ||
aa76c5
         st1->st_gid != st2->st_gid || st1->st_size != st2->st_size ||
aa76c5
         st1->st_mtime != st2->st_mtime;
aa76c5
}
aa76c5
838510
long long ndirs, nobjects, nregfiles, ncomp, nlinks, nsaved;
aa76c5
aa76c5
void doexit(int i)
aa76c5
{
7f38a6
  if (verbose) {
aa76c5
    fprintf(stderr, "\n\n");
aa76c5
    fprintf(stderr, "Directories %lld\n", ndirs);
aa76c5
    fprintf(stderr, "Objects %lld\n", nobjects);
aa76c5
    fprintf(stderr, "IFREG %lld\n", nregfiles);
aa76c5
    fprintf(stderr, "Comparisons %lld\n", ncomp);
aa76c5
    fprintf(stderr, "%s %lld\n", (no_link ? "Would link" : "Linked"), nlinks);
aa76c5
    fprintf(stderr, "%s %lld\n", (no_link ? "Would save" : "saved"), nsaved);
aa76c5
  }
aa76c5
  exit(i);
aa76c5
}
aa76c5
8c8d15
void usage(char *prog)
aa76c5
{
56723b
  fprintf (stderr, "Usage: %s [-cnvh] directories...\n", prog);
88049c
  fprintf (stderr, "  -c    When finding candidates for linking, compare only file contents.\n");
88049c
  fprintf (stderr, "  -n    Don't actually link anything, just report what would be done.\n");
3f7d2b
  fprintf (stderr, "  -v    Print summary after hardlinking.\n");
3f7d2b
  fprintf (stderr, "  -vv   Print every hardlinked file and bytes saved + summary.\n");
56723b
  fprintf (stderr, "  -h    Show help.\n");
aa76c5
  exit(255);
aa76c5
}
aa76c5
aa76c5
unsigned int buf[NBUF];
838510
char iobuf1[NIOBUF], iobuf2[NIOBUF];
aa76c5
7f38a6
inline size_t add2(size_t a, size_t b)
7f38a6
{
7f38a6
  size_t sum = a + b;
7f38a6
  if (sum < a) {
7f38a6
    fprintf(stderr, "\nInteger overflow\n");
7f38a6
    doexit(5);
7f38a6
  }
7f38a6
  return sum;
7f38a6
}
7f38a6
7f38a6
inline size_t add3(size_t a, size_t b, size_t c)
7f38a6
{
7f38a6
  return add2(add2(a, b), c);
7f38a6
}
7f38a6
7f38a6
typedef struct {
7f38a6
  char *buf;
7f38a6
  size_t alloc;
7f38a6
} dynstr;
7f38a6
7f38a6
void growstr(dynstr *str, size_t newlen)
7f38a6
{
7f38a6
  if (newlen < str->alloc)
7f38a6
    return;
7f38a6
  str->buf = realloc(str->buf, str->alloc = add2(newlen, 1));
7f38a6
  if (!str->buf) {
7f38a6
    fprintf(stderr, "\nOut of memory 4\n");
7f38a6
    doexit(4);
7f38a6
  }
7f38a6
}
7f38a6
7f38a6
void rf (const char *name)
aa76c5
{
aa76c5
  struct stat st, st2, st3;
7f38a6
  const size_t namelen = strlen(name);
aa76c5
  nobjects++;
aa76c5
  if (lstat (name, &st))
aa76c5
    return;
aa76c5
  if (S_ISDIR (st.st_mode)) {
7f38a6
    d * dp = malloc(add3(sizeof(d), namelen, 1));
aa76c5
    if (!dp) {
aa76c5
      fprintf(stderr, "\nOut of memory 3\n");
aa76c5
      doexit(3);
aa76c5
    }
7f38a6
    memcpy(dp->name, name, namelen + 1);
aa76c5
    dp->next = dirs;
aa76c5
    dirs = dp;
aa76c5
  } else if (S_ISREG (st.st_mode)) {
aa76c5
    int fd, i;
aa76c5
    f * fp, * fp2;
aa76c5
    h * hp;
7f38a6
    const char *n1, *n2;
aa76c5
    int cksumsize = sizeof(buf);
aa76c5
    unsigned int cksum;
aa76c5
    time_t mtime = content_only ? 0 : st.st_mtime;
aa76c5
    unsigned int hsh = hash (st.st_size, mtime);
838510
    off_t fsize;
aa76c5
    nregfiles++;
aa76c5
    if (verbose > 1)
aa76c5
      fprintf(stderr, "  %s", name);
aa76c5
    fd = open (name, O_RDONLY);
aa76c5
    if (fd < 0) return;
aa76c5
    if (st.st_size < sizeof(buf)) {
aa76c5
      cksumsize = st.st_size;
aa76c5
      memset (((char *)buf) + cksumsize, 0, (sizeof(buf) - cksumsize) % sizeof(buf[0]));
aa76c5
    }
aa76c5
    if (read (fd, buf, cksumsize) != cksumsize) {
aa76c5
      close(fd);
7f38a6
      if (verbose > 1 && namelen <= NAMELEN)
7f38a6
        fprintf(stderr, "\r%*s\r", (int)(namelen + 2), "");
aa76c5
      return;
aa76c5
    }
aa76c5
    cksumsize = (cksumsize + sizeof(buf[0]) - 1) / sizeof(buf[0]);
aa76c5
    for (i = 0, cksum = 0; i < cksumsize; i++) {
aa76c5
      if (cksum + buf[i] < cksum)
aa76c5
        cksum += buf[i] + 1;
aa76c5
      else
aa76c5
        cksum += buf[i];
aa76c5
    }
aa76c5
    for (hp = hps[hsh]; hp; hp = hp->next)
aa76c5
      if (hp->size == st.st_size && hp->mtime == mtime)
aa76c5
        break;
aa76c5
    if (!hp) {
aa76c5
      hp = malloc(sizeof(h));
aa76c5
      if (!hp) {
aa76c5
        fprintf(stderr, "\nOut of memory 1\n");
aa76c5
        doexit(1);
aa76c5
      }
aa76c5
      hp->size = st.st_size;
aa76c5
      hp->mtime = mtime;
aa76c5
      hp->chain = NULL;
aa76c5
      hp->next = hps[hsh];
aa76c5
      hps[hsh] = hp;
aa76c5
    }
aa76c5
    for (fp = hp->chain; fp; fp = fp->next)
aa76c5
      if (fp->cksum == cksum)
aa76c5
        break;
aa76c5
    for (fp2 = fp; fp2 && fp2->cksum == cksum; fp2 = fp2->next)
aa76c5
      if (fp2->ino == st.st_ino && fp2->dev == st.st_dev) {
aa76c5
        close(fd);
7f38a6
        if (verbose > 1 && namelen <= NAMELEN)
7f38a6
          fprintf(stderr, "\r%*s\r", (int)(namelen + 2), "");
aa76c5
        return;
aa76c5
      }
aa76c5
    for (fp2 = fp; fp2 && fp2->cksum == cksum; fp2 = fp2->next)
aa76c5
      if (!lstat (fp2->name, &st2) && S_ISREG (st2.st_mode) &&
aa76c5
          !stcmp (&st, &st2, content_only) &&
aa76c5
          st2.st_ino != st.st_ino &&
aa76c5
          st2.st_dev == st.st_dev) {
aa76c5
        int fd2 = open (fp2->name, O_RDONLY);
aa76c5
        if (fd2 < 0) continue;
687892
        if (fstat (fd2, &st2) || !S_ISREG (st2.st_mode) || st2.st_size == 0) {
aa76c5
          close (fd2);
aa76c5
          continue;
aa76c5
        }
aa76c5
        ncomp++;
838510
	lseek(fd, 0, SEEK_SET);
838510
	for (fsize = st.st_size; fsize > 0; fsize -= NIOBUF) {
838510
	  off_t rsize = fsize >= NIOBUF ? NIOBUF : fsize;
838510
	  if (read (fd, iobuf1, rsize) != rsize || read (fd2, iobuf2, rsize) != rsize) {
838510
	    close(fd);
838510
	    close(fd2);
838510
	    fprintf(stderr, "\nReading error\n");
838510
	    return;
838510
	  }
838510
	  if (memcmp (iobuf1, iobuf2, rsize)) break;
838510
	}
838510
	close(fd2);
838510
	if (fsize > 0) continue;
aa76c5
        if (lstat (name, &st3)) {
aa76c5
          fprintf(stderr, "\nCould not stat %s again\n", name);
aa76c5
          close(fd);
aa76c5
          return;
aa76c5
        }
aa76c5
        st3.st_atime = st.st_atime;
aa76c5
        if (stcmp (&st, &st3, 0)) {
aa76c5
          fprintf(stderr, "\nFile %s changed underneath us\n", name);
aa76c5
          close(fd);
aa76c5
          return;
aa76c5
        }
aa76c5
        n1 = fp2->name;
aa76c5
        n2 = name;
aa76c5
        if (!no_link) {
7f38a6
          const char *suffix = ".$$$___cleanit___$$$";
7f38a6
          const size_t suffixlen = strlen(suffix);
7f38a6
          size_t n2len = strlen(n2);
7f38a6
          dynstr nam2 = {NULL, 0};
7f38a6
          growstr(&nam2, add2(n2len, suffixlen));
7f38a6
          memcpy(nam2.buf, n2, n2len);
7f38a6
          memcpy(&nam2.buf[n2len], suffix, suffixlen + 1);
7f38a6
          if (rename (n2, nam2.buf)) {
7f38a6
            fprintf(stderr, "\nFailed to rename %s to %s\n", n2, nam2.buf);
7f38a6
            free(nam2.buf);
aa76c5
            continue;
aa76c5
          }
aa76c5
          if (link (n1, n2)) {
aa76c5
            fprintf(stderr, "\nFailed to hardlink %s to %s\n", n1, n2);
7f38a6
            if (rename (nam2.buf, n2)) {
7f38a6
              fprintf(stderr, "\nBad bad - failed to rename back %s to %s\n", nam2.buf, n2);
aa76c5
            }
aa76c5
            close(fd);
7f38a6
            free(nam2.buf);
aa76c5
            return;
aa76c5
          }
7f38a6
          unlink (nam2.buf);
7f38a6
          free(nam2.buf);
aa76c5
        }
aa76c5
        nlinks++;
aa76c5
        if (st3.st_nlink > 1) {
aa76c5
	  /* We actually did not save anything this time, since the link second argument
aa76c5
	     had some other links as well.  */
aa76c5
          if (verbose > 1)
7f38a6
            fprintf(stderr, "\r%*s\r%s %s to %s\n", (int)(((namelen > NAMELEN) ? 0 : namelen) + 2), "", (no_link ? "Would link" : "Linked"), n1, n2);
aa76c5
        } else {
aa76c5
          nsaved+=((st.st_size+4095)/4096)*4096;
aa76c5
          if (verbose > 1)
7f38a6
            fprintf(stderr, "\r%*s\r%s %s to %s, %s %ld\n", (int)(((namelen > NAMELEN) ? 0 : namelen) + 2), "", (no_link ? "Would link" : "Linked"), n1, n2, (no_link ? "would save" : "saved"), st.st_size);
aa76c5
	}
aa76c5
        close(fd);
aa76c5
        return;
aa76c5
      }
7f38a6
    fp2 = malloc(add3(sizeof(f), namelen, 1));
aa76c5
    if (!fp2) {
aa76c5
      fprintf(stderr, "\nOut of memory 2\n");
aa76c5
      doexit(2);
aa76c5
    }
aa76c5
    close(fd);
aa76c5
    fp2->ino = st.st_ino;
aa76c5
    fp2->dev = st.st_dev;
aa76c5
    fp2->cksum = cksum;
7f38a6
    memcpy(fp2->name, name, namelen + 1);
aa76c5
    if (fp) {
aa76c5
      fp2->next = fp->next;
aa76c5
      fp->next = fp2;
aa76c5
    } else {
aa76c5
      fp2->next = hp->chain;
aa76c5
      hp->chain = fp2;
aa76c5
    }
7f38a6
    if (verbose > 1 && namelen <= NAMELEN)
7f38a6
      fprintf(stderr, "\r%*s\r", (int)(namelen + 2), "");
aa76c5
    return;
aa76c5
  }
aa76c5
}
aa76c5
aa76c5
int main(int argc, char **argv)
aa76c5
{
aa76c5
  int ch;
aa76c5
  int i;
7f38a6
  dynstr nam1 = {NULL, 0};
56723b
  while ((ch = getopt (argc, argv, "cnvh")) != -1) {
aa76c5
    switch (ch) {
aa76c5
    case 'n':
aa76c5
      no_link++;
aa76c5
      break;
aa76c5
    case 'v':
aa76c5
      verbose++;
aa76c5
      break;
aa76c5
    case 'c':
aa76c5
      content_only++;
aa76c5
      break;
56723b
    case 'h':
aa76c5
    default:
aa76c5
      usage(argv[0]);
aa76c5
    }
aa76c5
  }
aa76c5
  if (optind >= argc)
aa76c5
    usage(argv[0]);
aa76c5
  for (i = optind; i < argc; i++)
aa76c5
    rf(argv[i]);
aa76c5
  while (dirs) {
7f38a6
    DIR *dh;
7f38a6
    struct dirent *di;
7f38a6
    d * dp = dirs;
7f38a6
    size_t nam1baselen = strlen(dp->name);
aa76c5
    dirs = dp->next;
7f38a6
    growstr(&nam1, add2(nam1baselen, 1));
7f38a6
    memcpy(nam1.buf, dp->name, nam1baselen);
aa76c5
    free (dp);
7f38a6
    nam1.buf[nam1baselen++] = '/';
7f38a6
    nam1.buf[nam1baselen] = 0;
7f38a6
    dh = opendir (nam1.buf);
aa76c5
    if (dh == NULL)
aa76c5
      continue;
aa76c5
    ndirs++;
aa76c5
    while ((di = readdir (dh)) != NULL) {
aa76c5
      if (!di->d_name[0])
aa76c5
        continue;
aa76c5
      if (di->d_name[0] == '.') {
aa76c5
        char *q;
aa76c5
        if (!di->d_name[1] || !strcmp (di->d_name, "..") || !strncmp (di->d_name, ".in.", 4))
aa76c5
          continue;
aa76c5
        q = strrchr (di->d_name, '.');
aa76c5
        if (q && strlen (q) == 7 && q != di->d_name) {
7f38a6
          nam1.buf[nam1baselen] = 0;
aa76c5
          if (verbose)
7f38a6
            fprintf(stderr, "Skipping %s%s\n", nam1.buf, di->d_name);
aa76c5
          continue;
aa76c5
        }
aa76c5
      }
7f38a6
      {
7f38a6
        size_t subdirlen;
7f38a6
        growstr(&nam1, add2(nam1baselen, subdirlen = strlen(di->d_name)));
7f38a6
        memcpy(&nam1.buf[nam1baselen], di->d_name, add2(subdirlen, 1));
7f38a6
      }
7f38a6
      rf(nam1.buf);
aa76c5
    }
aa76c5
    closedir(dh);
aa76c5
  }
aa76c5
  doexit(0);
aa76c5
  return 0;
aa76c5
}