Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/chunkio/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
)
set(libs
${libs}
Shell32.lib
Shlwapi.lib)
Shell32.lib)
else()
set(src
${src}
Expand Down
69 changes: 68 additions & 1 deletion lib/chunkio/src/cio_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,73 @@ int cio_os_isdir(const char *dir)
return -1;
}

#ifdef _WIN32
static int cio_os_win32_make_recursive_path(const char* path) {
char dir[MAX_PATH];
char* p;
size_t len;
size_t root_len = 0;
size_t i = 0, seps = 0;
char saved;

if (_fullpath(dir, path, MAX_PATH) == NULL) {
return 1;
}

/* Normalize to backslashes */
for (p = dir; *p; p++) {
if (*p == '/') {
*p = '\\';
}
}

len = strlen(dir);

/* Determine root length: "C:\" (3) or UNC root "\\server\share\" */
if (len >= 2 &&
((dir[0] >= 'A' && dir[0] <= 'Z') || (dir[0] >= 'a' && dir[0] <= 'z')) &&
dir[1] == ':') {
root_len = (len >= 3 && dir[2] == '\\') ? 3 : 2;
}
else if (len >= 5 && dir[0] == '\\' && dir[1] == '\\') {
/* Skip server and share components: \\server\share\ */
i = 2;
while (i < len && seps < 2) {
if (dir[i] == '\\') {
seps++;
}
i++;
}
root_len = i; /* points just past "\\server\share\" */
}

/* Create each intermediate component after the root */
for (p = dir + root_len; *p; p++) {
if (*p == '\\') {
saved = *p;
*p = '\0';
if (!CreateDirectoryA(dir, NULL)) {
DWORD err = GetLastError();
if (err != ERROR_ALREADY_EXISTS) {
*p = saved;
return 1;
}
}
*p = saved;
}
}

/* Create the final directory */
if (!CreateDirectoryA(dir, NULL)) {
DWORD err = GetLastError();
if (err != ERROR_ALREADY_EXISTS) {
return 1;
}
}
return 0;
}
#endif

/* Create directory */
int cio_os_mkpath(const char *dir, mode_t mode)
{
Expand Down Expand Up @@ -85,7 +152,7 @@ int cio_os_mkpath(const char *dir, mode_t mode)
return 1;
}

if (SHCreateDirectoryExA(NULL, path, NULL) != ERROR_SUCCESS) {
if (cio_os_win32_make_recursive_path(path) != ERROR_SUCCESS) {
return 1;
}
return 0;
Expand Down
22 changes: 20 additions & 2 deletions lib/chunkio/src/win32/dirent.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

#include <Windows.h>
#include <shlwapi.h>

#include "dirent.h"

Expand Down Expand Up @@ -75,11 +74,30 @@ static char *create_pattern(const char *path)
return buf;
}

/**
* @brief Checks if a given path string refers to an existing directory.
* @param pszPath The null-terminated string that contains the path.
* @return Returns TRUE if the path is a directory, otherwise FALSE.
*/
BOOL path_is_directory(LPCSTR pszPath) {
DWORD dwAttrib = GetFileAttributesA(pszPath);

if (dwAttrib == INVALID_FILE_ATTRIBUTES) {
return FALSE;
}

if (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) {
return TRUE;
}

return FALSE;
}

struct CIO_WIN32_DIR *cio_win32_opendir(const char *path)
{
struct CIO_WIN32_DIR *d;

if (!PathIsDirectoryA(path)) {
if (!path_is_directory(path)) {
return NULL;
}

Expand Down
Loading