Fix map delete on windows (concatenate paths correctly with / or \ depending on OS)

This commit is contained in:
Perttu Ahola 2011-10-16 16:16:47 +03:00
parent ef397dca7c
commit 67a6bc4ab5
8 changed files with 46 additions and 33 deletions

@ -279,7 +279,7 @@ bool RecursiveDeleteContent(std::string path)
{ {
if(trim(list[i].name) == "." || trim(list[i].name) == "..") if(trim(list[i].name) == "." || trim(list[i].name) == "..")
continue; continue;
std::string childpath = path + "/" + list[i].name; std::string childpath = path + DIR_DELIM + list[i].name;
bool r = RecursiveDelete(childpath); bool r = RecursiveDelete(childpath);
if(r == false) if(r == false)
{ {
@ -299,7 +299,7 @@ bool CreateAllDirs(std::string path)
while(!PathExists(basepath)) while(!PathExists(basepath))
{ {
tocreate.push_back(basepath); tocreate.push_back(basepath);
pos = basepath.rfind('/'); pos = basepath.rfind(DIR_DELIM_C);
if(pos == std::string::npos) if(pos == std::string::npos)
return false; return false;
basepath = basepath.substr(0,pos); basepath = basepath.substr(0,pos);

@ -24,6 +24,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <vector> #include <vector>
#include "exceptions.h" #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 namespace fs
{ {

@ -42,6 +42,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mainmenumanager.h" #include "mainmenumanager.h"
#include "gettext.h" #include "gettext.h"
#include "log.h" #include "log.h"
#include "filesys.h"
/* /*
TODO: Move content-aware stuff to separate file by adding properties TODO: Move content-aware stuff to separate file by adding properties
@ -1281,7 +1282,7 @@ void the_game(
irr::video::IImage* const image = driver->createScreenShot(); irr::video::IImage* const image = driver->createScreenShot();
if (image) { if (image) {
irr::c8 filename[256]; irr::c8 filename[256];
snprintf(filename, 256, "%s/screenshot_%u.png", snprintf(filename, 256, "%s" DIR_DELIM "screenshot_%u.png",
g_settings->get("screenshot_path").c_str(), g_settings->get("screenshot_path").c_str(),
device->getTimer()->getRealTime()); device->getTimer()->getRealTime());
if (driver->writeImageToFile(image, filename)) { if (driver->writeImageToFile(image, filename)) {

@ -1186,13 +1186,13 @@ int main(int argc, char *argv[])
// Create user data directory // Create user data directory
fs::CreateDir(porting::path_userdata); fs::CreateDir(porting::path_userdata);
init_gettext((porting::path_data+"/../locale").c_str()); init_gettext((porting::path_data+DIR_DELIM+".."+DIR_DELIM+"locale").c_str());
// Initialize debug streams // Initialize debug streams
#ifdef RUN_IN_PLACE #ifdef RUN_IN_PLACE
std::string debugfile = DEBUGFILE; std::string debugfile = DEBUGFILE;
#else #else
std::string debugfile = porting::path_userdata+"/"+DEBUGFILE; std::string debugfile = porting::path_userdata+DIR_DELIM+DEBUGFILE;
#endif #endif
debugstreams_init(disable_stderr, debugfile.c_str()); debugstreams_init(disable_stderr, debugfile.c_str());
// Initialize debug stacks // Initialize debug stacks
@ -1244,9 +1244,11 @@ int main(int argc, char *argv[])
else else
{ {
core::array<std::string> filenames; core::array<std::string> filenames;
filenames.push_back(porting::path_userdata + "/minetest.conf"); filenames.push_back(porting::path_userdata +
DIR_DELIM + "minetest.conf");
#ifdef RUN_IN_PLACE #ifdef RUN_IN_PLACE
filenames.push_back(porting::path_userdata + "/../minetest.conf"); filenames.push_back(porting::path_userdata +
DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
#endif #endif
for(u32 i=0; i<filenames.size(); i++) for(u32 i=0; i<filenames.size(); i++)
@ -1308,7 +1310,7 @@ int main(int argc, char *argv[])
port = 30000; port = 30000;
// Map directory // Map directory
std::string map_dir = porting::path_userdata+"/world"; std::string map_dir = porting::path_userdata+DIR_DELIM+"world";
if(cmd_args.exists("map-dir")) if(cmd_args.exists("map-dir"))
map_dir = cmd_args.get("map-dir"); map_dir = cmd_args.get("map-dir");
else if(g_settings->exists("map-dir")) else if(g_settings->exists("map-dir"))

@ -2820,7 +2820,7 @@ void ServerMap::verifyDatabase() {
return; return;
{ {
std::string dbp = m_savedir + "/map.sqlite"; std::string dbp = m_savedir + DIR_DELIM + "map.sqlite";
bool needs_create = false; bool needs_create = false;
int d; int d;
@ -2859,7 +2859,7 @@ void ServerMap::verifyDatabase() {
} }
bool ServerMap::loadFromFolders() { bool ServerMap::loadFromFolders() {
if(!m_database && !fs::PathExists(m_savedir + "/map.sqlite")) if(!m_database && !fs::PathExists(m_savedir + DIR_DELIM + "map.sqlite"))
return true; return true;
return false; return false;
} }
@ -2889,13 +2889,13 @@ std::string ServerMap::getSectorDir(v2s16 pos, int layout)
(unsigned int)pos.X&0xffff, (unsigned int)pos.X&0xffff,
(unsigned int)pos.Y&0xffff); (unsigned int)pos.Y&0xffff);
return m_savedir + "/sectors/" + cc; return m_savedir + DIR_DELIM + "sectors" + DIR_DELIM + cc;
case 2: case 2:
snprintf(cc, 9, "%.3x/%.3x", snprintf(cc, 9, "%.3x" DIR_DELIM "%.3x",
(unsigned int)pos.X&0xfff, (unsigned int)pos.X&0xfff,
(unsigned int)pos.Y&0xfff); (unsigned int)pos.Y&0xfff);
return m_savedir + "/sectors2/" + cc; return m_savedir + DIR_DELIM + "sectors2" + DIR_DELIM + cc;
default: default:
assert(false); assert(false);
} }
@ -2905,7 +2905,7 @@ v2s16 ServerMap::getSectorPos(std::string dirname)
{ {
unsigned int x, y; unsigned int x, y;
int r; int r;
size_t spos = dirname.rfind('/') + 1; size_t spos = dirname.rfind(DIR_DELIM_C) + 1;
assert(spos != std::string::npos); assert(spos != std::string::npos);
if(dirname.size() - spos == 8) if(dirname.size() - spos == 8)
{ {
@ -2915,7 +2915,7 @@ v2s16 ServerMap::getSectorPos(std::string dirname)
else if(dirname.size() - spos == 3) else if(dirname.size() - spos == 3)
{ {
// New layout // New layout
r = sscanf(dirname.substr(spos-4).c_str(), "%3x/%3x", &x, &y); r = sscanf(dirname.substr(spos-4).c_str(), "%3x" DIR_DELIM "%3x", &x, &y);
// Sign-extend the 12 bit values up to 16 bits... // Sign-extend the 12 bit values up to 16 bits...
if(x&0x800) x|=0xF000; if(x&0x800) x|=0xF000;
if(y&0x800) y|=0xF000; if(y&0x800) y|=0xF000;
@ -3036,7 +3036,7 @@ void ServerMap::saveMapMeta()
createDirs(m_savedir); createDirs(m_savedir);
std::string fullpath = m_savedir + "/map_meta.txt"; std::string fullpath = m_savedir + DIR_DELIM + "map_meta.txt";
std::ofstream os(fullpath.c_str(), std::ios_base::binary); std::ofstream os(fullpath.c_str(), std::ios_base::binary);
if(os.good() == false) if(os.good() == false)
{ {
@ -3062,7 +3062,7 @@ void ServerMap::loadMapMeta()
infostream<<"ServerMap::loadMapMeta(): Loading map metadata" infostream<<"ServerMap::loadMapMeta(): Loading map metadata"
<<std::endl; <<std::endl;
std::string fullpath = m_savedir + "/map_meta.txt"; std::string fullpath = m_savedir + DIR_DELIM + "map_meta.txt";
std::ifstream is(fullpath.c_str(), std::ios_base::binary); std::ifstream is(fullpath.c_str(), std::ios_base::binary);
if(is.good() == false) if(is.good() == false)
{ {
@ -3101,7 +3101,7 @@ void ServerMap::saveSectorMeta(ServerMapSector *sector)
std::string dir = getSectorDir(pos); std::string dir = getSectorDir(pos);
createDirs(dir); createDirs(dir);
std::string fullpath = dir + "/meta"; std::string fullpath = dir + DIR_DELIM + "meta";
std::ofstream o(fullpath.c_str(), std::ios_base::binary); std::ofstream o(fullpath.c_str(), std::ios_base::binary);
if(o.good() == false) if(o.good() == false)
throw FileNotGoodException("Cannot open sector metafile"); throw FileNotGoodException("Cannot open sector metafile");
@ -3119,7 +3119,7 @@ MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load
ServerMapSector *sector = NULL; ServerMapSector *sector = NULL;
std::string fullpath = sectordir + "/meta"; std::string fullpath = sectordir + DIR_DELIM + "meta";
std::ifstream is(fullpath.c_str(), std::ios_base::binary); std::ifstream is(fullpath.c_str(), std::ios_base::binary);
if(is.good() == false) if(is.good() == false)
{ {
@ -3305,7 +3305,7 @@ void ServerMap::saveBlock(MapBlock *block)
createDirs(sectordir); createDirs(sectordir);
std::string fullpath = sectordir+"/"+getBlockFilename(p3d); std::string fullpath = sectordir+DIR_DELIM+getBlockFilename(p3d);
std::ofstream o(fullpath.c_str(), std::ios_base::binary); std::ofstream o(fullpath.c_str(), std::ios_base::binary);
if(o.good() == false) if(o.good() == false)
throw FileNotGoodException("Cannot open block data"); throw FileNotGoodException("Cannot open block data");
@ -3351,7 +3351,7 @@ void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSecto
{ {
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
std::string fullpath = sectordir+"/"+blockfile; std::string fullpath = sectordir+DIR_DELIM+blockfile;
try{ try{
std::ifstream is(fullpath.c_str(), std::ios_base::binary); std::ifstream is(fullpath.c_str(), std::ios_base::binary);
@ -3577,7 +3577,7 @@ MapBlock* ServerMap::loadBlock(v3s16 blockpos)
*/ */
std::string blockfilename = getBlockFilename(blockpos); std::string blockfilename = getBlockFilename(blockpos);
if(fs::PathExists(sectordir+"/"+blockfilename) == false) if(fs::PathExists(sectordir+DIR_DELIM+blockfilename) == false)
return NULL; return NULL;
/* /*

@ -87,8 +87,8 @@ void signal_handler_init(void)
Path mangler Path mangler
*/ */
std::string path_data = "../data"; std::string path_data = ".." DIR_DELIM "data";
std::string path_userdata = "../"; std::string path_userdata = "..";
void pathRemoveFile(char *path, char delim) void pathRemoveFile(char *path, char delim)
{ {
@ -127,10 +127,10 @@ void initializePaths()
pathRemoveFile(buf, '\\'); pathRemoveFile(buf, '\\');
// Use "./bin/../data" // Use "./bin/../data"
path_data = std::string(buf) + "/../data"; path_data = std::string(buf) + DIR_DELIM ".." DIR_DELIM "data";
// Use "./bin/../" // Use "./bin/../"
path_userdata = std::string(buf) + "/../"; path_userdata = std::string(buf) + DIR_DELIM ".." DIR_DELIM;
/* /*
Linux Linux
@ -188,13 +188,13 @@ void initializePaths()
pathRemoveFile(buf, '\\'); pathRemoveFile(buf, '\\');
// Use "./bin/../data" // Use "./bin/../data"
path_data = std::string(buf) + "/../data"; path_data = std::string(buf) + DIR_DELIM ".." DIR_DELIM "data";
//path_data = std::string(buf) + "/../share/" + PROJECT_NAME; //path_data = std::string(buf) + "/../share/" + PROJECT_NAME;
// Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>" // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
len = GetEnvironmentVariable("APPDATA", buf, buflen); len = GetEnvironmentVariable("APPDATA", buf, buflen);
assert(len < buflen); assert(len < buflen);
path_userdata = std::string(buf) + "/" + PROJECT_NAME; path_userdata = std::string(buf) + DIR_DELIM + PROJECT_NAME;
/* /*
Linux Linux

@ -175,7 +175,7 @@ int main(int argc, char *argv[])
#ifdef RUN_IN_PLACE #ifdef RUN_IN_PLACE
std::string debugfile = DEBUGFILE; std::string debugfile = DEBUGFILE;
#else #else
std::string debugfile = porting::path_userdata+"/"+DEBUGFILE; std::string debugfile = porting::path_userdata+DIR_DELIM+DEBUGFILE;
#endif #endif
debugstreams_init(disable_stderr, debugfile.c_str()); debugstreams_init(disable_stderr, debugfile.c_str());
// Initialize debug stacks // Initialize debug stacks
@ -279,9 +279,11 @@ int main(int argc, char *argv[])
else else
{ {
core::array<std::string> filenames; core::array<std::string> filenames;
filenames.push_back(porting::path_userdata + "/minetest.conf"); filenames.push_back(porting::path_userdata +
DIR_DELIM + "minetest.conf");
#ifdef RUN_IN_PLACE #ifdef RUN_IN_PLACE
filenames.push_back(porting::path_userdata + "/../minetest.conf"); filenames.push_back(porting::path_userdata +
DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
#endif #endif
for(u32 i=0; i<filenames.size(); i++) for(u32 i=0; i<filenames.size(); i++)
@ -347,7 +349,7 @@ int main(int argc, char *argv[])
} }
// Figure out path to map // Figure out path to map
std::string map_dir = porting::path_userdata+"/world"; std::string map_dir = porting::path_userdata+DIR_DELIM+"world";
if(cmd_args.exists("map-dir")) if(cmd_args.exists("map-dir"))
map_dir = cmd_args.get("map-dir"); map_dir = cmd_args.get("map-dir");
else if(g_settings->exists("map-dir")) else if(g_settings->exists("map-dir"))

@ -118,7 +118,7 @@ std::string getTexturePath(const std::string &filename)
std::string texture_path = g_settings->get("texture_path"); std::string texture_path = g_settings->get("texture_path");
if(texture_path != "") if(texture_path != "")
{ {
std::string testpath = texture_path + '/' + filename; std::string testpath = texture_path + DIR_DELIM + filename;
// Check all filename extensions. Returns "" if not found. // Check all filename extensions. Returns "" if not found.
fullpath = getImagePath(testpath); fullpath = getImagePath(testpath);
} }