File.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * Copyright (c) 2006-2024 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. #include "File.h"
  21. // STD
  22. #include <cstring>
  23. // LOVE
  24. #include "Filesystem.h"
  25. #include "filesystem/FileData.h"
  26. #ifdef LOVE_ANDROID
  27. #include "common/android.h"
  28. #endif
  29. namespace love
  30. {
  31. namespace filesystem
  32. {
  33. namespace physfs
  34. {
  35. static bool setupWriteDirectory()
  36. {
  37. auto fs = Module::getInstance<love::filesystem::Filesystem>(Module::M_FILESYSTEM);
  38. return fs != nullptr && fs->setupWriteDirectory();
  39. }
  40. File::File(const std::string &filename, Mode mode)
  41. : filename(filename)
  42. , file(nullptr)
  43. , mode(MODE_CLOSED)
  44. , bufferMode(BUFFER_NONE)
  45. , bufferSize(0)
  46. {
  47. if (!open(mode))
  48. throw love::Exception("Could not open file at path %s", filename.c_str());
  49. #ifdef LOVE_ANDROID
  50. // In Android with t.externalstorage = true, make sure the file opened or
  51. // created in the save directory has permissions of ug+rw (0660) so that
  52. // it's accessible through MTP.
  53. auto fs = Module::getInstance<love::filesystem::Filesystem>(Module::M_FILESYSTEM);
  54. if (fs != nullptr && fs->isAndroidSaveExternal())
  55. {
  56. const char *realdir = PHYSFS_getRealDir(filename.c_str());
  57. const std::string &savedir = fs->getFullCommonPath(Filesystem::COMMONPATH_APP_SAVEDIR);
  58. if (realdir != nullptr && strcmp(realdir, savedir.c_str()) == 0)
  59. love::android::fixupPermissionSingleFile(savedir, filename);
  60. #endif
  61. }
  62. }
  63. File::File(const File &other)
  64. : filename(other.filename)
  65. , file(nullptr)
  66. , mode(MODE_CLOSED)
  67. , bufferMode(other.bufferMode)
  68. , bufferSize(other.bufferSize)
  69. {
  70. if (!open(other.mode))
  71. throw love::Exception("Could not open file at path %s", filename.c_str());
  72. }
  73. File::~File()
  74. {
  75. if (mode != MODE_CLOSED)
  76. close();
  77. }
  78. File *File::clone()
  79. {
  80. return new File(*this);
  81. }
  82. bool File::open(Mode mode)
  83. {
  84. if (mode == MODE_CLOSED)
  85. {
  86. close();
  87. return true;
  88. }
  89. if (!PHYSFS_isInit())
  90. throw love::Exception("PhysFS is not initialized.");
  91. // File must exist if read mode.
  92. if ((mode == MODE_READ) && !PHYSFS_exists(filename.c_str()))
  93. throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
  94. // Check whether the write directory is set.
  95. if ((mode == MODE_APPEND || mode == MODE_WRITE) && !setupWriteDirectory())
  96. throw love::Exception("Could not set write directory.");
  97. // File already open?
  98. if (file != nullptr)
  99. return false;
  100. PHYSFS_File *handle = nullptr;
  101. switch (mode)
  102. {
  103. case MODE_READ:
  104. handle = PHYSFS_openRead(filename.c_str());
  105. break;
  106. case MODE_APPEND:
  107. handle = PHYSFS_openAppend(filename.c_str());
  108. break;
  109. case MODE_WRITE:
  110. handle = PHYSFS_openWrite(filename.c_str());
  111. break;
  112. default:
  113. break;
  114. }
  115. if (handle == nullptr)
  116. {
  117. const char *err = PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode());
  118. if (err == nullptr)
  119. err = "unknown error";
  120. throw love::Exception("Could not open file %s (%s)", filename.c_str(), err);
  121. }
  122. file = handle;
  123. this->mode = mode;
  124. if (file != nullptr && !setBuffer(bufferMode, bufferSize))
  125. {
  126. // Revert to buffer defaults if we don't successfully set the buffer.
  127. bufferMode = BUFFER_NONE;
  128. bufferSize = 0;
  129. }
  130. return (file != nullptr);
  131. }
  132. bool File::close()
  133. {
  134. if (file == nullptr || !PHYSFS_close(file))
  135. return false;
  136. mode = MODE_CLOSED;
  137. file = nullptr;
  138. return true;
  139. }
  140. bool File::isOpen() const
  141. {
  142. return mode != MODE_CLOSED && file != nullptr;
  143. }
  144. int64 File::getSize()
  145. {
  146. // If the file is closed, open it to
  147. // check the size.
  148. if (file == nullptr)
  149. {
  150. open(MODE_READ);
  151. int64 size = (int64) PHYSFS_fileLength(file);
  152. close();
  153. return size;
  154. }
  155. return (int64) PHYSFS_fileLength(file);
  156. }
  157. int64 File::read(void *dst, int64 size)
  158. {
  159. if (!file || mode != MODE_READ)
  160. throw love::Exception("File is not opened for reading.");
  161. if (size < 0)
  162. throw love::Exception("Invalid read size.");
  163. return PHYSFS_readBytes(file, dst, (PHYSFS_uint64) size);
  164. }
  165. bool File::write(const void *data, int64 size)
  166. {
  167. if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
  168. throw love::Exception("File is not opened for writing.");
  169. if (size < 0)
  170. throw love::Exception("Invalid write size.");
  171. // Try to write.
  172. int64 written = PHYSFS_writeBytes(file, data, (PHYSFS_uint64) size);
  173. // Check that correct amount of data was written.
  174. if (written != size)
  175. return false;
  176. // Manually flush the buffer in BUFFER_LINE mode if we find a newline.
  177. if (bufferMode == BUFFER_LINE && bufferSize > size)
  178. {
  179. if (memchr(data, '\n', (size_t) size) != nullptr)
  180. flush();
  181. }
  182. return true;
  183. }
  184. bool File::flush()
  185. {
  186. if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
  187. throw love::Exception("File is not opened for writing.");
  188. return PHYSFS_flush(file) != 0;
  189. }
  190. bool File::isEOF()
  191. {
  192. return file == nullptr || PHYSFS_eof(file);
  193. }
  194. int64 File::tell()
  195. {
  196. if (file == nullptr)
  197. return -1;
  198. return (int64) PHYSFS_tell(file);
  199. }
  200. bool File::seek(int64 pos, SeekOrigin origin)
  201. {
  202. if (file != nullptr)
  203. {
  204. if (origin == SEEKORIGIN_CURRENT)
  205. pos += tell();
  206. else if (origin == SEEKORIGIN_END)
  207. pos += getSize();
  208. }
  209. if (pos < 0)
  210. return false;
  211. return file != nullptr && PHYSFS_seek(file, (PHYSFS_uint64) pos) != 0;
  212. }
  213. bool File::setBuffer(BufferMode bufmode, int64 size)
  214. {
  215. if (size < 0)
  216. return false;
  217. // If the file isn't open, we'll make sure the buffer values are set in
  218. // File::open.
  219. if (!isOpen())
  220. {
  221. bufferMode = bufmode;
  222. bufferSize = size;
  223. return true;
  224. }
  225. int ret = 1;
  226. switch (bufmode)
  227. {
  228. case BUFFER_NONE:
  229. default:
  230. ret = PHYSFS_setBuffer(file, 0);
  231. size = 0;
  232. break;
  233. case BUFFER_LINE:
  234. case BUFFER_FULL:
  235. ret = PHYSFS_setBuffer(file, size);
  236. break;
  237. }
  238. if (ret == 0)
  239. return false;
  240. bufferMode = bufmode;
  241. bufferSize = size;
  242. return true;
  243. }
  244. File::BufferMode File::getBuffer(int64 &size) const
  245. {
  246. size = bufferSize;
  247. return bufferMode;
  248. }
  249. const std::string &File::getFilename() const
  250. {
  251. return filename;
  252. }
  253. filesystem::File::Mode File::getMode() const
  254. {
  255. return mode;
  256. }
  257. } // physfs
  258. } // filesystem
  259. } // love