file_access_unix.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /**************************************************************************/
  2. /* file_access_unix.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "file_access_unix.h"
  31. #if defined(UNIX_ENABLED)
  32. #include "core/os/os.h"
  33. #include "core/string/print_string.h"
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <sys/stat.h>
  37. #include <sys/types.h>
  38. #include <unistd.h>
  39. void FileAccessUnix::check_errors() const {
  40. ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
  41. if (feof(f)) {
  42. last_error = ERR_FILE_EOF;
  43. }
  44. }
  45. Error FileAccessUnix::open_internal(const String &p_path, int p_mode_flags) {
  46. _close();
  47. path_src = p_path;
  48. path = fix_path(p_path);
  49. //printf("opening %s, %i\n", path.utf8().get_data(), Memory::get_static_mem_usage());
  50. ERR_FAIL_COND_V_MSG(f, ERR_ALREADY_IN_USE, "File is already in use.");
  51. const char *mode_string;
  52. if (p_mode_flags == READ) {
  53. mode_string = "rb";
  54. } else if (p_mode_flags == WRITE) {
  55. mode_string = "wb";
  56. } else if (p_mode_flags == READ_WRITE) {
  57. mode_string = "rb+";
  58. } else if (p_mode_flags == WRITE_READ) {
  59. mode_string = "wb+";
  60. } else {
  61. return ERR_INVALID_PARAMETER;
  62. }
  63. /* pretty much every implementation that uses fopen as primary
  64. backend (unix-compatible mostly) supports utf8 encoding */
  65. //printf("opening %s as %s\n", p_path.utf8().get_data(), path.utf8().get_data());
  66. struct stat st = {};
  67. int err = stat(path.utf8().get_data(), &st);
  68. if (!err) {
  69. switch (st.st_mode & S_IFMT) {
  70. case S_IFLNK:
  71. case S_IFREG:
  72. break;
  73. default:
  74. return ERR_FILE_CANT_OPEN;
  75. }
  76. }
  77. if (is_backup_save_enabled() && (p_mode_flags == WRITE)) {
  78. save_path = path;
  79. // Create a temporary file in the same directory as the target file.
  80. path = path + "-XXXXXX";
  81. CharString cs = path.utf8();
  82. int fd = mkstemp(cs.ptrw());
  83. if (fd == -1) {
  84. last_error = ERR_FILE_CANT_OPEN;
  85. return last_error;
  86. }
  87. fchmod(fd, 0666);
  88. path = String::utf8(cs.ptr());
  89. f = fdopen(fd, mode_string);
  90. if (f == nullptr) {
  91. // Delete temp file and close descriptor if open failed.
  92. ::unlink(cs.ptr());
  93. ::close(fd);
  94. last_error = ERR_FILE_CANT_OPEN;
  95. return last_error;
  96. }
  97. } else {
  98. f = fopen(path.utf8().get_data(), mode_string);
  99. }
  100. if (f == nullptr) {
  101. switch (errno) {
  102. case ENOENT: {
  103. last_error = ERR_FILE_NOT_FOUND;
  104. } break;
  105. default: {
  106. last_error = ERR_FILE_CANT_OPEN;
  107. } break;
  108. }
  109. return last_error;
  110. }
  111. // Set close on exec to avoid leaking it to subprocesses.
  112. int fd = fileno(f);
  113. if (fd != -1) {
  114. int opts = fcntl(fd, F_GETFD);
  115. fcntl(fd, F_SETFD, opts | FD_CLOEXEC);
  116. }
  117. last_error = OK;
  118. flags = p_mode_flags;
  119. return OK;
  120. }
  121. void FileAccessUnix::_close() {
  122. if (!f) {
  123. return;
  124. }
  125. fclose(f);
  126. f = nullptr;
  127. if (close_notification_func) {
  128. close_notification_func(path, flags);
  129. }
  130. if (!save_path.is_empty()) {
  131. int rename_error = rename(path.utf8().get_data(), save_path.utf8().get_data());
  132. if (rename_error && close_fail_notify) {
  133. close_fail_notify(save_path);
  134. }
  135. save_path = "";
  136. ERR_FAIL_COND(rename_error != 0);
  137. }
  138. }
  139. bool FileAccessUnix::is_open() const {
  140. return (f != nullptr);
  141. }
  142. String FileAccessUnix::get_path() const {
  143. return path_src;
  144. }
  145. String FileAccessUnix::get_path_absolute() const {
  146. return path;
  147. }
  148. void FileAccessUnix::seek(uint64_t p_position) {
  149. ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
  150. last_error = OK;
  151. if (fseeko(f, p_position, SEEK_SET)) {
  152. check_errors();
  153. }
  154. }
  155. void FileAccessUnix::seek_end(int64_t p_position) {
  156. ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
  157. if (fseeko(f, p_position, SEEK_END)) {
  158. check_errors();
  159. }
  160. }
  161. uint64_t FileAccessUnix::get_position() const {
  162. ERR_FAIL_NULL_V_MSG(f, 0, "File must be opened before use.");
  163. int64_t pos = ftello(f);
  164. if (pos < 0) {
  165. check_errors();
  166. ERR_FAIL_V(0);
  167. }
  168. return pos;
  169. }
  170. uint64_t FileAccessUnix::get_length() const {
  171. ERR_FAIL_NULL_V_MSG(f, 0, "File must be opened before use.");
  172. int64_t pos = ftello(f);
  173. ERR_FAIL_COND_V(pos < 0, 0);
  174. ERR_FAIL_COND_V(fseeko(f, 0, SEEK_END), 0);
  175. int64_t size = ftello(f);
  176. ERR_FAIL_COND_V(size < 0, 0);
  177. ERR_FAIL_COND_V(fseeko(f, pos, SEEK_SET), 0);
  178. return size;
  179. }
  180. bool FileAccessUnix::eof_reached() const {
  181. return last_error == ERR_FILE_EOF;
  182. }
  183. uint64_t FileAccessUnix::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  184. ERR_FAIL_NULL_V_MSG(f, -1, "File must be opened before use.");
  185. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  186. uint64_t read = fread(p_dst, 1, p_length, f);
  187. check_errors();
  188. return read;
  189. }
  190. Error FileAccessUnix::get_error() const {
  191. return last_error;
  192. }
  193. Error FileAccessUnix::resize(int64_t p_length) {
  194. ERR_FAIL_NULL_V_MSG(f, FAILED, "File must be opened before use.");
  195. int res = ::ftruncate(fileno(f), p_length);
  196. switch (res) {
  197. case 0:
  198. return OK;
  199. case EBADF:
  200. return ERR_FILE_CANT_OPEN;
  201. case EFBIG:
  202. return ERR_OUT_OF_MEMORY;
  203. case EINVAL:
  204. return ERR_INVALID_PARAMETER;
  205. default:
  206. return FAILED;
  207. }
  208. }
  209. void FileAccessUnix::flush() {
  210. ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
  211. fflush(f);
  212. }
  213. void FileAccessUnix::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  214. ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
  215. ERR_FAIL_COND(!p_src && p_length > 0);
  216. ERR_FAIL_COND(fwrite(p_src, 1, p_length, f) != p_length);
  217. }
  218. bool FileAccessUnix::file_exists(const String &p_path) {
  219. int err;
  220. struct stat st = {};
  221. String filename = fix_path(p_path);
  222. // Does the name exist at all?
  223. err = stat(filename.utf8().get_data(), &st);
  224. if (err) {
  225. return false;
  226. }
  227. // See if we have access to the file
  228. if (access(filename.utf8().get_data(), F_OK)) {
  229. return false;
  230. }
  231. // See if this is a regular file
  232. switch (st.st_mode & S_IFMT) {
  233. case S_IFLNK:
  234. case S_IFREG:
  235. return true;
  236. default:
  237. return false;
  238. }
  239. }
  240. uint64_t FileAccessUnix::_get_modified_time(const String &p_file) {
  241. String file = fix_path(p_file);
  242. struct stat status = {};
  243. int err = stat(file.utf8().get_data(), &status);
  244. if (!err) {
  245. return status.st_mtime;
  246. } else {
  247. WARN_PRINT("Failed to get modified time for: " + p_file);
  248. return 0;
  249. }
  250. }
  251. BitField<FileAccess::UnixPermissionFlags> FileAccessUnix::_get_unix_permissions(const String &p_file) {
  252. String file = fix_path(p_file);
  253. struct stat status = {};
  254. int err = stat(file.utf8().get_data(), &status);
  255. if (!err) {
  256. return status.st_mode & 0xFFF; //only permissions
  257. } else {
  258. ERR_FAIL_V_MSG(0, "Failed to get unix permissions for: " + p_file + ".");
  259. }
  260. }
  261. Error FileAccessUnix::_set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) {
  262. String file = fix_path(p_file);
  263. int err = chmod(file.utf8().get_data(), p_permissions);
  264. if (!err) {
  265. return OK;
  266. }
  267. return FAILED;
  268. }
  269. bool FileAccessUnix::_get_hidden_attribute(const String &p_file) {
  270. #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  271. String file = fix_path(p_file);
  272. struct stat st = {};
  273. int err = stat(file.utf8().get_data(), &st);
  274. ERR_FAIL_COND_V_MSG(err, false, "Failed to get attributes for: " + p_file);
  275. return (st.st_flags & UF_HIDDEN);
  276. #else
  277. return false;
  278. #endif
  279. }
  280. Error FileAccessUnix::_set_hidden_attribute(const String &p_file, bool p_hidden) {
  281. #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  282. String file = fix_path(p_file);
  283. struct stat st = {};
  284. int err = stat(file.utf8().get_data(), &st);
  285. ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to get attributes for: " + p_file);
  286. if (p_hidden) {
  287. err = chflags(file.utf8().get_data(), st.st_flags | UF_HIDDEN);
  288. } else {
  289. err = chflags(file.utf8().get_data(), st.st_flags & ~UF_HIDDEN);
  290. }
  291. ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to set attributes for: " + p_file);
  292. return OK;
  293. #else
  294. return ERR_UNAVAILABLE;
  295. #endif
  296. }
  297. bool FileAccessUnix::_get_read_only_attribute(const String &p_file) {
  298. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  299. String file = fix_path(p_file);
  300. struct stat st = {};
  301. int err = stat(file.utf8().get_data(), &st);
  302. ERR_FAIL_COND_V_MSG(err, false, "Failed to get attributes for: " + p_file);
  303. return st.st_flags & UF_IMMUTABLE;
  304. #else
  305. return false;
  306. #endif
  307. }
  308. Error FileAccessUnix::_set_read_only_attribute(const String &p_file, bool p_ro) {
  309. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  310. String file = fix_path(p_file);
  311. struct stat st = {};
  312. int err = stat(file.utf8().get_data(), &st);
  313. ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to get attributes for: " + p_file);
  314. if (p_ro) {
  315. err = chflags(file.utf8().get_data(), st.st_flags | UF_IMMUTABLE);
  316. } else {
  317. err = chflags(file.utf8().get_data(), st.st_flags & ~UF_IMMUTABLE);
  318. }
  319. ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to set attributes for: " + p_file);
  320. return OK;
  321. #else
  322. return ERR_UNAVAILABLE;
  323. #endif
  324. }
  325. void FileAccessUnix::close() {
  326. _close();
  327. }
  328. CloseNotificationFunc FileAccessUnix::close_notification_func = nullptr;
  329. FileAccessUnix::~FileAccessUnix() {
  330. _close();
  331. }
  332. #endif // UNIX_ENABLED