file_util.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. // Copyright 2013 Dolphin Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include "common/common.h"
  5. #include "common/common_paths.h"
  6. #include "common/file_util.h"
  7. #include "common/string_util.h"
  8. #ifdef _WIN32
  9. #include <windows.h>
  10. #include <shlobj.h> // for SHGetFolderPath
  11. #include <shellapi.h>
  12. #include <commdlg.h> // for GetSaveFileName
  13. #include <io.h>
  14. #include <direct.h> // getcwd
  15. #else
  16. #include <cerrno>
  17. #include <cstdlib>
  18. #include <sys/param.h>
  19. #include <sys/types.h>
  20. #include <dirent.h>
  21. #endif
  22. #if defined(__APPLE__)
  23. #include <CoreFoundation/CFString.h>
  24. #include <CoreFoundation/CFURL.h>
  25. #include <CoreFoundation/CFBundle.h>
  26. #endif
  27. #include <algorithm>
  28. #include <sys/stat.h>
  29. #include "common/string_util.h"
  30. #ifndef S_ISDIR
  31. #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
  32. #endif
  33. #ifdef BSD4_4
  34. #define stat64 stat
  35. #define fstat64 fstat
  36. #endif
  37. // This namespace has various generic functions related to files and paths.
  38. // The code still needs a ton of cleanup.
  39. // REMEMBER: strdup considered harmful!
  40. namespace File
  41. {
  42. // Remove any ending forward slashes from directory paths
  43. // Modifies argument.
  44. static void StripTailDirSlashes(std::string &fname)
  45. {
  46. if (fname.length() > 1)
  47. {
  48. size_t i = fname.length() - 1;
  49. while (fname[i] == DIR_SEP_CHR)
  50. fname[i--] = '\0';
  51. }
  52. return;
  53. }
  54. // Returns true if file filename exists
  55. bool Exists(const std::string &filename)
  56. {
  57. struct stat64 file_info;
  58. std::string copy(filename);
  59. StripTailDirSlashes(copy);
  60. #ifdef _WIN32
  61. int result = _tstat64(UTF8ToTStr(copy).c_str(), &file_info);
  62. #else
  63. int result = stat64(copy.c_str(), &file_info);
  64. #endif
  65. return (result == 0);
  66. }
  67. // Returns true if filename is a directory
  68. bool IsDirectory(const std::string &filename)
  69. {
  70. struct stat64 file_info;
  71. std::string copy(filename);
  72. StripTailDirSlashes(copy);
  73. #ifdef _WIN32
  74. int result = _tstat64(UTF8ToTStr(copy).c_str(), &file_info);
  75. #else
  76. int result = stat64(copy.c_str(), &file_info);
  77. #endif
  78. if (result < 0) {
  79. WARN_LOG(COMMON, "IsDirectory: stat failed on %s: %s",
  80. filename.c_str(), GetLastErrorMsg());
  81. return false;
  82. }
  83. return S_ISDIR(file_info.st_mode);
  84. }
  85. // Deletes a given filename, return true on success
  86. // Doesn't supports deleting a directory
  87. bool Delete(const std::string &filename)
  88. {
  89. INFO_LOG(COMMON, "Delete: file %s", filename.c_str());
  90. // Return true because we care about the file no
  91. // being there, not the actual delete.
  92. if (!Exists(filename))
  93. {
  94. WARN_LOG(COMMON, "Delete: %s does not exist", filename.c_str());
  95. return true;
  96. }
  97. // We can't delete a directory
  98. if (IsDirectory(filename))
  99. {
  100. WARN_LOG(COMMON, "Delete failed: %s is a directory", filename.c_str());
  101. return false;
  102. }
  103. #ifdef _WIN32
  104. if (!DeleteFile(UTF8ToTStr(filename).c_str()))
  105. {
  106. WARN_LOG(COMMON, "Delete: DeleteFile failed on %s: %s",
  107. filename.c_str(), GetLastErrorMsg());
  108. return false;
  109. }
  110. #else
  111. if (unlink(filename.c_str()) == -1) {
  112. WARN_LOG(COMMON, "Delete: unlink failed on %s: %s",
  113. filename.c_str(), GetLastErrorMsg());
  114. return false;
  115. }
  116. #endif
  117. return true;
  118. }
  119. // Returns true if successful, or path already exists.
  120. bool CreateDir(const std::string &path)
  121. {
  122. INFO_LOG(COMMON, "CreateDir: directory %s", path.c_str());
  123. #ifdef _WIN32
  124. if (::CreateDirectory(UTF8ToTStr(path).c_str(), NULL))
  125. return true;
  126. DWORD error = GetLastError();
  127. if (error == ERROR_ALREADY_EXISTS)
  128. {
  129. WARN_LOG(COMMON, "CreateDir: CreateDirectory failed on %s: already exists", path.c_str());
  130. return true;
  131. }
  132. ERROR_LOG(COMMON, "CreateDir: CreateDirectory failed on %s: %i", path.c_str(), error);
  133. return false;
  134. #else
  135. if (mkdir(path.c_str(), 0755) == 0)
  136. return true;
  137. int err = errno;
  138. if (err == EEXIST)
  139. {
  140. WARN_LOG(COMMON, "CreateDir: mkdir failed on %s: already exists", path.c_str());
  141. return true;
  142. }
  143. ERROR_LOG(COMMON, "CreateDir: mkdir failed on %s: %s", path.c_str(), strerror(err));
  144. return false;
  145. #endif
  146. }
  147. // Creates the full path of fullPath returns true on success
  148. bool CreateFullPath(const std::string &fullPath)
  149. {
  150. int panicCounter = 100;
  151. INFO_LOG(COMMON, "CreateFullPath: path %s", fullPath.c_str());
  152. if (File::Exists(fullPath))
  153. {
  154. INFO_LOG(COMMON, "CreateFullPath: path exists %s", fullPath.c_str());
  155. return true;
  156. }
  157. size_t position = 0;
  158. while (true)
  159. {
  160. // Find next sub path
  161. position = fullPath.find(DIR_SEP_CHR, position);
  162. // we're done, yay!
  163. if (position == fullPath.npos)
  164. return true;
  165. // Include the '/' so the first call is CreateDir("/") rather than CreateDir("")
  166. std::string const subPath(fullPath.substr(0, position + 1));
  167. if (!File::IsDirectory(subPath))
  168. File::CreateDir(subPath);
  169. // A safety check
  170. panicCounter--;
  171. if (panicCounter <= 0)
  172. {
  173. ERROR_LOG(COMMON, "CreateFullPath: directory structure is too deep");
  174. return false;
  175. }
  176. position++;
  177. }
  178. }
  179. // Deletes a directory filename, returns true on success
  180. bool DeleteDir(const std::string &filename)
  181. {
  182. INFO_LOG(COMMON, "DeleteDir: directory %s", filename.c_str());
  183. // check if a directory
  184. if (!File::IsDirectory(filename))
  185. {
  186. ERROR_LOG(COMMON, "DeleteDir: Not a directory %s", filename.c_str());
  187. return false;
  188. }
  189. #ifdef _WIN32
  190. if (::RemoveDirectory(UTF8ToTStr(filename).c_str()))
  191. return true;
  192. #else
  193. if (rmdir(filename.c_str()) == 0)
  194. return true;
  195. #endif
  196. ERROR_LOG(COMMON, "DeleteDir: %s: %s", filename.c_str(), GetLastErrorMsg());
  197. return false;
  198. }
  199. // renames file srcFilename to destFilename, returns true on success
  200. bool Rename(const std::string &srcFilename, const std::string &destFilename)
  201. {
  202. INFO_LOG(COMMON, "Rename: %s --> %s",
  203. srcFilename.c_str(), destFilename.c_str());
  204. if (rename(srcFilename.c_str(), destFilename.c_str()) == 0)
  205. return true;
  206. ERROR_LOG(COMMON, "Rename: failed %s --> %s: %s",
  207. srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg());
  208. return false;
  209. }
  210. // copies file srcFilename to destFilename, returns true on success
  211. bool Copy(const std::string &srcFilename, const std::string &destFilename)
  212. {
  213. INFO_LOG(COMMON, "Copy: %s --> %s",
  214. srcFilename.c_str(), destFilename.c_str());
  215. #ifdef _WIN32
  216. if (CopyFile(UTF8ToTStr(srcFilename).c_str(), UTF8ToTStr(destFilename).c_str(), FALSE))
  217. return true;
  218. ERROR_LOG(COMMON, "Copy: failed %s --> %s: %s",
  219. srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg());
  220. return false;
  221. #else
  222. // buffer size
  223. #define BSIZE 1024
  224. char buffer[BSIZE];
  225. // Open input file
  226. FILE *input = fopen(srcFilename.c_str(), "rb");
  227. if (!input)
  228. {
  229. ERROR_LOG(COMMON, "Copy: input failed %s --> %s: %s",
  230. srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg());
  231. return false;
  232. }
  233. // open output file
  234. FILE *output = fopen(destFilename.c_str(), "wb");
  235. if (!output)
  236. {
  237. fclose(input);
  238. ERROR_LOG(COMMON, "Copy: output failed %s --> %s: %s",
  239. srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg());
  240. return false;
  241. }
  242. // copy loop
  243. while (!feof(input))
  244. {
  245. // read input
  246. int rnum = fread(buffer, sizeof(char), BSIZE, input);
  247. if (rnum != BSIZE)
  248. {
  249. if (ferror(input) != 0)
  250. {
  251. ERROR_LOG(COMMON,
  252. "Copy: failed reading from source, %s --> %s: %s",
  253. srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg());
  254. goto bail;
  255. }
  256. }
  257. // write output
  258. int wnum = fwrite(buffer, sizeof(char), rnum, output);
  259. if (wnum != rnum)
  260. {
  261. ERROR_LOG(COMMON,
  262. "Copy: failed writing to output, %s --> %s: %s",
  263. srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg());
  264. goto bail;
  265. }
  266. }
  267. // close files
  268. fclose(input);
  269. fclose(output);
  270. return true;
  271. bail:
  272. if (input)
  273. fclose(input);
  274. if (output)
  275. fclose(output);
  276. return false;
  277. #endif
  278. }
  279. // Returns the size of filename (64bit)
  280. u64 GetSize(const std::string &filename)
  281. {
  282. if (!Exists(filename))
  283. {
  284. WARN_LOG(COMMON, "GetSize: failed %s: No such file", filename.c_str());
  285. return 0;
  286. }
  287. if (IsDirectory(filename))
  288. {
  289. WARN_LOG(COMMON, "GetSize: failed %s: is a directory", filename.c_str());
  290. return 0;
  291. }
  292. struct stat64 buf;
  293. #ifdef _WIN32
  294. if (_tstat64(UTF8ToTStr(filename).c_str(), &buf) == 0)
  295. #else
  296. if (stat64(filename.c_str(), &buf) == 0)
  297. #endif
  298. {
  299. DEBUG_LOG(COMMON, "GetSize: %s: %lld",
  300. filename.c_str(), (long long)buf.st_size);
  301. return buf.st_size;
  302. }
  303. ERROR_LOG(COMMON, "GetSize: Stat failed %s: %s",
  304. filename.c_str(), GetLastErrorMsg());
  305. return 0;
  306. }
  307. // Overloaded GetSize, accepts file descriptor
  308. u64 GetSize(const int fd)
  309. {
  310. struct stat64 buf;
  311. if (fstat64(fd, &buf) != 0) {
  312. ERROR_LOG(COMMON, "GetSize: stat failed %i: %s",
  313. fd, GetLastErrorMsg());
  314. return 0;
  315. }
  316. return buf.st_size;
  317. }
  318. // Overloaded GetSize, accepts FILE*
  319. u64 GetSize(FILE *f)
  320. {
  321. // can't use off_t here because it can be 32-bit
  322. u64 pos = ftello(f);
  323. if (fseeko(f, 0, SEEK_END) != 0) {
  324. ERROR_LOG(COMMON, "GetSize: seek failed %p: %s",
  325. f, GetLastErrorMsg());
  326. return 0;
  327. }
  328. u64 size = ftello(f);
  329. if ((size != pos) && (fseeko(f, pos, SEEK_SET) != 0)) {
  330. ERROR_LOG(COMMON, "GetSize: seek failed %p: %s",
  331. f, GetLastErrorMsg());
  332. return 0;
  333. }
  334. return size;
  335. }
  336. // creates an empty file filename, returns true on success
  337. bool CreateEmptyFile(const std::string &filename)
  338. {
  339. INFO_LOG(COMMON, "CreateEmptyFile: %s", filename.c_str());
  340. if (!File::IOFile(filename, "wb"))
  341. {
  342. ERROR_LOG(COMMON, "CreateEmptyFile: failed %s: %s",
  343. filename.c_str(), GetLastErrorMsg());
  344. return false;
  345. }
  346. return true;
  347. }
  348. // Scans the directory tree gets, starting from _Directory and adds the
  349. // results into parentEntry. Returns the number of files+directories found
  350. u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry)
  351. {
  352. INFO_LOG(COMMON, "ScanDirectoryTree: directory %s", directory.c_str());
  353. // How many files + directories we found
  354. u32 foundEntries = 0;
  355. #ifdef _WIN32
  356. // Find the first file in the directory.
  357. WIN32_FIND_DATA ffd;
  358. HANDLE hFind = FindFirstFile(UTF8ToTStr(directory + "\\*").c_str(), &ffd);
  359. if (hFind == INVALID_HANDLE_VALUE)
  360. {
  361. FindClose(hFind);
  362. return foundEntries;
  363. }
  364. // windows loop
  365. do
  366. {
  367. FSTEntry entry;
  368. const std::string virtualName(TStrToUTF8(ffd.cFileName));
  369. #else
  370. struct dirent dirent, *result = NULL;
  371. DIR *dirp = opendir(directory.c_str());
  372. if (!dirp)
  373. return 0;
  374. // non windows loop
  375. while (!readdir_r(dirp, &dirent, &result) && result)
  376. {
  377. FSTEntry entry;
  378. const std::string virtualName(result->d_name);
  379. #endif
  380. // check for "." and ".."
  381. if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
  382. ((virtualName[0] == '.') && (virtualName[1] == '.') &&
  383. (virtualName[2] == '\0')))
  384. continue;
  385. entry.virtualName = virtualName;
  386. entry.physicalName = directory;
  387. entry.physicalName += DIR_SEP + entry.virtualName;
  388. if (IsDirectory(entry.physicalName.c_str()))
  389. {
  390. entry.isDirectory = true;
  391. // is a directory, lets go inside
  392. entry.size = ScanDirectoryTree(entry.physicalName, entry);
  393. foundEntries += (u32)entry.size;
  394. }
  395. else
  396. { // is a file
  397. entry.isDirectory = false;
  398. entry.size = GetSize(entry.physicalName.c_str());
  399. }
  400. ++foundEntries;
  401. // Push into the tree
  402. parentEntry.children.push_back(entry);
  403. #ifdef _WIN32
  404. } while (FindNextFile(hFind, &ffd) != 0);
  405. FindClose(hFind);
  406. #else
  407. }
  408. closedir(dirp);
  409. #endif
  410. // Return number of entries found.
  411. return foundEntries;
  412. }
  413. // Deletes the given directory and anything under it. Returns true on success.
  414. bool DeleteDirRecursively(const std::string &directory)
  415. {
  416. INFO_LOG(COMMON, "DeleteDirRecursively: %s", directory.c_str());
  417. #ifdef _WIN32
  418. // Find the first file in the directory.
  419. WIN32_FIND_DATA ffd;
  420. HANDLE hFind = FindFirstFile(UTF8ToTStr(directory + "\\*").c_str(), &ffd);
  421. if (hFind == INVALID_HANDLE_VALUE)
  422. {
  423. FindClose(hFind);
  424. return false;
  425. }
  426. // windows loop
  427. do
  428. {
  429. const std::string virtualName(TStrToUTF8(ffd.cFileName));
  430. #else
  431. struct dirent dirent, *result = NULL;
  432. DIR *dirp = opendir(directory.c_str());
  433. if (!dirp)
  434. return false;
  435. // non windows loop
  436. while (!readdir_r(dirp, &dirent, &result) && result)
  437. {
  438. const std::string virtualName = result->d_name;
  439. #endif
  440. // check for "." and ".."
  441. if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
  442. ((virtualName[0] == '.') && (virtualName[1] == '.') &&
  443. (virtualName[2] == '\0')))
  444. continue;
  445. std::string newPath = directory + DIR_SEP_CHR + virtualName;
  446. if (IsDirectory(newPath))
  447. {
  448. if (!DeleteDirRecursively(newPath))
  449. {
  450. #ifndef _WIN32
  451. closedir(dirp);
  452. #endif
  453. return false;
  454. }
  455. }
  456. else
  457. {
  458. if (!File::Delete(newPath))
  459. {
  460. #ifndef _WIN32
  461. closedir(dirp);
  462. #endif
  463. return false;
  464. }
  465. }
  466. #ifdef _WIN32
  467. } while (FindNextFile(hFind, &ffd) != 0);
  468. FindClose(hFind);
  469. #else
  470. }
  471. closedir(dirp);
  472. #endif
  473. File::DeleteDir(directory);
  474. return true;
  475. }
  476. // Create directory and copy contents (does not overwrite existing files)
  477. void CopyDir(const std::string &source_path, const std::string &dest_path)
  478. {
  479. #ifndef _WIN32
  480. if (source_path == dest_path) return;
  481. if (!File::Exists(source_path)) return;
  482. if (!File::Exists(dest_path)) File::CreateFullPath(dest_path);
  483. struct dirent dirent, *result = NULL;
  484. DIR *dirp = opendir(source_path.c_str());
  485. if (!dirp) return;
  486. while (!readdir_r(dirp, &dirent, &result) && result)
  487. {
  488. const std::string virtualName(result->d_name);
  489. // check for "." and ".."
  490. if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
  491. ((virtualName[0] == '.') && (virtualName[1] == '.') &&
  492. (virtualName[2] == '\0')))
  493. continue;
  494. std::string source, dest;
  495. source = source_path + virtualName;
  496. dest = dest_path + virtualName;
  497. if (IsDirectory(source))
  498. {
  499. source += '/';
  500. dest += '/';
  501. if (!File::Exists(dest)) File::CreateFullPath(dest);
  502. CopyDir(source, dest);
  503. }
  504. else if (!File::Exists(dest)) File::Copy(source, dest);
  505. }
  506. closedir(dirp);
  507. #endif
  508. }
  509. // Returns the current directory
  510. std::string GetCurrentDir()
  511. {
  512. char *dir;
  513. // Get the current working directory (getcwd uses malloc)
  514. if (!(dir = __getcwd(NULL, 0))) {
  515. ERROR_LOG(COMMON, "GetCurrentDirectory failed: %s",
  516. GetLastErrorMsg());
  517. return NULL;
  518. }
  519. std::string strDir = dir;
  520. free(dir);
  521. return strDir;
  522. }
  523. // Sets the current directory to the given directory
  524. bool SetCurrentDir(const std::string &directory)
  525. {
  526. return __chdir(directory.c_str()) == 0;
  527. }
  528. #if defined(__APPLE__)
  529. std::string GetBundleDirectory()
  530. {
  531. CFURLRef BundleRef;
  532. char AppBundlePath[MAXPATHLEN];
  533. // Get the main bundle for the app
  534. BundleRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
  535. CFStringRef BundlePath = CFURLCopyFileSystemPath(BundleRef, kCFURLPOSIXPathStyle);
  536. CFStringGetFileSystemRepresentation(BundlePath, AppBundlePath, sizeof(AppBundlePath));
  537. CFRelease(BundleRef);
  538. CFRelease(BundlePath);
  539. return AppBundlePath;
  540. }
  541. #endif
  542. #ifdef _WIN32
  543. std::string& GetExeDirectory()
  544. {
  545. static std::string DolphinPath;
  546. if (DolphinPath.empty())
  547. {
  548. TCHAR Dolphin_exe_Path[2048];
  549. GetModuleFileName(NULL, Dolphin_exe_Path, 2048);
  550. DolphinPath = TStrToUTF8(Dolphin_exe_Path);
  551. DolphinPath = DolphinPath.substr(0, DolphinPath.find_last_of('\\'));
  552. }
  553. return DolphinPath;
  554. }
  555. #endif
  556. std::string GetSysDirectory()
  557. {
  558. std::string sysDir;
  559. #if defined (__APPLE__)
  560. sysDir = GetBundleDirectory();
  561. sysDir += DIR_SEP;
  562. sysDir += SYSDATA_DIR;
  563. #else
  564. sysDir = SYSDATA_DIR;
  565. #endif
  566. sysDir += DIR_SEP;
  567. INFO_LOG(COMMON, "GetSysDirectory: Setting to %s:", sysDir.c_str());
  568. return sysDir;
  569. }
  570. // Returns a string with a Dolphin data dir or file in the user's home
  571. // directory. To be used in "multi-user" mode (that is, installed).
  572. const std::string& GetUserPath(const unsigned int DirIDX, const std::string &newPath)
  573. {
  574. static std::string paths[NUM_PATH_INDICES];
  575. // Set up all paths and files on the first run
  576. if (paths[D_USER_IDX].empty())
  577. {
  578. #ifdef _WIN32
  579. paths[D_USER_IDX] = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP;
  580. #else
  581. if (File::Exists(ROOT_DIR DIR_SEP USERDATA_DIR))
  582. paths[D_USER_IDX] = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP;
  583. else
  584. paths[D_USER_IDX] = std::string(getenv("HOME") ?
  585. getenv("HOME") : getenv("PWD") ?
  586. getenv("PWD") : "") + DIR_SEP EMU_DATA_DIR DIR_SEP;
  587. #endif
  588. paths[D_CONFIG_IDX] = paths[D_USER_IDX] + CONFIG_DIR DIR_SEP;
  589. paths[D_GAMECONFIG_IDX] = paths[D_USER_IDX] + GAMECONFIG_DIR DIR_SEP;
  590. paths[D_MAPS_IDX] = paths[D_USER_IDX] + MAPS_DIR DIR_SEP;
  591. paths[D_CACHE_IDX] = paths[D_USER_IDX] + CACHE_DIR DIR_SEP;
  592. paths[D_SHADERCACHE_IDX] = paths[D_USER_IDX] + SHADERCACHE_DIR DIR_SEP;
  593. paths[D_SHADERS_IDX] = paths[D_USER_IDX] + SHADERS_DIR DIR_SEP;
  594. paths[D_STATESAVES_IDX] = paths[D_USER_IDX] + STATESAVES_DIR DIR_SEP;
  595. paths[D_SCREENSHOTS_IDX] = paths[D_USER_IDX] + SCREENSHOTS_DIR DIR_SEP;
  596. paths[D_DUMP_IDX] = paths[D_USER_IDX] + DUMP_DIR DIR_SEP;
  597. paths[D_DUMPFRAMES_IDX] = paths[D_DUMP_IDX] + DUMP_FRAMES_DIR DIR_SEP;
  598. paths[D_DUMPAUDIO_IDX] = paths[D_DUMP_IDX] + DUMP_AUDIO_DIR DIR_SEP;
  599. paths[D_DUMPTEXTURES_IDX] = paths[D_DUMP_IDX] + DUMP_TEXTURES_DIR DIR_SEP;
  600. paths[D_LOGS_IDX] = paths[D_USER_IDX] + LOGS_DIR DIR_SEP;
  601. paths[F_DEBUGGERCONFIG_IDX] = paths[D_CONFIG_IDX] + DEBUGGER_CONFIG;
  602. paths[F_LOGGERCONFIG_IDX] = paths[D_CONFIG_IDX] + LOGGER_CONFIG;
  603. paths[F_MAINLOG_IDX] = paths[D_LOGS_IDX] + MAIN_LOG;
  604. }
  605. if (!newPath.empty())
  606. {
  607. if (!File::IsDirectory(newPath))
  608. {
  609. WARN_LOG(COMMON, "Invalid path specified %s", newPath.c_str());
  610. return paths[DirIDX];
  611. }
  612. else
  613. {
  614. paths[DirIDX] = newPath;
  615. }
  616. switch (DirIDX)
  617. {
  618. case D_ROOT_IDX:
  619. paths[D_USER_IDX] = paths[D_ROOT_IDX] + DIR_SEP;
  620. paths[D_SYSCONF_IDX] = paths[D_USER_IDX] + SYSCONF_DIR + DIR_SEP;
  621. paths[F_SYSCONF_IDX] = paths[D_SYSCONF_IDX] + SYSCONF;
  622. break;
  623. case D_USER_IDX:
  624. paths[D_USER_IDX] = paths[D_ROOT_IDX] + DIR_SEP;
  625. paths[D_CONFIG_IDX] = paths[D_USER_IDX] + CONFIG_DIR DIR_SEP;
  626. paths[D_GAMECONFIG_IDX] = paths[D_USER_IDX] + GAMECONFIG_DIR DIR_SEP;
  627. paths[D_MAPS_IDX] = paths[D_USER_IDX] + MAPS_DIR DIR_SEP;
  628. paths[D_CACHE_IDX] = paths[D_USER_IDX] + CACHE_DIR DIR_SEP;
  629. paths[D_SHADERCACHE_IDX] = paths[D_USER_IDX] + SHADERCACHE_DIR DIR_SEP;
  630. paths[D_SHADERS_IDX] = paths[D_USER_IDX] + SHADERS_DIR DIR_SEP;
  631. paths[D_STATESAVES_IDX] = paths[D_USER_IDX] + STATESAVES_DIR DIR_SEP;
  632. paths[D_SCREENSHOTS_IDX] = paths[D_USER_IDX] + SCREENSHOTS_DIR DIR_SEP;
  633. paths[D_DUMP_IDX] = paths[D_USER_IDX] + DUMP_DIR DIR_SEP;
  634. paths[D_DUMPFRAMES_IDX] = paths[D_DUMP_IDX] + DUMP_FRAMES_DIR DIR_SEP;
  635. paths[D_DUMPAUDIO_IDX] = paths[D_DUMP_IDX] + DUMP_AUDIO_DIR DIR_SEP;
  636. paths[D_DUMPTEXTURES_IDX] = paths[D_DUMP_IDX] + DUMP_TEXTURES_DIR DIR_SEP;
  637. paths[D_LOGS_IDX] = paths[D_USER_IDX] + LOGS_DIR DIR_SEP;
  638. paths[D_SYSCONF_IDX] = paths[D_USER_IDX] + SYSCONF_DIR DIR_SEP;
  639. paths[F_EMUCONFIG_IDX] = paths[D_CONFIG_IDX] + EMU_CONFIG;
  640. paths[F_DEBUGGERCONFIG_IDX] = paths[D_CONFIG_IDX] + DEBUGGER_CONFIG;
  641. paths[F_LOGGERCONFIG_IDX] = paths[D_CONFIG_IDX] + LOGGER_CONFIG;
  642. paths[F_MAINLOG_IDX] = paths[D_LOGS_IDX] + MAIN_LOG;
  643. break;
  644. case D_CONFIG_IDX:
  645. paths[F_EMUCONFIG_IDX] = paths[D_CONFIG_IDX] + EMU_CONFIG;
  646. paths[F_DEBUGGERCONFIG_IDX] = paths[D_CONFIG_IDX] + DEBUGGER_CONFIG;
  647. paths[F_LOGGERCONFIG_IDX] = paths[D_CONFIG_IDX] + LOGGER_CONFIG;
  648. break;
  649. case D_DUMP_IDX:
  650. paths[D_DUMPFRAMES_IDX] = paths[D_DUMP_IDX] + DUMP_FRAMES_DIR DIR_SEP;
  651. paths[D_DUMPAUDIO_IDX] = paths[D_DUMP_IDX] + DUMP_AUDIO_DIR DIR_SEP;
  652. paths[D_DUMPTEXTURES_IDX] = paths[D_DUMP_IDX] + DUMP_TEXTURES_DIR DIR_SEP;
  653. break;
  654. case D_LOGS_IDX:
  655. paths[F_MAINLOG_IDX] = paths[D_LOGS_IDX] + MAIN_LOG;
  656. }
  657. }
  658. return paths[DirIDX];
  659. }
  660. //std::string GetThemeDir(const std::string& theme_name)
  661. //{
  662. // std::string dir = File::GetUserPath(D_THEMES_IDX) + theme_name + "/";
  663. //
  664. //#if !defined(_WIN32)
  665. // // If theme does not exist in user's dir load from shared directory
  666. // if (!File::Exists(dir))
  667. // dir = SHARED_USER_DIR THEMES_DIR "/" + theme_name + "/";
  668. //#endif
  669. //
  670. // return dir;
  671. //}
  672. bool WriteStringToFile(bool text_file, const std::string &str, const char *filename)
  673. {
  674. return File::IOFile(filename, text_file ? "w" : "wb").WriteBytes(str.data(), str.size());
  675. }
  676. bool ReadFileToString(bool text_file, const char *filename, std::string &str)
  677. {
  678. File::IOFile file(filename, text_file ? "r" : "rb");
  679. auto const f = file.GetHandle();
  680. if (!f)
  681. return false;
  682. str.resize(static_cast<u32>(GetSize(f)));
  683. return file.ReadArray(&str[0], str.size());
  684. }
  685. IOFile::IOFile()
  686. : m_file(NULL), m_good(true)
  687. {}
  688. IOFile::IOFile(std::FILE* file)
  689. : m_file(file), m_good(true)
  690. {}
  691. IOFile::IOFile(const std::string& filename, const char openmode[])
  692. : m_file(NULL), m_good(true)
  693. {
  694. Open(filename, openmode);
  695. }
  696. IOFile::~IOFile()
  697. {
  698. Close();
  699. }
  700. IOFile::IOFile(IOFile&& other)
  701. : m_file(NULL), m_good(true)
  702. {
  703. Swap(other);
  704. }
  705. IOFile& IOFile::operator=(IOFile&& other)
  706. {
  707. Swap(other);
  708. return *this;
  709. }
  710. void IOFile::Swap(IOFile& other)
  711. {
  712. std::swap(m_file, other.m_file);
  713. std::swap(m_good, other.m_good);
  714. }
  715. bool IOFile::Open(const std::string& filename, const char openmode[])
  716. {
  717. Close();
  718. #ifdef _WIN32
  719. _tfopen_s(&m_file, UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str());
  720. #else
  721. m_file = fopen(filename.c_str(), openmode);
  722. #endif
  723. m_good = IsOpen();
  724. return m_good;
  725. }
  726. bool IOFile::Close()
  727. {
  728. if (!IsOpen() || 0 != std::fclose(m_file))
  729. m_good = false;
  730. m_file = NULL;
  731. return m_good;
  732. }
  733. std::FILE* IOFile::ReleaseHandle()
  734. {
  735. std::FILE* const ret = m_file;
  736. m_file = NULL;
  737. return ret;
  738. }
  739. void IOFile::SetHandle(std::FILE* file)
  740. {
  741. Close();
  742. Clear();
  743. m_file = file;
  744. }
  745. u64 IOFile::GetSize()
  746. {
  747. if (IsOpen())
  748. return File::GetSize(m_file);
  749. else
  750. return 0;
  751. }
  752. bool IOFile::Seek(s64 off, int origin)
  753. {
  754. if (!IsOpen() || 0 != fseeko(m_file, off, origin))
  755. m_good = false;
  756. return m_good;
  757. }
  758. u64 IOFile::Tell()
  759. {
  760. if (IsOpen())
  761. return ftello(m_file);
  762. else
  763. return -1;
  764. }
  765. bool IOFile::Flush()
  766. {
  767. if (!IsOpen() || 0 != std::fflush(m_file))
  768. m_good = false;
  769. return m_good;
  770. }
  771. bool IOFile::Resize(u64 size)
  772. {
  773. if (!IsOpen() || 0 !=
  774. #ifdef _WIN32
  775. // ector: _chsize sucks, not 64-bit safe
  776. // F|RES: changed to _chsize_s. i think it is 64-bit safe
  777. _chsize_s(_fileno(m_file), size)
  778. #else
  779. // TODO: handle 64bit and growing
  780. ftruncate(fileno(m_file), size)
  781. #endif
  782. )
  783. m_good = false;
  784. return m_good;
  785. }
  786. } // namespace