FileFind.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // Windows/FileFind.cpp
  2. #include "StdAfx.h"
  3. #include "FileFind.h"
  4. #ifndef _UNICODE
  5. #include "../Common/StringConvert.h"
  6. #endif
  7. #ifndef _UNICODE
  8. extern bool g_IsNT;
  9. #endif
  10. namespace NWindows {
  11. namespace NFile {
  12. #if defined(WIN_LONG_PATH) && defined(_UNICODE)
  13. #define WIN_LONG_PATH2
  14. #endif
  15. bool GetLongPath(LPCWSTR fileName, UString &res);
  16. namespace NFind {
  17. static const TCHAR kDot = TEXT('.');
  18. bool CFileInfo::IsDots() const
  19. {
  20. if (!IsDirectory() || Name.IsEmpty())
  21. return false;
  22. if (Name[0] != kDot)
  23. return false;
  24. return Name.Length() == 1 || (Name[1] == kDot && Name.Length() == 2);
  25. }
  26. #ifndef _UNICODE
  27. bool CFileInfoW::IsDots() const
  28. {
  29. if (!IsDirectory() || Name.IsEmpty())
  30. return false;
  31. if (Name[0] != kDot)
  32. return false;
  33. return Name.Length() == 1 || (Name[1] == kDot && Name.Length() == 2);
  34. }
  35. #endif
  36. static void ConvertWIN32_FIND_DATA_To_FileInfo(
  37. const WIN32_FIND_DATA &findData,
  38. CFileInfo &fileInfo)
  39. {
  40. fileInfo.Attributes = findData.dwFileAttributes;
  41. fileInfo.CreationTime = findData.ftCreationTime;
  42. fileInfo.LastAccessTime = findData.ftLastAccessTime;
  43. fileInfo.LastWriteTime = findData.ftLastWriteTime;
  44. fileInfo.Size = (((UInt64)findData.nFileSizeHigh) << 32) + findData.nFileSizeLow;
  45. fileInfo.Name = findData.cFileName;
  46. #ifndef _WIN32_WCE
  47. fileInfo.ReparseTag = findData.dwReserved0;
  48. #else
  49. fileInfo.ObjectID = findData.dwOID;
  50. #endif
  51. }
  52. #ifndef _UNICODE
  53. static inline UINT GetCurrentCodePage() { return ::AreFileApisANSI() ? CP_ACP : CP_OEMCP; }
  54. static void ConvertWIN32_FIND_DATA_To_FileInfo(
  55. const WIN32_FIND_DATAW &findData,
  56. CFileInfoW &fileInfo)
  57. {
  58. fileInfo.Attributes = findData.dwFileAttributes;
  59. fileInfo.CreationTime = findData.ftCreationTime;
  60. fileInfo.LastAccessTime = findData.ftLastAccessTime;
  61. fileInfo.LastWriteTime = findData.ftLastWriteTime;
  62. fileInfo.Size = (((UInt64)findData.nFileSizeHigh) << 32) + findData.nFileSizeLow;
  63. fileInfo.Name = findData.cFileName;
  64. #ifndef _WIN32_WCE
  65. fileInfo.ReparseTag = findData.dwReserved0;
  66. #else
  67. fileInfo.ObjectID = findData.dwOID;
  68. #endif
  69. }
  70. static void ConvertWIN32_FIND_DATA_To_FileInfo(
  71. const WIN32_FIND_DATA &findData,
  72. CFileInfoW &fileInfo)
  73. {
  74. fileInfo.Attributes = findData.dwFileAttributes;
  75. fileInfo.CreationTime = findData.ftCreationTime;
  76. fileInfo.LastAccessTime = findData.ftLastAccessTime;
  77. fileInfo.LastWriteTime = findData.ftLastWriteTime;
  78. fileInfo.Size = (((UInt64)findData.nFileSizeHigh) << 32) + findData.nFileSizeLow;
  79. fileInfo.Name = GetUnicodeString(findData.cFileName, GetCurrentCodePage());
  80. #ifndef _WIN32_WCE
  81. fileInfo.ReparseTag = findData.dwReserved0;
  82. #else
  83. fileInfo.ObjectID = findData.dwOID;
  84. #endif
  85. }
  86. #endif
  87. ////////////////////////////////
  88. // CFindFile
  89. bool CFindFile::Close()
  90. {
  91. if (_handle == INVALID_HANDLE_VALUE)
  92. return true;
  93. if (!::FindClose(_handle))
  94. return false;
  95. _handle = INVALID_HANDLE_VALUE;
  96. return true;
  97. }
  98. bool CFindFile::FindFirst(LPCTSTR wildcard, CFileInfo &fileInfo)
  99. {
  100. if (!Close())
  101. return false;
  102. WIN32_FIND_DATA findData;
  103. _handle = ::FindFirstFile(wildcard, &findData);
  104. #ifdef WIN_LONG_PATH2
  105. if (_handle == INVALID_HANDLE_VALUE)
  106. {
  107. UString longPath;
  108. if (GetLongPath(wildcard, longPath))
  109. _handle = ::FindFirstFileW(longPath, &findData);
  110. }
  111. #endif
  112. if (_handle == INVALID_HANDLE_VALUE)
  113. return false;
  114. ConvertWIN32_FIND_DATA_To_FileInfo(findData, fileInfo);
  115. return true;
  116. }
  117. #ifndef _UNICODE
  118. bool CFindFile::FindFirst(LPCWSTR wildcard, CFileInfoW &fileInfo)
  119. {
  120. if (!Close())
  121. return false;
  122. if (g_IsNT)
  123. {
  124. WIN32_FIND_DATAW findData;
  125. _handle = ::FindFirstFileW(wildcard, &findData);
  126. #ifdef WIN_LONG_PATH
  127. if (_handle == INVALID_HANDLE_VALUE)
  128. {
  129. UString longPath;
  130. if (GetLongPath(wildcard, longPath))
  131. _handle = ::FindFirstFileW(longPath, &findData);
  132. }
  133. #endif
  134. if (_handle != INVALID_HANDLE_VALUE)
  135. ConvertWIN32_FIND_DATA_To_FileInfo(findData, fileInfo);
  136. }
  137. else
  138. {
  139. WIN32_FIND_DATAA findData;
  140. _handle = ::FindFirstFileA(UnicodeStringToMultiByte(wildcard,
  141. GetCurrentCodePage()), &findData);
  142. if (_handle != INVALID_HANDLE_VALUE)
  143. ConvertWIN32_FIND_DATA_To_FileInfo(findData, fileInfo);
  144. }
  145. return (_handle != INVALID_HANDLE_VALUE);
  146. }
  147. #endif
  148. bool CFindFile::FindNext(CFileInfo &fileInfo)
  149. {
  150. WIN32_FIND_DATA findData;
  151. bool result = BOOLToBool(::FindNextFile(_handle, &findData));
  152. if (result)
  153. ConvertWIN32_FIND_DATA_To_FileInfo(findData, fileInfo);
  154. return result;
  155. }
  156. #ifndef _UNICODE
  157. bool CFindFile::FindNext(CFileInfoW &fileInfo)
  158. {
  159. if (g_IsNT)
  160. {
  161. WIN32_FIND_DATAW findData;
  162. if (!::FindNextFileW(_handle, &findData))
  163. return false;
  164. ConvertWIN32_FIND_DATA_To_FileInfo(findData, fileInfo);
  165. }
  166. else
  167. {
  168. WIN32_FIND_DATAA findData;
  169. if (!::FindNextFileA(_handle, &findData))
  170. return false;
  171. ConvertWIN32_FIND_DATA_To_FileInfo(findData, fileInfo);
  172. }
  173. return true;
  174. }
  175. #endif
  176. bool FindFile(LPCTSTR wildcard, CFileInfo &fileInfo)
  177. {
  178. CFindFile finder;
  179. return finder.FindFirst(wildcard, fileInfo);
  180. }
  181. #ifndef _UNICODE
  182. bool FindFile(LPCWSTR wildcard, CFileInfoW &fileInfo)
  183. {
  184. CFindFile finder;
  185. return finder.FindFirst(wildcard, fileInfo);
  186. }
  187. #endif
  188. bool DoesFileExist(LPCTSTR name)
  189. {
  190. CFileInfo fileInfo;
  191. return FindFile(name, fileInfo);
  192. }
  193. #ifndef _UNICODE
  194. bool DoesFileExist(LPCWSTR name)
  195. {
  196. CFileInfoW fileInfo;
  197. return FindFile(name, fileInfo);
  198. }
  199. #endif
  200. /////////////////////////////////////
  201. // CEnumerator
  202. bool CEnumerator::NextAny(CFileInfo &fileInfo)
  203. {
  204. if (_findFile.IsHandleAllocated())
  205. return _findFile.FindNext(fileInfo);
  206. else
  207. return _findFile.FindFirst(_wildcard, fileInfo);
  208. }
  209. bool CEnumerator::Next(CFileInfo &fileInfo)
  210. {
  211. for (;;)
  212. {
  213. if (!NextAny(fileInfo))
  214. return false;
  215. if (!fileInfo.IsDots())
  216. return true;
  217. }
  218. }
  219. bool CEnumerator::Next(CFileInfo &fileInfo, bool &found)
  220. {
  221. if (Next(fileInfo))
  222. {
  223. found = true;
  224. return true;
  225. }
  226. found = false;
  227. return (::GetLastError() == ERROR_NO_MORE_FILES);
  228. }
  229. #ifndef _UNICODE
  230. bool CEnumeratorW::NextAny(CFileInfoW &fileInfo)
  231. {
  232. if (_findFile.IsHandleAllocated())
  233. return _findFile.FindNext(fileInfo);
  234. else
  235. return _findFile.FindFirst(_wildcard, fileInfo);
  236. }
  237. bool CEnumeratorW::Next(CFileInfoW &fileInfo)
  238. {
  239. for (;;)
  240. {
  241. if (!NextAny(fileInfo))
  242. return false;
  243. if (!fileInfo.IsDots())
  244. return true;
  245. }
  246. }
  247. bool CEnumeratorW::Next(CFileInfoW &fileInfo, bool &found)
  248. {
  249. if (Next(fileInfo))
  250. {
  251. found = true;
  252. return true;
  253. }
  254. found = false;
  255. return (::GetLastError() == ERROR_NO_MORE_FILES);
  256. }
  257. #endif
  258. ////////////////////////////////
  259. // CFindChangeNotification
  260. // FindFirstChangeNotification can return 0. MSDN doesn't tell about it.
  261. bool CFindChangeNotification::Close()
  262. {
  263. if (!IsHandleAllocated())
  264. return true;
  265. if (!::FindCloseChangeNotification(_handle))
  266. return false;
  267. _handle = INVALID_HANDLE_VALUE;
  268. return true;
  269. }
  270. HANDLE CFindChangeNotification::FindFirst(LPCTSTR pathName, bool watchSubtree, DWORD notifyFilter)
  271. {
  272. _handle = ::FindFirstChangeNotification(pathName, BoolToBOOL(watchSubtree), notifyFilter);
  273. #ifdef WIN_LONG_PATH2
  274. if (!IsHandleAllocated())
  275. {
  276. UString longPath;
  277. if (GetLongPath(pathName, longPath))
  278. _handle = ::FindFirstChangeNotificationW(longPath, BoolToBOOL(watchSubtree), notifyFilter);
  279. }
  280. #endif
  281. return _handle;
  282. }
  283. #ifndef _UNICODE
  284. HANDLE CFindChangeNotification::FindFirst(LPCWSTR pathName, bool watchSubtree, DWORD notifyFilter)
  285. {
  286. if (!g_IsNT)
  287. return FindFirst(UnicodeStringToMultiByte(pathName, GetCurrentCodePage()), watchSubtree, notifyFilter);
  288. _handle = ::FindFirstChangeNotificationW(pathName, BoolToBOOL(watchSubtree), notifyFilter);
  289. #ifdef WIN_LONG_PATH
  290. if (!IsHandleAllocated())
  291. {
  292. UString longPath;
  293. if (GetLongPath(pathName, longPath))
  294. _handle = ::FindFirstChangeNotificationW(longPath, BoolToBOOL(watchSubtree), notifyFilter);
  295. }
  296. #endif
  297. return _handle;
  298. }
  299. #endif
  300. #ifndef _WIN32_WCE
  301. bool MyGetLogicalDriveStrings(CSysStringVector &driveStrings)
  302. {
  303. driveStrings.Clear();
  304. UINT32 size = GetLogicalDriveStrings(0, NULL);
  305. if (size == 0)
  306. return false;
  307. CSysString buffer;
  308. UINT32 newSize = GetLogicalDriveStrings(size, buffer.GetBuffer(size));
  309. if (newSize == 0)
  310. return false;
  311. if (newSize > size)
  312. return false;
  313. CSysString string;
  314. for(UINT32 i = 0; i < newSize; i++)
  315. {
  316. TCHAR c = buffer[i];
  317. if (c == TEXT('\0'))
  318. {
  319. driveStrings.Add(string);
  320. string.Empty();
  321. }
  322. else
  323. string += c;
  324. }
  325. if (!string.IsEmpty())
  326. return false;
  327. return true;
  328. }
  329. #ifndef _UNICODE
  330. bool MyGetLogicalDriveStrings(UStringVector &driveStrings)
  331. {
  332. driveStrings.Clear();
  333. if (g_IsNT)
  334. {
  335. UINT32 size = GetLogicalDriveStringsW(0, NULL);
  336. if (size == 0)
  337. return false;
  338. UString buffer;
  339. UINT32 newSize = GetLogicalDriveStringsW(size, buffer.GetBuffer(size));
  340. if (newSize == 0)
  341. return false;
  342. if (newSize > size)
  343. return false;
  344. UString string;
  345. for(UINT32 i = 0; i < newSize; i++)
  346. {
  347. WCHAR c = buffer[i];
  348. if (c == L'\0')
  349. {
  350. driveStrings.Add(string);
  351. string.Empty();
  352. }
  353. else
  354. string += c;
  355. }
  356. return string.IsEmpty();
  357. }
  358. CSysStringVector driveStringsA;
  359. bool res = MyGetLogicalDriveStrings(driveStringsA);
  360. for (int i = 0; i < driveStringsA.Size(); i++)
  361. driveStrings.Add(GetUnicodeString(driveStringsA[i]));
  362. return res;
  363. }
  364. #endif
  365. #endif
  366. }}}