Skip to content

Commit

Permalink
Add option to log file deletions to chosen file.
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianlopezroche committed Jul 22, 2018
1 parent ca95a01 commit 076d319
Show file tree
Hide file tree
Showing 14 changed files with 478 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
bin_PROGRAMS = fdupes

if NO_NCURSES
fdupes_SOURCES = fdupes.c errormsg.c md5/md5.c fdupes.h errormsg.h md5/md5.h
fdupes_SOURCES = fdupes.c errormsg.c dir.c log.c fmatch.c sigint.c md5/md5.c fdupes.h errormsg.h dir.h log.h fmatch.h sigint.h md5/md5.h
notrans_dist_man1_MANS = fdupes.1

else
fdupes_SOURCES = fdupes.c ncurses-commands.c ncurses-getcommand.c ncurses-interface.c ncurses-print.c ncurses-prompt.c ncurses-status.c commandidentifier.c errormsg.c wcs.c md5/md5.c fdupes.h ncurses-commands.h ncurses-getcommand.h ncurses-interface.h ncurses-print.h ncurses-prompt.h ncurses-status.h commandidentifier.h errormsg.h wcs.h filegroup.h md5/md5.h
fdupes_SOURCES = fdupes.c ncurses-commands.c ncurses-getcommand.c ncurses-interface.c ncurses-print.c ncurses-prompt.c ncurses-status.c commandidentifier.c errormsg.c wcs.c dir.c log.c fmatch.c sigint.c md5/md5.c fdupes.h ncurses-commands.h ncurses-getcommand.h ncurses-interface.h ncurses-print.h ncurses-prompt.h ncurses-status.h commandidentifier.h errormsg.h wcs.h filegroup.h dir.h log.h fmatch.h sigint.h md5/md5.h
dist_man1_MANS = fdupes.1 fdupes-help.1

endif
Expand Down
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Usage: fdupes [options] DIRECTORY...
modification time (BY='time'; default), status
change time (BY='ctime'), or filename (BY='name')
-i --reverse reverse order while sorting
-l --log=LOGFILE log file deletion choices to LOGFILE
-v --version display fdupes version
-h --help display this help message

Expand Down
41 changes: 41 additions & 0 deletions dir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include "dir.h"

char *getworkingdirectory()
{
size_t size;
char *result;
char *new_result;
char *cwd;

size = 1024;

result = 0;
do
{
new_result = (char*) realloc(result, size);
if (new_result == 0)
{
if (result != 0)
free(result);

return 0;
}

result = new_result;

cwd = getcwd(result, size);

size *= 2;
} while (cwd == 0 && errno == ERANGE);

if (cwd == 0)
{
free(result);
return 0;
}

return result;
}
6 changes: 6 additions & 0 deletions dir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef DIR_H
#define DIR_H

char *getworkingdirectory();

#endif
3 changes: 3 additions & 0 deletions fdupes.1
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ time - sort by modification time, ctime - sort by status change time, name - sor
.B -i --reverse
reverse order while sorting
.TP
.B -l --log\fR=\fILOGFILE\fR
log file deletion choices to LOGFILE
.TP
.B -v --version
display fdupes version
.TP
Expand Down
91 changes: 83 additions & 8 deletions fdupes.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include "fdupes.h"
#include "errormsg.h"
#include "ncurses-interface.h"
#include "log.h"
#include "sigint.h"

#define ISFLAG(a,b) ((a & b) == b)
#define SETFLAG(a,b) (a |= b)
Expand Down Expand Up @@ -762,7 +764,7 @@ int relink(char *oldfile, char *newfile)
return 1;
}

void deletefiles(file_t *files, int prompt, FILE *tty)
void deletefiles(file_t *files, int prompt, FILE *tty, char *logfile)
{
int counter;
int groups = 0;
Expand All @@ -779,6 +781,8 @@ void deletefiles(file_t *files, int prompt, FILE *tty)
int max = 0;
int x;
int i;
struct log_info *loginfo;
int log_error;

curfile = files;

Expand Down Expand Up @@ -810,6 +814,12 @@ void deletefiles(file_t *files, int prompt, FILE *tty)
exit(1);
}

loginfo = 0;
if (logfile != 0)
loginfo = log_open(logfile, &log_error);

register_sigint_handler();

while (files) {
if (files->hasdupes) {
curgroup++;
Expand Down Expand Up @@ -845,7 +855,24 @@ void deletefiles(file_t *files, int prompt, FILE *tty)
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;

Expand All @@ -861,6 +888,7 @@ void deletefiles(file_t *files, int prompt, FILE *tty)
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;
Expand All @@ -886,24 +914,44 @@ void deletefiles(file_t *files, int prompt, FILE *tty)

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 (remove(dupelist[x]->d_name) == 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!\n");

if (loginfo)
log_file_remaining(loginfo, dupelist[x]->d_name);
}
}
}
printf("\n");

