From 1b88f95b250cecc6f6c4f6f2f7eee90d68ce6b6d Mon Sep 17 00:00:00 2001 From: darkrose Date: Wed, 10 May 2017 22:22:41 +1000 Subject: [PATCH] sane filesystem code --- src/CMakeLists.txt | 1 - src/environment.cpp | 1 - src/filesys.cpp | 298 -------------------------------------------- src/filesys.h | 70 ----------- src/game.cpp | 29 +++-- src/http.cpp | 1 - src/main.cpp | 1 - src/map.cpp | 139 --------------------- src/map.h | 14 --- src/porting.cpp | 1 - src/server.cpp | 1 - src/servermain.cpp | 1 - src/tile.cpp | 1 - 13 files changed, 16 insertions(+), 542 deletions(-) delete mode 100644 src/filesys.cpp delete mode 100644 src/filesys.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5966680..24da170 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -210,7 +210,6 @@ set(common_SRCS debug.cpp serialization.cpp light.cpp - filesys.cpp connection.cpp environment.cpp plantgrowth.cpp diff --git a/src/environment.cpp b/src/environment.cpp index 0cab7b5..59f029b 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -24,7 +24,6 @@ ************************************************************************/ #include "environment.h" -#include "filesys.h" #include "porting.h" #include "collision.h" #include "content_mapnode.h" diff --git a/src/filesys.cpp b/src/filesys.cpp deleted file mode 100644 index 535ce98..0000000 --- a/src/filesys.cpp +++ /dev/null @@ -1,298 +0,0 @@ -/************************************************************************ -* Minetest-c55 -* Copyright (C) 2010 celeron55, Perttu Ahola -* -* filesys.cpp -* voxelands - 3d voxel world sandbox game -* Copyright (C) Lisa 'darkrose' Milne 2014 -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, but -* WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -* See the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see -* -* License updated from GPLv2 or later to GPLv3 or later by Lisa Milne -* for Voxelands. -************************************************************************/ - -#include -#include "filesys.h" -#include "strfnd.h" -#include -#include - -namespace fs -{ - -#ifdef _WIN32 // WINDOWS - -#define _WIN32_WINNT 0x0501 -#include -#include -#include -#include -#include - -#define BUFSIZE MAX_PATH - -std::vector GetDirListing(std::string pathstring) -{ - std::vector listing; - - WIN32_FIND_DATA FindFileData; - HANDLE hFind = INVALID_HANDLE_VALUE; - DWORD dwError; - LPTSTR DirSpec; - INT retval; - - DirSpec = (LPTSTR) malloc (BUFSIZE); - - if( DirSpec == NULL ) - { - printf( "Insufficient memory available\n" ); - retval = 1; - goto Cleanup; - } - - // Check that the input is not larger than allowed. - if (pathstring.size() > (BUFSIZE - 2)) - { - _tprintf(TEXT("Input directory is too large.\n")); - retval = 3; - goto Cleanup; - } - - //_tprintf (TEXT("Target directory is %s.\n"), pathstring.c_str()); - - sprintf(DirSpec, "%s", (pathstring + "\\*").c_str()); - - // Find the first file in the directory. - hFind = FindFirstFile(DirSpec, &FindFileData); - - if (hFind == INVALID_HANDLE_VALUE) - { - _tprintf (TEXT("Invalid file handle. Error is %u.\n"), - GetLastError()); - retval = (-1); - } - else - { - // NOTE: - // Be very sure to not include '..' in the results, it will - // result in an epic failure when deleting stuff. - - DirListNode node; - node.name = FindFileData.cFileName; - node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; - if(node.name != "." && node.name != "..") - listing.push_back(node); - - // List all the other files in the directory. - while (FindNextFile(hFind, &FindFileData) != 0) - { - DirListNode node; - node.name = FindFileData.cFileName; - node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; - if(node.name != "." && node.name != "..") - listing.push_back(node); - } - - dwError = GetLastError(); - FindClose(hFind); - if (dwError != ERROR_NO_MORE_FILES) - { - _tprintf (TEXT("FindNextFile error. Error is %u.\n"), - dwError); - retval = (-1); - goto Cleanup; - } - } - retval = 0; - -Cleanup: - free(DirSpec); - - if(retval != 0) listing.clear(); - - //for(unsigned int i=0; i dir_list = GetDirListing(path); - for(int i = 0; i < dir_list.size(); i++) { - DirListNode &node = dir_list[i]; - std::string fpath = path + DIR_DELIM + node.name; - if(!RecursiveDelete(fpath)) { - std::cerr<<"Could not remove contents of \""< -#include -#include -#include -#include -#include -#include - -std::vector GetDirListing(std::string pathstring) -{ - std::vector listing; - - DIR *dp; - struct dirent *dirp; - if ((dp = opendir(pathstring.c_str())) == NULL) { - //std::cout<<"Error("<d_name[0]!='.') { - DirListNode node; - node.name = dirp->d_name; - if (dirp->d_type == DT_DIR) { - node.dir = true; - }else{ - node.dir = false; - } - if (node.name != "." && node.name != "..") - listing.push_back(node); - } - } - closedir(dp); - - return listing; -} - -bool CreateDir(std::string path) -{ - int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - if (r == 0) - return true; - - // If already exists, return true - if (errno == EEXIST) - return true; - return false; -} - -bool PathExists(std::string path) -{ - struct stat st; - return (stat(path.c_str(),&st) == 0); -} - -int unlink_cb(const char* fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) -{ - return remove(fpath); -} - -bool RecursiveDelete(std::string path) -{ - // because fuck that - if (path.substr(0,1) != "/" || path == "/") - return false; - // file tree walk, calls the unlink_cb function on every file/directory - int ret = nftw(path.c_str(), unlink_cb, 64, FTW_DEPTH | FTW_PHYS); - return (ret == 0); -} - -#endif - -bool RecursiveDeleteContent(std::string path) -{ - std::cerr<<"Removing content of \""< list = GetDirListing(path); - for(unsigned int i=0; i tocreate; - std::string basepath = path; - while(!PathExists(basepath)) - { - tocreate.push_back(basepath); - pos = basepath.rfind(DIR_DELIM_C); - if(pos == std::string::npos) - return false; - basepath = basepath.substr(0,pos); - } - for(int i=tocreate.size()-1;i>=0;i--) - CreateDir(tocreate[i]); - return true; -} - -} // namespace fs - diff --git a/src/filesys.h b/src/filesys.h deleted file mode 100644 index f4f6a56..0000000 --- a/src/filesys.h +++ /dev/null @@ -1,70 +0,0 @@ -/************************************************************************ -* Minetest-c55 -* Copyright (C) 2010 celeron55, Perttu Ahola -* -* filesys.h -* voxelands - 3d voxel world sandbox game -* Copyright (C) Lisa 'darkrose' Milne 2014 -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, but -* WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -* See the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see -* -* License updated from GPLv2 or later to GPLv3 or later by Lisa Milne -* for Voxelands. -************************************************************************/ - -#ifndef FILESYS_HEADER -#define FILESYS_HEADER - -#include -#include -#include "exceptions.h" - -#ifdef _WIN32 // WINDOWS -#define DIR_DELIM "\\" -#define DIR_DELIM_C '\\' -#else // POSIX -#define DIR_DELIM "/" -#define DIR_DELIM_C '/' -#endif - -namespace fs -{ - -struct DirListNode -{ - std::string name; - bool dir; -}; - -std::vector GetDirListing(std::string path); - -// Returns true if already exists -bool CreateDir(std::string path); - -// Create all directories on the given path that don't already exist. -bool CreateAllDirs(std::string path); - -bool PathExists(std::string path); - -// Only pass full paths to this one. True on success. -// NOTE: The WIN32 version returns always true. -bool RecursiveDelete(std::string path); - -// Only pass full paths to this one. True on success. -bool RecursiveDeleteContent(std::string path); - -}//fs - -#endif - diff --git a/src/game.cpp b/src/game.cpp index e5265b5..a74bf09 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -46,7 +46,6 @@ #include "mainmenumanager.h" #include "intl.h" #include "log.h" -#include "filesys.h" #include "path.h" #include "sound.h" #ifndef SERVER @@ -1164,20 +1163,24 @@ void the_game( }else if(input->wasKeyDown(getKeySetting(VLKC_SCREENSHOT))) { irr::video::IImage* const image = driver->createScreenShot(); if (image) { - irr::c8 filename[256]; - snprintf(filename, 256, "%s" DIR_DELIM "screenshot_%u.png", - g_settings->get("screenshot_path").c_str(), - device->getTimer()->getRealTime()); - if (driver->writeImageToFile(image, io::path(filename))) { - char buff[512]; - snprintf(buff, 512, gettext("Saved screenshot to '%s'"), filename); - infostream << "Saved screenshot to '" << filename << "'" << std::endl; - statustext = narrow_to_wide(buff); - statustext_time = 0; + char fn[256]; + char path[1024]; + if (snprintf(fn,256,"screenshot_%u.png", device->getTimer()->getRealTime()) >= 256) { + infostream << "Failed to save screenshot"<writeImageToFile(image, io::path(path))) { + char buff[512]; + snprintf(buff, 512, gettext("Saved screenshot to '%s'"), path); + infostream << "Saved screenshot to '" << fn << "'" << std::endl; + statustext = narrow_to_wide(buff); + statustext_time = 0; + }else{ + infostream << "Failed to save screenshot '" << fn << "'"<drop(); + } } - image->drop(); } }else if (input->wasKeyDown(getKeySetting(VLKC_TOGGLE_HUD))) { show_hud = !show_hud; diff --git a/src/http.cpp b/src/http.cpp index 6f7bb7e..9b62092 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -23,7 +23,6 @@ #include "http.h" #include "main.h" #include "settings.h" -#include "filesys.h" #include "debug.h" #include #include diff --git a/src/main.cpp b/src/main.cpp index 05d1144..c868afb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,7 +55,6 @@ #include "porting.h" #include "gettime.h" #include "guiMessageMenu.h" -#include "filesys.h" #include "config.h" #include "guiMainMenu.h" #include "mineral.h" diff --git a/src/map.cpp b/src/map.cpp index 51509fb..c1ea5ad 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -30,7 +30,6 @@ #ifndef SERVER #include "client.h" #endif -#include "filesys.h" #include "utility.h" #include "voxel.h" #include "porting.h" @@ -2365,65 +2364,6 @@ sqlite3_int64 ServerMap::getBlockAsInteger(const v3s16 pos) { (sqlite3_int64)pos.Y*4096 + (sqlite3_int64)pos.X; } -void ServerMap::createDirs(std::string path) -{ - if(fs::CreateAllDirs(path) == false) - { - m_dout<resetModified(); } -void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load) -{ - DSTACK(__FUNCTION_NAME); - - std::string fullpath = sectordir+DIR_DELIM+blockfile; - try{ - - std::ifstream is(fullpath.c_str(), std::ios_base::binary); - if(is.good() == false) - throw FileNotGoodException("Cannot open block file"); - - v3s16 p3d = getBlockPos(sectordir, blockfile); - v2s16 p2d(p3d.X, p3d.Z); - - assert(sector->getPos() == p2d); - - u8 version = SER_FMT_VER_INVALID; - is.read((char*)&version, 1); - - if(is.fail()) - throw SerializationError("ServerMap::loadBlock(): Failed" - " to read MapBlock version"); - - /*u32 block_size = MapBlock::serializedLength(version); - SharedBuffer data(block_size); - is.read((char*)*data, block_size);*/ - - // This will always return a sector because we're the server - //MapSector *sector = emergeSector(p2d); - - MapBlock *block = NULL; - bool created_new = false; - block = sector->getBlockNoCreateNoEx(p3d.Y); - if(block == NULL) - { - block = sector->createBlankBlockNoInsert(p3d.Y); - created_new = true; - } - - // Read basic data - block->deSerialize(is, version); - - // Read extra data stored on disk - block->deSerializeDiskExtra(is, version); - - // If it's a new block, insert it to the map - if(created_new) - sector->insertBlock(block); - - /* - Save blocks loaded in old format in new format - */ - - if(version < SER_FMT_VER_HIGHEST || save_after_load) - { - saveBlock(block); - - // Should be in database now, so delete the old file - fs::RecursiveDelete(fullpath); - } - - // We just loaded it from the disk, so it's up-to-date. - block->resetModified(); - - } - catch(SerializationError &e) - { - infostream<<"WARNING: Invalid block data on disk " - <<"fullpath="<