Skip to content

Commit 3f64f59

Browse files
cosmo0920edsiper
authored andcommitted
lib: chunkio: update to 98988d25
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 8ee0c4a commit 3f64f59

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed

lib/chunkio/src/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
2323
)
2424
set(libs
2525
${libs}
26-
Shell32.lib
27-
Shlwapi.lib)
26+
Shell32.lib)
2827
else()
2928
set(src
3029
${src}

lib/chunkio/src/cio_os.c

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

52+
#ifdef _WIN32
53+
static int cio_os_win32_make_recursive_path(const char* path) {
54+
char dir[MAX_PATH];
55+
char* p;
56+
size_t len;
57+
size_t root_len = 0;
58+
size_t i = 0, seps = 0;
59+
char saved;
60+
61+
if (_fullpath(dir, path, MAX_PATH) == NULL) {
62+
return 1;
63+
}
64+
65+
/* Normalize to backslashes */
66+
for (p = dir; *p; p++) {
67+
if (*p == '/') {
68+
*p = '\\';
69+
}
70+
}
71+
72+
len = strlen(dir);
73+
74+
/* Determine root length: "C:\" (3) or UNC root "\\server\share\" */
75+
if (len >= 2 &&
76+
((dir[0] >= 'A' && dir[0] <= 'Z') || (dir[0] >= 'a' && dir[0] <= 'z')) &&
77+
dir[1] == ':') {
78+
root_len = (len >= 3 && dir[2] == '\\') ? 3 : 2;
79+
}
80+
else if (len >= 5 && dir[0] == '\\' && dir[1] == '\\') {
81+
/* Skip server and share components: \\server\share\ */
82+
i = 2;
83+
while (i < len && seps < 2) {
84+
if (dir[i] == '\\') {
85+
seps++;
86+
}
87+
i++;
88+
}
89+
root_len = i; /* points just past "\\server\share\" */
90+
}
91+
92+
/* Create each intermediate component after the root */
93+
for (p = dir + root_len; *p; p++) {
94+
if (*p == '\\') {
95+
saved = *p;
96+
*p = '\0';
97+
if (!CreateDirectoryA(dir, NULL)) {
98+
DWORD err = GetLastError();
99+
if (err != ERROR_ALREADY_EXISTS) {
100+
*p = saved;
101+
return 1;
102+
}
103+
}
104+
*p = saved;
105+
}
106+
}
107+
108+
/* Create the final directory */
109+
if (!CreateDirectoryA(dir, NULL)) {
110+
DWORD err = GetLastError();
111+
if (err != ERROR_ALREADY_EXISTS) {
112+
return 1;
113+
}
114+
}
115+
return 0;
116+
}
117+
#endif
118+
52119
/* Create directory */
53120
int cio_os_mkpath(const char *dir, mode_t mode)
54121
{
@@ -85,7 +152,7 @@ int cio_os_mkpath(const char *dir, mode_t mode)
85152
return 1;
86153
}
87154

88-
if (SHCreateDirectoryExA(NULL, path, NULL) != ERROR_SUCCESS) {
155+
if (cio_os_win32_make_recursive_path(path) != ERROR_SUCCESS) {
89156
return 1;
90157
}
91158
return 0;

lib/chunkio/src/win32/dirent.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424

2525
#include <Windows.h>
26-
#include <shlwapi.h>
2726

2827
#include "dirent.h"
2928

@@ -75,11 +74,30 @@ static char *create_pattern(const char *path)
7574
return buf;
7675
}
7776

77+
/**
78+
* @brief Checks if a given path string refers to an existing directory.
79+
* @param pszPath The null-terminated string that contains the path.
80+
* @return Returns TRUE if the path is a directory, otherwise FALSE.
81+
*/
82+
BOOL path_is_directory(LPCSTR pszPath) {
83+
DWORD dwAttrib = GetFileAttributesA(pszPath);
84+
85+
if (dwAttrib == INVALID_FILE_ATTRIBUTES) {
86+
return FALSE;
87+
}
88+
89+
if (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) {
90+
return TRUE;
91+
}
92+
93+
return FALSE;
94+
}
95+
7896
struct CIO_WIN32_DIR *cio_win32_opendir(const char *path)
7997
{
8098
struct CIO_WIN32_DIR *d;
8199

82-
if (!PathIsDirectoryA(path)) {
100+
if (!path_is_directory(path)) {
83101
return NULL;
84102
}
85103

0 commit comments

Comments
 (0)