Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add openssl support #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ FILEOFFSET_64BIT = -D_FILE_OFFSET_BITS=64
#OMIT_GETOPT_LONG = -DOMIT_GETOPT_LONG

#
# To use the md5sum program for calculating signatures (instead of the
# built in MD5 message digest routines) uncomment the following
# line (try this if you're having trouble with built in code).
# Choose md5 implementation to use.
#
#EXTERNAL_MD5 = -DEXTERNAL_MD5=\"md5sum\"
# Openssl
MD5LIB_CFLAGS = -lcrypto -DWITH_OPENSSL
#
# Internal
#MD5LIB_OBJECT_FILE = md5/md5.o

#####################################################################
# Developer Configuration Section #
Expand Down Expand Up @@ -80,8 +82,7 @@ MKDIR = mkdir -p
CC ?= gcc
COMPILER_OPTIONS = -Wall -O -g

CFLAGS= $(COMPILER_OPTIONS) -I. -DVERSION=\"$(VERSION)\" $(EXTERNAL_MD5) $(OMIT_GETOPT_LONG) $(FILEOFFSET_64BIT)

CFLAGS= $(COMPILER_OPTIONS) -I. -DVERSION=\"$(VERSION)\" $(OMIT_GETOPT_LONG) $(MD5LIB_CFLAGS) $(FILEOFFSET_64BIT)
INSTALL_PROGRAM = $(INSTALL) -c -m 0755
INSTALL_DATA = $(INSTALL) -c -m 0644

Expand All @@ -91,7 +92,7 @@ INSTALL_DATA = $(INSTALL) -c -m 0644
#
#ADDITIONAL_OBJECTS = getopt.o

OBJECT_FILES = fdupes.o md5/md5.o $(ADDITIONAL_OBJECTS)
OBJECT_FILES = fdupes.o $(MD5LIB_OBJECT_FILE) $(ADDITIONAL_OBJECTS)

#####################################################################
# no need to modify anything beyond this point #
Expand Down
81 changes: 27 additions & 54 deletions fdupes.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
#include <errno.h>
#include <libgen.h>

#ifndef EXTERNAL_MD5
#ifdef WITH_OPENSSL
#include <openssl/md5.h>
#else
#include "md5/md5.h"
#endif

Expand Down Expand Up @@ -345,25 +347,31 @@ int grokdir(char *dir, file_t **filelistp)
return filecount;
}

#ifndef EXTERNAL_MD5

/* If EXTERNAL_MD5 is not defined, use L. Peter Deutsch's MD5 library.
*/
char *getcrcsignatureuntil(char *filename, off_t max_read)
{
int x;
off_t fsize;
off_t toread;
md5_state_t state;
md5_byte_t digest[16];
static md5_byte_t chunk[CHUNK_SIZE];
static char signature[16*2 + 1];
char *sigp;
FILE *file;


#ifdef WITH_OPENSSL
static char chunk[CHUNK_SIZE];
static char signature[MD5_DIGEST_LENGTH*2 + 1];
unsigned char digest[MD5_DIGEST_LENGTH];
MD5_CTX c;

MD5_Init(&c);
#else
static md5_byte_t chunk[CHUNK_SIZE];
static char signature[16*2 + 1];
md5_state_t state;
md5_byte_t digest[16];

md5_init(&state);
#endif



fsize = filesize(filename);

if (max_read != 0 && fsize > max_read)
Expand All @@ -382,11 +390,19 @@ char *getcrcsignatureuntil(char *filename, off_t max_read)
fclose(file);
return NULL;
}
#ifdef WITH_OPENSSL
MD5_Update(&c, chunk, toread);
#else
md5_append(&state, chunk, toread);
#endif
fsize -= toread;
}

#ifdef WITH_OPENSSL
MD5_Final(digest, &c);
#else
md5_finish(&state, digest);
#endif

sigp = signature;

Expand All @@ -410,49 +426,6 @@ char *getcrcpartialsignature(char *filename)
return getcrcsignatureuntil(filename, PARTIAL_MD5_SIZE);
}

#endif /* [#ifndef EXTERNAL_MD5] */

#ifdef EXTERNAL_MD5

/* If EXTERNAL_MD5 is defined, use md5sum program to calculate signatures.
*/
char *getcrcsignature(char *filename)
{
static char signature[256];
char *command;
char *separator;
FILE *result;

command = (char*) malloc(strlen(filename)+strlen(EXTERNAL_MD5)+2);
if (command == NULL) {
errormsg("out of memory\n");
exit(1);
}

sprintf(command, "%s %s", EXTERNAL_MD5, filename);

result = popen(command, "r");
if (result == NULL) {
errormsg("error invoking %s\n", EXTERNAL_MD5);
exit(1);
}

free(command);

if (fgets(signature, 256, result) == NULL) {
errormsg("error generating signature for %s\n", filename);
return NULL;
}
separator = strchr(signature, ' ');
if (separator) *separator = '\0';

pclose(result);

return signature;
}

#endif /* [#ifdef EXTERNAL_MD5] */

void purgetree(filetree_t *checktree)
{
if (checktree->left != NULL) purgetree(checktree->left);
Expand Down