Bump to 2.2.1
[platform/upstream/fdupes.git] / fdupes.c
index 46caa80..94bc581 100644 (file)
--- a/fdupes.c
+++ b/fdupes.c
@@ -1,4 +1,4 @@
-/* FDUPES Copyright (c) 1999-2018 Adrian Lopez
+/* FDUPES Copyright (c) 1999-2022 Adrian Lopez
 
    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation files
 #include "ncurses-interface.h"
 #endif
 #include "fdupes.h"
+#include "confirmmatch.h"
 #include "errormsg.h"
 #include "log.h"
 #include "sigint.h"
 #include "flags.h"
+#include "removeifnotchanged.h"
 
 long long minsize = -1;
 long long maxsize = -1;
@@ -62,32 +64,8 @@ char *program_name;
 
 ordertype_t ordertype = ORDER_MTIME;
 
-#define CHUNK_SIZE 8192
-
-#define INPUT_SIZE 256
-
-#define PARTIAL_MD5_SIZE 4096
-
 #define MD5_DIGEST_LENGTH 16
 
-/* 
-
-TODO: Partial sums (for working with very large files).
-
-typedef struct _signature
-{
-  md5_state_t state;
-  md5_byte_t  digest[16];
-} signature_t;
-
-typedef struct _signatures
-{
-  int         num_signatures;
-  signature_t *signatures;
-} signatures_t;
-
-*/
-
 typedef struct _filetree {
   file_t *file; 
   struct _filetree *left;
@@ -304,7 +282,7 @@ int grokdir(char *dir, file_t **filelistp, struct stat *logfile_status)
       }
 
       /* ignore logfile */
-      if (info.st_dev == logfile_status->st_dev && info.st_ino == logfile_status->st_ino)
+      if (logfile_status != 0 && info.st_dev == logfile_status->st_dev && info.st_ino == logfile_status->st_ino)
       {
         free(newfile->d_name);
         free(newfile);
@@ -628,7 +606,6 @@ file_t **checkmatch(filetree_t **root, filetree_t *checktree, file_t *file)
     }
 
     cmpresult = md5cmp(file->crcpartial, checktree->file->crcpartial);
-    /*if (cmpresult != 0) errormsg("    on %s vs %s\n", file->d_name, checktree->file->d_name);*/
 
     if (cmpresult == 0) {
       if (checktree->file->crcsignature == NULL) {
@@ -656,11 +633,6 @@ file_t **checkmatch(filetree_t **root, filetree_t *checktree, file_t *file)
       }
 
       cmpresult = md5cmp(file->crcsignature, checktree->file->crcsignature);
-      /*if (cmpresult != 0) errormsg("P   on %s vs %s\n", 
-          file->d_name, checktree->file->d_name);
-      else errormsg("P F on %s vs %s\n", file->d_name,
-          checktree->file->d_name);
-      printf("%s matches %s\n", file->d_name, checktree->file->d_name);*/
     }
   }
 
@@ -684,30 +656,6 @@ file_t **checkmatch(filetree_t **root, filetree_t *checktree, file_t *file)
   }
 }
 
