From 49c79b96b5ac156e5e00548559f921e5139cbe42 Mon Sep 17 00:00:00 2001 From: Craig Barratt <19445341+craigbarratt@users.noreply.github.com> Date: Mon, 8 Jun 2020 19:03:22 -0700 Subject: [PATCH] added bpc_fileDigest.c --- bpc_fileDigest.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 bpc_fileDigest.c diff --git a/bpc_fileDigest.c b/bpc_fileDigest.c new file mode 100644 index 0000000..1758daf --- /dev/null +++ b/bpc_fileDigest.c @@ -0,0 +1,49 @@ +/* + * Library routines + * + * Copyright (C) 2020 Craig Barratt. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, visit the http://fsf.org website. + */ + +#include "backuppc.h" + +/* + * Compute the md5 digest of a file. Returns 0 on success + */ +int bpc_fileDigest(char *fileName, int compress, bpc_digest *digest) +{ + md_context md5; + bpc_fileZIO_fd fd; + ssize_t nRead; + uchar buffer[1 << 20]; + + digest->len = 0; + md5_begin(&md5); + if ( bpc_fileZIO_open(&fd, fileName, 0, compress) ) { + bpc_logErrf("bpc_fileDigest: can't open %s for reading\n", fileName); + return -1; + } + while ( (nRead = bpc_fileZIO_read(&fd, buffer, sizeof(buffer))) > 0 ) { + md5_update(&md5, buffer, nRead); + } + bpc_fileZIO_close(&fd); + if ( nRead < 0 ) { + bpc_logErrf("bpc_fileDigest: failed to read %s\n", fileName); + return -1; + } + md5_result(&md5, digest->digest); + digest->len = MD5_DIGEST_LEN; + return 0; +}