/* Copyright (C) 2015-2020, Dirk Krause SPDX-License-Identifier: BSD-3-Clause */ /* WARNING: This file was generated by the dkct program (see http://dktools.sourceforge.net/ for details). Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: dk4mkdh8.ctr */ /** @file dk4mkdh8.c The dk4mkdh8 module. */ #line 168 "dk4mkdh8.ctr" #include "dk4conf.h" #include "dk4types.h" #if DK4_ON_WINDOWS #ifndef WINDOWS_H_INCLUDED #include #define WINDOWS_H_INCLUDED 1 #endif #endif #include "dk4mkdh8.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 DK4STR8_H_INCLUDED #include "dk4str8.h" #endif #ifndef DK4PATH8_H_INCLUDED #include "dk4path8.h" #endif #ifndef DK4STAT8_H_INCLUDED #include "dk4stat8.h" #endif #ifndef DK4STAT_H_INCLUDED #include "dk4stat.h" #endif #if DK4_HAVE_ASSERT_H #ifndef ASSERT_H_INCLUDED #include #define ASSERT_H_INCLUDED 1 #endif #endif #line 256 "dk4mkdh8.ctr" #if DK4_ON_WINDOWS /* +++ Windows */ /** Check whether c is a character. @param c Byte to check. @return 1 for characters, 0 for other bytes. */ static int dk4mkdir_hierarchy_c8_is_char(char c) { int back = 0; if (('a' <= c) && ('z' >= c)) { back = 1; } else { if (('A' <= c) && ('Z' >= c)) { back = 1; } } return back; } int dk4mkdir_hierarchy_c8_one(const char *fn, int ro, dk4_er_t *erp) { BY_HANDLE_FILE_INFORMATION bhfi; WIN32_FIND_DATAA ffdata; HANDLE ha; DWORD dwattr; int found; int i; int back = 0; #line 296 "dk4mkdh8.ctr" #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 = GetFileAttributesA(fn); if (INVALID_FILE_ATTRIBUTES == dwattr) { ha = FindFirstFileA(fn, &ffdata); if (INVALID_HANDLE_VALUE != ha) { dwattr = ffdata.dwFileAttributes; FindClose(ha); } } if (INVALID_FILE_ATTRIBUTES != dwattr) { if (0 != (FILE_ATTRIBUTE_DIRECTORY & dwattr)) { back = 1; } else { if (0 != (FILE_ATTRIBUTE_REPARSE_POINT & dwattr)) { found = 0; for (i = 0; ((3 > i) && (0 == back) && (0 == found)); i++) { ha = CreateFileA( 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 { if (0 != ro) { dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND); } else { #if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT if (CreateDirectoryA(fn, NULL)) { back = 1; } else { dk4error_set_ldetails( erp, DK4_E_CREATE_DIR_FAILED , (long)((unsigned long)GetLastError()) ); } #else if (CreateDirectoryA(fn, NULL)) { back = 1; } else { errno = 0; if (0 == _mkdir(fn)) { back = 1; } else { dk4error_set_idetails(erp, DK4_E_MKDIR_FAILED, errno); } } #endif } } #else back = 1; #endif #line 378 "dk4mkdh8.ctr" finished: return back; } char * dk4mkdir_hierarchy_c8_first_sep(char *fn, int *bptr, int lc, dk4_er_t *erp) { char *pc = NULL; int back = 0; char sc; #if DK4_USE_ASSERT assert(NULL != fn); #endif if (NULL == fn) { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); goto finished; } if ('\\' == *fn) { /* leading bs */ if ('\\' == fn[1]) { /* server and share */ pc = dk4str8_chr(&(fn[2]), '\\'); if (NULL != pc) { /* Share name */ pc = dk4str8_chr(&(pc[1]), '\\'); if (NULL != pc) { /* First dir */ *pc = '\0'; back = dk4mkdir_hierarchy_c8_one(fn, 1, erp); *pc = '\\'; if (0 != back) { back = 0; pc = dk4str8_chr(&(pc[1]), '\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_c8_one(fn, 0, erp); } else { back = 1; } } } else { pc = dk4str8_chr(&(pc[1]), '\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_c8_one(fn, 0, erp); } } } } else { /* Share name but no dir */ if (0 != lc) { back = dk4mkdir_hierarchy_c8_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 { /* abs path, no drive */ pc = dk4str8_chr(&(fn[1]), '\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_c8_one(fn, 0, erp); } else { back = 1; } } } } else { /* no leading bs */ if (0 != dk4mkdir_hierarchy_c8_is_char(*fn)) { /* leading char */ if (':' == fn[1]) { /* drive */ if ('\\' == fn[2]) { /* drive, abs path */ sc = fn[3]; fn[3] = '\0'; back = dk4mkdir_hierarchy_c8_one(fn, 1, erp); fn[3] = sc; if (0 != back) { back = 0; pc = dk4str8_chr(&(fn[3]), '\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_c8_one(fn, 0, erp); } else { back = 1; } } } } else { /* drive, rel path */ sc = fn[2]; fn[2] = '\0'; back = dk4mkdir_hierarchy_c8_one(fn, 1, erp); fn[2] = sc; if (0 != back) { back = 0; pc = dk4str8_chr(&(fn[3]), '\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_c8_one(fn, 0, erp); } else { back = 1; } } } } } else { /* no drive */ pc = dk4str8_chr(fn, '\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_c8_one(fn, 0, erp); } else { back = 1; } } } } else { /* no leading char */ pc = dk4str8_chr(fn, '\\'); if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_c8_one(fn, 0, erp); } else { back = 1; } } } } finished: if (NULL != bptr) { *bptr = back; } return pc; } /* --- Windows */ #else /* +++ non-Windows */ int dk4mkdir_hierarchy_c8_one(const char *fn, int ro, dk4_er_t *erp) { dk4_stat_t stb; int back = 0; #line 523 "dk4mkdh8.ctr" #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 if (0 != dk4stat_c8(&stb, fn, NULL)) { back = dk4stat_is_directory(&stb, erp); if (0 == back) { dk4error_set_simple_error_code(erp, DK4_E_NON_DIR); } } else { if (0 != ro) { dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND); } else { errno = 0; if (0 == mkdir(fn, 0700)) { back = 1; } else { dk4error_set_idetails(erp, DK4_E_MKDIR_FAILED, errno); } } } #else back = 1; #endif #line 552 "dk4mkdh8.ctr" finished: return back; } char * dk4mkdir_hierarchy_c8_first_sep(char *fn, int *bptr, int lc, dk4_er_t *erp) { char *pc = NULL; int back = 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 ('/' == *fn) { pc = dk4str8_chr(&(fn[1]), '/'); } else { pc = dk4str8_chr(fn, '/'); } if (NULL == pc) { if (0 != lc) { back = dk4mkdir_hierarchy_c8_one(fn, 0, erp); } else { back = 1; } } finished: if (NULL != bptr) { *bptr = back; } return pc; } /* --- non-Windows */ #endif /** Create directory structure using an editable copy of the path. @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. */ static int dk4mkdir_hierarchy_c8_on_copy(char *fn, int lc, dk4_er_t *erp) { char *pc; int back = 0; #line 629 "dk4mkdh8.ctr" #if DK4_USE_ASSERT assert(NULL != fn); #endif /* Correct file name */ dk4path8_correct_sep(fn); /* Find first real separator */ pc = dk4mkdir_hierarchy_c8_first_sep(fn, &back, lc, erp); /* Process directories */ if (NULL != pc) { back = 1; while (NULL != pc) { *pc = '\0'; if (0 < dk4str8_len(fn)) { if (0 == dk4mkdir_hierarchy_c8_one(fn, 0, erp)) { back = 0; } } #if DK4_HAVE_BACKSLASH_AS_SEP *pc = '\\'; pc = dk4str8_chr(&(pc[1]), '\\'); #else *pc = '/'; pc = dk4str8_chr(&(pc[1]), '/'); #endif } if (0 != lc) { if (0 == dk4mkdir_hierarchy_c8_one(fn, 0, erp)) { back = 0; } } } #line 664 "dk4mkdh8.ctr" return back; } /** Create directory structure using a local copy of the path. @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. */ static int dk4mkdir_hierarchy_c8_local(const char *fn, int lc, dk4_er_t *erp) { char mycp[DK4_MAX_PATH]; int back = 0; #line 700 "dk4mkdh8.ctr" #if DK4_USE_ASSERT assert(NULL != fn); #endif if (0 != dk4str8_cpy_s(mycp, sizeof(mycp), fn, erp)) { back = dk4mkdir_hierarchy_c8_on_copy(mycp, lc, erp); } #line 706 "dk4mkdh8.ctr" return back; } int dk4mkdir_hierarchy_c8(const char *fn, int lc, dk4_er_t *erp) { char *mycp; size_t sz; int back = 0; #line 718 "dk4mkdh8.ctr" #if DK4_USE_ASSERT assert(NULL != fn); #endif if (NULL != fn) { sz = dk4str8_len(fn); if (DK4_MAX_PATH > sz) { back = dk4mkdir_hierarchy_c8_local(fn, lc, erp); } else { mycp = dk4str8_dup(fn, erp); if (NULL != mycp) { back = dk4mkdir_hierarchy_c8_on_copy(mycp, lc, erp); dk4mem_free(mycp); } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } #line 735 "dk4mkdh8.ctr" return back; } #if DK4_HAVE_UID_T && DK4_HAVE_GID_T && DK4_HAVE_MODE_T && (!DK4_ON_WINDOWS) /** 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 the directory. @param uid User ID for new directories. @param gid Group ID for new directories. @param mode Mode for new directories. @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_MKDIR_FAILED
with errno value in iDetails1 if the mkdir() function failed on a non-Windows system, - DK4_E_CHOWN_FAILED
with errno value in iDetails1 if the chown() function failed on a non-Windows system, - DK4_E_CHMOD_FAILED
with errno value in iDetails1 if the chmod() function failed on a non-Windows system, - DK4_E_CREATE_DIR_FAILED
with GetLastError() result in lDetails1 if the CreateDirectory() function failed on a Windows system. */ static int dk4mkdir_hierarchy_ugm_c8_one( const char *fn, int ro, uid_t uid, gid_t gid, mode_t mode, dk4_er_t *erp ) { dk4_stat_t stb; int back = 0; #line 788 "dk4mkdh8.ctr" #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 if (0 != dk4stat_c8(&stb, fn, NULL)) { back = dk4stat_is_directory(&stb, erp); if (0 == back) { dk4error_set_simple_error_code(erp, DK4_E_NON_DIR); } } else { if (0 != ro) { dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND); } else { errno = 0; if (0 == mkdir(fn, mode)) { errno = 0; if (0 == chown(fn, uid, gid)) { errno = 0; if (0 == chmod(fn, mode)) { back = 1; } else { dk4error_set_idetails(erp, DK4_E_CHMOD_FAILED, errno); } } else { dk4error_set_idetails(erp, DK4_E_CHOWN_FAILED, errno); } } else { dk4error_set_idetails(erp, DK4_E_MKDIR_FAILED, errno); } } } #else back = 1; #endif #line 827 "dk4mkdh8.ctr" finished: return back; } /** Create directory structure using an editable copy of the path. @param fn File name. @param lc Flag: Last component in fn is directory too. @param uid User ID for new directories. @param gid Group ID for new directories. @param mode Mode for new directories. @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_CHOWN_FAILED
with errno value in iDetails1 if the chown() function failed on a non-Windows system, - DK4_E_CHMOD_FAILED
with errno value in iDetails1 if the chmod() function failed on a non-Windows system, - DK4_E_CREATE_DIR_FAILED
with GetLastError() result in lDetails1 if the CreateDirectory() function failed on a Windows system. */ static int dk4mkdir_hierarchy_ugm_c8_on_copy( char *fn, int lc, uid_t uid, gid_t gid, mode_t mode, dk4_er_t *erp ) { char *pc; int back = 0; #line 881 "dk4mkdh8.ctr" #if DK4_USE_ASSERT assert(NULL != fn); #endif /* Correct file name */ dk4path8_correct_sep(fn); /* Find first real separator */ pc = dk4mkdir_hierarchy_c8_first_sep(fn, &back, lc, erp); /* Process directories */ if (NULL != pc) { back = 1; while (NULL != pc) { *pc = '\0'; if (0 < dk4str8_len(fn)) { if (0 == dk4mkdir_hierarchy_ugm_c8_one(fn, 0, uid, gid, mode, erp)) { back = 0; } } #if DK4_HAVE_BACKSLASH_AS_SEP *pc = '\\'; pc = dk4str8_chr(&(pc[1]), '\\'); #else *pc = '/'; pc = dk4str8_chr(&(pc[1]), '/'); #endif } if (0 != lc) { if (0 == dk4mkdir_hierarchy_ugm_c8_one(fn, 0, uid, gid, mode, erp)) { back = 0; } } } #line 917 "dk4mkdh8.ctr" return back; } /** Create directory structure using a local copy of the path. @param fn File name. @param lc Flag: Last component in fn is directory too. @param uid User ID for new directories. @param gid Group ID for new directories. @param mode Mode for new directories. @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_CHOWN_FAILED
with errno value in iDetails1 if the chown() function failed on a non-Windows system, - DK4_E_CHMOD_FAILED
with errno value in iDetails1 if the chmod() function failed on a non-Windows system, - DK4_E_CREATE_DIR_FAILED
with GetLastError() result in lDetails1 if the CreateDirectory() function failed on a Windows system. */ static int dk4mkdir_hierarchy_ugm_c8_local( const char *fn, int lc, uid_t uid, gid_t gid, mode_t mode, dk4_er_t *erp ) { char mycp[DK4_MAX_PATH]; int back = 0; #line 969 "dk4mkdh8.ctr" #if DK4_USE_ASSERT assert(NULL != fn); #endif if (0 != dk4str8_cpy_s(mycp, sizeof(mycp), fn, erp)) { back = dk4mkdir_hierarchy_ugm_c8_on_copy(mycp, lc, uid, gid, mode, erp); } #line 975 "dk4mkdh8.ctr" return back; } int dk4mkdir_hierarchy_ugm_c8( const char *fn, int lc, uid_t uid, gid_t gid, mode_t mode, dk4_er_t *erp ) { char *mycp; size_t sz; int back = 0; #line 994 "dk4mkdh8.ctr" #if DK4_USE_ASSERT assert(NULL != fn); #endif if (NULL != fn) { sz = dk4str8_len(fn); if (DK4_MAX_PATH > sz) { back = dk4mkdir_hierarchy_ugm_c8_local(fn, lc, uid, gid, mode, erp); } else { mycp = dk4str8_dup(fn, erp); if (NULL != mycp) { back = dk4mkdir_hierarchy_ugm_c8_on_copy(mycp, lc, uid, gid, mode, erp); dk4mem_free(mycp); } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } #line 1011 "dk4mkdh8.ctr" return back; } #endif