Filesystem.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Copyright (c) 2006-2022 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "Filesystem.h"
  22. #include "common/utf8.h"
  23. // Assume POSIX or Visual Studio.
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #if defined(LOVE_MACOS) || defined(LOVE_IOS)
  27. #include "common/apple.h"
  28. #include <unistd.h>
  29. #elif defined(LOVE_WINDOWS)
  30. #include <windows.h>
  31. #include <fileapi.h>
  32. #include "common/utf8.h"
  33. #elif defined(LOVE_LINUX)
  34. #include <unistd.h>
  35. #endif
  36. namespace love
  37. {
  38. namespace filesystem
  39. {
  40. love::Type Filesystem::type("filesystem", &Module::type);
  41. Filesystem::Filesystem()
  42. {
  43. }
  44. Filesystem::~Filesystem()
  45. {
  46. }
  47. void Filesystem::setAndroidSaveExternal(bool useExternal)
  48. {
  49. this->useExternal = useExternal;
  50. }
  51. bool Filesystem::isAndroidSaveExternal() const
  52. {
  53. return useExternal;
  54. }
  55. FileData *Filesystem::newFileData(const void *data, size_t size, const char *filename) const
  56. {
  57. FileData *fd = new FileData(size, std::string(filename));
  58. memcpy(fd->getData(), data, size);
  59. return fd;
  60. }
  61. bool Filesystem::isRealDirectory(const std::string &path) const
  62. {
  63. FileType ftype = FILETYPE_MAX_ENUM;
  64. if (!getRealPathType(path, ftype))
  65. return false;
  66. return ftype == FILETYPE_DIRECTORY;
  67. }
  68. bool Filesystem::getRealPathType(const std::string &path, FileType &ftype) const
  69. {
  70. #ifdef LOVE_WINDOWS
  71. // make sure non-ASCII paths work.
  72. std::wstring wpath = to_widestr(path);
  73. struct _stat buf;
  74. if (_wstat(wpath.c_str(), &buf) != 0)
  75. return false;
  76. if ((buf.st_mode & _S_IFREG) == _S_IFREG)
  77. ftype = FILETYPE_FILE;
  78. else if ((buf.st_mode & _S_IFDIR) == _S_IFDIR)
  79. ftype = FILETYPE_DIRECTORY;
  80. else
  81. ftype = FILETYPE_OTHER;
  82. #else
  83. // Assume POSIX support...
  84. struct stat buf;
  85. if (stat(path.c_str(), &buf) != 0)
  86. return false;
  87. if (S_ISREG(buf.st_mode))
  88. ftype = FILETYPE_FILE;
  89. else if (S_ISDIR(buf.st_mode))
  90. ftype = FILETYPE_DIRECTORY;
  91. else if (S_ISLNK(buf.st_mode))
  92. ftype = FILETYPE_SYMLINK;
  93. else
  94. ftype = FILETYPE_OTHER;
  95. #endif
  96. return true;
  97. }
  98. static bool getContainingDirectory(const std::string &path, std::string &newpath)
  99. {
  100. size_t index = path.find_last_of("/\\");
  101. if (index == std::string::npos)
  102. return false;
  103. newpath = path.substr(0, index);
  104. // Bail if the root has been stripped out.
  105. return newpath.find_first_of("/\\") != std::string::npos;
  106. }
  107. static bool createDirectoryRaw(const std::string &path)
  108. {
  109. #ifdef LOVE_WINDOWS
  110. std::wstring wpath = to_widestr(path);
  111. return CreateDirectoryW(wpath.c_str(), nullptr) != 0;
  112. #else
  113. return mkdir(path.c_str(), S_IRWXU) == 0;
  114. #endif
  115. }
  116. bool Filesystem::createRealDirectory(const std::string &path)
  117. {
  118. FileType ftype = FILETYPE_MAX_ENUM;
  119. if (getRealPathType(path, ftype))
  120. return ftype == FILETYPE_DIRECTORY;
  121. std::vector<std::string> createpaths = {path};
  122. // Find the deepest subdirectory in the given path that actually exists.
  123. while (true)
  124. {
  125. std::string subpath;
  126. if (!getContainingDirectory(createpaths[0], subpath))
  127. break;
  128. if (isRealDirectory(subpath))
  129. break;
  130. createpaths.insert(createpaths.begin(), subpath);
  131. }
  132. // Try to create missing subdirectories starting from that existing one.
  133. for (const std::string &p : createpaths)
  134. {
  135. if (!createDirectoryRaw(p))
  136. return false;
  137. }
  138. return true;
  139. }
  140. std::string Filesystem::getExecutablePath() const
  141. {
  142. #if defined(LOVE_MACOS) || defined(LOVE_IOS)
  143. return love::apple::getExecutablePath();
  144. #elif defined(LOVE_WINDOWS)
  145. wchar_t buffer[MAX_PATH + 1] = {0};
  146. if (GetModuleFileNameW(nullptr, buffer, MAX_PATH) == 0)
  147. return "";
  148. return to_utf8(buffer);
  149. #elif defined(LOVE_LINUX)
  150. char buffer[2048] = {0};
  151. ssize_t len = readlink("/proc/self/exe", buffer, 2048);
  152. if (len <= 0)
  153. return "";
  154. return std::string(buffer, len);
  155. #else
  156. #error Missing implementation for Filesystem::getExecutablePath!
  157. #endif
  158. }
  159. STRINGMAP_CLASS_BEGIN(Filesystem, Filesystem::FileType, Filesystem::FILETYPE_MAX_ENUM, fileType)
  160. {
  161. { "file", Filesystem::FILETYPE_FILE },
  162. { "directory", Filesystem::FILETYPE_DIRECTORY },
  163. { "symlink", Filesystem::FILETYPE_SYMLINK },
  164. { "other", Filesystem::FILETYPE_OTHER },
  165. }
  166. STRINGMAP_CLASS_END(Filesystem, Filesystem::FileType, Filesystem::FILETYPE_MAX_ENUM, fileType)
  167. STRINGMAP_CLASS_BEGIN(Filesystem, Filesystem::CommonPath, Filesystem::COMMONPATH_MAX_ENUM, commonPath)
  168. {
  169. { "appsavedir", Filesystem::COMMONPATH_APP_SAVEDIR },
  170. { "appdocuments", Filesystem::COMMONPATH_APP_DOCUMENTS },
  171. { "userhome", Filesystem::COMMONPATH_USER_HOME },
  172. { "userappdata", Filesystem::COMMONPATH_USER_APPDATA },
  173. { "userdesktop", Filesystem::COMMONPATH_USER_DESKTOP },
  174. { "userdocuments", Filesystem::COMMONPATH_USER_DOCUMENTS },
  175. }
  176. STRINGMAP_CLASS_END(Filesystem, Filesystem::CommonPath, Filesystem::COMMONPATH_MAX_ENUM, commonPath)
  177. STRINGMAP_CLASS_BEGIN(Filesystem, Filesystem::MountPermissions, Filesystem::MOUNT_PERMISSIONS_MAX_ENUM, mountPermissions)
  178. {
  179. { "read", Filesystem::MOUNT_PERMISSIONS_READ },
  180. { "readwrite", Filesystem::MOUNT_PERMISSIONS_READWRITE },
  181. }
  182. STRINGMAP_CLASS_END(Filesystem, Filesystem::MountPermissions, Filesystem::MOUNT_PERMISSIONS_MAX_ENUM, mountPermissions)
  183. STRINGMAP_CLASS_BEGIN(Filesystem, Filesystem::LoadMode, Filesystem::LOADMODE_MAX_ENUM, loadMode)
  184. {
  185. { "b", Filesystem::LOADMODE_BINARY},
  186. { "t", Filesystem::LOADMODE_TEXT },
  187. { "bt", Filesystem::LOADMODE_ANY }
  188. }
  189. STRINGMAP_CLASS_END(Filesystem, Filesystem::LoadMode, Filesystem::LOADMODE_MAX_ENUM, loadMode)
  190. } // filesystem
  191. } // love