if (loginfo)
log_end_set(loginfo);
}

files = files->next;
}

if (loginfo)
log_close(loginfo);

free(dupelist);
free(preserve);
free(preservestr);
Expand Down Expand Up @@ -979,7 +1027,7 @@ void registerpair(file_t **matchlist, file_t *newmatch,
}

void deletesuccessor(file_t **existing, file_t *duplicate,
int (*comparef)(file_t *f1, file_t *f2))
int (*comparef)(file_t *f1, file_t *f2), struct log_info *loginfo)
{
file_t *to_keep;
file_t *to_delete;
Expand All @@ -1000,11 +1048,21 @@ void deletesuccessor(file_t **existing, file_t *duplicate,
if (!ISFLAG(flags, F_HIDEPROGRESS)) fprintf(stderr, "\r%40s\r", " ");

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 (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_remaining(loginfo, to_delete->d_name);
}

printf("\n");
Expand Down Expand Up @@ -1052,6 +1110,7 @@ void help_text()
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");
#ifndef HAVE_GETOPT_H
Expand All @@ -1073,6 +1132,8 @@ int main(int argc, char **argv) {
char **oldargv;
int firstrecurse;
char *logfile = 0;
struct log_info *loginfo;
int log_error;

#ifdef HAVE_GETOPT_H
static struct option long_options[] =
Expand Down Expand Up @@ -1101,6 +1162,7 @@ int main(int argc, char **argv) {
{ "permissions", 0, 0, 'p' },
{ "order", 1, 0, 'o' },
{ "reverse", 0, 0, 'i' },
{ "log", 1, 0, 'l' },
{ 0, 0, 0, 0 }
};
#define GETOPT getopt_long
Expand All @@ -1114,7 +1176,7 @@ int main(int argc, char **argv) {

oldargv = cloneargs(argc, argv);

while ((opt = GETOPT(argc, argv, "frRq1SsHlnAdPvhNImpo:i"
while ((opt = GETOPT(argc, argv, "frRq1SsHnAdPvhNImpo:il:"
#ifdef HAVE_GETOPT_H
, long_options, NULL
#endif
Expand Down Expand Up @@ -1189,6 +1251,19 @@ int main(int argc, char **argv) {
case 'i':
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);
break;

default:
fprintf(stderr, "Try `fdupes --help' for more information.\n");
Expand Down Expand Up @@ -1267,7 +1342,7 @@ int main(int argc, char **argv) {
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 );
ordertype == ORDER_CTIME) ? sort_pairs_by_time : sort_pairs_by_filename, loginfo );
else
registerpair(match, curfile,
(ordertype == ORDER_MTIME ||
Expand All @@ -1293,7 +1368,7 @@ int main(int argc, char **argv) {
{
if (ISFLAG(flags, F_NOPROMPT))
{
deletefiles(files, 0, 0);
deletefiles(files, 0, 0, logfile);
}
else
{
Expand All @@ -1302,7 +1377,7 @@ int main(int argc, char **argv) {
{
if (newterm(getenv("TERM"), stdout, stdin) != 0)
{
deletefiles_ncurses(files);
deletefiles_ncurses(files, logfile);
}
else
{
Expand All @@ -1320,7 +1395,7 @@ int main(int argc, char **argv) {
exit(1);
}

deletefiles(files, 1, stdin);
deletefiles(files, 1, stdin, logfile);
}
#else
stdin = freopen("/dev/tty", "r", stdin);
Expand All @@ -1330,7 +1405,7 @@ int main(int argc, char **argv) {
exit(1);
}

deletefiles(files, 1, stdin);
deletefiles(files, 1, stdin, logfile);
#endif
}
}
Expand Down
29 changes: 29 additions & 0 deletions fmatch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <string.h>
#include "fmatch.h"

/* Test whether given string matches text at current file position.
*/
void fmatch(FILE *file, char *matchstring, int *is_match, size_t *chars_read)
{
size_t len;
int x;
int c;

*is_match = 0;
*chars_read = 0;

len = strlen(matchstring);
for (x = 0; x < len; ++x)
{
c = fgetc(file);
if (c == EOF)
return;

(*chars_read)++;

if ((char)c != matchstring[x])
return;
}

*is_match = 1;
}
8 changes: 8 additions & 0 deletions fmatch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef FMATCH_H
#define FMATCH_H

#include <stdio.h>

void fmatch(FILE *file, char *matchstring, int *is_match, size_t *chars_read);

#endif
Loading

0 comments on commit 076d319

Please sign in to comment.