-/* Do a bit-for-bit comparison in case two different files produce the 
-   same signature. Unlikely, but better safe than sorry. */
-
-int confirmmatch(FILE *file1, FILE *file2)
-{
-  unsigned char c1[CHUNK_SIZE];
-  unsigned char c2[CHUNK_SIZE];
-  size_t r1;
-  size_t r2;
-  
-  fseek(file1, 0, SEEK_SET);
-  fseek(file2, 0, SEEK_SET);
-
-  do {
-    r1 = fread(c1, sizeof(unsigned char), sizeof(c1), file1);
-    r2 = fread(c2, sizeof(unsigned char), sizeof(c2), file2);
-
-    if (r1 != r2) return 0; /* file lengths are different */
-    if (memcmp (c1, c2, r1)) return 0; /* file contents are different */
-  } while (r2);
-  
-  return 1;
-}
-
 void summarizematches(file_t *files)
 {
   int numsets = 0;
@@ -843,6 +791,7 @@ void deletefiles(file_t *files, int prompt, FILE *tty, char *logfile)
   file_t *curfile;
   file_t **dupelist;
   int *preserve;
+  int firstpreserved;
   char *preservestr;
   char *token;
   char *tstr;
@@ -853,6 +802,10 @@ void deletefiles(file_t *files, int prompt, FILE *tty, char *logfile)
   int i;
   struct log_info *loginfo;
   int log_error;
+  FILE *file1;
+  FILE *file2;
+  int ismatch;
+  char *errorstring;
 
   curfile = files;
   
@@ -1022,18 +975,59 @@ void deletefiles(file_t *files, int prompt, FILE *tty, char *logfile)
             log_file_remaining(loginfo, dupelist[x]->d_name);
         }
        else {
-         if (remove(dupelist[x]->d_name) == 0) {
-           printf("   [-] %s\n", dupelist[x]->d_name);
+    if (ISFLAG(flags, F_DEFERCONFIRMATION))
+    {
+      firstpreserved = 0;
+      for (i = 1; i <= counter; ++i)
+      {
+        if (preserve[i])
+        {
+          firstpreserved = i;
+          break;
+        }
+      }
 
-            if (loginfo)
-              log_file_deleted(loginfo, dupelist[x]->d_name);
-         } else {
-           printf("   [!] %s ", dupelist[x]->d_name);
-           printf("-- unable to delete file!\n");
+      file1 = fopen(dupelist[x]->d_name, "rb");
+      file2 = fopen(dupelist[firstpreserved]->d_name, "rb");
 
-            if (loginfo)
-              log_file_remaining(loginfo, dupelist[x]->d_name);
-         }
+      if (file1 && file2)
+        ismatch = confirmmatch(file1, file2);
+      else
+        ismatch = 0;
+
+      if (file2)
+        fclose(file2);
+
+      if (file1)
+        fclose(file1);
+    }
+    else
+    {
+      ismatch = 1;
+    }
+
+    if (ismatch) {
+      if (removeifnotchanged(dupelist[x], &errorstring) == 0) {
+        printf("   [-] %s\n", dupelist[x]->d_name);
+
+        if (loginfo)
+          log_file_deleted(loginfo, dupelist[x]->d_name);
+      }
+      else {
+        printf("   [!] %s ", dupelist[x]->d_name);
+        printf("-- unable to delete file: %s!\n", errorstring);
+
+        if (loginfo)
+          log_file_remaining(loginfo, dupelist[x]->d_name);
+      }
+    }
+    else {
+      printf("   [!] %s\n", dupelist[x]->d_name);
+      printf(" -- unable to confirm match; file not deleted!\n");
+
+      if (loginfo)
+        log_file_remaining(loginfo, dupelist[x]->d_name);
+    }
        }
       }
       printf("\n");
@@ -1133,11 +1127,12 @@ void registerpair(file_t **matchlist, file_t *newmatch,
   }
 }
 
