fileutil.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. #include "precompile.h"
  2. #include <wchar.h>
  3. #include <errno.h>
  4. #include <assert.h>
  5. #ifdef _WIN32
  6. #include <mbstring.h>
  7. #include <limits.h>
  8. #include <ShellAPI.h>
  9. #else
  10. #include <winpr/wtypes.h>
  11. #include <winpr/error.h>
  12. #include <winpr/file.h>
  13. #include <libgen.h>
  14. #include <sys/types.h>
  15. #include <dirent.h>
  16. #include <sys/stat.h>
  17. #endif
  18. #include "array.h"
  19. #include "fileutil.h"
  20. #include "strutil.h"
  21. #include "memutil.h"
  22. #include "dbgutil.h"
  23. #include <winpr/wlog.h>
  24. #define TAG TOOLKIT_TAG("fileutil")
  25. TOOLKIT_API BOOL ExistsDirA(LPCSTR lpDirPath)
  26. {
  27. DWORD dwRet = GetFileAttributesA(lpDirPath);
  28. return (dwRet != INVALID_FILE_ATTRIBUTES) && (dwRet & FILE_ATTRIBUTE_DIRECTORY);
  29. }
  30. TOOLKIT_API BOOL ExistsDirW(LPCWSTR lpDirPath)
  31. {
  32. DWORD dwRet = GetFileAttributesW(lpDirPath);
  33. return (dwRet != INVALID_FILE_ATTRIBUTES) && (dwRet & FILE_ATTRIBUTE_DIRECTORY);
  34. }
  35. TOOLKIT_API BOOL ExistsFileA(LPCSTR lpFilePath)
  36. {
  37. DWORD dwRet = GetFileAttributesA(lpFilePath);
  38. return (dwRet != INVALID_FILE_ATTRIBUTES) && !(dwRet & FILE_ATTRIBUTE_DIRECTORY);
  39. }
  40. TOOLKIT_API BOOL ExistsFileW(LPCWSTR lpFilePath)
  41. {
  42. DWORD dwRet = GetFileAttributesW(lpFilePath);
  43. return (dwRet != INVALID_FILE_ATTRIBUTES) && !(dwRet & FILE_ATTRIBUTE_DIRECTORY);
  44. }
  45. TOOLKIT_API BOOL CreateDirA(LPCSTR lpDirPath, BOOL bRecursive)
  46. {
  47. char* slashPos = NULL;
  48. char* backSlashPos = NULL;
  49. size_t len = strlen(lpDirPath);
  50. if (len <= 2 || len >= MAX_PATH-2) {
  51. return FALSE;
  52. } else {
  53. if (!bRecursive) {
  54. if (ExistsDirA(lpDirPath))
  55. return TRUE;
  56. return CreateDirectoryA(lpDirPath, NULL);
  57. }
  58. else {
  59. CHAR tmp[MAX_PATH], * p;
  60. CHAR* q = NULL;
  61. q = p = &tmp[0];
  62. strncpy(tmp, lpDirPath, MAX_PATH);
  63. tmp[MAX_PATH - 1] = 0;
  64. if (tmp[len - 1] != BACK_SLASH && tmp[len - 1] != SLASH) {
  65. tmp[len++] = SPLIT_SLASH;
  66. tmp[len] = 0;
  67. }
  68. #ifdef _WIN32
  69. do {
  70. slashPos = strchr(p, SLASH);
  71. backSlashPos = strchr(p, BACK_SLASH);
  72. if (slashPos != NULL && backSlashPos != NULL) {
  73. p = slashPos < backSlashPos ? slashPos : backSlashPos;
  74. }
  75. else if (slashPos != NULL) {
  76. p = slashPos;
  77. }
  78. else {
  79. p = backSlashPos;
  80. }
  81. if (!p) {
  82. break;
  83. }
  84. *p = 0;
  85. if (!ExistsDirA(tmp)) {
  86. if (!CreateDirectoryA(tmp, NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
  87. return FALSE;
  88. }
  89. *p = SPLIT_SLASH;
  90. p++;
  91. }
  92. while (1);
  93. #else
  94. while ((p = strchr(p, SPLIT_SLASH)) != NULL) {
  95. if(p != q) {//linux compatibility.
  96. *p = 0;
  97. if (!ExistsDirA(tmp)) {
  98. if (!CreateDirectoryA(tmp, NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
  99. return FALSE;
  100. }
  101. *p = SPLIT_SLASH;
  102. }
  103. p++;
  104. q = p;
  105. }
  106. #endif
  107. }
  108. }
  109. return TRUE;
  110. }
  111. TOOLKIT_API BOOL CreateDirW(LPCWSTR lpDirPath, BOOL bRecursive)
  112. {
  113. WCHAR* slashPos = NULL;
  114. WCHAR* backSlashPos = NULL;
  115. size_t len = wcslen(lpDirPath);
  116. if (len <= 2 || len >= MAX_PATH-2) {
  117. return FALSE;
  118. } else {
  119. if (!bRecursive) {
  120. return CreateDirectoryW(lpDirPath, NULL) || (GetLastError() == ERROR_ALREADY_EXISTS);
  121. } else {
  122. WCHAR tmp[MAX_PATH], *p;
  123. WCHAR* q = NULL;
  124. q = p = &tmp[0];
  125. wcsncpy(tmp, lpDirPath, MAX_PATH);
  126. tmp[MAX_PATH-1] = 0;
  127. if (tmp[len-1] != '\\' && tmp[len-1] != '/') {
  128. tmp[len++] = SPLIT_SLASH;
  129. tmp[len] = 0;
  130. }
  131. #ifdef _WIN32
  132. do {
  133. slashPos = wcschr(p, SLASH);
  134. backSlashPos = wcschr(p, BACK_SLASH);
  135. if (slashPos != NULL && backSlashPos != NULL) {
  136. p = slashPos < backSlashPos ? slashPos : backSlashPos;
  137. }
  138. else if (slashPos != NULL) {
  139. p = slashPos;
  140. }
  141. else {
  142. p = backSlashPos;
  143. }
  144. if (!p) {
  145. break;
  146. }
  147. *p = 0;
  148. if (!ExistsDirW(tmp)) {
  149. if (!CreateDirectoryW(tmp, NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
  150. return FALSE;
  151. }
  152. *p = SPLIT_SLASH;
  153. p++;
  154. } while (1);
  155. #else
  156. while ((p = wcschr(p, SPLIT_SLASH)) != NULL) {
  157. if (p != q) {//linux compatibility.
  158. *p = 0;
  159. if (!ExistsDirW(tmp)) {
  160. if (!CreateDirectoryW(tmp, NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
  161. return FALSE;
  162. }
  163. *p = SPLIT_SLASH;
  164. }
  165. p++;
  166. q = p;
  167. }
  168. #endif
  169. }
  170. }
  171. return TRUE;
  172. }
  173. TOOLKIT_API BOOL CreateDirRecursiveA(LPCSTR lpDirPath) { return CreateDirA(lpDirPath, TRUE); }
  174. TOOLKIT_API BOOL CreateDirRecursiveW(LPCWSTR lpDirPath) { return CreateDirW(lpDirPath, TRUE); }
  175. TOOLKIT_API BOOL CreateParentDirA(LPCSTR lpPath, BOOL bRecursive)
  176. {
  177. CHAR pdir[MAX_PATH];
  178. const CHAR *p;
  179. p = strrchr(lpPath, '\\');
  180. if (!p) {
  181. p = strrchr(lpPath, '/');
  182. }
  183. if (!p) {
  184. return FALSE;
  185. }
  186. strncpy(pdir, lpPath, p-lpPath);
  187. pdir[p-lpPath] = 0;
  188. return CreateDirA(pdir, bRecursive);
  189. }
  190. TOOLKIT_API BOOL CreateParentDirW(LPCWSTR lpPath, BOOL bRecursive)
  191. {
  192. WCHAR pdir[MAX_PATH];
  193. const WCHAR *p;
  194. p = wcsrchr(lpPath, '\\');
  195. if (!p) {
  196. p = wcsrchr(lpPath, '/');
  197. }
  198. if (!p) {
  199. return FALSE;
  200. }
  201. wcsncpy(pdir, lpPath, p-lpPath);
  202. pdir[p-lpPath] = 0;
  203. return CreateDirW(pdir, bRecursive);
  204. }
  205. TOOLKIT_API BOOL ClearDirRecursiveA(LPCSTR lpDirPath)
  206. {
  207. array_header_t *arr;
  208. BOOL bRet = TRUE;
  209. TOOLKIT_ASSERT(lpDirPath);
  210. arr = fileutil_get_sub_files_a(lpDirPath);
  211. if (arr) {
  212. int i;
  213. for (i = 0; i < arr->nelts; ++i) {
  214. char *tmp = ARRAY_IDX(arr, i, char*);
  215. bRet &= DeleteFileA(tmp);
  216. }
  217. toolkit_array_free2(arr);
  218. }
  219. arr = fileutil_get_sub_dirs_a(lpDirPath);
  220. if (arr) {
  221. int i;
  222. for (i = 0; i < arr->nelts; ++i) {
  223. char *tmp = ARRAY_IDX(arr, i, char*);
  224. bRet &= ClearDirRecursiveA(tmp);
  225. }
  226. toolkit_array_free2(arr);
  227. }
  228. return bRet;
  229. }
  230. TOOLKIT_API BOOL ClearDirRecursiveW(LPCWSTR lpDirPath)
  231. {
  232. array_header_t *arr;
  233. BOOL bRet = TRUE;
  234. TOOLKIT_ASSERT(lpDirPath);
  235. arr = fileutil_get_sub_files_w(lpDirPath);
  236. if (arr) {
  237. int i;
  238. for (i = 0; i < arr->nelts; ++i) {
  239. wchar_t *tmp = ARRAY_IDX(arr, i, wchar_t*);
  240. bRet &= DeleteFileW(tmp);
  241. }
  242. toolkit_array_free2(arr);
  243. }
  244. arr = fileutil_get_sub_dirs_w(lpDirPath);
  245. if (arr) {
  246. int i;
  247. for (i = 0; i < arr->nelts; ++i) {
  248. wchar_t *tmp = ARRAY_IDX(arr, i, wchar_t*);
  249. bRet &= ClearDirRecursiveW(tmp);
  250. }
  251. toolkit_array_free2(arr);
  252. }
  253. return bRet;
  254. }
  255. TOOLKIT_API BOOL RemoveFileA(LPCSTR pszFile)
  256. {
  257. if(ExistsFileA(pszFile))
  258. {
  259. // remove readonly attribute
  260. DWORD dwRet = GetFileAttributesA(pszFile);
  261. if (dwRet & FILE_ATTRIBUTE_READONLY)
  262. {
  263. dwRet &= (~FILE_ATTRIBUTE_READONLY);
  264. SetFileAttributesA(pszFile, dwRet);
  265. }
  266. // delete it
  267. return DeleteFileA(pszFile);
  268. }
  269. return FALSE;
  270. }
  271. // 移除文件只读属性
  272. TOOLKIT_API BOOL RemoveFileReadOnlyAttributeA(LPCSTR pszFile)
  273. {
  274. if(ExistsFileA(pszFile))
  275. {
  276. // remove readonly attribute
  277. DWORD dwRet = GetFileAttributesA(pszFile);
  278. if (dwRet & FILE_ATTRIBUTE_READONLY)
  279. {
  280. dwRet &= (~FILE_ATTRIBUTE_READONLY);
  281. SetFileAttributesA(pszFile, dwRet);
  282. }
  283. return TRUE;
  284. }
  285. return FALSE;
  286. }
  287. // 递归拷贝所有文件及子文件夹
  288. TOOLKIT_API BOOL CopyDirA(LPCSTR pszSourceDir, LPCSTR pszDestDir)
  289. {
  290. array_header_t *arr;
  291. BOOL bRet = TRUE;
  292. if (!ExistsDirA(pszSourceDir))
  293. return FALSE;
  294. if (!ExistsDirA(pszDestDir))
  295. {
  296. if (!CreateDirRecursiveA(pszDestDir))
  297. return FALSE;
  298. }
  299. arr = fileutil_get_sub_files_a(pszSourceDir);
  300. if (arr)
  301. {
  302. int i;
  303. for (i = 0; i < arr->nelts; ++i)
  304. {
  305. char szDestFile[256] = {0};
  306. char *file = ARRAY_IDX(arr, i, char*);
  307. strcpy(szDestFile, pszDestDir);
  308. if (szDestFile[strlen(szDestFile)-1] != '\\' && szDestFile[strlen(szDestFile) - 1] != '/')
  309. strcat(szDestFile, MB_SPLIT_SLASH_STR);
  310. strcat(szDestFile, strrchr(file, SPLIT_SLASH)+1);
  311. bRet &= CopyFileA(file, szDestFile, FALSE);
  312. }
  313. toolkit_array_free2(arr);
  314. }
  315. arr = fileutil_get_sub_dirs_a(pszSourceDir);
  316. if (arr)
  317. {
  318. int i;
  319. for (i = 0; i < arr->nelts; ++i)
  320. {
  321. char szDestSubDir[256] = {0};
  322. char *dir = ARRAY_IDX(arr, i, char*);
  323. strcpy(szDestSubDir, pszDestDir);
  324. if (szDestSubDir[strlen(szDestSubDir)-1] != '\\' && szDestSubDir[strlen(szDestSubDir) - 1] != '/')
  325. strcat(szDestSubDir, MB_SPLIT_SLASH_STR);
  326. strcat(szDestSubDir, strrchr(dir, SPLIT_SLASH)+1);
  327. bRet &= CopyDirA(dir, szDestSubDir);
  328. }
  329. toolkit_array_free2(arr);
  330. }
  331. return bRet;
  332. }
  333. TOOLKIT_API BOOL RemoveDirRecursiveA(LPCSTR lpDirPath)
  334. {
  335. #ifdef _WIN32
  336. int nRet = 0;
  337. char szDir[256] = {0};
  338. SHFILEOPSTRUCT fo;
  339. memset(&fo, 0, sizeof(fo));
  340. memcpy(szDir, lpDirPath, strlen(lpDirPath));
  341. fo.wFunc = FO_DELETE;
  342. fo.pFrom = szDir;
  343. fo.fFlags = FOF_NO_UI;
  344. nRet = SHFileOperationA(&fo);
  345. return nRet ==0 ? TRUE: FALSE;
  346. #else
  347. array_header_t* arr;
  348. BOOL bRet = TRUE;
  349. TOOLKIT_ASSERT(lpDirPath);
  350. arr = fileutil_get_sub_files_a(lpDirPath);
  351. if (arr) {
  352. int i;
  353. for (i = 0; i < arr->nelts; ++i) {
  354. char* tmp = ARRAY_IDX(arr, i, char*);
  355. bRet &= DeleteFileA(tmp);
  356. }
  357. toolkit_array_free2(arr);
  358. }
  359. arr = fileutil_get_sub_dirs_a(lpDirPath);
  360. if (arr) {
  361. int i;
  362. for (i = 0; i < arr->nelts; ++i) {
  363. char* tmp = ARRAY_IDX(arr, i, char*);
  364. bRet &= RemoveDirRecursiveA(tmp);
  365. }
  366. toolkit_array_free2(arr);
  367. }
  368. if (bRet) {
  369. bRet = RemoveDirectoryA(lpDirPath);
  370. }
  371. return bRet;
  372. #endif
  373. }
  374. TOOLKIT_API BOOL RemoveDirRecursiveW(LPCWSTR lpDirPath)
  375. {
  376. array_header_t *arr;
  377. BOOL bRet = TRUE;
  378. TOOLKIT_ASSERT(lpDirPath);
  379. arr = fileutil_get_sub_files_w(lpDirPath);
  380. if (arr) {
  381. int i;
  382. for (i = 0; i < arr->nelts; ++i) {
  383. wchar_t *tmp = ARRAY_IDX(arr, i, wchar_t*);
  384. bRet &= DeleteFileW(tmp);
  385. }
  386. toolkit_array_free2(arr);
  387. }
  388. arr = fileutil_get_sub_dirs_w(lpDirPath);
  389. if (arr) {
  390. int i;
  391. for (i = 0; i < arr->nelts; ++i) {
  392. wchar_t *tmp = ARRAY_IDX(arr, i, wchar_t*);
  393. bRet &= RemoveDirRecursiveW(tmp);
  394. }
  395. toolkit_array_free2(arr);
  396. }
  397. if (bRet) {
  398. bRet = RemoveDirectoryW(lpDirPath);
  399. }
  400. return bRet;
  401. }
  402. TOOLKIT_API HANDLE ExtCreateFileW(LPCWSTR lpFileName,
  403. DWORD dwDesiredAccess,
  404. DWORD dwShareMode,
  405. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  406. DWORD dwCreationDisposition,
  407. DWORD dwFlagsAndAttributes,
  408. HANDLE hTemplateFile)
  409. {
  410. if (!lpFileName)
  411. return INVALID_HANDLE_VALUE;
  412. if (lpFileName[0] != '\\')
  413. CreateParentDirW(lpFileName, TRUE);
  414. return CreateFileW(lpFileName,
  415. dwDesiredAccess,
  416. dwShareMode,
  417. lpSecurityAttributes,
  418. dwCreationDisposition,
  419. dwFlagsAndAttributes,
  420. hTemplateFile);
  421. }
  422. TOOLKIT_API HANDLE ExtCreateFileA(LPCSTR lpFileName,
  423. DWORD dwDesiredAccess,
  424. DWORD dwShareMode,
  425. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  426. DWORD dwCreationDisposition,
  427. DWORD dwFlagsAndAttributes,
  428. HANDLE hTemplateFile)
  429. {
  430. if (!lpFileName)
  431. return INVALID_HANDLE_VALUE;
  432. if (lpFileName[0] != '\\')
  433. CreateParentDirA(lpFileName, TRUE);
  434. return CreateFileA(lpFileName,
  435. dwDesiredAccess,
  436. dwShareMode,
  437. lpSecurityAttributes,
  438. dwCreationDisposition,
  439. dwFlagsAndAttributes,
  440. hTemplateFile);
  441. }
  442. TOOLKIT_API int fileutil_copy_file(const char* dest_file_path, const char* src_file_path)
  443. {
  444. if (CopyFileA(src_file_path, dest_file_path, FALSE)) {
  445. return 0;
  446. }
  447. return -1;
  448. }
  449. TOOLKIT_API void fileutil_delete_file(const char* filename)
  450. {
  451. #ifdef _WIN32
  452. DeleteFileA(filename);
  453. #else
  454. unlink(filename);
  455. #endif //_WIN32
  456. }
  457. /*
  458. * isfile: only fetch file type elem or not.
  459. * limitation: max elem count to satsfy.
  460. */
  461. static array_header_t *fileutil_get_sub_a(const char *path, int isfile, int limitation)
  462. {
  463. #if 1
  464. array_header_t* arr = NULL;
  465. char tmp[MAX_PATH];
  466. int npath;
  467. if (!path)
  468. return NULL;
  469. npath = strlen(path);
  470. if (npath < 2)
  471. return NULL;
  472. strcpy(tmp, path);
  473. if (tmp[npath - 1] != '\\' && tmp[npath - 1] != '/') {
  474. tmp[npath++] = SPLIT_SLASH;
  475. tmp[npath] = 0;
  476. }
  477. tmp[npath++] = '*';
  478. tmp[npath] = 0;
  479. arr = array_make(3, sizeof(char*));
  480. {
  481. WIN32_FIND_DATAA fd;
  482. HANDLE hFind = FindFirstFileA(tmp, &fd);
  483. if (hFind != INVALID_HANDLE_VALUE) {
  484. tmp[--npath] = 0;
  485. do {
  486. int isdir = !!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  487. if (isdir) {
  488. if (strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0) {
  489. continue;
  490. }
  491. }
  492. /*[if require file while is not dir] or [not require file but is dir]*/
  493. if (isfile ^ isdir) {
  494. char* v = malloc(sizeof(char) * (npath + strlen(fd.cFileName) + 1));
  495. strcpy(v, tmp);
  496. strcat(v, fd.cFileName);
  497. ARRAY_PUSH(arr, char*) = v;
  498. }
  499. } while (FindNextFileA(hFind, &fd) && arr->nelts < limitation);
  500. FindClose(hFind);
  501. }
  502. }
  503. #else
  504. array_header_t* arr = NULL;
  505. char tmp[MAX_PATH];
  506. char tmp2[MAX_PATH];
  507. DIR* d = NULL;
  508. struct dirent* dp = NULL;
  509. struct stat st;
  510. int npath;
  511. if (!path)
  512. return NULL;
  513. npath = strlen(path);
  514. if (npath < 1)
  515. return NULL;
  516. strcpy(tmp, path);
  517. if (tmp[npath - 1] == '/') {
  518. tmp[npath - 1] = '\0';
  519. }
  520. if (stat(tmp, &st) < 0 || !S_ISDIR(st.st_mode)) {
  521. return NULL;
  522. }
  523. if (!(d = opendir(tmp))) {
  524. return NULL;
  525. }
  526. if (tmp[npath - 1] != '/') {
  527. tmp[npath++] = '/';
  528. tmp[npath] = 0;
  529. }
  530. arr = array_make(3, sizeof(char*));
  531. while ((dp = readdir(d)) != NULL && arr->nelts < limitation)
  532. {
  533. if((!strncmp(dp->d_name, ".", 1)) || (!strncmp(dp->d_name, "..", 2))) {
  534. continue;
  535. }
  536. snprintf(tmp2, sizeof(tmp2) - 1, "%s%s", tmp, dp->d_name);
  537. stat(tmp2, &st);
  538. int isdir = !!(S_ISDIR(st.st_mode));
  539. if (isfile ^ isdir) {
  540. char* v = malloc(sizeof(char) * (npath + strlen(dp->d_name) + 1));
  541. strcpy(v, tmp);
  542. strcat(v, dp->d_name);
  543. ARRAY_PUSH(arr, char*) = v;
  544. }
  545. }
  546. closedir(d);
  547. #endif //1
  548. return arr;
  549. }
  550. static array_header_t *fileutil_get_sub_w(const wchar_t *path, int isfile, int limitation)
  551. {
  552. array_header_t *arr = NULL;
  553. WCHAR tmp[MAX_PATH];
  554. int npath;
  555. if (!path)
  556. return NULL;
  557. npath = wcslen(path);
  558. if (npath < 2)
  559. return NULL;
  560. wcscpy(tmp, path);
  561. if (tmp[npath - 1] != '\\' && tmp[npath - 1] != '/') {
  562. tmp[npath++] = SPLIT_SLASH;
  563. tmp[npath] = 0;
  564. }
  565. tmp[npath++] = '*';
  566. tmp[npath] = 0;
  567. arr = array_make(3, sizeof(wchar_t*));
  568. {
  569. WIN32_FIND_DATAW fd;
  570. HANDLE hFind = FindFirstFileW(tmp, &fd);
  571. if (hFind != INVALID_HANDLE_VALUE) {
  572. tmp[--npath] = 0;
  573. do {
  574. int isdir = !!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  575. if (isfile ^ isdir) {
  576. wchar_t* v = malloc(sizeof(wchar_t) * (npath + wcslen(fd.cFileName) + 1));
  577. wcscpy(v, tmp);
  578. wcscat(v, fd.cFileName);
  579. ARRAY_PUSH(arr, wchar_t*) = v;
  580. }
  581. } while (FindNextFileW(hFind, &fd) && arr->nelts < limitation);
  582. FindClose(hFind);
  583. }
  584. }
  585. return arr;
  586. }
  587. TOOLKIT_API array_header_t *fileutil_get_sub_files_a(const char *path)
  588. {
  589. return fileutil_get_sub_a(path, 1, INT_MAX);
  590. }
  591. TOOLKIT_API array_header_t *fileutil_get_sub_dirs_a(const char *path)
  592. {
  593. return fileutil_get_sub_a(path, 0, INT_MAX);
  594. }
  595. TOOLKIT_API array_header_t *fileutil_get_sub_files_w(const wchar_t *path)
  596. {
  597. return fileutil_get_sub_w(path, 1, INT_MAX);
  598. }
  599. TOOLKIT_API array_header_t *fileutil_get_sub_dirs_w(const wchar_t *path)
  600. {
  601. return fileutil_get_sub_w(path, 0, INT_MAX);
  602. }
  603. TOOLKIT_API array_header_t *fileutil_get_sub_files2_a(const char *path, int limitation)
  604. {
  605. return fileutil_get_sub_a(path, 1, limitation);
  606. }
  607. TOOLKIT_API array_header_t *fileutil_get_sub_dirs2_a(const char *path, int limitation)
  608. {
  609. return fileutil_get_sub_a(path, 0, limitation);
  610. }
  611. TOOLKIT_API array_header_t *fileutil_get_sub_files2_w(const wchar_t *path, int limitation)
  612. {
  613. return fileutil_get_sub_w(path, 1, limitation);
  614. }
  615. TOOLKIT_API array_header_t *fileutil_get_sub_dirs2_w(const wchar_t *path, int limitation)
  616. {
  617. return fileutil_get_sub_w(path, 0, limitation);
  618. }
  619. TOOLKIT_API FILE *fileutil_transaction_fopen(const char *filename, const char *mode)
  620. {
  621. DWORD dwAttr;
  622. char t[MAX_PATH];
  623. int write_flag;
  624. if (!filename || !mode) {
  625. errno = EINVAL;
  626. return NULL;
  627. }
  628. write_flag = (int)strchr(mode, 'w');
  629. strcpy(t, filename);
  630. strcat(t, ".bak");
  631. dwAttr = GetFileAttributesA(filename);
  632. if (dwAttr != INVALID_FILE_ATTRIBUTES && dwAttr & FILE_ATTRIBUTE_DIRECTORY) {
  633. errno = EIO;
  634. return NULL;
  635. }
  636. if (dwAttr != INVALID_FILE_ATTRIBUTES) {
  637. if (!(dwAttr & FILE_ATTRIBUTE_READONLY)) {
  638. BOOL bRet = DeleteFileA(filename);
  639. if (bRet) {
  640. if (!write_flag) {
  641. if (ExistsFileA(t)) {
  642. bRet = CopyFileA(t, filename, FALSE);
  643. if (bRet)
  644. bRet = SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL);
  645. }
  646. }
  647. }
  648. if (!bRet) {
  649. errno = EIO;
  650. return NULL;
  651. }
  652. } else {
  653. if (write_flag) {
  654. BOOL bRet = TRUE;
  655. if (ExistsFileA(t)) {
  656. bRet = SetFileAttributesA(t, dwAttr & ~FILE_ATTRIBUTE_READONLY);
  657. }
  658. if (bRet) {
  659. bRet = CopyFileA(filename, t, FALSE);
  660. }
  661. if (!bRet) {
  662. errno = EIO;
  663. return NULL;
  664. }
  665. }
  666. }
  667. } else {
  668. if (!write_flag) {
  669. BOOL bRet = TRUE;
  670. if (ExistsFileA(t)) {
  671. bRet = CopyFileA(t, filename, FALSE);
  672. } else {
  673. bRet = FALSE;
  674. }
  675. if (!bRet) {
  676. errno = EIO;
  677. return NULL;
  678. }
  679. }
  680. }
  681. return fopen(filename, mode);
  682. }
  683. TOOLKIT_API int fileutil_transaction_fclose(const char *filename, FILE *fp)
  684. {
  685. int rc = fclose(fp);
  686. if (!rc && filename) {
  687. DWORD dwAttr = GetFileAttributesA(filename);
  688. if (dwAttr != INVALID_FILE_ATTRIBUTES) {
  689. if (!(dwAttr & FILE_ATTRIBUTE_READONLY))
  690. SetFileAttributesA(filename, dwAttr | FILE_ATTRIBUTE_READONLY);
  691. }
  692. }
  693. return rc;
  694. }
  695. #ifndef _WIN32
  696. void _splitpath(const char* path, char* drive, char* dir, char* fname, char* ext)
  697. {
  698. char resolved_path[4096];
  699. char* p;
  700. char* q;
  701. char* last_slash = NULL;
  702. char* dot = NULL;
  703. size_t len, sub_len;
  704. if (drive) {
  705. *drive = '\0';
  706. }
  707. if (path == NULL) {
  708. if (dir) { *dir = '\0'; }
  709. if (fname) { *fname = '\0'; }
  710. if (ext) { *ext = '\0'; }
  711. return;
  712. }
  713. if (realpath(path, resolved_path) == NULL) {
  714. WLog_ERR(TAG, "realpath failed: %s", strerror(errno));
  715. strcpy(resolved_path, path);
  716. }
  717. else {
  718. //WLog_DBG(TAG, "resolved_path: %s", resolved_path);
  719. }
  720. len = strlen(resolved_path);
  721. if (resolved_path[0] == '/') {
  722. p = strchr(resolved_path + 1, '/');
  723. if (p != NULL) {
  724. *p = '\0';
  725. }
  726. if (drive) {
  727. strcpy(drive, resolved_path);
  728. }
  729. if (p != NULL) {
  730. *p = '/';
  731. }
  732. else {
  733. p = resolved_path + len;
  734. }
  735. } else {
  736. p = resolved_path;
  737. }
  738. q = p;
  739. for (last_slash = NULL; p && *p != '\0'; ++p) {
  740. if (*p == '/') {
  741. last_slash = p + 1;
  742. if (dot) {
  743. dot = NULL;
  744. }
  745. }
  746. else if(*p == '.') {
  747. dot = p;
  748. }
  749. }
  750. if (last_slash) {
  751. if (dir) {
  752. sub_len = last_slash - q;
  753. strncpy(dir, q, sub_len);
  754. *(dir + sub_len) = '\0';
  755. }
  756. q = last_slash;
  757. }
  758. else if(dir){
  759. *dir = '\0';
  760. }
  761. if (dot) {
  762. if (fname) {
  763. sub_len = dot - q;
  764. strncpy(fname, q, sub_len);
  765. *(fname + sub_len) = '\0';
  766. }
  767. if (ext) {
  768. sub_len = p - dot;
  769. strncpy(ext, dot, sub_len);
  770. *(ext + sub_len) = '\0';
  771. }
  772. }
  773. else {
  774. if (fname) {
  775. sub_len = p - q;
  776. strncpy(fname, q, sub_len);
  777. *(fname + sub_len) = '\0';
  778. }
  779. if (ext) {
  780. *ext = '\0';
  781. }
  782. }
  783. }
  784. #endif //NOT _WIN32