File: bpc_fileDigest.c

package info (click to toggle)
libbackuppc-xs-perl 0.62-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,348 kB
  • sloc: ansic: 9,652; sh: 3,084; perl: 73; makefile: 3
file content (49 lines) | stat: -rw-r--r-- 1,488 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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;
}