filesys.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "filesys.h"
  17. #include "util/string.h"
  18. #include <iostream>
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <cstring>
  22. #include <cerrno>
  23. #include <fstream>
  24. #include "log.h"
  25. #include "config.h"
  26. #include "porting.h"
  27. #ifndef SERVER
  28. #include "irr_ptr.h"
  29. #include <IFileArchive.h>
  30. #include <IFileSystem.h>
  31. #endif
  32. #ifdef __linux__
  33. #include <fcntl.h>
  34. #include <sys/ioctl.h>
  35. #ifndef FICLONE
  36. #define FICLONE _IOW(0x94, 9, int)
  37. #endif
  38. #endif
  39. namespace fs
  40. {
  41. #ifdef _WIN32
  42. /***********
  43. * Windows *
  44. ***********/
  45. #include <windows.h>
  46. #include <shlwapi.h>
  47. #include <io.h>
  48. #include <direct.h>
  49. std::vector<DirListNode> GetDirListing(const std::string &pathstring)
  50. {
  51. std::vector<DirListNode> listing;
  52. WIN32_FIND_DATA FindFileData;
  53. HANDLE hFind = INVALID_HANDLE_VALUE;
  54. DWORD dwError;
  55. std::string dirSpec = pathstring + "\\*";
  56. // Find the first file in the directory.
  57. hFind = FindFirstFile(dirSpec.c_str(), &FindFileData);
  58. if (hFind == INVALID_HANDLE_VALUE) {
  59. dwError = GetLastError();
  60. if (dwError != ERROR_FILE_NOT_FOUND && dwError != ERROR_PATH_NOT_FOUND) {
  61. errorstream << "GetDirListing: FindFirstFile error."
  62. << " Error is " << dwError << std::endl;
  63. }
  64. } else {
  65. // NOTE:
  66. // Be very sure to not include '..' in the results, it will
  67. // result in an epic failure when deleting stuff.
  68. DirListNode node;
  69. node.name = FindFileData.cFileName;
  70. node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
  71. if (node.name != "." && node.name != "..")
  72. listing.push_back(node);
  73. // List all the other files in the directory.
  74. while (FindNextFile(hFind, &FindFileData) != 0) {
  75. DirListNode node;
  76. node.name = FindFileData.cFileName;
  77. node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
  78. if(node.name != "." && node.name != "..")
  79. listing.push_back(node);
  80. }
  81. dwError = GetLastError();
  82. FindClose(hFind);
  83. if (dwError != ERROR_NO_MORE_FILES) {
  84. errorstream << "GetDirListing: FindNextFile error."
  85. << " Error is " << dwError << std::endl;
  86. listing.clear();
  87. return listing;
  88. }
  89. }
  90. return listing;
  91. }
  92. bool CreateDir(const std::string &path)
  93. {
  94. bool r = CreateDirectory(path.c_str(), NULL);
  95. if(r == true)
  96. return true;
  97. if(GetLastError() == ERROR_ALREADY_EXISTS)
  98. return true;
  99. return false;
  100. }
  101. bool PathExists(const std::string &path)
  102. {
  103. return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
  104. }
  105. bool IsPathAbsolute(const std::string &path)
  106. {
  107. return !PathIsRelative(path.c_str());
  108. }
  109. bool IsDir(const std::string &path)
  110. {
  111. DWORD attr = GetFileAttributes(path.c_str());
  112. return (attr != INVALID_FILE_ATTRIBUTES &&
  113. (attr & FILE_ATTRIBUTE_DIRECTORY));
  114. }
  115. bool IsExecutable(const std::string &path)
  116. {
  117. DWORD type;
  118. return GetBinaryType(path.c_str(), &type) != 0;
  119. }
  120. bool IsDirDelimiter(char c)
  121. {
  122. return c == '/' || c == '\\';
  123. }
  124. bool RecursiveDelete(const std::string &path)
  125. {
  126. infostream << "Recursively deleting \"" << path << "\"" << std::endl;
  127. if (!IsDir(path)) {
  128. infostream << "RecursiveDelete: Deleting file " << path << std::endl;
  129. if (!DeleteFile(path.c_str())) {
  130. errorstream << "RecursiveDelete: Failed to delete file "
  131. << path << std::endl;
  132. return false;
  133. }
  134. return true;
  135. }
  136. infostream << "RecursiveDelete: Deleting content of directory "
  137. << path << std::endl;
  138. std::vector<DirListNode> content = GetDirListing(path);
  139. for (const DirListNode &n: content) {
  140. std::string fullpath = path + DIR_DELIM + n.name;
  141. if (!RecursiveDelete(fullpath)) {
  142. errorstream << "RecursiveDelete: Failed to recurse to "
  143. << fullpath << std::endl;
  144. return false;
  145. }
  146. }
  147. infostream << "RecursiveDelete: Deleting directory " << path << std::endl;
  148. if (!RemoveDirectory(path.c_str())) {
  149. errorstream << "Failed to recursively delete directory "
  150. << path << std::endl;
  151. return false;
  152. }
  153. return true;
  154. }
  155. bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
  156. {
  157. DWORD attr = GetFileAttributes(path.c_str());
  158. bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
  159. (attr & FILE_ATTRIBUTE_DIRECTORY));
  160. if(!is_directory)
  161. {
  162. bool did = DeleteFile(path.c_str());
  163. return did;
  164. }
  165. else
  166. {
  167. bool did = RemoveDirectory(path.c_str());
  168. return did;
  169. }
  170. }
  171. std::string TempPath()
  172. {
  173. DWORD bufsize = GetTempPath(0, NULL);
  174. if(bufsize == 0){
  175. errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
  176. return "";
  177. }
  178. std::string buf;
  179. buf.resize(bufsize);
  180. DWORD len = GetTempPath(bufsize, &buf[0]);
  181. if(len == 0 || len > bufsize){
  182. errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
  183. return "";
  184. }
  185. buf.resize(len);
  186. return buf;
  187. }
  188. std::string CreateTempFile()
  189. {
  190. std::string path = TempPath() + DIR_DELIM "MT_XXXXXX";
  191. _mktemp_s(&path[0], path.size() + 1); // modifies path
  192. HANDLE file = CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr,
  193. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
  194. if (file == INVALID_HANDLE_VALUE)
  195. return "";
  196. CloseHandle(file);
  197. return path;
  198. }
  199. bool CopyFileContents(const std::string &source, const std::string &target)
  200. {
  201. BOOL ok = CopyFileEx(source.c_str(), target.c_str(), nullptr, nullptr,
  202. nullptr, COPY_FILE_ALLOW_DECRYPTED_DESTINATION);
  203. if (!ok) {
  204. errorstream << "copying " << source << " to " << target
  205. << " failed: " << GetLastError() << std::endl;
  206. return false;
  207. }
  208. // docs: "File attributes for the existing file are copied to the new file."
  209. // This is not our intention so get rid of unwanted attributes:
  210. DWORD attr = GetFileAttributes(target.c_str());
  211. if (attr == INVALID_FILE_ATTRIBUTES) {
  212. errorstream << target << ": file disappeared after copy" << std::endl;
  213. return false;
  214. }
  215. attr &= ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN);
  216. SetFileAttributes(target.c_str(), attr);
  217. tracestream << "copied " << source << " to " << target
  218. << " using CopyFileEx" << std::endl;
  219. return true;
  220. }
  221. #else
  222. /*********
  223. * POSIX *
  224. *********/
  225. #include <sys/types.h>
  226. #include <dirent.h>
  227. #include <sys/stat.h>
  228. #include <sys/wait.h>
  229. #include <unistd.h>
  230. std::vector<DirListNode> GetDirListing(const std::string &pathstring)
  231. {
  232. std::vector<DirListNode> listing;
  233. DIR *dp;
  234. struct dirent *dirp;
  235. if((dp = opendir(pathstring.c_str())) == NULL) {
  236. //infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
  237. return listing;
  238. }
  239. while ((dirp = readdir(dp)) != NULL) {
  240. // NOTE:
  241. // Be very sure to not include '..' in the results, it will
  242. // result in an epic failure when deleting stuff.
  243. if(strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
  244. continue;
  245. DirListNode node;
  246. node.name = dirp->d_name;
  247. int isdir = -1; // -1 means unknown
  248. /*
  249. POSIX doesn't define d_type member of struct dirent and
  250. certain filesystems on glibc/Linux will only return
  251. DT_UNKNOWN for the d_type member.
  252. Also we don't know whether symlinks are directories or not.
  253. */
  254. #ifdef _DIRENT_HAVE_D_TYPE
  255. if(dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
  256. isdir = (dirp->d_type == DT_DIR);
  257. #endif /* _DIRENT_HAVE_D_TYPE */
  258. /*
  259. Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
  260. If so, try stat().
  261. */
  262. if(isdir == -1) {
  263. struct stat statbuf{};
  264. if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
  265. continue;
  266. isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
  267. }
  268. node.dir = isdir;
  269. listing.push_back(node);
  270. }
  271. closedir(dp);
  272. return listing;
  273. }
  274. bool CreateDir(const std::string &path)
  275. {
  276. int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  277. if (r == 0) {
  278. return true;
  279. }
  280. // If already exists, return true
  281. if (errno == EEXIST)
  282. return true;
  283. return false;
  284. }
  285. bool PathExists(const std::string &path)
  286. {
  287. struct stat st{};
  288. return (stat(path.c_str(),&st) == 0);
  289. }
  290. bool IsPathAbsolute(const std::string &path)
  291. {
  292. return path[0] == '/';
  293. }
  294. bool IsDir(const std::string &path)
  295. {
  296. struct stat statbuf{};
  297. if(stat(path.c_str(), &statbuf))
  298. return false; // Actually error; but certainly not a directory
  299. return ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
  300. }
  301. bool IsExecutable(const std::string &path)
  302. {
  303. return access(path.c_str(), X_OK) == 0;
  304. }
  305. bool IsDirDelimiter(char c)
  306. {
  307. return c == '/';
  308. }
  309. bool RecursiveDelete(const std::string &path)
  310. {
  311. /*
  312. Execute the 'rm' command directly, by fork() and execve()
  313. */
  314. infostream<<"Removing \""<<path<<"\""<<std::endl;
  315. pid_t child_pid = fork();
  316. if(child_pid == 0)
  317. {
  318. // Child
  319. const char *argv[4] = {
  320. #ifdef __ANDROID__
  321. "/system/bin/rm",
  322. #else
  323. "/bin/rm",
  324. #endif
  325. "-rf",
  326. path.c_str(),
  327. NULL
  328. };
  329. verbosestream<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
  330. <<argv[2]<<"'"<<std::endl;
  331. execv(argv[0], const_cast<char**>(argv));
  332. // Execv shouldn't return. Failed.
  333. _exit(1);
  334. }
  335. else
  336. {
  337. // Parent
  338. int child_status;
  339. pid_t tpid;
  340. do{
  341. tpid = wait(&child_status);
  342. }while(tpid != child_pid);
  343. return (child_status == 0);
  344. }
  345. }
  346. bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
  347. {
  348. if (IsDir(path)) {
  349. bool did = (rmdir(path.c_str()) == 0);
  350. if (!did)
  351. errorstream << "rmdir errno: " << errno << ": " << strerror(errno)
  352. << std::endl;
  353. return did;
  354. }
  355. bool did = (unlink(path.c_str()) == 0);
  356. if (!did)
  357. errorstream << "unlink errno: " << errno << ": " << strerror(errno)
  358. << std::endl;
  359. return did;
  360. }
  361. std::string TempPath()
  362. {
  363. /*
  364. Should the environment variables TMPDIR, TMP and TEMP
  365. and the macro P_tmpdir (if defined by stdio.h) be checked
  366. before falling back on /tmp?
  367. Probably not, because this function is intended to be
  368. compatible with lua's os.tmpname which under the default
  369. configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
  370. */
  371. #ifdef __ANDROID__
  372. return porting::path_cache;
  373. #else
  374. return DIR_DELIM "tmp";
  375. #endif
  376. }
  377. std::string CreateTempFile()
  378. {
  379. std::string path = TempPath() + DIR_DELIM "MT_XXXXXX";
  380. int fd = mkstemp(&path[0]); // modifies path
  381. if (fd == -1)
  382. return "";
  383. close(fd);
  384. return path;
  385. }
  386. namespace {
  387. struct FileDeleter {
  388. void operator()(FILE *stream) {
  389. fclose(stream);
  390. }
  391. };
  392. typedef std::unique_ptr<FILE, FileDeleter> FileUniquePtr;
  393. }
  394. bool CopyFileContents(const std::string &source, const std::string &target)
  395. {
  396. FileUniquePtr sourcefile, targetfile;
  397. #ifdef __linux__
  398. // Try to clone using Copy-on-Write (CoW). This is instant but supported
  399. // only by some filesystems.
  400. int srcfd, tgtfd;
  401. srcfd = open(source.c_str(), O_RDONLY);
  402. if (srcfd == -1) {
  403. errorstream << source << ": can't open for reading: "
  404. << strerror(errno) << std::endl;
  405. return false;
  406. }
  407. tgtfd = open(target.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
  408. if (tgtfd == -1) {
  409. errorstream << target << ": can't open for writing: "
  410. << strerror(errno) << std::endl;
  411. close(srcfd);
  412. return false;
  413. }
  414. if (ioctl(tgtfd, FICLONE, srcfd) == 0) {
  415. tracestream << "copied " << source << " to " << target
  416. << " using FICLONE" << std::endl;
  417. close(srcfd);
  418. close(tgtfd);
  419. return true;
  420. }
  421. // fallback to normal copy, but no need to reopen the files
  422. sourcefile.reset(fdopen(srcfd, "rb"));
  423. targetfile.reset(fdopen(tgtfd, "wb"));
  424. goto fallback;
  425. #endif
  426. sourcefile.reset(fopen(source.c_str(), "rb"));
  427. targetfile.reset(fopen(target.c_str(), "wb"));
  428. fallback:
  429. if (!sourcefile) {
  430. errorstream << source << ": can't open for reading: "
  431. << strerror(errno) << std::endl;
  432. return false;
  433. }
  434. if (!targetfile) {
  435. errorstream << target << ": can't open for writing: "
  436. << strerror(errno) << std::endl;
  437. return false;
  438. }
  439. size_t total = 0;
  440. bool done = false;
  441. char readbuffer[BUFSIZ];
  442. while (!done) {
  443. size_t readbytes = fread(readbuffer, 1,
  444. sizeof(readbuffer), sourcefile.get());
  445. total += readbytes;
  446. if (ferror(sourcefile.get())) {
  447. errorstream << source << ": IO error: "
  448. << strerror(errno) << std::endl;
  449. return false;
  450. }
  451. if (readbytes > 0)
  452. fwrite(readbuffer, 1, readbytes, targetfile.get());
  453. if (feof(sourcefile.get())) {
  454. // flush destination file to catch write errors (e.g. disk full)
  455. fflush(targetfile.get());
  456. done = true;
  457. }
  458. if (ferror(targetfile.get())) {
  459. errorstream << target << ": IO error: "
  460. << strerror(errno) << std::endl;
  461. return false;
  462. }
  463. }
  464. tracestream << "copied " << total << " bytes from "
  465. << source << " to " << target << std::endl;
  466. return true;
  467. }
  468. #endif
  469. /****************************
  470. * portable implementations *
  471. ****************************/
  472. void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir)
  473. {
  474. static const std::set<char> chars_to_ignore = { '_', '.' };
  475. if (dir.empty() || !IsDir(dir))
  476. return;
  477. dirs.push_back(dir);
  478. fs::GetRecursiveSubPaths(dir, dirs, false, chars_to_ignore);
  479. }
  480. std::vector<std::string> GetRecursiveDirs(const std::string &dir)
  481. {
  482. std::vector<std::string> result;
  483. GetRecursiveDirs(result, dir);
  484. return result;
  485. }
  486. void GetRecursiveSubPaths(const std::string &path,
  487. std::vector<std::string> &dst,
  488. bool list_files,
  489. const std::set<char> &ignore)
  490. {
  491. std::vector<DirListNode> content = GetDirListing(path);
  492. for (const auto &n : content) {
  493. std::string fullpath = path + DIR_DELIM + n.name;
  494. if (ignore.count(n.name[0]))
  495. continue;
  496. if (list_files || n.dir)
  497. dst.push_back(fullpath);
  498. if (n.dir)
  499. GetRecursiveSubPaths(fullpath, dst, list_files, ignore);
  500. }
  501. }
  502. bool RecursiveDeleteContent(const std::string &path)
  503. {
  504. infostream<<"Removing content of \""<<path<<"\""<<std::endl;
  505. std::vector<DirListNode> list = GetDirListing(path);
  506. for (const DirListNode &dln : list) {
  507. if(trim(dln.name) == "." || trim(dln.name) == "..")
  508. continue;
  509. std::string childpath = path + DIR_DELIM + dln.name;
  510. bool r = RecursiveDelete(childpath);
  511. if(!r) {
  512. errorstream << "Removing \"" << childpath << "\" failed" << std::endl;
  513. return false;
  514. }
  515. }
  516. return true;
  517. }
  518. bool CreateAllDirs(const std::string &path)
  519. {
  520. std::vector<std::string> tocreate;
  521. std::string basepath = path;
  522. while(!PathExists(basepath))
  523. {
  524. tocreate.push_back(basepath);
  525. basepath = RemoveLastPathComponent(basepath);
  526. if(basepath.empty())
  527. break;
  528. }
  529. for(int i=tocreate.size()-1;i>=0;i--)
  530. if(!CreateDir(tocreate[i]))
  531. return false;
  532. return true;
  533. }
  534. bool CopyDir(const std::string &source, const std::string &target)
  535. {
  536. if(PathExists(source)){
  537. if(!PathExists(target)){
  538. fs::CreateAllDirs(target);
  539. }
  540. bool retval = true;
  541. std::vector<DirListNode> content = fs::GetDirListing(source);
  542. for (const auto &dln : content) {
  543. std::string sourcechild = source + DIR_DELIM + dln.name;
  544. std::string targetchild = target + DIR_DELIM + dln.name;
  545. if(dln.dir){
  546. if(!fs::CopyDir(sourcechild, targetchild)){
  547. retval = false;
  548. }
  549. }
  550. else {
  551. if(!fs::CopyFileContents(sourcechild, targetchild)){
  552. retval = false;
  553. }
  554. }
  555. }
  556. return retval;
  557. }
  558. return false;
  559. }
  560. bool MoveDir(const std::string &source, const std::string &target)
  561. {
  562. infostream << "Moving \"" << source << "\" to \"" << target << "\"" << std::endl;
  563. // If target exists as empty folder delete, otherwise error
  564. if (fs::PathExists(target)) {
  565. if (rmdir(target.c_str()) != 0) {
  566. errorstream << "MoveDir: target \"" << target
  567. << "\" exists as file or non-empty folder" << std::endl;
  568. return false;
  569. }
  570. }
  571. // Try renaming first which is instant
  572. if (fs::Rename(source, target))
  573. return true;
  574. infostream << "MoveDir: rename not possible, will copy instead" << std::endl;
  575. bool retval = fs::CopyDir(source, target);
  576. if (retval)
  577. retval &= fs::RecursiveDelete(source);
  578. return retval;
  579. }
  580. bool PathStartsWith(const std::string &path, const std::string &prefix)
  581. {
  582. size_t pathsize = path.size();
  583. size_t pathpos = 0;
  584. size_t prefixsize = prefix.size();
  585. size_t prefixpos = 0;
  586. for(;;){
  587. bool delim1 = pathpos == pathsize
  588. || IsDirDelimiter(path[pathpos]);
  589. bool delim2 = prefixpos == prefixsize
  590. || IsDirDelimiter(prefix[prefixpos]);
  591. if(delim1 != delim2)
  592. return false;
  593. if(delim1){
  594. while(pathpos < pathsize &&
  595. IsDirDelimiter(path[pathpos]))
  596. ++pathpos;
  597. while(prefixpos < prefixsize &&
  598. IsDirDelimiter(prefix[prefixpos]))
  599. ++prefixpos;
  600. if(prefixpos == prefixsize)
  601. return true;
  602. if(pathpos == pathsize)
  603. return false;
  604. }
  605. else{
  606. size_t len = 0;
  607. do{
  608. char pathchar = path[pathpos+len];
  609. char prefixchar = prefix[prefixpos+len];
  610. if(FILESYS_CASE_INSENSITIVE){
  611. pathchar = tolower(pathchar);
  612. prefixchar = tolower(prefixchar);
  613. }
  614. if(pathchar != prefixchar)
  615. return false;
  616. ++len;
  617. } while(pathpos+len < pathsize
  618. && !IsDirDelimiter(path[pathpos+len])
  619. && prefixpos+len < prefixsize
  620. && !IsDirDelimiter(
  621. prefix[prefixpos+len]));
  622. pathpos += len;
  623. prefixpos += len;
  624. }
  625. }
  626. }
  627. std::string RemoveLastPathComponent(const std::string &path,
  628. std::string *removed, int count)
  629. {
  630. if(removed)
  631. removed->clear();
  632. size_t remaining = path.size();
  633. for(int i = 0; i < count; ++i){
  634. // strip a dir delimiter
  635. while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
  636. remaining--;
  637. // strip a path component
  638. size_t component_end = remaining;
  639. while(remaining != 0 && !IsDirDelimiter(path[remaining-1]))
  640. remaining--;
  641. size_t component_start = remaining;
  642. // strip a dir delimiter
  643. while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
  644. remaining--;
  645. if(removed){
  646. std::string component = path.substr(component_start,
  647. component_end - component_start);
  648. if(i)
  649. *removed = component + DIR_DELIM + *removed;
  650. else
  651. *removed = component;
  652. }
  653. }
  654. return path.substr(0, remaining);
  655. }
  656. std::string RemoveRelativePathComponents(std::string path)
  657. {
  658. size_t pos = path.size();
  659. size_t dotdot_count = 0;
  660. while (pos != 0) {
  661. size_t component_with_delim_end = pos;
  662. // skip a dir delimiter
  663. while (pos != 0 && IsDirDelimiter(path[pos-1]))
  664. pos--;
  665. // strip a path component
  666. size_t component_end = pos;
  667. while (pos != 0 && !IsDirDelimiter(path[pos-1]))
  668. pos--;
  669. size_t component_start = pos;
  670. std::string component = path.substr(component_start,
  671. component_end - component_start);
  672. bool remove_this_component = false;
  673. if (component == ".") {
  674. remove_this_component = true;
  675. } else if (component == "..") {
  676. remove_this_component = true;
  677. dotdot_count += 1;
  678. } else if (dotdot_count != 0) {
  679. remove_this_component = true;
  680. dotdot_count -= 1;
  681. }
  682. if (remove_this_component) {
  683. while (pos != 0 && IsDirDelimiter(path[pos-1]))
  684. pos--;
  685. if (component_start == 0) {
  686. // We need to remove the delemiter too
  687. path = path.substr(component_with_delim_end, std::string::npos);
  688. } else {
  689. path = path.substr(0, pos) + DIR_DELIM +
  690. path.substr(component_with_delim_end, std::string::npos);
  691. }
  692. if (pos > 0)
  693. pos++;
  694. }
  695. }
  696. if (dotdot_count > 0)
  697. return "";
  698. // remove trailing dir delimiters
  699. pos = path.size();
  700. while (pos != 0 && IsDirDelimiter(path[pos-1]))
  701. pos--;
  702. return path.substr(0, pos);
  703. }
  704. std::string AbsolutePath(const std::string &path)
  705. {
  706. #ifdef _WIN32
  707. char *abs_path = _fullpath(NULL, path.c_str(), MAX_PATH);
  708. #else
  709. char *abs_path = realpath(path.c_str(), NULL);
  710. #endif
  711. if (!abs_path) return "";
  712. std::string abs_path_str(abs_path);
  713. free(abs_path);
  714. return abs_path_str;
  715. }
  716. const char *GetFilenameFromPath(const char *path)
  717. {
  718. const char *filename = strrchr(path, DIR_DELIM_CHAR);
  719. // Consistent with IsDirDelimiter this function handles '/' too
  720. if (DIR_DELIM_CHAR != '/') {
  721. const char *tmp = strrchr(path, '/');
  722. if (tmp && tmp > filename)
  723. filename = tmp;
  724. }
  725. return filename ? filename + 1 : path;
  726. }
  727. bool safeWriteToFile(const std::string &path, std::string_view content)
  728. {
  729. std::string tmp_file = path + ".~mt";
  730. // Write to a tmp file
  731. bool tmp_success = false;
  732. #ifdef _WIN32
  733. // We've observed behavior suggesting that the MSVC implementation of std::ofstream::flush doesn't
  734. // actually flush, so we use win32 APIs.
  735. HANDLE tmp_handle = CreateFile(
  736. tmp_file.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
  737. if (tmp_handle == INVALID_HANDLE_VALUE) {
  738. return false;
  739. }
  740. DWORD bytes_written;
  741. tmp_success = (WriteFile(tmp_handle, content.data(), content.size(), &bytes_written, nullptr) &&
  742. FlushFileBuffers(tmp_handle));
  743. CloseHandle(tmp_handle);
  744. #else
  745. std::ofstream os(tmp_file.c_str(), std::ios::binary);
  746. if (!os.good()) {
  747. return false;
  748. }
  749. os << content;
  750. os.flush();
  751. os.close();
  752. tmp_success = !os.fail();
  753. #endif
  754. if (!tmp_success) {
  755. remove(tmp_file.c_str());
  756. return false;
  757. }
  758. bool rename_success = false;
  759. // Move the finished temporary file over the real file
  760. #ifdef _WIN32
  761. // When creating the file, it can cause Windows Search indexer, virus scanners and other apps
  762. // to query the file. This can make the move file call below fail.
  763. // We retry up to 5 times, with a 1ms sleep between, before we consider the whole operation failed
  764. for (int attempt = 0; attempt < 5; attempt++) {
  765. rename_success = MoveFileEx(tmp_file.c_str(), path.c_str(),
  766. MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
  767. if (rename_success)
  768. break;
  769. sleep_ms(1);
  770. }
  771. #else
  772. // On POSIX compliant systems rename() is specified to be able to swap the
  773. // file in place of the destination file, making this a truly error-proof
  774. // transaction.
  775. rename_success = rename(tmp_file.c_str(), path.c_str()) == 0;
  776. #endif
  777. if (!rename_success) {
  778. warningstream << "Failed to write to file: " << path.c_str() << std::endl;
  779. // Remove the temporary file because moving it over the target file
  780. // failed.
  781. remove(tmp_file.c_str());
  782. return false;
  783. }
  784. return true;
  785. }
  786. #ifndef SERVER
  787. bool extractZipFile(io::IFileSystem *fs, const char *filename, const std::string &destination)
  788. {
  789. // Be careful here not to touch the global file hierarchy in Irrlicht
  790. // since this function needs to be thread-safe!
  791. io::IArchiveLoader *zip_loader = nullptr;
  792. for (u32 i = 0; i < fs->getArchiveLoaderCount(); i++) {
  793. if (fs->getArchiveLoader(i)->isALoadableFileFormat(io::EFAT_ZIP)) {
  794. zip_loader = fs->getArchiveLoader(i);
  795. break;
  796. }
  797. }
  798. if (!zip_loader) {
  799. warningstream << "fs::extractZipFile(): Irrlicht said it doesn't support ZIPs." << std::endl;
  800. return false;
  801. }
  802. irr_ptr<io::IFileArchive> opened_zip(zip_loader->createArchive(filename, false, false));
  803. const io::IFileList* files_in_zip = opened_zip->getFileList();
  804. for (u32 i = 0; i < files_in_zip->getFileCount(); i++) {
  805. std::string fullpath = destination + DIR_DELIM;
  806. fullpath += files_in_zip->getFullFileName(i).c_str();
  807. std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
  808. if (files_in_zip->isDirectory(i))
  809. continue; // ignore, we create dirs as necessary
  810. if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir))
  811. return false;
  812. irr_ptr<io::IReadFile> toread(opened_zip->createAndOpenFile(i));
  813. std::ofstream os(fullpath.c_str(), std::ios::binary);
  814. if (!os.good())
  815. return false;
  816. char buffer[4096];
  817. long total_read = 0;
  818. while (total_read < toread->getSize()) {
  819. long bytes_read = toread->read(buffer, sizeof(buffer));
  820. bool error = true;
  821. if (bytes_read != 0) {
  822. os.write(buffer, bytes_read);
  823. error = os.fail();
  824. }
  825. if (error) {
  826. os.close();
  827. remove(fullpath.c_str());
  828. return false;
  829. }
  830. total_read += bytes_read;
  831. }
  832. }
  833. return true;
  834. }
  835. #endif
  836. bool ReadFile(const std::string &path, std::string &out)
  837. {
  838. std::ifstream is(path, std::ios::binary | std::ios::ate);
  839. if (!is.good()) {
  840. return false;
  841. }
  842. auto size = is.tellg();
  843. out.resize(size);
  844. is.seekg(0);
  845. is.read(&out[0], size);
  846. return !is.fail();
  847. }
  848. bool Rename(const std::string &from, const std::string &to)
  849. {
  850. return rename(from.c_str(), to.c_str()) == 0;
  851. }
  852. } // namespace fs