diff options
Diffstat (limited to 'drivers/unix/file_access_unix.cpp')
-rw-r--r-- | drivers/unix/file_access_unix.cpp | 105 |
1 files changed, 42 insertions, 63 deletions
diff --git a/drivers/unix/file_access_unix.cpp b/drivers/unix/file_access_unix.cpp index 91164dc3f9..6ea55219bb 100644 --- a/drivers/unix/file_access_unix.cpp +++ b/drivers/unix/file_access_unix.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,7 +33,7 @@ #if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED) #include "core/os/os.h" -#include "core/print_string.h" +#include "core/string/print_string.h" #include <sys/stat.h> #include <sys/types.h> @@ -63,38 +63,37 @@ #endif void FileAccessUnix::check_errors() const { - ERR_FAIL_COND_MSG(!f, "File must be opened before use."); if (feof(f)) { - last_error = ERR_FILE_EOF; } } Error FileAccessUnix::_open(const String &p_path, int p_mode_flags) { - - if (f) + if (f) { fclose(f); - f = NULL; + } + f = nullptr; path_src = p_path; path = fix_path(p_path); - //printf("opening %ls, %i\n", path.c_str(), Memory::get_static_mem_usage()); + //printf("opening %s, %i\n", path.utf8().get_data(), Memory::get_static_mem_usage()); ERR_FAIL_COND_V_MSG(f, ERR_ALREADY_IN_USE, "File is already in use."); const char *mode_string; - if (p_mode_flags == READ) + if (p_mode_flags == READ) { mode_string = "rb"; - else if (p_mode_flags == WRITE) + } else if (p_mode_flags == WRITE) { mode_string = "wb"; - else if (p_mode_flags == READ_WRITE) + } else if (p_mode_flags == READ_WRITE) { mode_string = "rb+"; - else if (p_mode_flags == WRITE_READ) + } else if (p_mode_flags == WRITE_READ) { mode_string = "wb+"; - else + } else { return ERR_INVALID_PARAMETER; + } /* pretty much every implementation that uses fopen as primary backend (unix-compatible mostly) supports utf8 encoding */ @@ -119,7 +118,7 @@ Error FileAccessUnix::_open(const String &p_path, int p_mode_flags) { f = fopen(path.utf8().get_data(), mode_string); - if (f == NULL) { + if (f == nullptr) { switch (errno) { case ENOENT: { last_error = ERR_FILE_NOT_FOUND; @@ -150,12 +149,12 @@ Error FileAccessUnix::_open(const String &p_path, int p_mode_flags) { } void FileAccessUnix::close() { - - if (!f) + if (!f) { return; + } fclose(f); - f = NULL; + f = nullptr; if (close_notification_func) { close_notification_func(path, flags); @@ -174,42 +173,38 @@ void FileAccessUnix::close() { } bool FileAccessUnix::is_open() const { - - return (f != NULL); + return (f != nullptr); } String FileAccessUnix::get_path() const { - return path_src; } String FileAccessUnix::get_path_absolute() const { - return path; } -void FileAccessUnix::seek(size_t p_position) { - +void FileAccessUnix::seek(uint64_t p_position) { ERR_FAIL_COND_MSG(!f, "File must be opened before use."); last_error = OK; - if (fseek(f, p_position, SEEK_SET)) + if (fseeko(f, p_position, SEEK_SET)) { check_errors(); + } } void FileAccessUnix::seek_end(int64_t p_position) { - ERR_FAIL_COND_MSG(!f, "File must be opened before use."); - if (fseek(f, p_position, SEEK_END)) + if (fseeko(f, p_position, SEEK_END)) { check_errors(); + } } -size_t FileAccessUnix::get_position() const { - +uint64_t FileAccessUnix::get_position() const { ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); - long pos = ftell(f); + int64_t pos = ftello(f); if (pos < 0) { check_errors(); ERR_FAIL_V(0); @@ -217,27 +212,24 @@ size_t FileAccessUnix::get_position() const { return pos; } -size_t FileAccessUnix::get_len() const { - +uint64_t FileAccessUnix::get_length() const { ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); - long pos = ftell(f); + int64_t pos = ftello(f); ERR_FAIL_COND_V(pos < 0, 0); - ERR_FAIL_COND_V(fseek(f, 0, SEEK_END), 0); - long size = ftell(f); + ERR_FAIL_COND_V(fseeko(f, 0, SEEK_END), 0); + int64_t size = ftello(f); ERR_FAIL_COND_V(size < 0, 0); - ERR_FAIL_COND_V(fseek(f, pos, SEEK_SET), 0); + ERR_FAIL_COND_V(fseeko(f, pos, SEEK_SET), 0); return size; } bool FileAccessUnix::eof_reached() const { - return last_error == ERR_FILE_EOF; } uint8_t FileAccessUnix::get_8() const { - ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); uint8_t b; if (fread(&b, 1, 1, f) == 0) { @@ -247,53 +239,51 @@ uint8_t FileAccessUnix::get_8() const { return b; } -int FileAccessUnix::get_buffer(uint8_t *p_dst, int p_length) const { - +uint64_t FileAccessUnix::get_buffer(uint8_t *p_dst, uint64_t p_length) const { + ERR_FAIL_COND_V(!p_dst && p_length > 0, -1); ERR_FAIL_COND_V_MSG(!f, -1, "File must be opened before use."); - int read = fread(p_dst, 1, p_length, f); + + uint64_t read = fread(p_dst, 1, p_length, f); check_errors(); return read; }; Error FileAccessUnix::get_error() const { - return last_error; } void FileAccessUnix::flush() { - ERR_FAIL_COND_MSG(!f, "File must be opened before use."); fflush(f); } void FileAccessUnix::store_8(uint8_t p_dest) { - ERR_FAIL_COND_MSG(!f, "File must be opened before use."); ERR_FAIL_COND(fwrite(&p_dest, 1, 1, f) != 1); } -void FileAccessUnix::store_buffer(const uint8_t *p_src, int p_length) { - +void FileAccessUnix::store_buffer(const uint8_t *p_src, uint64_t p_length) { ERR_FAIL_COND_MSG(!f, "File must be opened before use."); - ERR_FAIL_COND(!p_src); - ERR_FAIL_COND((int)fwrite(p_src, 1, p_length, f) != p_length); + ERR_FAIL_COND(!p_src && p_length > 0); + ERR_FAIL_COND(fwrite(p_src, 1, p_length, f) != p_length); } bool FileAccessUnix::file_exists(const String &p_path) { - int err; struct stat st; String filename = fix_path(p_path); // Does the name exist at all? err = stat(filename.utf8().get_data(), &st); - if (err) + if (err) { return false; + } #ifdef UNIX_ENABLED // See if we have access to the file - if (access(filename.utf8().get_data(), F_OK)) + if (access(filename.utf8().get_data(), F_OK)) { return false; + } #else if (_access(filename.utf8().get_data(), 4) == -1) return false; @@ -310,7 +300,6 @@ bool FileAccessUnix::file_exists(const String &p_path) { } uint64_t FileAccessUnix::_get_modified_time(const String &p_file) { - String file = fix_path(p_file); struct stat flags; int err = stat(file.utf8().get_data(), &flags); @@ -323,7 +312,6 @@ uint64_t FileAccessUnix::_get_modified_time(const String &p_file) { } uint32_t FileAccessUnix::_get_unix_permissions(const String &p_file) { - String file = fix_path(p_file); struct stat flags; int err = stat(file.utf8().get_data(), &flags); @@ -336,7 +324,6 @@ uint32_t FileAccessUnix::_get_unix_permissions(const String &p_file) { } Error FileAccessUnix::_set_unix_permissions(const String &p_file, uint32_t p_permissions) { - String file = fix_path(p_file); int err = chmod(file.utf8().get_data(), p_permissions); @@ -348,20 +335,12 @@ Error FileAccessUnix::_set_unix_permissions(const String &p_file, uint32_t p_per } FileAccess *FileAccessUnix::create_libc() { - return memnew(FileAccessUnix); } -CloseNotificationFunc FileAccessUnix::close_notification_func = NULL; - -FileAccessUnix::FileAccessUnix() : - f(NULL), - flags(0), - last_error(OK) { -} +CloseNotificationFunc FileAccessUnix::close_notification_func = nullptr; FileAccessUnix::~FileAccessUnix() { - close(); } |