Skip to content

Commit b882fe8

Browse files
author
Damien Churchill
committed
move all common logic out to common
1 parent ede3653 commit b882fe8

File tree

9 files changed

+103
-48
lines changed

9 files changed

+103
-48
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ endif
1717

1818
all: $(TARGETS)
1919

20-
blksync: action.o chunk.o worker.o writer.o
20+
blksync: action.o chunk.o common.o worker.o writer.o
2121

2222
testAction: action.o
2323

action.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*
1919
*/
2020

21-
#ifndef BS_ACTION
22-
#define BS_ACTION 1
21+
#ifndef BS_ACTION_H
22+
#define BS_ACTION_H
2323

2424
typedef struct bs_action_t *Action;
2525

blksync.c

+7-41
Original file line numberDiff line numberDiff line change
@@ -28,54 +28,19 @@
2828
#include <mqueue.h>
2929
#include <sys/stat.h>
3030

31+
#ifdef USE_GCRYPT
32+
#include <gcrypt.h>
33+
GCRY_THREAD_OPTION_PTHREAD_IMPL;
34+
#endif
35+
3136
#include "action.h"
3237
#include "chunk.h"
3338
#include "common.h"
3439
#include "worker.h"
35-
36-
#ifdef USE_OPENSSL
37-
#include <openssl/sha.h>
38-
static inline void bs_sha1(void *hash, const void *message, size_t length) {
39-
SHA1(message, length, hash);
40-
}
41-
#elif USE_GCRYPT
42-
#include <gcrypt.h>
43-
44-
GCRY_THREAD_OPTION_PTHREAD_IMPL;
45-
46-
static inline void bs_sha1(void *hash, const void *message, size_t length) {
47-
gcry_md_hash_buffer(GCRY_MD_SHA1, hash, message, length);
48-
}
49-
#else
50-
#error No cryptographic library specififed.
51-
#endif
40+
#include "writer.h"
5241

5342
#define NUM_THREADS 1
5443

55-
/**
56-
* Safely open a file for read/write if it does not exist, without wiping
57-
* the contents of the file.
58-
*/
59-
FILE *bs_open_rw(char *path) {
60-
FILE *fp;
61-
62-
fp = fopen(path, "r+");
63-
if (fp == NULL && errno == ENOENT) {
64-
fp = fopen(path, "w+");
65-
}
66-
return fp;
67-
}
68-
69-
/**
70-
* Print out a sha1 hash
71-
*/
72-
static inline void bs_print_hash(unsigned char *hash, int length) {
73-
int i;
74-
for (i = 0; i < length; i++) {
75-
printf("%02x", hash[i]);
76-
}
77-
}
78-
7944
/**
8045
* Main thead
8146
*/
@@ -92,6 +57,7 @@ int main(int argc, char **argv) {
9257
struct mq_attr qattrs;
9358

9459
#ifdef USE_GCRYPT
60+
9561
if (!gcry_check_version(GCRYPT_VERSION)) {
9662
fputs ("libgcrypt version mismatch\n", stderr);
9763
exit(2);

chunk.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*
1919
*/
2020

21-
#ifndef BS_CHUNK
22-
#define BS_CHUNK 1
21+
#ifndef BS_CHUNK_H
22+
#define BS_CHUNK_H
2323

2424
typedef struct bs_chunk_t *Chunk;
2525

common.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}

common.h

+18-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
*
1919
*/
2020

21-
#ifndef BS_COMMON
22-
#define BS_COMMON 1
21+
#include <mqueue.h>
22+
23+
#ifndef BS_COMMON_H
24+
#define BS_COMMON_H
2325

2426
#define TRUE 1
2527
#define FALSE 0
@@ -37,4 +39,18 @@ typedef struct {
3739
int hash_length;
3840
int chunk_size;
3941
} params;
42+
43+
#ifdef USE_OPENSSL
44+
#include <openssl/sha.h>
45+
#elif USE_GCRYPT
46+
#include <gcrypt.h>
47+
#else
48+
#error No cryptographic library specififed.
49+
#endif
50+
51+
void bs_sha1(void *hash, const void *message, size_t length);
52+
53+
void bs_print_hash(unsigned char *hash, int length);
54+
55+
FILE *bs_open_rw(char *path);
4056
#endif

worker.h

+5
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@
1818
*
1919
*/
2020

21+
#ifndef BS_WORKER_H
22+
#define BS_WORKER_H
23+
2124
void *bs_hash_chunk(void *arg);
25+
26+
#endif

writer.c

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*
1919
*/
2020

21+
#include <stdlib.h>
22+
2123
#include "writer.h"
2224

2325
void *bs_write_backupfile(void *arg) {

writer.h

+5
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@
1818
*
1919
*/
2020

21+
#ifndef BS_WRITER_H
22+
#define BS_WRITER_H
23+
2124
void *bs_write_backupfile(void *arg);
25+
26+
#endif

0 commit comments

Comments
 (0)