%% options copyright owner = Dirk Krause copyright year = 2015-xxxx SPDX-License-Identifier: BSD-3-Clause %% header /** @file Make directory hierarchy (create all parent directories for a file or directory and the directory itself if required), the file or directory name is specified as wchar_t string. */ #ifndef DK4CONF_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4conf.h" #else #include #endif #endif #ifndef DK4TYPES_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4types.h" #else #include #endif #endif #ifndef DK4ERROR_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4error.h" #else #include #endif #endif #if DK4_HAVE_WCHAR_H #ifndef WCHAR_H_INCLUDED #include #endif #endif #ifdef __cplusplus extern "C" { #endif /** Find position of first separator for real directory. This function is not intended for use in application programs, it is exported only for use from the dk4appmkdh module. The function either returns a value or sets the back variable to 1 or reports an error. @param fn File name. @param bptr Pointer to the back variable. @param lc Flag: Last component in fn is directory too. @param erp Error report, may be NULL. @return First separator after a real directory on success if found, NULL otherwise. */ wchar_t * dk4mkdir_hierarchy_wc_first_sep(wchar_t *fn, int *bptr, int lc, dk4_er_t *erp); /** Check whether directory exists or attempt to create directory. @param fn File name. @param ro Flag: Read only (test directory presence), do not attempt to create directory. @param erp Error report, may be NULL. @return 1 if directory is available now, 0 otherwise. Error codes: - DK4_E_INVALID_ARGUMENTS
if fn is NULL, - DK4_E_NOT_FOUND
if a server/share combination is not present on Windows, - DK4_E_NON_DIR
if any component in the path is not a directory, - DK4_E_CREATE_DIR_FAILED
with GetLastError() result in lDetails1 if the CreateDirectory() function failed. */ int dk4mkdir_hierarchy_wc_one(const wchar_t *fn, int ro, dk4_er_t *erp); /** Create directory structure. CRT on Windows: Optional. @param fn File name. @param lc Flag: Last component in fn is directory too. @param erp Error report, may be NULL. @return 1 on success, 0 on error. Error codes: - DK4_E_INVALID_ARGUMENTS
if fn is NULL, - DK4_E_BUFFER_TOO_SMALL
if fn is too long, - DK4_E_MATH_OVERFLOW
if a mathematical overflow occured in size allocation, - DK4_E_NOT_FOUND
if a server/share combination is not present on Windows, - DK4_E_NON_DIR
if one of the components in fn is not a directory, - DK4_E_MKDIR_FAILED
with errno in idetails if the function fails to create the directory or a parent directory, - DK4_E_CREATE_DIR_FAILED
with GetLastError() result in lDetails1 if the CreateDirectory() function failed on a Windows system. */ int dk4mkdir_hierarchy_wc(const wchar_t *fn, int lc, dk4_er_t *erp); #ifdef __cplusplus } #endif %% module #include "dk4conf.h" #include "dk4types.h" #if DK4_ON_WINDOWS #ifndef WINDOWS_H_INCLUDED #include #define WINDOWS_H_INCLUDED 1 #endif #endif #include "dk4mkdhw.h" #if DK4_HAVE_SYS_STAT_H #ifndef SYS_STAT_H_INCLUDED #include #define SYS_STAT_H_INCLUDED 1 #endif #endif #if DK4_HAVE_SYS_TYPES_H #ifndef SYS_TYPES_H_INCLUDED #include #define SYS_TYPES_H_INCLUDED 1 #endif #endif #if DK4_HAVE_DIRECT_H #ifndef DIRECT_H_INCLUDED #include #define DIRECT_H_INCLUDED 1 #endif #endif #if DK4_HAVE_STDLIB_H #ifndef STDLIB_H_INCLUDED #include #define STDLIB_H_INCLUDED 1 #endif #endif #if DK4_HAVE_UNISTD_H #ifndef UNISTD_H_INCLUDED #include #define UNISTD_H_INCLUDED 1 #endif #endif #if DK4_HAVE_ERRNO_H #ifndef ERRNO_H_INCLUDED #include #define ERRNO_H_INCLUDED 1 #endif #endif #ifndef DK4MEM_H_INCLUDED #include "dk4mem.h" #endif #ifndef DK4MPL_H_INCLUDED #include "dk4mpl.h" #endif #ifndef DK4STRW_H_INCLUDED #include "dk4strw.h" #endif #ifndef DK4PATHW_H_INCLUDED #include "dk4pathw.h" #endif #ifndef DK4STATW_H_INCLUDED #include "dk4statw.h" #endif #ifndef DK4STAT_H_INCLUDED #include "dk4stat.h" #endif #include "dk4unused.h" #if DK4_HAVE_ASSERT_H #ifndef ASSERT_H_INCLUDED #include #define ASSERT_H_INCLUDED 1 #endif #endif $!trace-include #if DK4_ON_WINDOWS static int dk4mkdir_hierarchy_wc_is_char(wchar_t c) { int back = 0; if ((L'a' <= c) && (L'z' >= c)) { back = 1; } else { if ((L'A' <= c) && (L'Z' >= c)) { back = 1; } } return back; } int dk4mkdir_hierarchy_wc_one(const wchar_t *fn, int ro, dk4_er_t *erp) { BY_HANDLE_FILE_INFORMATION bhfi; WIN32_FIND_DATAW ffdata; HANDLE ha; DWORD dwattr; int found; int i; int back = 0; $? "+ dk4mkdir_hierarchy_wc_one \"%ls\"", fn #if DK4_USE_ASSERT assert(NULL != fn); #endif if (NULL == fn) { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); goto finished; } #if 1 dwattr = GetFileAttributesW(fn); if (INVALID_FILE_ATTRIBUTES == dwattr) { ha = FindFirstFileW(fn, &ffdata); if (INVALID_HANDLE_VALUE != ha) { dwattr = ffdata.dwFileAttributes; FindClose(ha); } } if (INVALID_FILE_ATTRIBUTES != dwattr) { $? ". exists" if (0 != (FILE_ATTRIBUTE_DIRECTORY & dwattr)) { $? ". dir" back = 1; } else { $? "! not dir" if (0 != (FILE_ATTRIBUTE_REPARSE_POINT & dwattr)) { found = 0; for (i = 0; ((3 > i) && (0 == back) && (0 == found)); i++) { ha = CreateFileW( fn, ( (0 == i) ? (0) : ((1 == i) ? (FILE_READ_ATTRIBUTES) : (GENERIC_READ)) ), (FILE_SHARE_READ | FILE_SHARE_WRITE), NULL, OPEN_EXISTING, (dwattr | FILE_FLAG_BACKUP_SEMANTICS), NULL ); if (INVALID_HANDLE_VALUE != ha) { if (GetFileInformationByHandle(ha, &bhfi)) { found = 1; if (0 != (FILE_ATTRIBUTE_DIRECTORY & (bhfi.dwFileAttributes))) { back = 1; } } CloseHandle(ha); } } } if (0 == back) { dk4error_set_simple_error_code(erp, DK4_E_NON_DIR); } } } else { $? ". not yet" if (0 != ro) { dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND); } else { #if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT if (CreateDirectoryW(fn, NULL)) { $? ". created" back = 1; } else { $? "! CreateDirectoryW" dk4error_set_ldetails( erp, DK4_E_CREATE_DIR_FAILED , (long)((unsigned long)GetLastError()) ); } #else if (CreateDirectoryW(fn, NULL)) { $? ". created" back = 1; } else { $? "! CreateDirectoryW" errno = 0; if (0 == _wmkdir(fn)) { back = 1; } else { dk4error_set_idetails(erp, DK4_E_MKDIR_FAILED, errno); } } #endif } } #else back = 1; #endif finished: $? "- dk4mkdir_hierarchy_wc_one %d", back return back; } wchar_t * dk4mkdir_hierarchy_wc_first_sep(wchar_t *fn, int *bptr, int lc, dk4_er_t *erp) { wchar_t *pc = NULL; wchar_t sc; int back = 0; int isunc = 0; #if DK4_USE_ASSERT assert(NULL != fn); #endif if (NULL == fn) { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); goto finished; } if (L'\\' == fn[0]) { /* One backslash at start */ if (L'\\' == fn[1]) { /* Two backslashes */ if (L'?' == fn[2]) { /* Probably extended long path */ if (L'\\' == fn[3]) { /* Extended path */ if (dk4mkdir_hierarchy_wc_is_char(fn[4])) { if (L':' == fn[5]) { /* Extended, drive */ if (L'\\' == fn[6]) { /* Extended, drive abs */ sc = fn[7]; fn[7] = L'\0'; back = dk4mkdir_hierarchy_wc_one(fn, 1, erp); fn[7] = sc; if (0 != back) { pc = dk4strw_chr(&(fn[7]), L'\\'); if (NULL == pc) { back = dk4mkdir_hierarchy_wc_one(fn, 0, erp); } else { back = 1; } } else { pc = NULL; } } else { /* Extended, drive rel */ #if 0 pc = dk4strw_chr(&(fn[7]), L'\\'); if (NULL == pc) { back = 1; } #endif /* 2015-02-05 For extended paths we can use full absolute path names only! */ dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { /* Extended, server and share */ if ((L'U' == fn[4]) || (L'u' == fn[4])) { if ((L'N' == fn[5]) || (L'n' == fn[5])) { if ((L'C' == fn[6]) || (L'c' == fn[6])) { if (L'\\' == fn[7]) { isunc = 1; } } } } if (0 != isunc) { /* UNC path */ pc = dk4strw_chr(&(fn[8]), L'\\'); if (NULL != pc) { /* Share */ pc = dk4strw_chr(&(pc[1]), L'\\'); if (NULL != pc) { /* First real directory */ *pc = L'\0'; back = dk4mkdir_hierarchy_wc_one(fn, 1, erp); *pc = L'\\'; if (0 != back) { back = 0; pc = dk4strw_chr(&(pc[1]), L'\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_wc_one(fn, 0, erp); } else { back = 1; } } } else { pc = dk4strw_chr(&(pc[1]), L'\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_wc_one(fn, 0, erp); } } } } else { /* No real directory after share */ if (0 != lc) { back = dk4mkdir_hierarchy_wc_one(fn, 1, erp); } else { dk4error_set_simple_error_code( erp, DK4_E_INVALID_ARGUMENTS ); } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { /* Not UNC, normal path */ pc = dk4strw_chr(&(fn[4]), L'\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_wc_one(fn, 0, erp); } else { back = 1; } } } } } else { /* Some unknown extended path */ pc = dk4strw_chr(&(fn[4]), L'\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_wc_one(fn, 0, erp); } else { back = 1; } } } } else { /* Illegal path */ dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { /* Server and share */ pc = dk4strw_chr(&(fn[2]), L'\\'); if (NULL != pc) { /* Share */ pc = dk4strw_chr(&(pc[1]), L'\\'); if (NULL != pc) { /* First dir */ *pc = L'\0'; back = dk4mkdir_hierarchy_wc_one(fn, 1, erp); *pc = L'\\'; if (0 != back) { back = 0; pc = dk4strw_chr(&(pc[1]), L'\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_wc_one(fn, 0, erp); } else { back = 1; } } } else { pc = dk4strw_chr(&(pc[1]), L'\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_wc_one(fn, 0, erp); } } } } else { /* Share name but no dir */ if (0 != lc) { back = dk4mkdir_hierarchy_wc_one(fn, 1, erp); } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } } else { /* No share */ dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } } else { /* Absolute path wo drive */ pc = dk4strw_chr(&(fn[1]), L'\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_wc_one(fn, 0, erp); } else { back = 1; } } } } else { /* No backslash at start */ if (dk4mkdir_hierarchy_wc_is_char(fn[0])) { if (L':' == fn[1]) { /* Drive */ if (L'\\' == fn[2]) { /* Drive abs */ sc = fn[3]; fn[3] = L'\0'; back = dk4mkdir_hierarchy_wc_one(fn, 1, erp); fn[3] = sc; if (0 != back) { back = 0; pc = dk4strw_chr(&(fn[3]), L'\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_wc_one(fn, 0, erp); } else { back = 1; } } } } else { /* Drive rel */ sc = fn[2]; fn[2] = L'\0'; back = dk4mkdir_hierarchy_wc_one(fn, 1, erp); fn[2] = sc; if (0 != back) { back = 0; pc = dk4strw_chr(&(fn[3]), L'\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_wc_one(fn, 0, erp); } else { back = 1; } } } } } else { /* Normal file name */ pc = dk4strw_chr(fn, L'\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_wc_one(fn, 0, erp); } else { back = 1; } } } } else { /* Normal file name */ pc = dk4strw_chr(fn, L'\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_wc_one(fn, 0, erp); } else { back = 1; } } } } finished: if (NULL != bptr) { *bptr = back; } return pc; } /** Run on writable copy of file name. @param fn File name. @param lc Flag: Last component is directory too. @param erp Error report, may be NULL. @return 1 on success, 0 on error. */ static int dk4mkdir_hierarchy_wc_on_copy(wchar_t *fn, int lc, dk4_er_t *erp) { wchar_t *pc = NULL; int back = 0; #if DK4_USE_ASSERT assert(NULL != fn); #endif /* Correct path name */ dk4pathw_correct_sep(fn); /* Find first real separator */ pc = dk4mkdir_hierarchy_wc_first_sep(fn, &back, lc, erp); /* Attempt to create directory, parent directories too if necessary */ if (NULL != pc) { back = 1; while (NULL != pc) { *pc = L'\0'; if (0 < dk4strw_len(fn)) { if (0 == dk4mkdir_hierarchy_wc_one(fn, 0, erp)) { back = 0; } } *pc = L'\\'; pc = dk4strw_chr(&(pc[1]), L'\\'); } if (0 != lc) { if (0 == dk4mkdir_hierarchy_wc_one(fn, 0, erp)) { back = 0; } } } else { if (0 != back) { if (0 != lc) { if (0 == dk4mkdir_hierarchy_wc_one(fn, 0, erp)) { back = 0; } } } } return back; } /** Create writable copy in local buffer and run. @param fn Original file name. @param lc Flag: Last component is directory too. @param erp Error report, may be NULL. @return 1 on success, 0 on error. */ static int dk4mkdir_hierarchy_wc_local(const wchar_t *fn, int lc, dk4_er_t *erp) { wchar_t mycp[DK4_MAX_PATH]; int back = 0; #if DK4_USE_ASSERT assert(NULL != fn); #endif if (dk4strw_cpy_s(mycp, DK4_SIZEOF(mycp,wchar_t), fn, erp)) { back = dk4mkdir_hierarchy_wc_on_copy(mycp, lc, erp); } return back; } #endif int dk4mkdir_hierarchy_wc( #if DK4_ON_WINDOWS const wchar_t *fn, int lc, #else const wchar_t * DK4_ARG_UNUSED(fn), int DK4_ARG_UNUSED(lc), #endif dk4_er_t *erp ) { #if DK4_ON_WINDOWS wchar_t *mycp; size_t sz; int back = 0; #if DK4_USE_ASSERT assert(NULL != fn); #endif if (NULL != fn) { sz = dk4strw_len(fn); if (0 < sz) { if (DK4_MAX_PATH > sz) { back = dk4mkdir_hierarchy_wc_local(fn, lc, erp); } else { mycp = dk4strw_dup(fn, erp); if (NULL != mycp) { dk4mkdir_hierarchy_wc_on_copy(mycp, lc, erp); dk4mem_free(mycp); } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; #else DK4_UNUSED_ARG(fn) DK4_UNUSED_ARG(lc) dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED); $? "+ dk4mkdir_hierarchy" $? "! not supported on this platform" $? "- dk4mkdir_hierarchy 0" return 0; #endif }