Skip to content

Commit 6ee11e0

Browse files
cosmo0920edsiper
authored andcommitted
os: Remove SHCreateDirectoryEx dependency
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent 1d276c2 commit 6ee11e0

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

src/cio_os.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,45 @@ int cio_os_isdir(const char *dir)
4949
return -1;
5050
}
5151

52+
#ifdef _WIN32
53+
inline int cio_os_win32_make_recursive_path(const char* path) {
54+
char dir[MAX_PATH];
55+
char* p = NULL;
56+
57+
if (_fullpath(dir, path, MAX_PATH) == NULL) {
58+
return 1;
59+
}
60+
61+
for (p = dir; *p; p++) {
62+
/* Skip the drive letter (e.g., "C:") */
63+
if (p > dir && *p == ':' && *(p - 1) != '\0') {
64+
continue;
65+
}
66+
67+
if (*p == '\\' || *p == '/') {
68+
char original_char = *p;
69+
*p = '\0';
70+
71+
if (!CreateDirectoryA(dir, NULL)) {
72+
if (GetLastError() != ERROR_ALREADY_EXISTS) {
73+
*p = original_char;
74+
return 1;
75+
}
76+
}
77+
*p = original_char;
78+
}
79+
}
80+
81+
if (!CreateDirectoryA(dir, NULL)) {
82+
if (GetLastError() != ERROR_ALREADY_EXISTS) {
83+
return 1;
84+
}
85+
}
86+
87+
return 0;
88+
}
89+
#endif
90+
5291
/* Create directory */
5392
int cio_os_mkpath(const char *dir, mode_t mode)
5493
{
@@ -85,7 +124,7 @@ int cio_os_mkpath(const char *dir, mode_t mode)
85124
return 1;
86125
}
87126

88-
if (SHCreateDirectoryExA(NULL, path, NULL) != ERROR_SUCCESS) {
127+
if (cio_os_win32_make_recursive_path(path) != ERROR_SUCCESS) {
89128
return 1;
90129
}
91130
return 0;

0 commit comments

Comments
 (0)