File Coverage

bpc_fileDigest.c
Criterion Covered Total %
statement 0 15 0.0
branch 0 6 0.0
condition n/a
subroutine n/a
pod n/a
total 0 21 0.0


line stmt bran cond sub pod time code
1             /*
2             * Library routines
3             *
4             * Copyright (C) 2020 Craig Barratt.
5             *
6             * This program is free software; you can redistribute it and/or modify
7             * it under the terms of the GNU General Public License as published by
8             * the Free Software Foundation; either version 3 of the License, or
9             * (at your option) any later version.
10             *
11             * This program is distributed in the hope that it will be useful,
12             * but WITHOUT ANY WARRANTY; without even the implied warranty of
13             * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14             * GNU General Public License for more details.
15             *
16             * You should have received a copy of the GNU General Public License along
17             * with this program; if not, visit the http://fsf.org website.
18             */
19              
20             #include "backuppc.h"
21              
22             /*
23             * Compute the md5 digest of a file. Returns 0 on success
24             */
25 0           int bpc_fileDigest(char *fileName, int compress, bpc_digest *digest)
26             {
27             md_context md5;
28             bpc_fileZIO_fd fd;
29             ssize_t nRead;
30             uchar buffer[1 << 20];
31              
32 0           digest->len = 0;
33 0           md5_begin(&md5);
34 0 0         if ( bpc_fileZIO_open(&fd, fileName, 0, compress) ) {
35 0           bpc_logErrf("bpc_fileDigest: can't open %s for reading\n", fileName);
36 0           return -1;
37             }
38 0 0         while ( (nRead = bpc_fileZIO_read(&fd, buffer, sizeof(buffer))) > 0 ) {
39 0           md5_update(&md5, buffer, nRead);
40             }
41 0           bpc_fileZIO_close(&fd);
42 0 0         if ( nRead < 0 ) {
43 0           bpc_logErrf("bpc_fileDigest: failed to read %s\n", fileName);
44 0           return -1;
45             }
46 0           md5_result(&md5, digest->digest);
47 0           digest->len = MD5_DIGEST_LEN;
48 0           return 0;
49             }