Skip to content

Commit

Permalink
Merge branch 'cw-840' into 'dev'
Browse files Browse the repository at this point in the history
Exit non-zero if an input cannot be read [CW-840]

See merge request epi2melabs/fastcat!7
  • Loading branch information
SamStudio8 committed Jul 26, 2022
2 parents ba55b9b + 039a24f commit 67cd5d8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v0.4.11]
### Changed
- `fastcat` will exit non-zero if an input file (named or recursed) cannot be opened

## [v0.4.10]
### Fixed
- Use of uninitialized memory in thread pool init, leading to memory leak.
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ clean_htslib:
.PHONY: mem_check_fastcat
mem_check_fastcat: fastcat
valgrind --error-exitcode=1 --tool=memcheck --leak-check=full --show-leak-kinds=all -s \
./fastcat test_data/data/*.fastq.gz > /dev/null
./fastcat test/data/*.fastq.gz > /dev/null

.PHONY: mem_check_bamstats
mem_check_bamstats: bamstats
Expand Down
5 changes: 3 additions & 2 deletions src/fastcat/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
const char *argp_program_bug_address = "chris.wright@nanoporetech.com";
static char doc[] =
"fastcat -- concatenate and summarise .fastq(.gz) files.\
\vInput files may be given on stdin by specifing the input as '-'.\
When the -x option is given inputs may be directories.";
\vInput files may be given on stdin by specifing the input as '-'. \
When the -x option is given inputs may be directories. The command \
will exit non-zero if any file encountered cannot be read.";
static char args_doc[] = "reads1.fastq(.gz) reads2.fastq(.gz) ...";
static struct argp_option options[] = {
{"read", 'r', "READ SUMMARY", 0,
Expand Down
42 changes: 29 additions & 13 deletions src/fastcat/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>

#include <zlib.h>
#include <stdio.h>
Expand Down Expand Up @@ -69,13 +70,16 @@ size_t nfiletypes = 4;
// defined below -- recursion
int process_file(char* fname, writer writer, arguments_t *args);

void process_dir(const char *name, writer writer, arguments_t *args) {
int process_dir(const char *name, writer writer, arguments_t *args) {
int status = 0;
DIR *dir;
struct dirent *entry;
char* search;

if (!(dir = opendir(name)))
return;
if (!(dir = opendir(name))) {
fprintf(stderr, "Error: could not process directory %s: %s\n", name, strerror(errno));
return errno;
}

while ((entry = readdir(dir)) != NULL) {
char *path = calloc(strlen(name) + strlen(entry->d_name) + 2, sizeof(char));
Expand All @@ -85,39 +89,45 @@ void process_dir(const char *name, writer writer, arguments_t *args) {
free(path);
continue;
}
process_dir(path, writer, args);
int rtn = process_dir(path, writer, args);
status = max(status, rtn);
} else {
for (size_t i=0; i<nfiletypes; ++i) {
search = strstr(entry->d_name, filetypes[i]);
if (search != NULL) {
fprintf(stderr, "Processing %s\n", path);
process_file(path, writer, args);
int rtn = process_file(path, writer, args);
status = max(status, rtn);
break;
}
}
}
free(path);
}
closedir(dir);
return status;
}


int process_file(char* fname, writer writer, arguments_t* args) {
int status = 0;
struct stat finfo;
int res = stat(fname, &finfo);
if (res == -1) {
fprintf(stderr, "Warning: file '%s' cannot be read.\n", fname);
return 0;
// Failure will bubble up through process_file, process_dir to main
fprintf(stderr, "Error: could not process file %s: %s\n", fname, strerror(errno));
return errno;
}
if ((finfo.st_mode & S_IFMT) == S_IFDIR) {
if (args->recurse) {
char* sfname = strip_path(fname);
process_dir(sfname, writer, args);
int rtn = process_dir(sfname, writer, args);
status = max(status, rtn);
free(sfname);
} else {
fprintf(stderr, "Warning: input '%s' is a directory and -x (recursive mode) was not given.\n", fname);
}
return 0;
return status;
}

gzFile fp;
Expand Down Expand Up @@ -161,6 +171,7 @@ int main(int argc, char **argv) {
if (writer == NULL) exit(1);

int nfile = 0;
int status = 0;
for( ; args.files[nfile] ; nfile++);

if (nfile==1 && strcmp(args.files[0], "-") == 0) {
Expand All @@ -170,15 +181,20 @@ int main(int argc, char **argv) {
while ((nchr = getline (&ln, &n, stdin)) != -1) {
ln[strcspn(ln, "\r\n")] = 0;
int rtn = process_file(ln, writer, &args);
if (rtn != 0) return rtn;
status = max(status, rtn);
}
free(ln);
} else {
} else {
for (size_t i=0; i<nfile; ++i) {
int rtn = process_file(args.files[i], writer, &args);
if (rtn != 0) return rtn;
status = max(status, rtn);
}
}
destroy_writer(writer);
return 0;

if (status != 0) {
fprintf(stderr, "Completed processing with errors. Outputs may be incomplete.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

const char *argp_program_version = "0.4.10";
const char *argp_program_version = "0.4.11";

0 comments on commit 67cd5d8

Please sign in to comment.