fileutil.c 19 KB

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