Skip to content

Commit 5da92b8

Browse files
committed
better error handling for making directories. Semi-solves Issue #296
1 parent 0f90e01 commit 5da92b8

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/libduc/duc.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdlib.h>
55
#include <limits.h>
66
#include <stdio.h>
7+
#include <errno.h>
78
#include <string.h>
89
#include <assert.h>
910
#include <stdarg.h>
@@ -59,6 +60,7 @@ void duc_set_log_callback(duc *duc, duc_log_callback cb)
5960
int duc_open(duc *duc, const char *path_db, duc_open_flags flags)
6061
{
6162
char tmp[DUC_PATH_MAX];
63+
int res = 0;
6264

6365
/* An empty path means check the ENV path instead */
6466
if(path_db == NULL) {
@@ -94,7 +96,11 @@ int duc_open(duc *duc, const char *path_db, duc_open_flags flags)
9496
/* Append parent folder */
9597
snprintf(tmp, sizeof tmp, "%s/duc", home);
9698
/* Create if needed */
97-
mkdir(tmp, 0700);
99+
res = mkdir(tmp, 0700);
100+
if (res != 0) {
101+
duc_log(duc, DUC_LOG_FTL, "Error! Cannot create mkdir \"%s\", %s", tmp, strerror(errno));
102+
exit(1);
103+
}
98104
/* Append file to folder*/
99105
snprintf(tmp, sizeof tmp, "%s/duc/duc.db", home);
100106
path_db = tmp;
@@ -107,7 +113,11 @@ int duc_open(duc *duc, const char *path_db, duc_open_flags flags)
107113
/* Append parent folder */
108114
snprintf(tmp, sizeof tmp, "%s/.cache/duc", home);
109115
/* Create if needed */
110-
mkdir(tmp, 0700);
116+
res = mkdir(tmp, 0700);
117+
if (res != 0) {
118+
duc_log(duc, DUC_LOG_FTL, "Error! Cannot create mkdir \"%s\", %s", tmp, strerror(errno));
119+
exit(1);
120+
}
111121
/* Append file to folder*/
112122
snprintf(tmp, sizeof tmp, "%s/.cache/duc/duc.db", home);
113123
path_db = tmp;

0 commit comments

Comments
 (0)