Blame SOURCES/hardlink.c

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