Bump to 2.2.1
[platform/upstream/fdupes.git] / fdupes.c
index 7bab2a8..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;
@@ -126,14 +104,6 @@ void escapefilename(char *escape_list, char **filename_ptr)
   }
 }
 
-off_t filesize(char *filename) {
-  struct stat s;
-
-  if (stat(filename, &s) != 0) return -1;
-
-  return s.st_size;
-}
-
 dev_t getdevice(char *filename) {
   struct stat s;
 
@@ -150,27 +120,11 @@ ino_t getinode(char *filename) {
   return s.st_ino;   
 }
 
-time_t getmtime(char *filename) {
-  struct stat s;
-
-  if (stat(filename, &s) != 0) return 0;
-
-  return s.st_mtime;
-}
-
-time_t getctime(char *filename) {
-  struct stat s;
-
-  if (stat(filename, &s) != 0) return 0;
-
-  return s.st_ctime;
-}
-
-char *fmtmtime(char *filename) {
+char *fmttime(time_t t) {
   static char buf[64];
-  time_t t = getmtime(filename);
 
   strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", localtime(&t));
+
   return buf;
 }
 
@@ -230,22 +184,13 @@ int nonoptafter(char *option, int argc, char **oldargv,
   return x;
 }
 
-void getfilestats(file_t *file)
+void getfilestats(file_t *file, struct stat *info, struct stat *linfo)
 {
-  file->size = filesize(file->d_name);
-  file->inode = getinode(file->d_name);
-  file->device = getdevice(file->d_name);
-
-  switch (ordertype)
-  {
-    case ORDER_CTIME:
-      file->sorttime = getctime(file->d_name);
-      break;
-    case ORDER_MTIME:
-    default:
-      file->sorttime = getmtime(file->d_name);
-      break;
-  }
+  file->size = info->st_size;;
+  file->inode = info->st_ino;
+  file->device = info->st_dev;
+  file->ctime = info->st_ctime;
+  file->mtime = info->st_mtime;
 }
 
 int grokdir(char *dir, file_t **filelistp, struct stat *logfile_status)
@@ -260,7 +205,6 @@ int grokdir(char *dir, file_t **filelistp, struct stat *logfile_status)
   static int progress = 0;
   static char indicator[] = "-\\|/";
   char *fullname, *name;
-  off_t size;
 
   cd = opendir(dir);
 
@@ -319,6 +263,7 @@ int grokdir(char *dir, file_t **filelistp, struct stat *logfile_status)
        if (name[0] == '.' && strcmp(name, ".") && strcmp(name, "..") ) {
          free(newfile->d_name);
          free(newfile);
+         free(fullname);
          continue;
        }
        free(fullname);
@@ -330,14 +275,14 @@ int grokdir(char *dir, file_t **filelistp, struct stat *logfile_status)
         continue;
       }
       
-      size = filesize(newfile->d_name);
-      if (!S_ISDIR(info.st_mode) && (((size == 0 && ISFLAG(flags, F_EXCLUDEEMPTY)) || size < minsize || (size > maxsize && maxsize != -1)))) {
+      if (!S_ISDIR(info.st_mode) && (((info.st_size == 0 && ISFLAG(flags, F_EXCLUDEEMPTY)) || info.st_size < minsize || (info.st_size > maxsize && maxsize != -1)))) {
         free(newfile->d_name);
         free(newfile);
         continue;
       }
 
-      if (info.st_dev == logfile_status->st_dev && info.st_ino == logfile_status->st_ino)
+      /* ignore logfile */
+      if (logfile_status != 0 && info.st_dev == logfile_status->st_dev && info.st_ino == logfile_status->st_ino)
       {
         free(newfile->d_name);
         free(newfile);
@@ -357,7 +302,7 @@ int grokdir(char *dir, file_t **filelistp, struct stat *logfile_status)
        free(newfile);
       } else {
        if (S_ISREG(linfo.st_mode) || (S_ISLNK(linfo.st_mode) && ISFLAG(flags, F_FOLLOWLINKS))) {
-         getfilestats(newfile);
+         getfilestats(newfile, &info, &linfo);
          *filelistp = newfile;
          filecount++;
        } else {
@@ -373,9 +318,8 @@ int grokdir(char *dir, file_t **filelistp, struct stat *logfile_status)
   return filecount;
 }
 
-md5_byte_t *getcrcsignatureuntil(char *filename, off_t max_read)
+md5_byte_t *getcrcsignatureuntil(char *filename, off_t fsize, off_t max_read)
 {
-  off_t fsize;
   off_t toread;
   md5_state_t state;
   static md5_byte_t digest[MD5_DIGEST_LENGTH];  
@@ -383,9 +327,6 @@ md5_byte_t *getcrcsignatureuntil(char *filename, off_t max_read)
   FILE *file;
    
   md5_init(&state);
-
-  fsize = filesize(filename);
   
   if (max_read != 0 && fsize > max_read)
     fsize = max_read;
@@ -414,14 +355,14 @@ md5_byte_t *getcrcsignatureuntil(char *filename, off_t max_read)
   return digest;
 }
 
-md5_byte_t *getcrcsignature(char *filename)
+md5_byte_t *getcrcsignature(char *filename, off_t fsize)
 {
-  return getcrcsignatureuntil(filename, 0);
+  return getcrcsignatureuntil(filename, fsize, 0);
 }
 
-md5_byte_t *getcrcpartialsignature(char *filename)
+md5_byte_t *getcrcpartialsignature(char *filename, off_t fsize)
 {
-  return getcrcsignatureuntil(filename, PARTIAL_MD5_SIZE);
+  return getcrcsignatureuntil(filename, fsize, PARTIAL_MD5_SIZE);
 }
 
 int md5cmp(const md5_byte_t *a, const md5_byte_t *b)
@@ -606,7 +547,6 @@ file_t **checkmatch(filetree_t **root, filetree_t *checktree, file_t *file)
 {
   int cmpresult;
   md5_byte_t *crcsignature;
-  off_t fsize;
 
   if (ISFLAG(flags, F_CONSIDERHARDLINKS))
   {
@@ -625,20 +565,18 @@ file_t **checkmatch(filetree_t **root, filetree_t *checktree, file_t *file)
     if (is_hardlink(checktree, file))
       return NULL;
   }
-
-  fsize = filesize(file->d_name);
   
-  if (fsize < checktree->file->size) 
+  if (file->size < checktree->file->size)
     cmpresult = -1;
   else 
-    if (fsize > checktree->file->size) cmpresult = 1;
+    if (file->size > checktree->file->size) cmpresult = 1;
   else
     if (ISFLAG(flags, F_PERMISSIONS) &&
         !same_permissions(file->d_name, checktree->file->d_name))
         cmpresult = -1;
   else {
     if (checktree->file->crcpartial == NULL) {
-      crcsignature = getcrcpartialsignature(checktree->file->d_name);
+      crcsignature = getcrcpartialsignature(checktree->file->d_name, checktree->file->size);
       if (crcsignature == NULL) {
         errormsg ("cannot read file %s\n", checktree->file->d_name);
         return NULL;
@@ -653,7 +591,7 @@ file_t **checkmatch(filetree_t **root, filetree_t *checktree, file_t *file)
     }
 
     if (file->crcpartial == NULL) {
-      crcsignature = getcrcpartialsignature(file->d_name);
+      crcsignature = getcrcpartialsignature(file->d_name, file->size);
       if (crcsignature == NULL) {
         errormsg ("cannot read file %s\n", file->d_name);
         return NULL;
@@ -668,11 +606,10 @@ 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) {
-       crcsignature = getcrcsignature(checktree->file->d_name);
+       crcsignature = getcrcsignature(checktree->file->d_name, checktree->file->size);
        if (crcsignature == NULL) return NULL;
 
        checktree->file->crcsignature = (md5_byte_t*) malloc(MD5_DIGEST_LENGTH * sizeof(md5_byte_t));
@@ -684,7 +621,7 @@ file_t **checkmatch(filetree_t **root, filetree_t *checktree, file_t *file)
       }
 
       if (file->crcsignature == NULL) {
-       crcsignature = getcrcsignature(file->d_name);
+       crcsignature = getcrcsignature(file->d_name, file->size);
        if (crcsignature == NULL) return NULL;
 
        file->crcsignature = (md5_byte_t*) malloc(MD5_DIGEST_LENGTH * sizeof(md5_byte_t));
@@ -696,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);*/
     }
   }
 
@@ -724,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;
@@ -797,14 +705,14 @@ void printmatches(file_t *files)
        if (ISFLAG(flags, F_SHOWSIZE)) printf("%lld byte%seach:\n", (long long int)files->size,
         (files->size != 1) ? "s " : " ");
         if (ISFLAG(flags, F_SHOWTIME))
-          printf("%s ", fmtmtime(files->d_name));
+          printf("%s ", fmttime(files->mtime));
        if (ISFLAG(flags, F_DSAMELINE)) escapefilename("\\ ", &files->d_name);
        printf("%s%c", files->d_name, ISFLAG(flags, F_DSAMELINE)?' ':'\n');
       }
       tmpfile = files->duplicates;
       while (tmpfile != NULL) {
         if (ISFLAG(flags, F_SHOWTIME))
-          printf("%s ", fmtmtime(tmpfile->d_name));
+          printf("%s ", fmttime(tmpfile->mtime));
        if (ISFLAG(flags, F_DSAMELINE)) escapefilename("\\ ", &tmpfile->d_name);
        printf("%s%c", tmpfile->d_name, ISFLAG(flags, F_DSAMELINE)?' ':'\n');
        tmpfile = tmpfile->duplicates;
@@ -883,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;
@@ -893,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;
   
@@ -939,7 +852,7 @@ void deletefiles(file_t *files, int prompt, FILE *tty, char *logfile)
       if (prompt) 
       {
         if (ISFLAG(flags, F_SHOWTIME))
-          printf("[%d] [%s] %s\n", counter, fmtmtime(files->d_name), files->d_name);
+          printf("[%d] [%s] %s\n", counter, fmttime(files->mtime), files->d_name);
         else
           printf("[%d] %s\n", counter, files->d_name);
       }
@@ -951,7 +864,7 @@ void deletefiles(file_t *files, int prompt, FILE *tty, char *logfile)
         if (prompt)
         {
           if (ISFLAG(flags, F_SHOWTIME))
-            printf("[%d] [%s] %s\n", counter, fmtmtime(tmpfile->d_name), tmpfile->d_name);
+            printf("[%d] [%s] %s\n", counter, fmttime(tmpfile->mtime), tmpfile->d_name);
           else
             printf("[%d] %s\n", counter, tmpfile->d_name);
         }
@@ -1062,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");
@@ -1101,16 +1055,26 @@ int sort_pairs_by_arrival(file_t *f1, file_t *f2)
   return !ISFLAG(flags, F_REVERSE) ? -1 : 1;
 }
 
-int sort_pairs_by_time(file_t *f1, file_t *f2)
+int sort_pairs_by_ctime(file_t *f1, file_t *f2)
 {
-  if (f1->sorttime < f2->sorttime)
+  if (f1->ctime < f2->ctime)
     return !ISFLAG(flags, F_REVERSE) ? -1 : 1;
-  else if (f1->sorttime > f2->sorttime)
+  else if (f1->ctime > f2->ctime)
     return !ISFLAG(flags, F_REVERSE) ? 1 : -1;
 
   return 0;
 }
 
+int sort_pairs_by_mtime(file_t *f1, file_t *f2)
+{
+  if (f1->mtime < f2->mtime)
+    return !ISFLAG(flags, F_REVERSE) ? -1 : 1;
+  else if (f1->mtime > f2->mtime)
+    return !ISFLAG(flags, F_REVERSE) ? 1 : -1;
+  else
+    return sort_pairs_by_ctime(f1, f2);
+}
+
 int sort_pairs_by_filename(file_t *f1, file_t *f2)
 {
   int strvalue = strcmp(f1->d_name, f2->d_name);
@@ -1163,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)
   {
@@ -1184,24 +1149,41 @@ void deletesuccessor(file_t **existing, file_t *duplicate,
 
   if (!ISFLAG(flags, F_HIDEPROGRESS)) fprintf(stderr, "\r%40s\r", " ");
 
+  if (loginfo)
+    log_begin_set(loginfo);
+
   printf("   [+] %s\n", to_keep->d_name);
 
   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);
   }
 
+  if (loginfo)
+    log_end_set(loginfo);
+
   printf("\n");
 }
 
@@ -1209,49 +1191,54 @@ void help_text()
 {
   printf("Usage: fdupes [options] DIRECTORY...\n\n");
 
-  printf(" -r --recurse     \tfor every directory given follow subdirectories\n");
-  printf("                  \tencountered within\n");
-  printf(" -R --recurse:    \tfor each directory given after this option follow\n");
-  printf("                  \tsubdirectories encountered within (note the ':' at\n");
-  printf("                  \tthe end of the option, manpage for more details)\n");
-  printf(" -s --symlinks    \tfollow symlinks\n");
-  printf(" -H --hardlinks   \tnormally, when two or more files point to the same\n");
-  printf("                  \tdisk area they are treated as non-duplicates; this\n"); 
-  printf("                  \toption will change this behavior\n");
-  printf(" -G --minsize=SIZE\tconsider only files greater than or equal to SIZE\n");
-  printf(" -L --maxsize=SIZE\tconsider only files less than or equal to SIZE\n");
-  printf(" -n --noempty     \texclude zero-length files from consideration\n");
-  printf(" -A --nohidden    \texclude hidden files from consideration\n");
-  printf(" -f --omitfirst   \tomit the first file in each set of matches\n");
-  printf(" -1 --sameline    \tlist each set of matches on a single line\n");
-  printf(" -S --size        \tshow size of duplicate files\n");
-  printf(" -t --time        \tshow modification time of duplicate files\n");
-  printf(" -m --summarize   \tsummarize dupe information\n");
-  printf(" -q --quiet       \thide progress indicator\n");
-  printf(" -d --delete      \tprompt user for files to preserve and delete all\n"); 
-  printf("                  \tothers; important: under particular circumstances,\n");
-  printf("                  \tdata may be lost when using this option together\n");
-  printf("                  \twith -s or --symlinks, or when specifying a\n");
-  printf("                  \tparticular directory more than once; refer to the\n");
-  printf("                  \tfdupes documentation for additional information\n");
+  /*     0        1 0       2 0       3 0       4 0       5 0       6 0       7 0       8 0
+  -------"---------|---------|---------|---------|---------|---------|---------|---------|"
+  */
+  printf(" -r --recurse            for every directory given follow subdirectories\n");
+  printf("                         encountered within\n");
+  printf(" -R --recurse:           for each directory given after this option follow\n");
+  printf("                         subdirectories encountered within (note the ':' at the\n");
+  printf("                         end of the option, manpage for more details)\n");
+  printf(" -s --symlinks           follow symlinks\n");
+  printf(" -H --hardlinks          normally, when two or more files point to the same\n");
+  printf("                         disk area they are treated as non-duplicates; this\n");
+  printf("                         option will change this behavior\n");
+  printf(" -G --minsize=SIZE       consider only files greater than or equal to SIZE bytes\n");
+  printf(" -L --maxsize=SIZE       consider only files less than or equal to SIZE bytes\n");
+  printf(" -n --noempty            exclude zero-length files from consideration\n");
+  printf(" -A --nohidden           exclude hidden files from consideration\n");
+  printf(" -f --omitfirst          omit the first file in each set of matches\n");
+  printf(" -1 --sameline           list each set of matches on a single line\n");
+  printf(" -S --size               show size of duplicate files\n");
+  printf(" -t --time               show modification time of duplicate files\n");
+  printf(" -m --summarize          summarize dupe information\n");
+  printf(" -q --quiet              hide progress indicator\n");
+  printf(" -d --delete             prompt user for files to preserve and delete all\n");
+  printf("                         others; important: under particular circumstances,\n");
+  printf("                         data may be lost when using this option together\n");
+  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       \twith --delete, use line-based prompt (as with older\n");
-  printf("                  \tversions of fdupes) instead of screen-mode interface\n");
+  printf(" -P --plain              with --delete, use line-based prompt (as with older\n");
+  printf("                         versions of fdupes) instead of screen-mode interface\n");
 #endif
-  printf(" -N --noprompt    \ttogether with --delete, preserve the first file in\n");
-  printf("                  \teach set of duplicates and delete the rest without\n");
-  printf("                  \tprompting the user\n");
-  printf(" -I --immediate   \tdelete duplicates as they are encountered, without\n");
-  printf("                  \tgrouping into sets; implies --noprompt\n");
-  printf(" -p --permissions \tdon't consider files with different owner/group or\n");
-  printf("                  \tpermission bits as duplicates\n");
-  printf(" -o --order=BY    \tselect sort order for output and deleting; by file\n");
-  printf("                  \tmodification time (BY='time'; default), status\n");
-  printf("                  \tchange time (BY='ctime'), or filename (BY='name')\n");
-  printf(" -i --reverse     \treverse order while sorting\n");
-  printf(" -l --log=LOGFILE \tlog file deletion choices to LOGFILE\n");
-  printf(" -v --version     \tdisplay fdupes version\n");
-  printf(" -h --help        \tdisplay this help message\n\n");
+  printf(" -N --noprompt           together with --delete, preserve the first file in\n");
+  printf("                         each set of duplicates and delete the rest without\n");
+  printf("                         prompting the user\n");
+  printf(" -I --immediate          delete duplicates as they are encountered, without\n");
+  printf("                         grouping into sets; implies --noprompt\n");
+  printf(" -p --permissions        don't consider files with different owner/group or\n");
+  printf("                         permission bits as duplicates\n");
+  printf(" -o --order=BY           select sort order for output and deleting; by file\n");
+  printf("                         modification time (BY='time'; default), status\n");
+  printf("                         change time (BY='ctime'), or filename (BY='name')\n");
+  printf(" -i --reverse            reverse order while sorting\n");
+  printf(" -l --log=LOGFILE        log file deletion choices to LOGFILE\n");
+  printf(" -v --version            display fdupes version\n");
+  printf(" -h --help               display this help message\n\n");
 #ifndef HAVE_GETOPT_H
   printf("Note: Long options are not supported in this fdupes build.\n\n");
 #endif
@@ -1271,7 +1258,7 @@ int main(int argc, char **argv) {
   char **oldargv;
   int firstrecurse;
   char *logfile = 0;
-  struct log_info *loginfo;
+  struct log_info *loginfo = NULL;
   int log_error;
   struct stat logfile_status;
   char *endptr;
@@ -1304,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
@@ -1317,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
@@ -1412,24 +1400,10 @@ int main(int argc, char **argv) {
       SETFLAG(flags, F_REVERSE);
       break;
     case 'l':
-      loginfo = log_open(logfile=optarg, &log_error);
-      if (loginfo == 0)
-      {
-        if (log_error == LOG_ERROR_NOT_A_LOG_FILE)
-          errormsg("%s: doesn't look like an fdupes log file\n", logfile);
-        else
-          errormsg("%s: could not open log file\n", logfile);
-
-        exit(1);
-      }
-      log_close(loginfo);
-
-      if (stat(logfile, &logfile_status) != 0)
-      {
-        errormsg("could not read log file status\n");
-        exit(1);
-      }
-
+      logfile = optarg;
+      break;
+    case 'D':
+      SETFLAG(flags, F_DEFERCONFIRMATION);
       break;
     default:
       fprintf(stderr, "Try `fdupes --help' for more information.\n");
@@ -1452,6 +1426,35 @@ 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;
+
+  if (logfile != 0)
+  {
+    loginfo = log_open(logfile, &log_error);
+    if (loginfo == 0)
+    {
+      if (log_error == LOG_ERROR_NOT_A_LOG_FILE)
+        errormsg("%s: doesn't look like an fdupes log file\n", logfile);
+      else
+        errormsg("%s: could not open log file\n", logfile);
+
+      exit(1);
+    }
+
+    if (stat(logfile, &logfile_status) != 0)
+    {
+      errormsg("could not read log file status\n");
+      exit(1);
+    }
+  }
+
   if (ISFLAG(flags, F_RECURSEAFTER)) {
     firstrecurse = nonoptafter("--recurse:", argc, oldargv, argv, optind);
     
@@ -1465,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) {
@@ -1504,17 +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,
-              (ordertype == ORDER_MTIME || 
-               ordertype == ORDER_CTIME) ? sort_pairs_by_time : sort_pairs_by_filename, loginfo );
-        else
-          registerpair(match, curfile,
-              (ordertype == ORDER_MTIME ||
-               ordertype == ORDER_CTIME) ? sort_pairs_by_time : sort_pairs_by_filename );
+      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 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);
     }
@@ -1530,9 +1535,15 @@ int main(int argc, char **argv) {
 
   if (!ISFLAG(flags, F_HIDEPROGRESS)) fprintf(stderr, "\r%40s\r", " ");
 
+  if (loginfo != 0)
+  {
+    log_close(loginfo);
+    loginfo = 0;
+  }
+
   if (ISFLAG(flags, F_DELETEFILES))
   {
-    if (ISFLAG(flags, F_NOPROMPT))
+    if (ISFLAG(flags, F_NOPROMPT) || ISFLAG(flags, F_IMMEDIATE))
     {
       deletefiles(files, 0, 0, logfile);
     }