Skip to content

Commit 20c104c

Browse files
jmarshalllh3
authored andcommitted
Report errno on file opening failures and I/O errors
Add the underlying operating system error (usually "No such file" or "Out of space" respectively, but highly informative when it is not) to these error messages.
1 parent 238b6bb commit 20c104c

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

Diff for: main.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdlib.h>
22
#include <stdio.h>
33
#include <string.h>
4+
#include <errno.h>
45
#include "bseq.h"
56
#include "minimap.h"
67
#include "mmpriv.h"
@@ -172,7 +173,7 @@ int main(int argc, char *argv[])
172173
else if (c == 'o') {
173174
if (strcmp(o.arg, "-") != 0) {
174175
if (freopen(o.arg, "wb", stdout) == NULL) {
175-
fprintf(stderr, "[ERROR]\033[1;31m failed to write the output to file '%s'\033[0m\n", o.arg);
176+
fprintf(stderr, "[ERROR]\033[1;31m failed to write the output to file '%s'\033[0m: %s\n", o.arg, strerror(errno));
176177
exit(1);
177178
}
178179
}
@@ -337,7 +338,7 @@ int main(int argc, char *argv[])
337338
}
338339
idx_rdr = mm_idx_reader_open(argv[o.ind], &ipt, fnw);
339340
if (idx_rdr == 0) {
340-
fprintf(stderr, "[ERROR] failed to open file '%s'\n", argv[o.ind]);
341+
fprintf(stderr, "[ERROR] failed to open file '%s': %s\n", argv[o.ind], strerror(errno));
341342
return 1;
342343
}
343344
if (!idx_rdr->is_idx && fnw == 0 && argc - o.ind < 2) {
@@ -384,7 +385,7 @@ int main(int argc, char *argv[])
384385
mm_split_merge(argc - (o.ind + 1), (const char**)&argv[o.ind + 1], &opt, n_parts);
385386

386387
if (fflush(stdout) == EOF) {
387-
fprintf(stderr, "[ERROR] failed to write the results\n");
388+
perror("[ERROR] failed to write the results");
388389
exit(EXIT_FAILURE);
389390
}
390391

Diff for: map.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdlib.h>
22
#include <string.h>
33
#include <assert.h>
4+
#include <errno.h>
45
#include "kthread.h"
56
#include "kvec.h"
67
#include "kalloc.h"
@@ -622,7 +623,7 @@ static mm_bseq_file_t **open_bseqs(int n, const char **fn)
622623
for (i = 0; i < n; ++i) {
623624
if ((fp[i] = mm_bseq_open(fn[i])) == 0) {
624625
if (mm_verbose >= 1)
625-
fprintf(stderr, "ERROR: failed to open file '%s'\n", fn[i]);
626+
fprintf(stderr, "ERROR: failed to open file '%s': %s\n", fn[i], strerror(errno));
626627
for (j = 0; j < i; ++j)
627628
mm_bseq_close(fp[j]);
628629
free(fp);

Diff for: misc.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void mm_err_puts(const char *str)
125125
int ret;
126126
ret = puts(str);
127127
if (ret == EOF) {
128-
fprintf(stderr, "[ERROR] failed to write the results\n");
128+
perror("[ERROR] failed to write the results");
129129
exit(EXIT_FAILURE);
130130
}
131131
}
@@ -135,7 +135,7 @@ void mm_err_fwrite(const void *p, size_t size, size_t nitems, FILE *fp)
135135
int ret;
136136
ret = fwrite(p, size, nitems, fp);
137137
if (ret == EOF) {
138-
fprintf(stderr, "[ERROR] failed to write data\n");
138+
perror("[ERROR] failed to write data");
139139
exit(EXIT_FAILURE);
140140
}
141141
}
@@ -145,7 +145,7 @@ void mm_err_fread(void *p, size_t size, size_t nitems, FILE *fp)
145145
int ret;
146146
ret = fread(p, size, nitems, fp);
147147
if (ret == EOF) {
148-
fprintf(stderr, "[ERROR] failed to read data\n");
148+
perror("[ERROR] failed to read data");
149149
exit(EXIT_FAILURE);
150150
}
151151
}

Diff for: splitidx.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <assert.h>
33
#include <stdlib.h>
44
#include <stdio.h>
5+
#include <errno.h>
56
#include "mmpriv.h"
67

78
FILE *mm_split_init(const char *prefix, const mm_idx_t *mi)
@@ -13,7 +14,7 @@ FILE *mm_split_init(const char *prefix, const mm_idx_t *mi)
1314
sprintf(fn, "%s.%.4d.tmp", prefix, mi->index);
1415
if ((fp = fopen(fn, "wb")) == NULL) {
1516
if (mm_verbose >= 1)
16-
fprintf(stderr, "[ERROR]\033[1;31m failed to write to temporary file '%s'\033[0m\n", fn);
17+
fprintf(stderr, "[ERROR]\033[1;31m failed to write to temporary file '%s'\033[0m: %s\n", fn, strerror(errno));
1718
exit(1);
1819
}
1920
mm_err_fwrite(&k, 4, 1, fp);
@@ -41,7 +42,7 @@ mm_idx_t *mm_split_merge_prep(const char *prefix, int n_splits, FILE **fp, uint3
4142
sprintf(fn, "%s.%.4d.tmp", prefix, i);
4243
if ((fp[i] = fopen(fn, "rb")) == 0) {
4344
if (mm_verbose >= 1)
44-
fprintf(stderr, "ERROR: failed to open temporary file '%s'\n", fn);
45+
fprintf(stderr, "ERROR: failed to open temporary file '%s': %s\n", fn, strerror(errno));
4546
for (j = 0; j < i; ++j)
4647
fclose(fp[j]);
4748
free(fn);

0 commit comments

Comments
 (0)