/* WARNING: This file was generated by dkct. 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 */ /* Copyright (C) 2015-2017, Dirk Krause Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** @file dk4mkdh8.c The dk4mkdh8 module. */ #line 156 "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 #line 238 "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 278 "dk4mkdh8.ctr" 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 357 "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 (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 499 "dk4mkdh8.ctr" 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 525 "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 (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 599 "dk4mkdh8.ctr" /* 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 631 "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 667 "dk4mkdh8.ctr" if (0 != dk4str8_cpy_s(mycp, sizeof(mycp), fn, erp)) { back = dk4mkdir_hierarchy_c8_on_copy(mycp, lc, erp); } #line 670 "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 682 "dk4mkdh8.ctr" 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 696 "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 749 "dk4mkdh8.ctr" 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 785 "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 839 "dk4mkdh8.ctr" /* 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 871 "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 923 "dk4mkdh8.ctr" if (0 != dk4str8_cpy_s(mycp, sizeof(mycp), fn, erp)) { back = dk4mkdir_hierarchy_ugm_c8_on_copy(mycp, lc, uid, gid, mode, erp); } #line 926 "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 945 "dk4mkdh8.ctr" 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 959 "dk4mkdh8.ctr" return back; } #endif