From 4c76239818f5159314f30883f98b977d30aaa26c Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Fri, 22 Jan 2021 15:09:26 +0000 Subject: Remove dead code (#10845) --- src/filesys.cpp | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'src/filesys.cpp') diff --git a/src/filesys.cpp b/src/filesys.cpp index 28a33f4d0..eeba0c564 100644 --- a/src/filesys.cpp +++ b/src/filesys.cpp @@ -401,21 +401,6 @@ void GetRecursiveSubPaths(const std::string &path, } } -bool DeletePaths(const std::vector &paths) -{ - bool success = true; - // Go backwards to succesfully delete the output of GetRecursiveSubPaths - for(int i=paths.size()-1; i>=0; i--){ - const std::string &path = paths[i]; - bool did = DeleteSingleFileOrEmptyDirectory(path); - if(!did){ - errorstream<<"Failed to delete "< Date: Sun, 7 Feb 2021 15:27:24 +0000 Subject: Use consistent temp folder path (#10892) --- builtin/mainmenu/common.lua | 36 ++++++++---------------------------- doc/menu_lua_api.txt | 1 + src/defaultsettings.cpp | 1 - src/filesys.cpp | 6 ++---- src/script/lua_api/l_mainmenu.cpp | 11 +++++++++++ src/script/lua_api/l_mainmenu.h | 2 ++ 6 files changed, 24 insertions(+), 33 deletions(-) (limited to 'src/filesys.cpp') diff --git a/builtin/mainmenu/common.lua b/builtin/mainmenu/common.lua index 01f9a30b9..cd896f9ec 100644 --- a/builtin/mainmenu/common.lua +++ b/builtin/mainmenu/common.lua @@ -146,35 +146,15 @@ end -------------------------------------------------------------------------------- os.tempfolder = function() - if core.settings:get("TMPFolder") then - return core.settings:get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000) - end - - local filetocheck = os.tmpname() - os.remove(filetocheck) - - -- luacheck: ignore - -- https://blogs.msdn.microsoft.com/vcblog/2014/06/18/c-runtime-crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1/ - -- The C runtime (CRT) function called by os.tmpname is tmpnam. - -- Microsofts tmpnam implementation in older CRT / MSVC releases is defective. - -- tmpnam return values starting with a backslash characterize this behavior. - -- https://sourceforge.net/p/mingw-w64/bugs/555/ - -- MinGW tmpnam implementation is forwarded to the CRT directly. - -- https://sourceforge.net/p/mingw-w64/discussion/723797/thread/55520785/ - -- MinGW links to an older CRT release (msvcrt.dll). - -- Due to legal concerns MinGW will never use a newer CRT. - -- - -- Make use of TEMP to compose the temporary filename if an old - -- style tmpnam return value is detected. - if filetocheck:sub(1, 1) == "\\" then - local tempfolder = os.getenv("TEMP") - return tempfolder .. filetocheck - end + local temp = core.get_temp_path() + return temp .. DIR_DELIM .. "MT_" .. math.random(0, 10000) +end - local randname = "MTTempModFolder_" .. math.random(0,10000) - local backstring = filetocheck:reverse() - return filetocheck:sub(0, filetocheck:len() - backstring:find(DIR_DELIM) + 1) .. - randname +-------------------------------------------------------------------------------- +os.tmpname = function() + local path = os.tempfolder() + io.open(path, "w"):close() + return path end -------------------------------------------------------------------------------- diff --git a/doc/menu_lua_api.txt b/doc/menu_lua_api.txt index db49c1736..b3975bc1d 100644 --- a/doc/menu_lua_api.txt +++ b/doc/menu_lua_api.txt @@ -85,6 +85,7 @@ core.get_video_drivers() core.get_mapgen_names([include_hidden=false]) -> table of map generator algorithms registered in the core (possible in async calls) core.get_cache_path() -> path of cache +core.get_temp_path() -> path of temp folder HTTP Requests diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index d34ec324b..41c4922a4 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -461,7 +461,6 @@ void set_default_settings() settings->setDefault("screen_h", "0"); settings->setDefault("fullscreen", "true"); settings->setDefault("touchtarget", "true"); - settings->setDefault("TMPFolder", porting::path_cache); settings->setDefault("touchscreen_threshold","20"); settings->setDefault("fixed_virtual_joystick", "false"); settings->setDefault("virtual_joystick_triggers_aux", "false"); diff --git a/src/filesys.cpp b/src/filesys.cpp index eeba0c564..5ffb4506e 100644 --- a/src/filesys.cpp +++ b/src/filesys.cpp @@ -27,9 +27,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "log.h" #include "config.h" #include "porting.h" -#ifdef __ANDROID__ -#include "settings.h" // For g_settings -#endif namespace fs { @@ -359,8 +356,9 @@ std::string TempPath() compatible with lua's os.tmpname which under the default configuration hardcodes mkstemp("/tmp/lua_XXXXXX"). */ + #ifdef __ANDROID__ - return g_settings->get("TMPFolder"); + return porting::path_cache; #else return DIR_DELIM "tmp"; #endif diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index 4733c4003..ba7f708a4 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -529,6 +529,7 @@ int ModApiMainMenu::l_get_texturepath(lua_State *L) return 1; } +/******************************************************************************/ int ModApiMainMenu::l_get_texturepath_share(lua_State *L) { std::string gamepath = fs::RemoveRelativePathComponents( @@ -537,12 +538,20 @@ int ModApiMainMenu::l_get_texturepath_share(lua_State *L) return 1; } +/******************************************************************************/ int ModApiMainMenu::l_get_cache_path(lua_State *L) { lua_pushstring(L, fs::RemoveRelativePathComponents(porting::path_cache).c_str()); return 1; } +/******************************************************************************/ +int ModApiMainMenu::l_get_temp_path(lua_State *L) +{ + lua_pushstring(L, fs::TempPath().c_str()); + return 1; +} + /******************************************************************************/ int ModApiMainMenu::l_create_dir(lua_State *L) { const char *path = luaL_checkstring(L, 1); @@ -942,6 +951,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top) API_FCT(get_texturepath); API_FCT(get_texturepath_share); API_FCT(get_cache_path); + API_FCT(get_temp_path); API_FCT(create_dir); API_FCT(delete_dir); API_FCT(copy_dir); @@ -975,6 +985,7 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top) API_FCT(get_texturepath); API_FCT(get_texturepath_share); API_FCT(get_cache_path); + API_FCT(get_temp_path); API_FCT(create_dir); API_FCT(delete_dir); API_FCT(copy_dir); diff --git a/src/script/lua_api/l_mainmenu.h b/src/script/lua_api/l_mainmenu.h index 580a0df72..49ce7c251 100644 --- a/src/script/lua_api/l_mainmenu.h +++ b/src/script/lua_api/l_mainmenu.h @@ -122,6 +122,8 @@ private: static int l_get_cache_path(lua_State *L); + static int l_get_temp_path(lua_State *L); + static int l_create_dir(lua_State *L); static int l_delete_dir(lua_State *L); -- cgit v1.2.3 From 225d4541ffb4d59001841747e0877a175da50c17 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 6 May 2021 09:02:11 +0200 Subject: fix: extractZipFile is not part of Client but more generic. This solve a crash from mainmenu while extracting the zip --- src/client/client.cpp | 66 --------------------------------------- src/client/client.h | 2 -- src/filesys.cpp | 64 +++++++++++++++++++++++++++++++++++++ src/filesys.h | 6 ++++ src/script/lua_api/l_mainmenu.cpp | 3 +- 5 files changed, 72 insertions(+), 69 deletions(-) (limited to 'src/filesys.cpp') diff --git a/src/client/client.cpp b/src/client/client.cpp index c0da27e44..00ae8f6b8 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -725,72 +725,6 @@ bool Client::loadMedia(const std::string &data, const std::string &filename, return false; } -bool Client::extractZipFile(const char *filename, const std::string &destination) -{ - auto fs = m_rendering_engine->get_filesystem(); - - if (!fs->addFileArchive(filename, false, false, io::EFAT_ZIP)) { - return false; - } - - sanity_check(fs->getFileArchiveCount() > 0); - - /**********************************************************************/ - /* WARNING this is not threadsafe!! */ - /**********************************************************************/ - io::IFileArchive* opened_zip = fs->getFileArchive(fs->getFileArchiveCount() - 1); - - const io::IFileList* files_in_zip = opened_zip->getFileList(); - - unsigned int number_of_files = files_in_zip->getFileCount(); - - for (unsigned int i=0; i < number_of_files; i++) { - std::string fullpath = destination; - fullpath += DIR_DELIM; - fullpath += files_in_zip->getFullFileName(i).c_str(); - std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath); - - if (!files_in_zip->isDirectory(i)) { - if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) { - fs->removeFileArchive(fs->getFileArchiveCount()-1); - return false; - } - - io::IReadFile* toread = opened_zip->createAndOpenFile(i); - - FILE *targetfile = fopen(fullpath.c_str(),"wb"); - - if (targetfile == NULL) { - fs->removeFileArchive(fs->getFileArchiveCount()-1); - return false; - } - - char read_buffer[1024]; - long total_read = 0; - - while (total_read < toread->getSize()) { - - unsigned int bytes_read = - toread->read(read_buffer,sizeof(read_buffer)); - if ((bytes_read == 0 ) || - (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read)) - { - fclose(targetfile); - fs->removeFileArchive(fs->getFileArchiveCount() - 1); - return false; - } - total_read += bytes_read; - } - - fclose(targetfile); - } - - } - - fs->removeFileArchive(fs->getFileArchiveCount() - 1); - return true; -} - // Virtual methods from con::PeerHandler void Client::peerAdded(con::Peer *peer) { diff --git a/src/client/client.h b/src/client/client.h index c9a72b1b3..85ca24049 100644 --- a/src/client/client.h +++ b/src/client/client.h @@ -384,8 +384,6 @@ public: bool loadMedia(const std::string &data, const std::string &filename, bool from_media_push = false); - bool extractZipFile(const char *filename, const std::string &destination); - // Send a request for conventional media transfer void request_media(const std::vector &file_requests); diff --git a/src/filesys.cpp b/src/filesys.cpp index 5ffb4506e..99b030624 100644 --- a/src/filesys.cpp +++ b/src/filesys.cpp @@ -727,6 +727,70 @@ bool safeWriteToFile(const std::string &path, const std::string &content) return true; } +bool extractZipFile(io::IFileSystem *fs, const char *filename, const std::string &destination) +{ + if (!fs->addFileArchive(filename, false, false, io::EFAT_ZIP)) { + return false; + } + + sanity_check(fs->getFileArchiveCount() > 0); + + /**********************************************************************/ + /* WARNING this is not threadsafe!! */ + /**********************************************************************/ + io::IFileArchive* opened_zip = fs->getFileArchive(fs->getFileArchiveCount() - 1); + + const io::IFileList* files_in_zip = opened_zip->getFileList(); + + unsigned int number_of_files = files_in_zip->getFileCount(); + + for (unsigned int i=0; i < number_of_files; i++) { + std::string fullpath = destination; + fullpath += DIR_DELIM; + fullpath += files_in_zip->getFullFileName(i).c_str(); + std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath); + + if (!files_in_zip->isDirectory(i)) { + if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) { + fs->removeFileArchive(fs->getFileArchiveCount()-1); + return false; + } + + io::IReadFile* toread = opened_zip->createAndOpenFile(i); + + FILE *targetfile = fopen(fullpath.c_str(),"wb"); + + if (targetfile == NULL) { + fs->removeFileArchive(fs->getFileArchiveCount()-1); + return false; + } + + char read_buffer[1024]; + long total_read = 0; + + while (total_read < toread->getSize()) { + + unsigned int bytes_read = + toread->read(read_buffer,sizeof(read_buffer)); + if ((bytes_read == 0 ) || + (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read)) + { + fclose(targetfile); + fs->removeFileArchive(fs->getFileArchiveCount() - 1); + return false; + } + total_read += bytes_read; + } + + fclose(targetfile); + } + + } + + fs->removeFileArchive(fs->getFileArchiveCount() - 1); + return true; +} + bool ReadFile(const std::string &path, std::string &out) { std::ifstream is(path, std::ios::binary | std::ios::ate); diff --git a/src/filesys.h b/src/filesys.h index cfbfa02bf..a9584b036 100644 --- a/src/filesys.h +++ b/src/filesys.h @@ -36,6 +36,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #define PATH_DELIM ":" #endif +namespace irr { namespace io { +class IFileSystem; +}} + namespace fs { @@ -125,6 +129,8 @@ const char *GetFilenameFromPath(const char *path); bool safeWriteToFile(const std::string &path, const std::string &content); +bool extractZipFile(irr::io::IFileSystem *fs, const char *filename, const std::string &destination); + bool ReadFile(const std::string &path, std::string &out); bool Rename(const std::string &from, const std::string &to); diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index b13398539..ee3e72dea 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -628,8 +628,9 @@ int ModApiMainMenu::l_extract_zip(lua_State *L) std::string absolute_destination = fs::RemoveRelativePathComponents(destination); if (ModApiMainMenu::mayModifyPath(absolute_destination)) { + auto rendering_engine = getGuiEngine(L)->m_rendering_engine; fs::CreateAllDirs(absolute_destination); - lua_pushboolean(L, getClient(L)->extractZipFile(zipfile, destination)); + lua_pushboolean(L, fs::extractZipFile(rendering_engine->get_filesystem(), zipfile, destination)); return 1; } -- cgit v1.2.3