|
| 1 | +/*** |
| 2 | + * blksync - synchronize chunks of data between files and/or block devices |
| 3 | + * |
| 4 | + * Copyright (C) 2011 Damien Churchill <[email protected]> |
| 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 |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#include <errno.h> |
| 22 | +#include <stdio.h> |
| 23 | +#include <pthread.h> |
| 24 | + |
| 25 | +#include "common.h" |
| 26 | + |
| 27 | +#ifdef USE_OPENSSL |
| 28 | +void bs_sha1(void *hash, const void *message, size_t length) { |
| 29 | + SHA1(message, length, hash); |
| 30 | +} |
| 31 | +#elif USE_GCRYPT |
| 32 | +void bs_sha1(void *hash, const void *message, size_t length) { |
| 33 | + gcry_md_hash_buffer(GCRY_MD_SHA1, hash, message, length); |
| 34 | +} |
| 35 | +#else |
| 36 | +#error No cryptographic library specififed. |
| 37 | +#endif |
| 38 | + |
| 39 | +/** |
| 40 | + * Print out a sha1 hash |
| 41 | + */ |
| 42 | +void bs_print_hash(unsigned char *hash, int length) { |
| 43 | + int i; |
| 44 | + for (i = 0; i < length; i++) { |
| 45 | + printf("%02x", hash[i]); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Safely open a file for read/write if it does not exist, without wiping |
| 51 | + * the contents of the file. |
| 52 | + */ |
| 53 | +FILE *bs_open_rw(char *path) { |
| 54 | + FILE *fp; |
| 55 | + |
| 56 | + fp = fopen(path, "r+"); |
| 57 | + if (fp == NULL && errno == ENOENT) { |
| 58 | + fp = fopen(path, "w+"); |
| 59 | + } |
| 60 | + return fp; |
| 61 | +} |
0 commit comments