forked from adrianlopezroche/fdupes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdupes.c
1637 lines (1368 loc) · 39.8 KB
/
fdupes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "config.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <errno.h>
#include <libgen.h>
#include <locale.h>
#ifndef NO_NCURSES
#ifdef HAVE_NCURSESW_CURSES_H
#include <ncursesw/curses.h>
#else
#include <curses.h>
#endif
#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;
typedef enum {
ORDER_MTIME = 0,
ORDER_CTIME,
ORDER_NAME
} ordertype_t;
char *program_name;
ordertype_t ordertype = ORDER_MTIME;
#define MD5_DIGEST_LENGTH 16
typedef struct _filetree {
file_t *file;
struct _filetree *left;
struct _filetree *right;
} filetree_t;
void escapefilename(char *escape_list, char **filename_ptr)
{
int x;
int tx;
char *tmp;
char *filename;
filename = *filename_ptr;
tmp = (char*) malloc(strlen(filename) * 2 + 1);
if (tmp == NULL) {
errormsg("out of memory!\n");
exit(1);
}
for (x = 0, tx = 0; x < strlen(filename); x++) {
if (strchr(escape_list, filename[x]) != NULL) tmp[tx++] = '\\';
tmp[tx++] = filename[x];
}
tmp[tx] = '\0';
if (x != tx) {
*filename_ptr = realloc(*filename_ptr, strlen(tmp) + 1);
if (*filename_ptr == NULL) {
errormsg("out of memory!\n");
exit(1);
}
strcpy(*filename_ptr, tmp);
}
}
dev_t getdevice(char *filename) {
struct stat s;
if (stat(filename, &s) != 0) return 0;
return s.st_dev;
}
ino_t getinode(char *filename) {
struct stat s;
if (stat(filename, &s) != 0) return 0;
return s.st_ino;
}
char *fmttime(time_t t) {
static char buf[64];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", localtime(&t));
return buf;
}
char **cloneargs(int argc, char **argv)
{
int x;
char **args;
args = (char **) malloc(sizeof(char*) * argc);
if (args == NULL) {
errormsg("out of memory!\n");
exit(1);
}
for (x = 0; x < argc; x++) {
args[x] = (char*) malloc(strlen(argv[x]) + 1);
if (args[x] == NULL) {
free(args);
errormsg("out of memory!\n");
exit(1);
}
strcpy(args[x], argv[x]);
}
return args;
}
int findarg(char *arg, int start, int argc, char **argv)
{
int x;
for (x = start; x < argc; x++)
if (strcmp(argv[x], arg) == 0)
return x;
return x;
}
/* Find the first non-option argument after specified option. */
int nonoptafter(char *option, int argc, char **oldargv,
char **newargv, int optind, int *foundoption)
{
int x;
int targetind;
int testind;
int startat = 1;
targetind = findarg(option, 1, argc, oldargv);
*foundoption = targetind < argc;
for (x = optind; x < argc; x++) {
testind = findarg(newargv[x], startat, argc, oldargv);
if (testind > targetind) return x;
else startat = testind;
}
return x;
}
void getfilestats(file_t *file, struct stat *info, struct stat *linfo)
{
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;
#ifdef HAVE_NSEC_TIMES
file->ctime_nsec = info->st_ctim.tv_nsec;
file->mtime_nsec = info->st_mtim.tv_nsec;
#else
file->ctime_nsec = 0;
file->mtime_nsec = 0;
#endif
}
int grokdir(char *dir, file_t **filelistp, struct stat *logfile_status)
{
DIR *cd;
file_t *newfile;
struct dirent *dirinfo;
int lastchar;
int filecount = 0;
struct stat info;
struct stat linfo;
static int progress = 0;
static char indicator[] = "-\\|/";
char *fullname, *name;
cd = opendir(dir);
if (!cd) {
errormsg("could not chdir to %s\n", dir);
return 0;
}
while ((dirinfo = readdir(cd)) != NULL) {
if (strcmp(dirinfo->d_name, ".") && strcmp(dirinfo->d_name, "..")) {
if (!ISFLAG(flags, F_HIDEPROGRESS)) {
fprintf(stderr, "\rBuilding file list %c ", indicator[progress]);
progress = (progress + 1) % 4;
}
newfile = (file_t*) malloc(sizeof(file_t));
if (!newfile) {
errormsg("out of memory!\n");
closedir(cd);
exit(1);
} else newfile->next = *filelistp;
newfile->device = 0;
newfile->inode = 0;
newfile->crcsignature = NULL;
newfile->crcpartial = NULL;
newfile->duplicates = NULL;
newfile->hasdupes = 0;
newfile->d_name = (char*)malloc(strlen(dir)+strlen(dirinfo->d_name)+2);
if (!newfile->d_name) {
errormsg("out of memory!\n");
free(newfile);
closedir(cd);
exit(1);
}
strcpy(newfile->d_name, dir);
lastchar = strlen(dir) - 1;
if (lastchar >= 0 && dir[lastchar] != '/')
strcat(newfile->d_name, "/");
strcat(newfile->d_name, dirinfo->d_name);
if (ISFLAG(flags, F_EXCLUDEHIDDEN)) {
fullname = strdup(newfile->d_name);
if (fullname == 0)
{
errormsg("out of memory!\n");
free(newfile);
closedir(cd);
exit(1);
}
name = basename(fullname);
if (name[0] == '.' && strcmp(name, ".") && strcmp(name, "..") ) {
free(newfile->d_name);
free(newfile);
free(fullname);
continue;
}
free(fullname);
}
if (stat(newfile->d_name, &info) == -1) {
free(newfile->d_name);
free(newfile);
continue;
}
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;
}
/* 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);
continue;
}
if (lstat(newfile->d_name, &linfo) == -1) {
free(newfile->d_name);
free(newfile);
continue;
}
if (S_ISDIR(info.st_mode)) {
if (ISFLAG(flags, F_RECURSE) && (ISFLAG(flags, F_FOLLOWLINKS) || !S_ISLNK(linfo.st_mode)))
filecount += grokdir(newfile->d_name, filelistp, logfile_status);
free(newfile->d_name);
free(newfile);
} else {
if (S_ISREG(linfo.st_mode) || (S_ISLNK(linfo.st_mode) && ISFLAG(flags, F_FOLLOWLINKS))) {
getfilestats(newfile, &info, &linfo);
*filelistp = newfile;
filecount++;
} else {
free(newfile->d_name);
free(newfile);
}
}
}
}
closedir(cd);
return filecount;
}
md5_byte_t *getcrcsignatureuntil(char *filename, off_t fsize, off_t max_read)
{
off_t toread;
md5_state_t state;
static md5_byte_t digest[MD5_DIGEST_LENGTH];
static md5_byte_t chunk[CHUNK_SIZE];
FILE *file;
md5_init(&state);
if (max_read != 0 && fsize > max_read)
fsize = max_read;
file = fopen(filename, "rb");
if (file == NULL) {
errormsg("error opening file %s\n", filename);
return NULL;
}
while (fsize > 0) {
toread = (fsize >= CHUNK_SIZE) ? CHUNK_SIZE : fsize;
if (fread(chunk, toread, 1, file) != 1) {
errormsg("error reading from file %s\n", filename);
fclose(file);
return NULL;
}
md5_append(&state, chunk, toread);
fsize -= toread;
}
md5_finish(&state, digest);
fclose(file);
return digest;
}
md5_byte_t *getcrcsignature(char *filename, off_t fsize)
{
return getcrcsignatureuntil(filename, fsize, 0);
}
md5_byte_t *getcrcpartialsignature(char *filename, off_t fsize)
{
return getcrcsignatureuntil(filename, fsize, PARTIAL_MD5_SIZE);
}
int md5cmp(const md5_byte_t *a, const md5_byte_t *b)
{
int x;
for (x = 0; x < MD5_DIGEST_LENGTH; ++x)
{
if (a[x] < b[x])
return -1;
else if (a[x] > b[x])
return 1;
}
return 0;
}
void md5copy(md5_byte_t *to, const md5_byte_t *from)
{
int x;
for (x = 0; x < MD5_DIGEST_LENGTH; ++x)
to[x] = from[x];
}
void purgetree(filetree_t *checktree)
{
if (checktree->left != NULL) purgetree(checktree->left);
if (checktree->right != NULL) purgetree(checktree->right);
free(checktree);
}
int registerfile(filetree_t **branch, file_t *file)
{
*branch = (filetree_t*) malloc(sizeof(filetree_t));
if (*branch == NULL) {
errormsg("out of memory!\n");
exit(1);
}
(*branch)->file = file;
(*branch)->left = NULL;
(*branch)->right = NULL;
return 1;
}
int same_permissions(char* name1, char* name2)
{
struct stat s1, s2;
if (stat(name1, &s1) != 0) return -1;
if (stat(name2, &s2) != 0) return -1;
return (s1.st_mode == s2.st_mode &&
s1.st_uid == s2.st_uid &&
s1.st_gid == s2.st_gid);
}
int is_hardlink(filetree_t *checktree, file_t *file)
{
file_t *dupe;
if ((file->inode == checktree->file->inode) &&
(file->device == checktree->file->device))
return 1;
if (checktree->file->hasdupes)
{
dupe = checktree->file->duplicates;
do {
if ((file->inode == dupe->inode) &&
(file->device == dupe->device))
return 1;
dupe = dupe->duplicates;
} while (dupe != NULL);
}
return 0;
}
/* check whether two paths represent the same file (deleting one would delete the other) */
int is_same_file(file_t *file_a, file_t *file_b)
{
char *filename_a;
char *filename_b;
char *dirname_a;
char *dirname_b;
char *basename_a;
char *basename_b;
struct stat dirstat_a;
struct stat dirstat_b;
/* if files on different devices and/or different inodes, they are not the same file */
if (file_a->device != file_b->device || file_a->inode != file_b->inode)
return 0;
/* copy filenames (basename and dirname may modify these) */
filename_a = strdup(file_a->d_name);
if (filename_a == 0)
return -1;
filename_b = strdup(file_b->d_name);
if (filename_b == 0)
return -1;
/* get file basenames */
basename_a = basename(filename_a);
memmove(filename_a, basename_a, strlen(basename_a) + 1);
basename_b = basename(filename_b);
memmove(filename_b, basename_b, strlen(basename_b) + 1);
/* if files have different names, they are not the same file */
if (strcmp(filename_a, filename_b) != 0)
{
free(filename_b);
free(filename_a);
return 0;
}
/* restore paths */
strcpy(filename_a, file_a->d_name);
strcpy(filename_b, file_b->d_name);
/* get directory names */
dirname_a = dirname(filename_a);
if (stat(dirname_a, &dirstat_a) != 0)
{
free(filename_b);
free(filename_a);
return -1;
}
dirname_b = dirname(filename_b);
if (stat(dirname_b, &dirstat_b) != 0)
{
free(filename_b);
free(filename_a);
return -1;
}
free(filename_b);
free(filename_a);
/* if directories on which files reside are different, they are not the same file */
if (dirstat_a.st_dev != dirstat_b.st_dev || dirstat_a.st_ino != dirstat_b.st_ino)
return 0;
/* same device, inode, filename, and directory; therefore, same file */
return 1;
}
/* check whether given tree node already contains a copy of given file */
int has_same_file(filetree_t *checktree, file_t *file)
{
file_t *dupe;
if (is_same_file(checktree->file, file))
return 1;
if (checktree->file->hasdupes)
{
dupe = checktree->file->duplicates;
do {
if (is_same_file(dupe, file))
return 1;
dupe = dupe->duplicates;
} while (dupe != NULL);
}
return 0;
}
file_t **checkmatch(filetree_t **root, filetree_t *checktree, file_t *file)
{
int cmpresult;
md5_byte_t *crcsignature;
if (ISFLAG(flags, F_CONSIDERHARDLINKS))
{
/* If node already contains file, we don't want to add it again.
*/
if (has_same_file(checktree, file))
return NULL;
}
else
{
/* If device and inode fields are equal one of the files is a
hard link to the other or the files have been listed twice
unintentionally. We don't want to flag these files as
duplicates unless the user specifies otherwise.
*/
if (is_hardlink(checktree, file))
return NULL;
}
if (file->size < checktree->file->size)
cmpresult = -1;
else
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, checktree->file->size);
if (crcsignature == NULL) {
errormsg ("cannot read file %s\n", checktree->file->d_name);
return NULL;
}
checktree->file->crcpartial = (md5_byte_t*) malloc(MD5_DIGEST_LENGTH * sizeof(md5_byte_t));
if (checktree->file->crcpartial == NULL) {
errormsg("out of memory\n");
exit(1);
}
md5copy(checktree->file->crcpartial, crcsignature);
}
if (file->crcpartial == NULL) {
crcsignature = getcrcpartialsignature(file->d_name, file->size);
if (crcsignature == NULL) {
errormsg ("cannot read file %s\n", file->d_name);
return NULL;
}
file->crcpartial = (md5_byte_t*) malloc(MD5_DIGEST_LENGTH * sizeof(md5_byte_t));
if (file->crcpartial == NULL) {
errormsg("out of memory\n");
exit(1);
}
md5copy(file->crcpartial, crcsignature);
}
cmpresult = md5cmp(file->crcpartial, checktree->file->crcpartial);
if (cmpresult == 0) {
if (checktree->file->crcsignature == NULL) {
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));
if (checktree->file->crcsignature == NULL) {
errormsg("out of memory\n");
exit(1);
}
md5copy(checktree->file->crcsignature, crcsignature);
}
if (file->crcsignature == NULL) {
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));
if (file->crcsignature == NULL) {
errormsg("out of memory\n");
exit(1);
}
md5copy(file->crcsignature, crcsignature);
}
cmpresult = md5cmp(file->crcsignature, checktree->file->crcsignature);
}
}
if (cmpresult < 0) {
if (checktree->left != NULL) {
return checkmatch(root, checktree->left, file);
} else {
registerfile(&(checktree->left), file);
return NULL;
}
} else if (cmpresult > 0) {
if (checktree->right != NULL) {
return checkmatch(root, checktree->right, file);
} else {
registerfile(&(checktree->right), file);
return NULL;
}
} else
{
return &checktree->file;
}
}
void summarizematches(file_t *files)
{
int numsets = 0;
double numbytes = 0.0;
int numfiles = 0;
file_t *tmpfile;
while (files != NULL)
{
if (files->hasdupes)
{
numsets++;
tmpfile = files->duplicates;
while (tmpfile != NULL)
{
numfiles++;
numbytes += files->size;
tmpfile = tmpfile->duplicates;
}
}
files = files->next;
}
if (numsets == 0)
printf("No duplicates found.\n\n");
else
{
if (numbytes < 1024.0)
printf("%d duplicate files (in %d sets), occupying %.0f bytes.\n\n", numfiles, numsets, numbytes);
else if (numbytes <= (1000.0 * 1000.0))
printf("%d duplicate files (in %d sets), occupying %.1f kilobytes\n\n", numfiles, numsets, numbytes / 1000.0);
else
printf("%d duplicate files (in %d sets), occupying %.1f megabytes\n\n", numfiles, numsets, numbytes / (1000.0 * 1000.0));
}
}
void printmatches(file_t *files)
{
file_t *tmpfile;
while (files != NULL) {
if (files->hasdupes) {
if (!ISFLAG(flags, F_OMITFIRST)) {
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 ", 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 ", 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;
}
printf("\n");
}
files = files->next;
}
}
/*
#define REVISE_APPEND "_tmp"
char *revisefilename(char *path, int seq)
{
int digits;
char *newpath;
char *scratch;
char *dot;
digits = numdigits(seq);
newpath = malloc(strlen(path) + strlen(REVISE_APPEND) + digits + 1);
if (!newpath) return newpath;
scratch = malloc(strlen(path) + 1);
if (!scratch) return newpath;
strcpy(scratch, path);
dot = strrchr(scratch, '.');
if (dot)
{
*dot = 0;
sprintf(newpath, "%s%s%d.%s", scratch, REVISE_APPEND, seq, dot + 1);
}
else
{
sprintf(newpath, "%s%s%d", path, REVISE_APPEND, seq);
}
free(scratch);
return newpath;
} */
int relink(char *oldfile, char *newfile)
{
dev_t od;
dev_t nd;
ino_t oi;
ino_t ni;
od = getdevice(oldfile);
oi = getinode(oldfile);
if (link(oldfile, newfile) != 0)
return 0;
/* make sure we're working with the right file (the one we created) */
nd = getdevice(newfile);
ni = getinode(newfile);
if (nd != od || oi != ni)
return 0; /* file is not what we expected */
return 1;
}
void deletefiles(file_t *files, int prompt, FILE *tty, char *logfile)
{
int counter;
int groups = 0;
int curgroup = 0;
file_t *tmpfile;
file_t *curfile;
file_t **dupelist;
int *preserve;
int firstpreserved;
char *preservestr;
char *token;
char *tstr;
int number;
int sum;
int max = 0;
int x;
int i;
struct log_info *loginfo;
int log_error;
FILE *file1;
FILE *file2;
int ismatch;
char *errorstring;
curfile = files;
while (curfile) {
if (curfile->hasdupes) {
counter = 1;
groups++;
tmpfile = curfile->duplicates;
while (tmpfile) {
counter++;
tmpfile = tmpfile->duplicates;
}
if (counter > max) max = counter;
}
curfile = curfile->next;
}
max++;
dupelist = (file_t**) malloc(sizeof(file_t*) * max);
preserve = (int*) malloc(sizeof(int) * max);
preservestr = (char*) malloc(INPUT_SIZE);
if (!dupelist || !preserve || !preservestr) {
errormsg("out of memory\n");
exit(1);
}
loginfo = 0;
if (logfile != 0)
loginfo = log_open(logfile, &log_error);
register_sigint_handler();
while (files) {
if (files->hasdupes) {
curgroup++;
counter = 1;
dupelist[counter] = files;
if (prompt)
{
if (ISFLAG(flags, F_SHOWTIME))
printf("[%d] [%s] %s\n", counter, fmttime(files->mtime), files->d_name);
else
printf("[%d] %s\n", counter, files->d_name);
}
tmpfile = files->duplicates;
while (tmpfile) {
dupelist[++counter] = tmpfile;
if (prompt)
{
if (ISFLAG(flags, F_SHOWTIME))
printf("[%d] [%s] %s\n", counter, fmttime(tmpfile->mtime), tmpfile->d_name);
else
printf("[%d] %s\n", counter, tmpfile->d_name);
}
tmpfile = tmpfile->duplicates;
}
if (prompt) printf("\n");
if (!prompt) /* preserve only the first file */
{
preserve[1] = 1;
for (x = 2; x <= counter; x++) preserve[x] = 0;
}
else /* prompt for files to preserve */
do {
printf("Set %d of %d, preserve files [1 - %d, all, quit]",
curgroup, groups, counter);
if (ISFLAG(flags, F_SHOWSIZE)) printf(" (%lld byte%seach)", (long long int)files->size,
(files->size != 1) ? "s " : " ");
printf(": ");
fflush(stdout);
if (!fgets(preservestr, INPUT_SIZE, tty))
{
preservestr[0] = '\n'; /* treat fgets() failure as if nothing was entered */
preservestr[1] = '\0';
if (got_sigint)
{
if (loginfo)
log_close(loginfo);
free(dupelist);
free(preserve);
free(preservestr);
printf("\n");
exit(0);
}
}
i = strlen(preservestr) - 1;
while (preservestr[i]!='\n'){ /* tail of buffer must be a newline */
tstr = (char*)
realloc(preservestr, strlen(preservestr) + 1 + INPUT_SIZE);
if (!tstr) { /* couldn't allocate memory, treat as fatal */
errormsg("out of memory!\n");
exit(1);
}
preservestr = tstr;
if (!fgets(preservestr + i + 1, INPUT_SIZE, tty))
{
preservestr[0] = '\n'; /* treat fgets() failure as if nothing was entered */
preservestr[1] = '\0';
break;
}
i = strlen(preservestr)-1;
}
if (strcmp(preservestr, "q\n") == 0 || strcmp(preservestr, "quit\n") == 0)
{
if (loginfo)
log_close(loginfo);
free(dupelist);
free(preserve);
free(preservestr);
printf("\n");
exit(0);
}
for (x = 1; x <= counter; x++) preserve[x] = 0;
token = strtok(preservestr, " ,\n");
while (token != NULL) {
if (strcasecmp(token, "all") == 0 || strcasecmp(token, "a") == 0)
for (x = 0; x <= counter; x++) preserve[x] = 1;
number = 0;
sscanf(token, "%d", &number);
if (number > 0 && number <= counter) preserve[number] = 1;
token = strtok(NULL, " ,\n");
}
for (sum = 0, x = 1; x <= counter; x++) sum += preserve[x];
} while (sum < 1); /* make sure we've preserved at least one file */
printf("\n");
if (loginfo)
log_begin_set(loginfo);
for (x = 1; x <= counter; x++) {
if (preserve[x])
{
printf(" [+] %s\n", dupelist[x]->d_name);
if (loginfo)
log_file_remaining(loginfo, dupelist[x]->d_name);
}
else {
if (ISFLAG(flags, F_DEFERCONFIRMATION))
{
firstpreserved = 0;
for (i = 1; i <= counter; ++i)
{
if (preserve[i])
{
firstpreserved = i;
break;
}
}
file1 = fopen(dupelist[x]->d_name, "rb");
file2 = fopen(dupelist[firstpreserved]->d_name, "rb");