file_access.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*************************************************************************/
  2. /* file_access.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/crypto/crypto_core.h"
  33. #include "core/io/file_access_pack.h"
  34. #include "core/io/marshalls.h"
  35. #include "core/os/os.h"
  36. FileAccess::CreateFunc FileAccess::create_func[ACCESS_MAX] = { nullptr, nullptr };
  37. FileAccess::FileCloseFailNotify FileAccess::close_fail_notify = nullptr;
  38. bool FileAccess::backup_save = false;
  39. FileAccess *FileAccess::create(AccessType p_access) {
  40. ERR_FAIL_INDEX_V(p_access, ACCESS_MAX, nullptr);
  41. FileAccess *ret = create_func[p_access]();
  42. ret->_set_access_type(p_access);
  43. return ret;
  44. }
  45. bool FileAccess::exists(const String &p_name) {
  46. if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_name)) {
  47. return true;
  48. }
  49. FileAccess *f = open(p_name, READ);
  50. if (!f) {
  51. return false;
  52. }
  53. memdelete(f);
  54. return true;
  55. }
  56. void FileAccess::_set_access_type(AccessType p_access) {
  57. _access_type = p_access;
  58. }
  59. FileAccess *FileAccess::create_for_path(const String &p_path) {
  60. FileAccess *ret = nullptr;
  61. if (p_path.begins_with("res://")) {
  62. ret = create(ACCESS_RESOURCES);
  63. } else if (p_path.begins_with("user://")) {
  64. ret = create(ACCESS_USERDATA);
  65. } else {
  66. ret = create(ACCESS_FILESYSTEM);
  67. }
  68. return ret;
  69. }
  70. Error FileAccess::reopen(const String &p_path, int p_mode_flags) {
  71. return _open(p_path, p_mode_flags);
  72. }
  73. FileAccess *FileAccess::open(const String &p_path, int p_mode_flags, Error *r_error) {
  74. //try packed data first
  75. FileAccess *ret = nullptr;
  76. if (!(p_mode_flags & WRITE) && PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled()) {
  77. ret = PackedData::get_singleton()->try_open_path(p_path);
  78. if (ret) {
  79. if (r_error) {
  80. *r_error = OK;
  81. }
  82. return ret;
  83. }
  84. }
  85. ret = create_for_path(p_path);
  86. Error err = ret->_open(p_path, p_mode_flags);
  87. if (r_error) {
  88. *r_error = err;
  89. }
  90. if (err != OK) {
  91. memdelete(ret);
  92. ret = nullptr;
  93. }
  94. return ret;
  95. }
  96. FileAccess::CreateFunc FileAccess::get_create_func(AccessType p_access) {
  97. return create_func[p_access];
  98. }
  99. String FileAccess::fix_path(const String &p_path) const {
  100. //helper used by file accesses that use a single filesystem
  101. String r_path = p_path.replace("\\", "/");
  102. switch (_access_type) {
  103. case ACCESS_RESOURCES: {
  104. if (ProjectSettings::get_singleton()) {
  105. if (r_path.begins_with("res://")) {
  106. String resource_path = ProjectSettings::get_singleton()->get_resource_path();
  107. if (!resource_path.is_empty()) {
  108. return r_path.replace("res:/", resource_path);
  109. }
  110. return r_path.replace("res://", "");
  111. }
  112. }
  113. } break;
  114. case ACCESS_USERDATA: {
  115. if (r_path.begins_with("user://")) {
  116. String data_dir = OS::get_singleton()->get_user_data_dir();
  117. if (!data_dir.is_empty()) {
  118. return r_path.replace("user:/", data_dir);
  119. }
  120. return r_path.replace("user://", "");
  121. }
  122. } break;
  123. case ACCESS_FILESYSTEM: {
  124. return r_path;
  125. } break;
  126. case ACCESS_MAX:
  127. break; // Can't happen, but silences warning
  128. }
  129. return r_path;
  130. }
  131. /* these are all implemented for ease of porting, then can later be optimized */
  132. uint16_t FileAccess::get_16() const {
  133. uint16_t res;
  134. uint8_t a, b;
  135. a = get_8();
  136. b = get_8();
  137. if (big_endian) {
  138. SWAP(a, b);
  139. }
  140. res = b;
  141. res <<= 8;
  142. res |= a;
  143. return res;
  144. }
  145. uint32_t FileAccess::get_32() const {
  146. uint32_t res;
  147. uint16_t a, b;
  148. a = get_16();
  149. b = get_16();
  150. if (big_endian) {
  151. SWAP(a, b);
  152. }
  153. res = b;
  154. res <<= 16;
  155. res |= a;
  156. return res;
  157. }
  158. uint64_t FileAccess::get_64() const {
  159. uint64_t res;
  160. uint32_t a, b;
  161. a = get_32();
  162. b = get_32();
  163. if (big_endian) {
  164. SWAP(a, b);
  165. }
  166. res = b;
  167. res <<= 32;
  168. res |= a;
  169. return res;
  170. }
  171. float FileAccess::get_float() const {
  172. MarshallFloat m;
  173. m.i = get_32();
  174. return m.f;
  175. }
  176. real_t FileAccess::get_real() const {
  177. if (real_is_double) {
  178. return get_double();
  179. } else {
  180. return get_float();
  181. }
  182. }
  183. double FileAccess::get_double() const {
  184. MarshallDouble m;
  185. m.l = get_64();
  186. return m.d;
  187. }
  188. String FileAccess::get_token() const {
  189. CharString token;
  190. char32_t c = get_8();
  191. while (!eof_reached()) {
  192. if (c <= ' ') {
  193. if (token.length()) {
  194. break;
  195. }
  196. } else {
  197. token += c;
  198. }
  199. c = get_8();
  200. }
  201. return String::utf8(token.get_data());
  202. }
  203. class CharBuffer {
  204. Vector<char> vector;
  205. char stack_buffer[256];
  206. char *buffer = nullptr;
  207. int capacity = 0;
  208. int written = 0;
  209. bool grow() {
  210. if (vector.resize(next_power_of_2(1 + written)) != OK) {
  211. return false;
  212. }
  213. if (buffer == stack_buffer) { // first chunk?
  214. for (int i = 0; i < written; i++) {
  215. vector.write[i] = stack_buffer[i];
  216. }
  217. }
  218. buffer = vector.ptrw();
  219. capacity = vector.size();
  220. ERR_FAIL_COND_V(written >= capacity, false);
  221. return true;
  222. }
  223. public:
  224. _FORCE_INLINE_ CharBuffer() :
  225. buffer(stack_buffer),
  226. capacity(sizeof(stack_buffer) / sizeof(char)) {
  227. }
  228. _FORCE_INLINE_ void push_back(char c) {
  229. if (written >= capacity) {
  230. ERR_FAIL_COND(!grow());
  231. }
  232. buffer[written++] = c;
  233. }
  234. _FORCE_INLINE_ const char *get_data() const {
  235. return buffer;
  236. }
  237. };
  238. String FileAccess::get_line() const {
  239. CharBuffer line;
  240. char32_t c = get_8();
  241. while (!eof_reached()) {
  242. if (c == '\n' || c == '\0') {
  243. line.push_back(0);
  244. return String::utf8(line.get_data());
  245. } else if (c != '\r') {
  246. line.push_back(c);
  247. }
  248. c = get_8();
  249. }
  250. line.push_back(0);
  251. return String::utf8(line.get_data());
  252. }
  253. Vector<String> FileAccess::get_csv_line(const String &p_delim) const {
  254. ERR_FAIL_COND_V_MSG(p_delim.length() != 1, Vector<String>(), "Only single character delimiters are supported to parse CSV lines.");
  255. ERR_FAIL_COND_V_MSG(p_delim[0] == '"', Vector<String>(), "The double quotation mark character (\") is not supported as a delimiter for CSV lines.");
  256. String line;
  257. // CSV can support entries with line breaks as long as they are enclosed
  258. // in double quotes. So our "line" might be more than a single line in the
  259. // text file.
  260. int qc = 0;
  261. do {
  262. if (eof_reached()) {
  263. break;
  264. }
  265. line += get_line() + "\n";
  266. qc = 0;
  267. for (int i = 0; i < line.length(); i++) {
  268. if (line[i] == '"') {
  269. qc++;
  270. }
  271. }
  272. } while (qc % 2);
  273. // Remove the extraneous newline we've added above.
  274. line = line.substr(0, line.length() - 1);
  275. Vector<String> strings;
  276. bool in_quote = false;
  277. String current;
  278. for (int i = 0; i < line.length(); i++) {
  279. char32_t c = line[i];
  280. // A delimiter ends the current entry, unless it's in a quoted string.
  281. if (!in_quote && c == p_delim[0]) {
  282. strings.push_back(current);
  283. current = String();
  284. } else if (c == '"') {
  285. // Doubled quotes are escapes for intentional quotes in the string.
  286. if (line[i + 1] == '"' && in_quote) {
  287. current += '"';
  288. i++;
  289. } else {
  290. in_quote = !in_quote;
  291. }
  292. } else {
  293. current += c;
  294. }
  295. }
  296. strings.push_back(current);
  297. return strings;
  298. }
  299. uint64_t FileAccess::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  300. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  301. uint64_t i = 0;
  302. for (i = 0; i < p_length && !eof_reached(); i++) {
  303. p_dst[i] = get_8();
  304. }
  305. return i;
  306. }
  307. String FileAccess::get_as_utf8_string() const {
  308. Vector<uint8_t> sourcef;
  309. uint64_t len = get_length();
  310. sourcef.resize(len + 1);
  311. uint8_t *w = sourcef.ptrw();
  312. uint64_t r = get_buffer(w, len);
  313. ERR_FAIL_COND_V(r != len, String());
  314. w[len] = 0;
  315. String s;
  316. if (s.parse_utf8((const char *)w)) {
  317. return String();
  318. }
  319. return s;
  320. }
  321. void FileAccess::store_16(uint16_t p_dest) {
  322. uint8_t a, b;
  323. a = p_dest & 0xFF;
  324. b = p_dest >> 8;
  325. if (big_endian) {
  326. SWAP(a, b);
  327. }
  328. store_8(a);
  329. store_8(b);
  330. }
  331. void FileAccess::store_32(uint32_t p_dest) {
  332. uint16_t a, b;
  333. a = p_dest & 0xFFFF;
  334. b = p_dest >> 16;
  335. if (big_endian) {
  336. SWAP(a, b);
  337. }
  338. store_16(a);
  339. store_16(b);
  340. }
  341. void FileAccess::store_64(uint64_t p_dest) {
  342. uint32_t a, b;
  343. a = p_dest & 0xFFFFFFFF;
  344. b = p_dest >> 32;
  345. if (big_endian) {
  346. SWAP(a, b);
  347. }
  348. store_32(a);
  349. store_32(b);
  350. }
  351. void FileAccess::store_real(real_t p_real) {
  352. if (sizeof(real_t) == 4) {
  353. store_float(p_real);
  354. } else {
  355. store_double(p_real);
  356. }
  357. }
  358. void FileAccess::store_float(float p_dest) {
  359. MarshallFloat m;
  360. m.f = p_dest;
  361. store_32(m.i);
  362. }
  363. void FileAccess::store_double(double p_dest) {
  364. MarshallDouble m;
  365. m.d = p_dest;
  366. store_64(m.l);
  367. }
  368. uint64_t FileAccess::get_modified_time(const String &p_file) {
  369. if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) {
  370. return 0;
  371. }
  372. FileAccess *fa = create_for_path(p_file);
  373. ERR_FAIL_COND_V_MSG(!fa, 0, "Cannot create FileAccess for path '" + p_file + "'.");
  374. uint64_t mt = fa->_get_modified_time(p_file);
  375. memdelete(fa);
  376. return mt;
  377. }
  378. uint32_t FileAccess::get_unix_permissions(const String &p_file) {
  379. if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) {
  380. return 0;
  381. }
  382. FileAccess *fa = create_for_path(p_file);
  383. ERR_FAIL_COND_V_MSG(!fa, 0, "Cannot create FileAccess for path '" + p_file + "'.");
  384. uint32_t mt = fa->_get_unix_permissions(p_file);
  385. memdelete(fa);
  386. return mt;
  387. }
  388. Error FileAccess::set_unix_permissions(const String &p_file, uint32_t p_permissions) {
  389. if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) {
  390. return ERR_UNAVAILABLE;
  391. }
  392. FileAccess *fa = create_for_path(p_file);
  393. ERR_FAIL_COND_V_MSG(!fa, ERR_CANT_CREATE, "Cannot create FileAccess for path '" + p_file + "'.");
  394. Error err = fa->_set_unix_permissions(p_file, p_permissions);
  395. memdelete(fa);
  396. return err;
  397. }
  398. void FileAccess::store_string(const String &p_string) {
  399. if (p_string.length() == 0) {
  400. return;
  401. }
  402. CharString cs = p_string.utf8();
  403. store_buffer((uint8_t *)&cs[0], cs.length());
  404. }
  405. void FileAccess::store_pascal_string(const String &p_string) {
  406. CharString cs = p_string.utf8();
  407. store_32(cs.length());
  408. store_buffer((uint8_t *)&cs[0], cs.length());
  409. }
  410. String FileAccess::get_pascal_string() {
  411. uint32_t sl = get_32();
  412. CharString cs;
  413. cs.resize(sl + 1);
  414. get_buffer((uint8_t *)cs.ptr(), sl);
  415. cs[sl] = 0;
  416. String ret;
  417. ret.parse_utf8(cs.ptr());
  418. return ret;
  419. }
  420. void FileAccess::store_line(const String &p_line) {
  421. store_string(p_line);
  422. store_8('\n');
  423. }
  424. void FileAccess::store_csv_line(const Vector<String> &p_values, const String &p_delim) {
  425. ERR_FAIL_COND(p_delim.length() != 1);
  426. String line = "";
  427. int size = p_values.size();
  428. for (int i = 0; i < size; ++i) {
  429. String value = p_values[i];
  430. if (value.contains("\"") || value.contains(p_delim) || value.contains("\n")) {
  431. value = "\"" + value.replace("\"", "\"\"") + "\"";
  432. }
  433. if (i < size - 1) {
  434. value += p_delim;
  435. }
  436. line += value;
  437. }
  438. store_line(line);
  439. }
  440. void FileAccess::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  441. ERR_FAIL_COND(!p_src && p_length > 0);
  442. for (uint64_t i = 0; i < p_length; i++) {
  443. store_8(p_src[i]);
  444. }
  445. }
  446. Vector<uint8_t> FileAccess::get_file_as_array(const String &p_path, Error *r_error) {
  447. FileAccess *f = FileAccess::open(p_path, READ, r_error);
  448. if (!f) {
  449. if (r_error) { // if error requested, do not throw error
  450. return Vector<uint8_t>();
  451. }
  452. ERR_FAIL_V_MSG(Vector<uint8_t>(), "Can't open file from path '" + String(p_path) + "'.");
  453. }
  454. Vector<uint8_t> data;
  455. data.resize(f->get_length());
  456. f->get_buffer(data.ptrw(), data.size());
  457. memdelete(f);
  458. return data;
  459. }
  460. String FileAccess::get_file_as_string(const String &p_path, Error *r_error) {
  461. Error err;
  462. Vector<uint8_t> array = get_file_as_array(p_path, &err);
  463. if (r_error) {
  464. *r_error = err;
  465. }
  466. if (err != OK) {
  467. if (r_error) {
  468. return String();
  469. }
  470. ERR_FAIL_V_MSG(String(), "Can't get file as string from path '" + String(p_path) + "'.");
  471. }
  472. String ret;
  473. ret.parse_utf8((const char *)array.ptr(), array.size());
  474. return ret;
  475. }
  476. String FileAccess::get_md5(const String &p_file) {
  477. FileAccess *f = FileAccess::open(p_file, READ);
  478. if (!f) {
  479. return String();
  480. }
  481. CryptoCore::MD5Context ctx;
  482. ctx.start();
  483. unsigned char step[32768];
  484. while (true) {
  485. uint64_t br = f->get_buffer(step, 32768);
  486. if (br > 0) {
  487. ctx.update(step, br);
  488. }
  489. if (br < 4096) {
  490. break;
  491. }
  492. }
  493. unsigned char hash[16];
  494. ctx.finish(hash);
  495. memdelete(f);
  496. return String::md5(hash);
  497. }
  498. String FileAccess::get_multiple_md5(const Vector<String> &p_file) {
  499. CryptoCore::MD5Context ctx;
  500. ctx.start();
  501. for (int i = 0; i < p_file.size(); i++) {
  502. FileAccess *f = FileAccess::open(p_file[i], READ);
  503. ERR_CONTINUE(!f);
  504. unsigned char step[32768];
  505. while (true) {
  506. uint64_t br = f->get_buffer(step, 32768);
  507. if (br > 0) {
  508. ctx.update(step, br);
  509. }
  510. if (br < 4096) {
  511. break;
  512. }
  513. }
  514. memdelete(f);
  515. }
  516. unsigned char hash[16];
  517. ctx.finish(hash);
  518. return String::md5(hash);
  519. }
  520. String FileAccess::get_sha256(const String &p_file) {
  521. FileAccess *f = FileAccess::open(p_file, READ);
  522. if (!f) {
  523. return String();
  524. }
  525. CryptoCore::SHA256Context ctx;
  526. ctx.start();
  527. unsigned char step[32768];
  528. while (true) {
  529. uint64_t br = f->get_buffer(step, 32768);
  530. if (br > 0) {
  531. ctx.update(step, br);
  532. }
  533. if (br < 4096) {
  534. break;
  535. }
  536. }
  537. unsigned char hash[32];
  538. ctx.finish(hash);
  539. memdelete(f);
  540. return String::hex_encode_buffer(hash, 32);
  541. }