static int _mkdir(struct request *r, char *path, int exists_ok) { if (io_mkdir(path, S_IRWXU | S_IXGRP)) { char buf[MSG_SIZE]; int err = errno; if (err == EEXIST) { struct stat st; if (!exists_ok) return EEXIST; /* * Verify the path is a directory. */ if (_stat(r, path, &st)) { _error(r, "_mkdir: _stat() failed"); return ENOENT; } return S_ISDIR(st.st_mode) ? 0 : ENOTDIR; } snprintf(buf, sizeof buf, "mkdir(%s) failed: %s", path, strerror(err)); _panic(r, buf); } return 0; }
int io_openfile(const char* path) { /* Make sure that all parts of the path exist */ io_mkdir(path_getdir(path)); /* Now I can open the file */ file = fopen(path, "w"); if (file == NULL) { printf("** Unable to open file '%s' for writing\n", path); return 0; } else { return 1; } }