Skip to content

Commit

Permalink
many refactorings
Browse files Browse the repository at this point in the history
- use simple exit codes in function archive_sum
- fixes check with different base dir usage help text, adds test for this
- adds some more comments
- new utility to sanitize filenames
  - this simplifies file handling in sum and check
- aligns sum and check, they are basically largely the same
  - with a generic archive entry iterator, this could become even simpler
- removes unused check function test setup
  • Loading branch information
wookietreiber committed Jan 31, 2017
1 parent a1dab79 commit db34a86
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 75 deletions.
4 changes: 3 additions & 1 deletion src/archive-sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main(int argc, char **argv) {
snprintf(usage, 2048,
"%s\n"
"\n"
"usage: %s [-c [dir]] [-d digest] archive...\n"
"usage: %s [-c[dir]] [-d digest] archive...\n"
"\n"
"general options:\n"
" -d | --digest choose digest: md5, sha1, sha256, sha512, ...\n"
Expand Down Expand Up @@ -125,6 +125,8 @@ int main(int argc, char **argv) {

} else {

// iterate through archive files

if (check) {

for (i = optind; i < argc; i++)
Expand Down
6 changes: 3 additions & 3 deletions src/archive-sum.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

typedef enum { NORMAL, QUIET, STATUS } verbosity_t;

int archive_check(const EVP_MD *md, const char *check_dir, const char *archive,
verbosity_t verbosity);
int archive_check(const EVP_MD *md, const char *check_dir, char *archive,
const verbosity_t verbosity);

int archive_sum(const EVP_MD *md, const char *filename);
int archive_sum(const EVP_MD *md, char *filename);

#endif
55 changes: 24 additions & 31 deletions src/check.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
#include "util.h"

#include <fcntl.h>
#include <string.h>

#include <archive.h>
#include <archive_entry.h>

int archive_check(const EVP_MD *md, const char *check_dir, const char *archive,
verbosity_t verbosity) {
int archive_check(const EVP_MD *md, const char *check_dir, char *filename,
const verbosity_t verbosity) {

struct archive *a;
struct archive_entry *e;
Expand All @@ -29,27 +28,19 @@ int archive_check(const EVP_MD *md, const char *check_dir, const char *archive,
archive_read_support_filter_all(a);
archive_read_support_format_all(a);

if (archive == NULL || strcmp("-", archive) == 0) {
// sanitize filename for opening and for error messages
char *error_filename, *open_filename;
sanitize_filename(filename, &open_filename, &error_filename);

archive_bsize = 32768; // TODO magic number

if (archive_read_open_filename(a, NULL, archive_bsize) != ARCHIVE_OK) {
fprintf(stderr, "%s: stdin\n", archive_error_string(a));
archive_read_free(a);
return 0;
}

} else {

// get fs bsize for archive
if (!bsize(archive, &archive_bsize))
return 0;
// get fs bsize for archive
if (!bsize(open_filename, &archive_bsize))
return 0;

if (archive_read_open_filename(a, archive, archive_bsize) != ARCHIVE_OK) {
fprintf(stderr, "%s: %s\n", archive_error_string(a), archive);
archive_read_free(a);
return 0;
}
// open archive
if (archive_read_open_filename(a, open_filename, archive_bsize) != ARCHIVE_OK) {
fprintf(stderr, "%s: %s\n", archive_error_string(a), error_filename);
archive_read_free(a);
return 0;
}

char buf[archive_bsize];
Expand Down Expand Up @@ -128,26 +119,28 @@ int archive_check(const EVP_MD *md, const char *check_dir, const char *archive,
}
}

// free digest
EVP_MD_CTX_destroy(mdctx);

// free archive
if (archive_read_free(a) != ARCHIVE_OK) {
fprintf(stderr, "%s: %s\n", archive_error_string(a), error_filename);
return 0;
}

// issue warning summaries
if (missing > 0 && verbosity != STATUS) {
fprintf(stderr, "%s: WARNING: %u listed %s could not be read\n", archive, missing,
fprintf(stderr, "%s: WARNING: %u listed %s could not be read\n", error_filename, missing,
warning == 1 ? "file" : "files");
}

if (warning > 0 && verbosity != STATUS) {
fprintf(stderr, "%s: WARNING: %u computed %s did NOT match\n", archive, warning,
fprintf(stderr, "%s: WARNING: %u computed %s did NOT match\n", error_filename, warning,
warning == 1 ? "checksum" : "checksums");
}

if (archive_read_free(a) != ARCHIVE_OK) {
fprintf(stderr, "%s: %s\n", archive_error_string(a), archive);
return 0;
}

if (missing > 0 || warning > 0)
return 0;
else
return 1;

return 1;
}
40 changes: 15 additions & 25 deletions src/sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <archive.h>
#include <archive_entry.h>

int archive_sum(const EVP_MD *md, const char *archive) {
int archive_sum(const EVP_MD *md, char *filename) {
struct archive *a;
struct archive_entry *e;

Expand All @@ -19,33 +19,23 @@ int archive_sum(const EVP_MD *md, const char *archive) {
archive_read_support_filter_all(a);
archive_read_support_format_all(a);

if (archive == NULL || strcmp("-", archive) == 0) {
// sanitize filename for opening and for error messages
char *error_filename, *open_filename;
sanitize_filename(filename, &open_filename, &error_filename);

archive_bsize = 32768; // TODO magic number
// get fs bsize for archive
if (!bsize(open_filename, &archive_bsize))
return 0;

if (archive_read_open_filename(a, NULL, archive_bsize) != ARCHIVE_OK) {
fprintf(stderr, "%s: stdin\n", archive_error_string(a));
archive_read_free(a);
return EXIT_FAILURE;
}

} else {

// get fs bsize for archive
if (!bsize(archive, &archive_bsize))
return EXIT_FAILURE;

if (archive_read_open_filename(a, archive, archive_bsize) != ARCHIVE_OK) {
fprintf(stderr, "%s: %s\n", archive_error_string(a), archive);
archive_read_free(a);
return EXIT_FAILURE;
}
// open archive
if (archive_read_open_filename(a, open_filename, archive_bsize) != ARCHIVE_OK) {
fprintf(stderr, "%s: %s\n", archive_error_string(a), error_filename);
archive_read_free(a);
return 0;
}

char buf[archive_bsize];

// open archive

// init digest
EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
unsigned char md_value[EVP_MAX_MD_SIZE];
Expand Down Expand Up @@ -76,9 +66,9 @@ int archive_sum(const EVP_MD *md, const char *archive) {

// free archive
if (archive_read_free(a) != ARCHIVE_OK) {
fprintf(stderr, "%s: %s\n", archive_error_string(a), archive);
return EXIT_FAILURE;
fprintf(stderr, "%s: %s\n", archive_error_string(a), error_filename);
return 0;
}

return EXIT_SUCCESS;
return 1;
}
34 changes: 25 additions & 9 deletions src/util.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
#include "archive-sum.h"
#include "util.h"
#include "archive-sum.h"

#include <sys/stat.h>
#include <stdio.h>
#include <sys/stat.h>

int bsize(const char *file, blksize_t *size) {
struct stat s;
#define STDIN_BUF_SIZE 32768

if (stat(file, &s) == -1) {
perror(file);
return 0;
}
int bsize(const char *filename, blksize_t *size) {
if (filename == NULL) {
*size = STDIN_BUF_SIZE;
} else {
struct stat s;

*size = s.st_blksize;
if (stat(filename, &s) == -1) {
perror(filename);
return 0;
}

*size = s.st_blksize;
}

return 1;
}

void sanitize_filename(char *filename, char **open_filename, char **sanitized_filename) {
if (filename == NULL || strcmp("-", filename) == 0) {
*open_filename = NULL;
*sanitized_filename = "stdin";
} else {
*open_filename = filename;
*sanitized_filename = filename;
}
}
2 changes: 2 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@

int bsize(const char *file, blksize_t *size);

void sanitize_filename(char *filename, char **open_filename, char **sanitized_filename);

#endif
1 change: 1 addition & 0 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ NORMAL_TESTS = \
check-missing.sh \
check-quiet.sh \
check-status.sh \
check-with-dir.sh \
stdin-cli-empty-check.sh \
stdin-cli-empty-sum.sh \
stdin-cli-dash-check.sh \
Expand Down
9 changes: 9 additions & 0 deletions test/check-with-dir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

source setup-archive-sum-test.sh || exit 1
trap "rm -fr $TMP_DIR" EXIT

mkdir migrate
mv $TEST_ARCHIVE_DIR migrate

$ARCHIVE_SUM -cmigrate $TEST_ARCHIVE
6 changes: 0 additions & 6 deletions test/setup-archive-sum-test.sh.in
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#!/bin/bash

function check {
return 0
}

export -f check

export ARCHIVE_SUM=$PWD/../src/archive-sum

export TMP_DIR=$(mktemp -d)
Expand Down

0 comments on commit db34a86

Please sign in to comment.