From 99a69b869e6c2ab061ff92f05b703fb050ee1783 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 4 Jun 2021 09:59:09 +0200 Subject: ignore existing files with hash :tada: --- makefile | 2 +- musicopy.c | 41 ++++++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/makefile b/makefile index b6b9831..05da12f 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,7 @@ CC = gcc LD = gcc RM = rm -f -CFLAGS = -lcwalk -linih +CFLAGS = -lcwalk -linih -lcrypto OBJECTS := $(patsubst %.c,%.o, *.c) diff --git a/musicopy.c b/musicopy.c index 67ef96a..d0967f3 100644 --- a/musicopy.c +++ b/musicopy.c @@ -8,6 +8,7 @@ #include #include #include +#include char* music_dir; char* playlist_dir; @@ -128,6 +129,23 @@ void fix_include_exclude(char*** listv, int listc) { } } +void sha1_file(char* path, unsigned char (*hash)[SHA_DIGEST_LENGTH]) { + SHA_CTX ctx; + SHA1_Init(&ctx); + + FILE *file; + file = fopen(path, "rb"); + + char buffer[buffer_size]; + size_t size; + + while ((size = fread(buffer, 1, buffer_size, file))) + SHA1_Update(&ctx, buffer, size); + + fclose(file); + SHA1_Final(*hash, &ctx); +} + bool duplicate(char* source, char* dest) { if (strcmp(existing, "none") == 0) return true; else if (strcmp(existing, "lazy") == 0) return false; @@ -138,7 +156,10 @@ bool duplicate(char* source, char* dest) { return src_st.st_size != dst_st.st_size; } else if (strcmp(existing, "hash") == 0) { - printf("compare hash\n"); + unsigned char src_hash[SHA_DIGEST_LENGTH], dst_hash[SHA_DIGEST_LENGTH]; + sha1_file(source, &src_hash); + sha1_file(dest, &dst_hash); + return memcmp(src_hash, dst_hash, SHA_DIGEST_LENGTH) != 0; } return false; @@ -188,24 +209,6 @@ int dir_callback(const char* path, const struct stat *sb, int tflag) { return 0; } -void print_opts() { - printf("{\n"); - printf(" \"music_dir\": \"%s\",\n", music_dir); - printf(" \"playlist_dir\": \"%s\",\n", playlist_dir); - printf(" \"target_music_dir\": \"%s\",\n", target_music_dir); - printf(" \"target_playlist_dir\": \"%s\",\n", target_playlist_dir); - printf(" \"existing\": \"%s\",\n", existing); - printf(" \"include\": [\n"); - for(int i = 0; i < include_length; i++) - printf(" \"%s\"%c\n", include[i], i+1 == include_length ? ' ' : ','); - printf(" ],\n"); - printf(" \"exclude\": [\n"); - for(int i = 0; i < exclude_length; i++) - printf(" \"%s\"%c\n", exclude[i], i+1 == exclude_length ? ' ' : ','); - printf(" ]\n"); - printf("}\n"); -} - int main() { load_config(); -- cgit v1.2.3