Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
6.1 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. MKDIR.CPP
  5. Abstract:
  6. Creates directories
  7. History:
  8. --*/
  9. #include "precomp.h"
  10. #include "corepol.h"
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <mbstring.h>
  14. #include <helper.h>
  15. #include <sddl.h>
  16. #include <accctrl.h>
  17. #include <aclapi.h>
  18. class CTmpStrException
  19. {
  20. };
  21. class TmpStr
  22. {
  23. private:
  24. TCHAR *pString;
  25. public:
  26. TmpStr() :
  27. pString(NULL)
  28. {
  29. }
  30. ~TmpStr()
  31. {
  32. delete [] pString;
  33. }
  34. TmpStr &operator =(const TCHAR *szStr)
  35. {
  36. delete [] pString;
  37. pString = NULL;
  38. if (szStr)
  39. {
  40. size_t stringSize = lstrlen(szStr) + 1;
  41. pString = new TCHAR[stringSize];
  42. if (!pString)
  43. throw CTmpStrException();
  44. StringCchCopy(pString, stringSize, szStr);
  45. }
  46. return *this;
  47. }
  48. operator const TCHAR *() const
  49. {
  50. return pString;
  51. }
  52. TCHAR Right(int i)
  53. {
  54. if (pString && (lstrlen(pString) >= i))
  55. {
  56. return pString[lstrlen(pString) - i];
  57. }
  58. else
  59. {
  60. return '\0';
  61. }
  62. }
  63. TmpStr &operator +=(const TCHAR ch)
  64. {
  65. if (pString)
  66. {
  67. size_t stringLength = lstrlen(pString) + 2;
  68. TCHAR *tmpstr = new TCHAR[stringLength];
  69. if (!tmpstr)
  70. throw CTmpStrException();
  71. StringCchCopy(tmpstr, stringLength, pString);
  72. tmpstr[lstrlen(pString)] = ch;
  73. tmpstr[lstrlen(pString) + 1] = TEXT('\0');
  74. delete [] pString;
  75. pString = tmpstr;
  76. }
  77. else
  78. {
  79. TCHAR *tmpstr = new TCHAR[2];
  80. if (!tmpstr)
  81. throw CTmpStrException();
  82. tmpstr[0] = ch;
  83. tmpstr[1] = TEXT('\0');
  84. pString = tmpstr;
  85. }
  86. return *this;
  87. }
  88. TmpStr &operator +=(const TCHAR *sz)
  89. {
  90. if (sz && pString)
  91. {
  92. size_t stringLength = lstrlen(pString) + lstrlen(sz) + 1;
  93. TCHAR *tmpstr = new TCHAR[stringLength];
  94. if (!tmpstr)
  95. throw CTmpStrException();
  96. StringCchCopy(tmpstr,stringLength, pString);
  97. StringCchCat(tmpstr, stringLength, sz);
  98. delete [] pString;
  99. pString = tmpstr;
  100. }
  101. else if (sz)
  102. {
  103. size_t stringLength = lstrlen(sz) + 1;
  104. TCHAR *tmpstr = new TCHAR[stringLength];
  105. if (!tmpstr)
  106. throw CTmpStrException();
  107. StringCchCopy(tmpstr, stringLength, sz);
  108. pString = tmpstr;
  109. }
  110. return *this;
  111. }
  112. };
  113. BOOL POLARITY WbemCreateDirectory(const wchar_t *pszDirName)
  114. {
  115. BOOL bStat = TRUE;
  116. wchar_t *pCurrent = NULL;
  117. size_t stringLength = wcslen(pszDirName) + 1;
  118. wchar_t *pDirName = new wchar_t[stringLength];
  119. if (!pDirName)
  120. return FALSE;
  121. StringCchCopy(pDirName, stringLength, pszDirName);
  122. try
  123. {
  124. TmpStr szDirName;
  125. pCurrent = wcstok(pDirName, TEXT("\\"));
  126. szDirName = pCurrent;
  127. while (pCurrent)
  128. {
  129. if ((pCurrent[lstrlen(pCurrent)-1] != ':') && //This is "<drive>:\\"
  130. (pCurrent[0] != TEXT('\\'))) //There is double slash in name
  131. {
  132. struct _stat stats;
  133. int dwstat = _wstat(szDirName, &stats);
  134. if ((dwstat == 0) &&
  135. !(stats.st_mode & _S_IFDIR))
  136. {
  137. bStat = FALSE;
  138. break;
  139. }
  140. else if (dwstat == -1)
  141. {
  142. DWORD dwStatus = GetLastError();
  143. if (!CreateDirectory(szDirName, 0))
  144. {
  145. bStat = FALSE;
  146. break;
  147. }
  148. }
  149. // else it exists already
  150. }
  151. szDirName += TEXT('\\');
  152. pCurrent = wcstok(0, TEXT("\\"));
  153. szDirName += pCurrent;
  154. }
  155. }
  156. catch(...)
  157. {
  158. bStat = FALSE;
  159. }
  160. delete [] pDirName;
  161. return bStat;
  162. }
  163. //
  164. // Test for directory Existence
  165. // if the name is a file, it deletes it.
  166. // if the directory is not found, then it creates the directory
  167. // with the specified Security descriptor
  168. //
  169. ///////////////////////////////////////////////////////
  170. HRESULT POLARITY TestDirExistAndCreateWithSDIfNotThere(TCHAR * pDirectory, TCHAR * pSDDLString)
  171. {
  172. DWORD dwRes = 0;
  173. DWORD dwAttr = GetFileAttributes(pDirectory);
  174. dwRes = GetLastError();
  175. if (INVALID_FILE_ATTRIBUTES != dwAttr)
  176. {
  177. if (FILE_ATTRIBUTE_DIRECTORY & dwAttr)
  178. {
  179. // it's there and it's a directory
  180. return S_OK;
  181. }
  182. // it can be a file, wipe it out
  183. if (FALSE == DeleteFile(pDirectory))
  184. return MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32,GetLastError());
  185. else
  186. dwRes = ERROR_PATH_NOT_FOUND;
  187. }
  188. // if here, the directory was not found, or it was found as a file
  189. if (ERROR_FILE_NOT_FOUND == dwRes ||
  190. ERROR_PATH_NOT_FOUND == dwRes)
  191. {
  192. PSECURITY_DESCRIPTOR pSD = NULL;
  193. if (FALSE == ConvertStringSecurityDescriptorToSecurityDescriptor(pSDDLString,
  194. SDDL_REVISION_1,
  195. &pSD,
  196. NULL)) return MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32,GetLastError());
  197. OnDelete<HLOCAL,HLOCAL(*)(HLOCAL),LocalFree> dm1(pSD);
  198. SECURITY_ATTRIBUTES SecAttr = {sizeof(SecAttr),pSD,FALSE};
  199. if (FALSE == CreateDirectory(pDirectory,&SecAttr))
  200. dwRes = GetLastError();
  201. else
  202. dwRes = ERROR_SUCCESS;
  203. }
  204. if (ERROR_SUCCESS != dwRes)
  205. return MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, dwRes);
  206. else
  207. return S_OK;
  208. };