mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-08-09 09:56:35 +02:00
Make replace parameter of RenameFile explicit
Also replace a few rename calls with RenameFile calls. Old code doesn't expect rename to overwrite existing files without question, when it in fact can.
This commit is contained in:
@@ -1176,7 +1176,7 @@ String Client::DoMigration(ByteString fromDir, ByteString toDir)
|
|||||||
std::string to = toDir + directory + "/" + item;
|
std::string to = toDir + directory + "/" + item;
|
||||||
if (!Platform::FileExists(to))
|
if (!Platform::FileExists(to))
|
||||||
{
|
{
|
||||||
if (rename(from.c_str(), to.c_str()))
|
if (Platform::RenameFile(from, to, false))
|
||||||
{
|
{
|
||||||
failedCount++;
|
failedCount++;
|
||||||
logFile << "failed to move " << from << " to " << to << std::endl;
|
logFile << "failed to move " << from << " to " << to << std::endl;
|
||||||
@@ -1206,7 +1206,7 @@ String Client::DoMigration(ByteString fromDir, ByteString toDir)
|
|||||||
ByteString to = toDir + filename;
|
ByteString to = toDir + filename;
|
||||||
if (!Platform::FileExists(to))
|
if (!Platform::FileExists(to))
|
||||||
{
|
{
|
||||||
if (rename(from.c_str(), to.c_str()))
|
if (Platform::RenameFile(from, to, false))
|
||||||
{
|
{
|
||||||
logFile << "failed to move " << from << " to " << to << std::endl;
|
logFile << "failed to move " << from << " to " << to << std::endl;
|
||||||
result << "\n\br" << filename.FromUtf8() << " migration failed\x0E";
|
result << "\n\br" << filename.FromUtf8() << " migration failed\x0E";
|
||||||
|
@@ -21,7 +21,7 @@ namespace Platform
|
|||||||
* @return true on success
|
* @return true on success
|
||||||
*/
|
*/
|
||||||
bool RemoveFile(ByteString filename);
|
bool RemoveFile(ByteString filename);
|
||||||
bool RenameFile(ByteString filename, ByteString newFilename, bool replace = false);
|
bool RenameFile(ByteString filename, ByteString newFilename, bool replace);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true on success
|
* @return true on success
|
||||||
|
@@ -89,6 +89,13 @@ bool RemoveFile(ByteString filename)
|
|||||||
|
|
||||||
bool RenameFile(ByteString filename, ByteString newFilename, bool replace)
|
bool RenameFile(ByteString filename, ByteString newFilename, bool replace)
|
||||||
{
|
{
|
||||||
|
// TODO: Make atomic :( Could use renameat2 with RENAME_NOREPLACE on linux and
|
||||||
|
// renamex_np with RENAME_EXCL on darwin, but both require filesystem support;
|
||||||
|
// I don't think it's worth it for now. -- LBPHacker
|
||||||
|
if (!replace && FileExists(newFilename))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return rename(filename.c_str(), newFilename.c_str()) == 0;
|
return rename(filename.c_str(), newFilename.c_str()) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,12 +161,12 @@ void DoRestart()
|
|||||||
|
|
||||||
bool UpdateStart(const std::vector<char> &data)
|
bool UpdateStart(const std::vector<char> &data)
|
||||||
{
|
{
|
||||||
ByteString exeName = Platform::ExecutableName(), updName;
|
ByteString exeName = Platform::ExecutableName();
|
||||||
|
|
||||||
if (!exeName.length())
|
if (!exeName.length())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
updName = exeName + "-update";
|
auto updName = exeName + "-update";
|
||||||
|
|
||||||
if (!WriteFile(data, updName))
|
if (!WriteFile(data, updName))
|
||||||
{
|
{
|
||||||
@@ -173,7 +180,7 @@ bool UpdateStart(const std::vector<char> &data)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!RenameFile(updName, exeName))
|
if (!RenameFile(updName, exeName, true))
|
||||||
{
|
{
|
||||||
RemoveFile(updName);
|
RemoveFile(updName);
|
||||||
return false;
|
return false;
|
||||||
|
@@ -304,7 +304,7 @@ bool UpdateStart(const std::vector<char> &data)
|
|||||||
updName = exeName.substr(0, exeName.length() - 4);
|
updName = exeName.substr(0, exeName.length() - 4);
|
||||||
updName = updName + "_upd.exe";
|
updName = updName + "_upd.exe";
|
||||||
|
|
||||||
if (!RenameFile(exeName, updName))
|
if (!RenameFile(exeName, updName, false))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!WriteFile(data, exeName))
|
if (!WriteFile(data, exeName))
|
||||||
|
@@ -10,7 +10,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
constexpr auto VIDXRES = WINDOWW;
|
constexpr auto VIDXRES = WINDOWW;
|
||||||
constexpr auto VIDYRES = WINDOWH;
|
// constexpr auto VIDYRES = WINDOWH; // not actually used anywhere
|
||||||
|
|
||||||
VideoBuffer * Renderer::WallIcon(int wallID, int width, int height)
|
VideoBuffer * Renderer::WallIcon(int wallID, int width, int height)
|
||||||
{
|
{
|
||||||
|
@@ -151,8 +151,7 @@ void FileBrowserActivity::RenameSave(SaveFile * file)
|
|||||||
if (newName.length())
|
if (newName.length())
|
||||||
{
|
{
|
||||||
newName = ByteString::Build(directory, PATH_SEP_CHAR, newName, ".cps");
|
newName = ByteString::Build(directory, PATH_SEP_CHAR, newName, ".cps");
|
||||||
int ret = rename(file->GetName().c_str(), newName.c_str());
|
if (!Platform::RenameFile(file->GetName(), newName, false))
|
||||||
if (ret)
|
|
||||||
ErrorMessage::Blocking("Error", "Could not rename file");
|
ErrorMessage::Blocking("Error", "Could not rename file");
|
||||||
else
|
else
|
||||||
loadDirectory(directory, "");
|
loadDirectory(directory, "");
|
||||||
|
@@ -4050,7 +4050,8 @@ int LuaScriptInterface::fileSystem_move(lua_State * l)
|
|||||||
{
|
{
|
||||||
auto filename = tpt_lua_checkByteString(l, 1);
|
auto filename = tpt_lua_checkByteString(l, 1);
|
||||||
auto newFilename = tpt_lua_checkByteString(l, 2);
|
auto newFilename = tpt_lua_checkByteString(l, 2);
|
||||||
lua_pushboolean(l, Platform::RenameFile(filename, newFilename));
|
bool replace = lua_toboolean(l, 3);
|
||||||
|
lua_pushboolean(l, Platform::RenameFile(filename, newFilename, replace));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user