fileutil.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. #include "precompile.h"
  2. #include <wchar.h>
  3. #include <errno.h>
  4. #ifdef _WIN32
  5. #include <mbstring.h>
  6. #include <limits.h>
  7. #include <ShellAPI.h>
  8. #else
  9. #include <winpr/wtypes.h>
  10. #include <winpr/error.h>
  11. #include <winpr/file.h>
  12. #endif
  13. #include "array.h"
  14. #include "fileutil.h"
  15. #include "strutil.h"
  16. TOOLKIT_API BOOL CreateDirA(LPCSTR lpDirPath, BOOL bRecursive)
  17. {
  18. size_t len = strlen(lpDirPath);
  19. if (len <= 2 || len >= MAX_PATH-2) {
  20. return FALSE;
  21. } else {
  22. if (!bRecursive) {
  23. return CreateDirectoryA(lpDirPath, NULL) || (GetLastError() == ERROR_ALREADY_EXISTS);
  24. } else {
  25. CHAR tmp[MAX_PATH], *p;
  26. p = &tmp[0];
  27. strncpy(tmp, lpDirPath, MAX_PATH);
  28. tmp[MAX_PATH-1] = 0;
  29. if (tmp[len-1] != '\\') {
  30. tmp[len++] = '\\';
  31. tmp[len] = 0;
  32. }
  33. while ((p = strchr(p, '\\')) != NULL) {
  34. *p = 0;
  35. if (!ExistsDirA(tmp)) {
  36. if (!CreateDirectoryA(tmp, NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
  37. return FALSE;
  38. }
  39. *p = '\\';
  40. p++;
  41. }
  42. }
  43. }
  44. return TRUE;
  45. }
  46. TOOLKIT_API BOOL CreateDirW(LPCWSTR lpDirPath, BOOL bRecursive)
  47. {
  48. size_t len = wcslen(lpDirPath);
  49. if (len <= 2 || len >= MAX_PATH-2) {
  50. return FALSE;
  51. } else {
  52. if (!bRecursive) {
  53. return CreateDirectoryW(lpDirPath, NULL) || (GetLastError() == ERROR_ALREADY_EXISTS);
  54. } else {
  55. WCHAR tmp[MAX_PATH], *p;
  56. p = &tmp[0];
  57. wcsncpy(tmp, lpDirPath, MAX_PATH);
  58. tmp[MAX_PATH-1] = 0;
  59. if (tmp[len-1] != '\\') {
  60. tmp[len++] = '\\';
  61. tmp[len] = 0;
  62. }
  63. while ((p = wcschr(p, '\\')) != NULL) {
  64. *p = 0;
  65. if (!ExistsDirW(tmp)) {
  66. if (!CreateDirectoryW(tmp, NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
  67. return FALSE;
  68. }
  69. *p = '\\';
  70. p++;
  71. }
  72. }
  73. }
  74. return TRUE;
  75. }
  76. TOOLKIT_API BOOL CreateParentDirA(LPCSTR lpPath, BOOL bRecursive)
  77. {
  78. CHAR pdir[MAX_PATH];
  79. const CHAR *p;
  80. p = strrchr(lpPath, '\\');
  81. if (!p)
  82. return FALSE;
  83. strncpy(pdir, lpPath, p-lpPath);
  84. pdir[p-lpPath] = 0;
  85. return CreateDirA(pdir, bRecursive);
  86. }
  87. TOOLKIT_API BOOL CreateParentDirW(LPCWSTR lpPath, BOOL bRecursive)
  88. {
  89. WCHAR pdir[MAX_PATH];
  90. const WCHAR *p;
  91. p = wcsrchr(lpPath, '\\');
  92. if (!p)
  93. return FALSE;
  94. wcsncpy(pdir, lpPath, p-lpPath);
  95. pdir[p-lpPath] = 0;
  96. return CreateDirW(pdir, bRecursive);
  97. }
  98. TOOLKIT_API BOOL ClearDirRecursiveA(LPCSTR lpDirPath)
  99. {
  100. array_header_t *arr;
  101. BOOL bRet = TRUE;
  102. assert(lpDirPath);
  103. arr = fileutil_get_sub_files_a(lpDirPath);
  104. if (arr) {
  105. int i;
  106. for (i = 0; i < arr->nelts; ++i) {
  107. char *tmp = ARRAY_IDX(arr, i, char*);
  108. bRet &= DeleteFileA(tmp);
  109. }
  110. toolkit_array_free2(arr);
  111. }
  112. arr = fileutil_get_sub_dirs_a(lpDirPath);
  113. if (arr) {
  114. int i;
  115. for (i = 0; i < arr->nelts; ++i) {
  116. char *tmp = ARRAY_IDX(arr, i, char*);
  117. bRet &= ClearDirRecursiveA(tmp);
  118. }
  119. toolkit_array_free2(arr);
  120. }
  121. return bRet;
  122. }
  123. TOOLKIT_API BOOL ClearDirRecursiveW(LPCWSTR lpDirPath)
  124. {
  125. array_header_t *arr;
  126. BOOL bRet = TRUE;
  127. assert(lpDirPath);
  128. arr = fileutil_get_sub_files_w(lpDirPath);
  129. if (arr) {
  130. int i;
  131. for (i = 0; i < arr->nelts; ++i) {
  132. wchar_t *tmp = ARRAY_IDX(arr, i, wchar_t*);
  133. bRet &= DeleteFileW(tmp);
  134. }
  135. toolkit_array_free2(arr);
  136. }
  137. arr = fileutil_get_sub_dirs_w(lpDirPath);
  138. if (arr) {
  139. int i;
  140. for (i = 0; i < arr->nelts; ++i) {
  141. wchar_t *tmp = ARRAY_IDX(arr, i, wchar_t*);
  142. bRet &= ClearDirRecursiveW(tmp);
  143. }
  144. toolkit_array_free2(arr);
  145. }
  146. return bRet;
  147. }
  148. TOOLKIT_API BOOL RemoveFileA(LPCSTR pszFile)
  149. {
  150. if(ExistsFileA(pszFile))
  151. {
  152. // remove readonly attribute
  153. DWORD dwRet = GetFileAttributesA(pszFile);
  154. if (dwRet & FILE_ATTRIBUTE_READONLY)
  155. {
  156. dwRet &= (~FILE_ATTRIBUTE_READONLY);
  157. SetFileAttributesA(pszFile, dwRet);
  158. }
  159. // delete it
  160. return DeleteFileA(pszFile);
  161. }
  162. return FALSE;
  163. }
  164. // 移除文件只读属性
  165. TOOLKIT_API BOOL RemoveFileReadOnlyAttributeA(LPCSTR pszFile)
  166. {
  167. if(ExistsFileA(pszFile))
  168. {
  169. // remove readonly attribute
  170. DWORD dwRet = GetFileAttributesA(pszFile);
  171. if (dwRet & FILE_ATTRIBUTE_READONLY)
  172. {
  173. dwRet &= (~FILE_ATTRIBUTE_READONLY);
  174. SetFileAttributesA(pszFile, dwRet);
  175. }
  176. return TRUE;
  177. }
  178. return FALSE;
  179. }
  180. // 递归拷贝所有文件及子文件夹
  181. TOOLKIT_API BOOL CopyDirA(LPCSTR pszSourceDir, LPCSTR pszDestDir)
  182. {
  183. array_header_t *arr;
  184. BOOL bRet = TRUE;
  185. if (!ExistsDirA(pszSourceDir))
  186. return FALSE;
  187. if (!ExistsDirA(pszDestDir))
  188. {
  189. if (!CreateDirRecursiveA(pszDestDir))
  190. return FALSE;
  191. }
  192. arr = fileutil_get_sub_files_a(pszSourceDir);
  193. if (arr)
  194. {
  195. int i;
  196. for (i = 0; i < arr->nelts; ++i)
  197. {
  198. char szDestFile[256] = {0};
  199. char *file = ARRAY_IDX(arr, i, char*);
  200. strcpy(szDestFile, pszDestDir);
  201. if (szDestFile[strlen(szDestFile)-1] != '\\')
  202. strcat(szDestFile, "\\");
  203. strcat(szDestFile, strrchr(file, '\\')+1);
  204. bRet &= CopyFileA(file, szDestFile, FALSE);
  205. }
  206. toolkit_array_free2(arr);
  207. }
  208. arr = fileutil_get_sub_dirs_a(pszSourceDir);
  209. if (arr)
  210. {
  211. int i;
  212. for (i = 0; i < arr->nelts; ++i)
  213. {
  214. char szDestSubDir[256] = {0};
  215. char *dir = ARRAY_IDX(arr, i, char*);
  216. strcpy(szDestSubDir, pszDestDir);
  217. if (szDestSubDir[strlen(szDestSubDir)-1] != '\\')
  218. strcat(szDestSubDir, "\\");
  219. strcat(szDestSubDir, strrchr(dir, '\\')+1);
  220. bRet &= CopyDirA(dir, szDestSubDir);
  221. }
  222. toolkit_array_free2(arr);
  223. }
  224. return bRet;
  225. }
  226. // 只能删除非只读文件或目录
  227. //TOOLKIT_API BOOL RemoveDirRecursiveA(LPCSTR lpDirPath)
  228. //{
  229. // array_header_t *arr;
  230. // BOOL bRet = TRUE;
  231. //
  232. // assert(lpDirPath);
  233. //
  234. // arr = fileutil_get_sub_files_a(lpDirPath);
  235. // if (arr) {
  236. // int i;
  237. // for (i = 0; i < arr->nelts; ++i) {
  238. // char *tmp = ARRAY_IDX(arr, i, char*);
  239. // bRet &= DeleteFileA(tmp);
  240. // }
  241. // toolkit_array_free2(arr);
  242. // }
  243. //
  244. // arr = fileutil_get_sub_dirs_a(lpDirPath);
  245. // if (arr) {
  246. // int i;
  247. // for (i = 0; i < arr->nelts; ++i) {
  248. // char *tmp = ARRAY_IDX(arr, i, char*);
  249. // bRet &= RemoveDirRecursiveA(tmp);
  250. // }
  251. // toolkit_array_free2(arr);
  252. // }
  253. //
  254. // if (bRet) {
  255. // bRet = RemoveDirectoryA(lpDirPath);
  256. // }
  257. //
  258. // return bRet;
  259. //}
  260. TOOLKIT_API BOOL RemoveDirRecursiveA(LPCSTR lpDirPath)
  261. {
  262. #ifdef _WIN32
  263. int nRet = 0;
  264. char szDir[256] = {0};
  265. SHFILEOPSTRUCT fo;
  266. memset(&fo, 0, sizeof(fo));
  267. memcpy(szDir, lpDirPath, strlen(lpDirPath));
  268. fo.wFunc = FO_DELETE;
  269. fo.pFrom = szDir;
  270. fo.fFlags = FOF_NO_UI;
  271. nRet = SHFileOperationA(&fo);
  272. return nRet ==0 ? TRUE: FALSE;
  273. #else
  274. array_header_t* arr;
  275. BOOL bRet = TRUE;
  276. assert(lpDirPath);
  277. arr = fileutil_get_sub_files_a(lpDirPath);
  278. if (arr) {
  279. int i;
  280. for (i = 0; i < arr->nelts; ++i) {
  281. char* tmp = ARRAY_IDX(arr, i, char*);
  282. bRet &= DeleteFileA(tmp);
  283. }
  284. toolkit_array_free2(arr);
  285. }
  286. arr = fileutil_get_sub_dirs_a(lpDirPath);
  287. if (arr) {
  288. int i;
  289. for (i = 0; i < arr->nelts; ++i) {
  290. char* tmp = ARRAY_IDX(arr, i, char*);
  291. bRet &= RemoveDirRecursiveA(tmp);
  292. }
  293. toolkit_array_free2(arr);
  294. }
  295. if (bRet) {
  296. bRet = RemoveDirectoryA(lpDirPath);
  297. }
  298. return bRet;
  299. #endif
  300. }
  301. TOOLKIT_API BOOL RemoveDirRecursiveW(LPCWSTR lpDirPath)
  302. {
  303. array_header_t *arr;
  304. BOOL bRet = TRUE;
  305. assert(lpDirPath);
  306. arr = fileutil_get_sub_files_w(lpDirPath);
  307. if (arr) {
  308. int i;
  309. for (i = 0; i < arr->nelts; ++i) {
  310. wchar_t *tmp = ARRAY_IDX(arr, i, wchar_t*);
  311. bRet &= DeleteFileW(tmp);
  312. }
  313. toolkit_array_free2(arr);
  314. }
  315. arr = fileutil_get_sub_dirs_w(lpDirPath);
  316. if (arr) {
  317. int i;
  318. for (i = 0; i < arr->nelts; ++i) {
  319. wchar_t *tmp = ARRAY_IDX(arr, i, wchar_t*);
  320. bRet &= RemoveDirRecursiveW(tmp);
  321. }
  322. toolkit_array_free2(arr);
  323. }
  324. if (bRet) {
  325. bRet = RemoveDirectoryW(lpDirPath);
  326. }
  327. return bRet;
  328. }
  329. static array_header_t *fileutil_get_sub_a(const char *path, int isfile, int limitation)
  330. {
  331. array_header_t *arr = NULL;
  332. char tmp[MAX_PATH];
  333. int npath;
  334. if (!path)
  335. return NULL;
  336. npath = strlen(path);
  337. if (npath < 2)
  338. return NULL;
  339. strcpy(tmp, path);
  340. if (tmp[npath-1] != '\\') {
  341. tmp[npath++] = '\\';
  342. tmp[npath] = 0;
  343. }
  344. tmp[npath++] = '*';
  345. tmp[npath] = 0;
  346. arr = array_make(3, sizeof(char*));
  347. {
  348. WIN32_FIND_DATAA fd;
  349. HANDLE hFind = FindFirstFileA(tmp, &fd);
  350. if (hFind != INVALID_HANDLE_VALUE) {
  351. tmp[--npath] = 0;
  352. do {
  353. int isdir = !!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  354. if (isdir) {
  355. if (strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0) {
  356. continue;;
  357. }
  358. }
  359. if (isfile ^ isdir) {
  360. char *v = malloc(sizeof(char)*(npath + strlen(fd.cFileName)+1));
  361. strcpy(v, tmp);
  362. strcat(v, fd.cFileName);
  363. ARRAY_PUSH(arr, char*) = v;
  364. }
  365. } while (FindNextFileA(hFind, &fd) && arr->nelts < limitation);
  366. FindClose(hFind);
  367. }
  368. }
  369. return arr;
  370. }
  371. static array_header_t *fileutil_get_sub_w(const wchar_t *path, int isfile, int limitation)
  372. {
  373. array_header_t *arr = NULL;
  374. WCHAR tmp[MAX_PATH];
  375. int npath;
  376. if (!path)
  377. return NULL;
  378. npath = wcslen(path);
  379. if (npath < 2)
  380. return NULL;
  381. wcscpy(tmp, path);
  382. if (tmp[npath-1] != '\\') {
  383. tmp[npath++] = '\\';
  384. tmp[npath] = 0;
  385. }
  386. tmp[npath++] = '*';
  387. tmp[npath] = 0;
  388. arr = array_make(3, sizeof(wchar_t*));
  389. {
  390. WIN32_FIND_DATAW fd;
  391. HANDLE hFind = FindFirstFileW(tmp, &fd);
  392. if (hFind != INVALID_HANDLE_VALUE) {
  393. tmp[--npath] = 0;
  394. do {
  395. int isdir = !!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  396. if (isfile ^ isdir) {
  397. wchar_t *v = malloc(sizeof(wchar_t)*(npath + wcslen(fd.cFileName)+1));
  398. wcscpy(v, tmp);
  399. wcscat(v, fd.cFileName);
  400. ARRAY_PUSH(arr, wchar_t*) = v;
  401. }
  402. } while (FindNextFileW(hFind, &fd) && arr->nelts < limitation);
  403. FindClose(hFind);
  404. }
  405. }
  406. return arr;
  407. }
  408. TOOLKIT_API array_header_t *fileutil_get_sub_files_a(const char *path)
  409. {
  410. return fileutil_get_sub_a(path, 1, INT_MAX);
  411. }
  412. TOOLKIT_API array_header_t *fileutil_get_sub_dirs_a(const char *path)
  413. {
  414. return fileutil_get_sub_a(path, 0, INT_MAX);
  415. }
  416. TOOLKIT_API array_header_t *fileutil_get_sub_files_w(const wchar_t *path)
  417. {
  418. return fileutil_get_sub_w(path, 1, INT_MAX);
  419. }
  420. TOOLKIT_API array_header_t *fileutil_get_sub_dirs_w(const wchar_t *path)
  421. {
  422. return fileutil_get_sub_w(path, 0, INT_MAX);
  423. }
  424. TOOLKIT_API array_header_t *fileutil_get_sub_files2_a(const char *path, int limitation)
  425. {
  426. return fileutil_get_sub_a(path, 1, limitation);
  427. }
  428. TOOLKIT_API array_header_t *fileutil_get_sub_dirs2_a(const char *path, int limitation)
  429. {
  430. return fileutil_get_sub_a(path, 0, limitation);
  431. }
  432. TOOLKIT_API array_header_t *fileutil_get_sub_files2_w(const wchar_t *path, int limitation)
  433. {
  434. return fileutil_get_sub_w(path, 1, limitation);
  435. }
  436. TOOLKIT_API array_header_t *fileutil_get_sub_dirs2_w(const wchar_t *path, int limitation)
  437. {
  438. return fileutil_get_sub_w(path, 0, limitation);
  439. }
  440. TOOLKIT_API FILE *fileutil_transaction_fopen(const char *filename, const char *mode)
  441. {
  442. DWORD dwAttr;
  443. char t[MAX_PATH];
  444. int write_flag;
  445. if (!filename || !mode) {
  446. errno = EINVAL;
  447. return NULL;
  448. }
  449. write_flag = (int)strchr(mode, 'w');
  450. strcpy(t, filename);
  451. strcat(t, ".bak");
  452. dwAttr = GetFileAttributesA(filename);
  453. if (dwAttr != INVALID_FILE_ATTRIBUTES && dwAttr & FILE_ATTRIBUTE_DIRECTORY) {
  454. errno = EIO;
  455. return NULL;
  456. }
  457. if (dwAttr != INVALID_FILE_ATTRIBUTES) {
  458. if (!(dwAttr & FILE_ATTRIBUTE_READONLY)) {
  459. BOOL bRet = DeleteFileA(filename);
  460. if (bRet) {
  461. if (!write_flag) {
  462. if (ExistsFileA(t)) {
  463. bRet = CopyFileA(t, filename, FALSE);
  464. if (bRet)
  465. bRet = SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL);
  466. }
  467. }
  468. }
  469. if (!bRet) {
  470. errno = EIO;
  471. return NULL;
  472. }
  473. } else {
  474. if (write_flag) {
  475. BOOL bRet = TRUE;
  476. if (ExistsFileA(t)) {
  477. bRet = SetFileAttributesA(t, dwAttr & ~FILE_ATTRIBUTE_READONLY);
  478. }
  479. if (bRet) {
  480. bRet = CopyFileA(filename, t, FALSE);
  481. }
  482. if (!bRet) {
  483. errno = EIO;
  484. return NULL;
  485. }
  486. }
  487. }
  488. } else {
  489. if (!write_flag) {
  490. BOOL bRet = TRUE;
  491. if (ExistsFileA(t)) {
  492. bRet = CopyFileA(t, filename, FALSE);
  493. } else {
  494. bRet = FALSE;
  495. }
  496. if (!bRet) {
  497. errno = EIO;
  498. return NULL;
  499. }
  500. }
  501. }
  502. return fopen(filename, mode);
  503. }
  504. TOOLKIT_API int fileutil_transaction_fclose(const char *filename, FILE *fp)
  505. {
  506. int rc = fclose(fp);
  507. if (!rc && filename) {
  508. DWORD dwAttr = GetFileAttributesA(filename);
  509. if (dwAttr != INVALID_FILE_ATTRIBUTES) {
  510. if (!(dwAttr & FILE_ATTRIBUTE_READONLY))
  511. SetFileAttributesA(filename, dwAttr | FILE_ATTRIBUTE_READONLY);
  512. }
  513. }
  514. return rc;
  515. }