-void deletesuccessor(file_t **existing, file_t *duplicate, 
+void deletesuccessor(file_t **existing, file_t *duplicate, int matchconfirmed,
       int (*comparef)(file_t *f1, file_t *f2), struct log_info *loginfo)
 {
   file_t *to_keep;
   file_t *to_delete;
+  char *errorstring;
 
   if (comparef(duplicate, *existing) >= 0)
   {
@@ -1162,14 +1157,25 @@ void deletesuccessor(file_t **existing, file_t *duplicate,
   if (loginfo)
     log_file_remaining(loginfo, to_keep->d_name);
 
-  if (remove(to_delete->d_name) == 0) {
-    printf("   [-] %s\n", to_delete->d_name);
+  if (matchconfirmed)
+  {
+    if (removeifnotchanged(to_delete, &errorstring) == 0) {
+      printf("   [-] %s\n", to_delete->d_name);
 
-    if (loginfo)
-      log_file_deleted(loginfo, to_delete->d_name);
-  } else {
-    printf("   [!] %s ", to_delete->d_name);
-    printf("-- unable to delete file!\n");
+      if (loginfo)
+        log_file_deleted(loginfo, to_delete->d_name);
+    } else {
+      printf("   [!] %s ", to_delete->d_name);
+      printf("-- unable to delete file: %s!\n", errorstring);
+
+      if (loginfo)
+        log_file_remaining(loginfo, to_delete->d_name);
+    }
+  }
+  else
+  {
+    printf("   [!] %s\n", to_delete->d_name);
+    printf(" -- unable to confirm match; file not deleted!\n");
 
     if (loginfo)
       log_file_remaining(loginfo, to_delete->d_name);
@@ -1213,6 +1219,8 @@ void help_text()
   printf("                         with -s or --symlinks, or when specifying a\n");
   printf("                         particular directory more than once; refer to the\n");
   printf("                         fdupes documentation for additional information\n");
+  printf(" -D --deferconfirmation  in interactive mode, defer byte-for-byte confirmation\n");
+  printf("                         of duplicates until just before file deletion\n");
 #ifndef NO_NCURSES
   printf(" -P --plain              with --delete, use line-based prompt (as with older\n");
   printf("                         versions of fdupes) instead of screen-mode interface\n");
@@ -1283,6 +1291,7 @@ int main(int argc, char **argv) {
     { "order", 1, 0, 'o' },
     { "reverse", 0, 0, 'i' },
     { "log", 1, 0, 'l' },
+    { "deferconfirmation", 0, 0, 'D' },
     { 0, 0, 0, 0 }
   };
 #define GETOPT getopt_long
@@ -1296,7 +1305,7 @@ int main(int argc, char **argv) {
 
   oldargv = cloneargs(argc, argv);
 
-  while ((opt = GETOPT(argc, argv, "frRq1StsHG:L:nAdPvhNImpo:il:"
+  while ((opt = GETOPT(argc, argv, "frRq1StsHG:L:nAdPvhNImpo:il:D"
 #ifdef HAVE_GETOPT_H
           , long_options, NULL
 #endif
@@ -1393,6 +1402,9 @@ int main(int argc, char **argv) {
     case 'l':
       logfile = optarg;
       break;
+    case 'D':
+      SETFLAG(flags, F_DEFERCONFIRMATION);
+      break;
     default:
       fprintf(stderr, "Try `fdupes --help' for more information.\n");
       exit(1);
@@ -1414,6 +1426,12 @@ int main(int argc, char **argv) {
     exit(1);
   }
 
+  if (ISFLAG(flags, F_DEFERCONFIRMATION) && (!ISFLAG(flags, F_DELETEFILES) || ISFLAG(flags, F_NOPROMPT)))
+  {
+    errormsg("--deferconfirmation only works with interactive deletion modes\n");
+    exit(1);
+  }
+
   if (!ISFLAG(flags, F_DELETEFILES))
     logfile = 0;
 
@@ -1450,16 +1468,16 @@ int main(int argc, char **argv) {
 
     /* F_RECURSE is not set for directories before --recurse: */
     for (x = optind; x < firstrecurse; x++)
-      filecount += grokdir(argv[x], &files, &logfile_status);
+      filecount += grokdir(argv[x], &files, logfile ? &logfile_status : 0);
 
     /* Set F_RECURSE for directories after --recurse: */
     SETFLAG(flags, F_RECURSE);
 
     for (x = firstrecurse; x < argc; x++)
-      filecount += grokdir(argv[x], &files, &logfile_status);
+      filecount += grokdir(argv[x], &files, logfile ? &logfile_status : 0);
   } else {
     for (x = optind; x < argc; x++)
-      filecount += grokdir(argv[x], &files, &logfile_status);
+      filecount += grokdir(argv[x], &files, logfile ? &logfile_status : 0);
   }
 
   if (!files) {
@@ -1489,19 +1507,19 @@ int main(int argc, char **argv) {
        continue;
       }
 
-      if (confirmmatch(file1, file2)) {
-        if (ISFLAG(flags, F_DELETEFILES) && ISFLAG(flags, F_IMMEDIATE))
-          deletesuccessor(match, curfile,
+      if (ISFLAG(flags, F_DELETEFILES) && ISFLAG(flags, F_IMMEDIATE))
+      {
+          deletesuccessor(match, curfile, confirmmatch(file1, file2),
               ordertype == ORDER_MTIME ? sort_pairs_by_mtime :
               ordertype == ORDER_CTIME ? sort_pairs_by_ctime :
                                          sort_pairs_by_filename, loginfo );
-        else
-          registerpair(match, curfile,
-              ordertype == ORDER_MTIME ? sort_pairs_by_mtime :
-              ordertype == ORDER_CTIME ? sort_pairs_by_ctime :
-                                         sort_pairs_by_filename );
       }
-      
+      else if (ISFLAG(flags, F_DEFERCONFIRMATION) || confirmmatch(file1, file2))
+        registerpair(match, curfile,
+            ordertype == ORDER_MTIME ? sort_pairs_by_mtime :
+            ordertype == ORDER_CTIME ? sort_pairs_by_ctime :
+                                       sort_pairs_by_filename );
+
       fclose(file1);
       fclose(file2);
     }