file_access.cpp 15 KB

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