This repository was archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathfile_utils.cpp
More file actions
63 lines (53 loc) · 1.46 KB
/
file_utils.cpp
File metadata and controls
63 lines (53 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
# =============================================================================
# Copyright (c) 2016 - 2021 Blue Brain Project/EPFL
#
# See top-level LICENSE file for details.
# =============================================================================
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <sys/stat.h>
#include <errno.h>
#if defined(MINGW)
#define mkdir(dir_name, permission) _mkdir(dir_name)
#endif
/* adapted from : gist@jonathonreinhart/mkdir_p.c */
int mkdir_p(const char* path) {
const int path_len = strlen(path);
if (path_len == 0) {
printf("Warning: Empty path for creating directory");
return -1;
}
char* dirpath = new char[path_len + 1];
strcpy(dirpath, path);
errno = 0;
/* iterate from outer upto inner dir */
for (char* p = dirpath + 1; *p; p++) {
if (*p == '/') {
/* temporarily truncate to sub-dir */
*p = '\0';
if (mkdir(dirpath, S_IRWXU) != 0) {
if (errno != EEXIST)
return -1;
}
*p = '/';
}
}
if (mkdir(dirpath, S_IRWXU) != 0) {
if (errno != EEXIST) {
return -1;
}
}
delete[] dirpath;
return 0;
}
bool fs_exists(const char* path) {
struct stat buffer;
return (stat(path, &buffer) == 0);
}
bool fs_isdir(const char* path) {
struct stat buffer;
return (stat(path, &buffer) == 0 && S_ISDIR(buffer.st_mode));
}