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.

253 lines
6.9 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. enumfile.c
  5. Abstract:
  6. File enumater, given a root direcory, it will enumerates all file under
  7. this directory.
  8. Author:
  9. Xiaofeng Zang (xiaoz) 08-Oct-2001 Created
  10. Revision History:
  11. <alias> <date> <comments>
  12. --*/
  13. #include "StdAfx.h"
  14. #include "clmt.h"
  15. BOOL EnumBranch
  16. (
  17. HANDLE hFile,
  18. LPWIN32_FIND_DATA pfd,
  19. LPTSTR szCurDir,
  20. LPTSTR szExt,
  21. FILEENUMPROC lpEnumProc
  22. )
  23. {
  24. TCHAR *p;
  25. BOOL bDone;
  26. TCHAR szSubDir[3*MAX_PATH+3],*lpSubDir;
  27. HANDLE hFileSub;
  28. TCHAR szFullPath[3*MAX_PATH+3],*lpFullPath;
  29. HRESULT hr;
  30. size_t cchSubDirLen,cchFullPathLen;
  31. BOOL bExit;
  32. BOOL bReturn = TRUE;
  33. if (!pfd || !szCurDir)
  34. {
  35. SetLastError(ERROR_INVALID_PARAMETER);
  36. return FALSE;
  37. }
  38. bExit = FALSE;
  39. do
  40. {
  41. if(pfd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  42. {
  43. if(0 == lstrcmp(pfd->cFileName , TEXT(".")))
  44. {
  45. continue;
  46. }
  47. if(0 == lstrcmp(pfd->cFileName , TEXT("..")))
  48. {
  49. continue;
  50. }
  51. //we statically allocated szSubDir 3*MAX_PATH
  52. //but limit the len use to 2*MAX_PATH, so that
  53. //later on ConcatenatePaths a file name will not fail
  54. hr = StringCchCopy(szSubDir, 2*(MAX_PATH+1),szCurDir);
  55. if (SUCCEEDED(hr))
  56. {
  57. cchSubDirLen = 3*(MAX_PATH+1);
  58. lpSubDir = szSubDir;
  59. }
  60. else
  61. {
  62. if (HRESULT_CODE(hr) == ERROR_INSUFFICIENT_BUFFER)
  63. {
  64. cchSubDirLen = lstrlen(szCurDir)+1 + MAX_PATH + 1;
  65. lpSubDir = malloc(cchSubDirLen * sizeof(TCHAR));
  66. if (!lpSubDir)
  67. {
  68. bExit = TRUE;
  69. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  70. bReturn = FALSE;
  71. goto Exit1;
  72. }
  73. if (FAILED(StringCchCopy(lpSubDir, cchSubDirLen,szCurDir)))
  74. {
  75. bExit = TRUE;
  76. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  77. bReturn = FALSE;
  78. goto Exit1;
  79. }
  80. }
  81. else
  82. {
  83. bExit = TRUE;
  84. goto Exit1;
  85. }
  86. }
  87. ConcatenatePaths(lpSubDir, pfd->cFileName ,cchSubDirLen);
  88. p = lpSubDir + lstrlen(lpSubDir);
  89. ConcatenatePaths(lpSubDir, TEXT("*.*"),cchSubDirLen);
  90. hFileSub = FindFirstFile(lpSubDir, pfd);
  91. if(INVALID_HANDLE_VALUE == hFileSub)
  92. {
  93. continue;
  94. }
  95. *p = TEXT('\0');
  96. if (!EnumBranch(hFileSub, pfd, lpSubDir,szExt,lpEnumProc))
  97. {
  98. bReturn = FALSE;
  99. bExit = TRUE;
  100. }
  101. FindClose(hFileSub);
  102. Exit1:
  103. if (lpSubDir && (lpSubDir != szSubDir))
  104. {
  105. free(lpSubDir);
  106. lpSubDir = NULL;
  107. }
  108. }
  109. else
  110. if(pfd->cFileName)
  111. {
  112. if (lstrlen(pfd->cFileName) > lstrlen(szExt))
  113. {
  114. p = pfd->cFileName + (lstrlen(pfd->cFileName) - lstrlen(szExt));
  115. if (!MyStrCmpI(p,szExt))
  116. {
  117. hr = StringCchCopy(szFullPath, 2*(MAX_PATH+1),szCurDir);
  118. if (SUCCEEDED(hr))
  119. {
  120. cchFullPathLen = 3*(MAX_PATH+1);
  121. lpFullPath = szFullPath;
  122. }
  123. else
  124. {
  125. if (HRESULT_CODE(hr) == ERROR_INSUFFICIENT_BUFFER)
  126. {
  127. cchFullPathLen = lstrlen(szCurDir)+1 + MAX_PATH + 1;
  128. lpFullPath = malloc(cchFullPathLen * sizeof(TCHAR));
  129. if (!lpFullPath)
  130. {
  131. bExit = TRUE;
  132. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  133. bReturn = FALSE;
  134. goto Exit2;
  135. }
  136. if (FAILED(StringCchCopy(lpFullPath, cchFullPathLen,szCurDir)))
  137. {
  138. bExit = TRUE;
  139. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  140. bReturn = FALSE;
  141. goto Exit2;
  142. }
  143. }
  144. else
  145. {
  146. bExit = TRUE;
  147. goto Exit2;
  148. }
  149. }
  150. ConcatenatePaths(lpFullPath, pfd->cFileName,cchFullPathLen);
  151. if (!lpEnumProc(szFullPath))
  152. {
  153. bReturn = FALSE;
  154. bExit = TRUE;
  155. }
  156. Exit2:
  157. if (lpFullPath && (lpFullPath != szFullPath))
  158. {
  159. free(lpFullPath);
  160. lpFullPath = NULL;
  161. }
  162. }
  163. }
  164. }
  165. }
  166. while(FindNextFile( hFile, pfd) && !bExit);
  167. return (bReturn);
  168. }
  169. BOOL
  170. MyEnumFiles
  171. (
  172. LPTSTR szRoot,
  173. LPTSTR szExt,
  174. FILEENUMPROC lpEnumProc
  175. )
  176. {
  177. HANDLE hFile;
  178. WIN32_FIND_DATA fd;
  179. TCHAR *szSubDir;
  180. TCHAR *p;
  181. size_t cbRoot;
  182. BOOL bRet = FALSE;
  183. if (!szRoot || !lpEnumProc)
  184. {
  185. SetLastError(ERROR_INVALID_PARAMETER);
  186. return FALSE;
  187. }
  188. cbRoot = lstrlen(szRoot);
  189. szSubDir = malloc( (cbRoot+MAX_PATH) * sizeof(TCHAR));
  190. if (!szSubDir)
  191. {
  192. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  193. goto Cleanup;
  194. }
  195. if (FAILED(StringCchCopy(szSubDir, cbRoot+MAX_PATH, szRoot)))
  196. {
  197. SetLastError(ERROR_INVALID_PARAMETER);
  198. goto Cleanup;
  199. }
  200. p = szSubDir + lstrlen(szSubDir);
  201. ConcatenatePaths(szSubDir, TEXT("\\*.*"),cbRoot+MAX_PATH);
  202. hFile = FindFirstFile(szSubDir, &fd);
  203. if(INVALID_HANDLE_VALUE == hFile)
  204. {
  205. goto Cleanup;
  206. }
  207. *p = TEXT('\0');
  208. EnumBranch(hFile, &fd, szSubDir,szExt, lpEnumProc);
  209. FindClose(hFile);
  210. bRet = TRUE;
  211. Cleanup:
  212. if (szSubDir)
  213. {
  214. free(szSubDir);
  215. }
  216. return bRet;
  217. }