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:
Tamás Bálint Misius 2023-02-01 21:45:58 +01:00
parent aa78a1ee6b
commit 0fe8d79e60
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
7 changed files with 18 additions and 11 deletions

View File

@ -1176,7 +1176,7 @@ String Client::DoMigration(ByteString fromDir, ByteString toDir)
std::string to = toDir + directory + "/" + item;
if (!Platform::FileExists(to))
{
if (rename(from.c_str(), to.c_str()))
if (Platform::RenameFile(from, to, false))
{
failedCount++;
logFile << "failed to move " << from << " to " << to << std::endl;
@ -1206,7 +1206,7 @@ String Client::DoMigration(ByteString fromDir, ByteString toDir)
ByteString to = toDir + filename;
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;
result << "\n\br" << filename.FromUtf8() << " migration failed\x0E";

View File

@ -21,7 +21,7 @@ namespace Platform
* @return true on success
*/
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

View File

@ -89,6 +89,13 @@ bool RemoveFile(ByteString filename)
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;
}
@ -154,12 +161,12 @@ void DoRestart()
bool UpdateStart(const std::vector<char> &data)
{
ByteString exeName = Platform::ExecutableName(), updName;
ByteString exeName = Platform::ExecutableName();
if (!exeName.length())
return false;
updName = exeName + "-update";
auto updName = exeName + "-update";
if (!WriteFile(data, updName))
{
@ -173,7 +180,7 @@ bool UpdateStart(const std::vector<char> &data)
return false;
}
if (!RenameFile(updName, exeName))
if (!RenameFile(updName, exeName, true))
{
RemoveFile(updName);
return false;

View File

@ -304,7 +304,7 @@ bool UpdateStart(const std::vector<char> &data)
updName = exeName.substr(0, exeName.length() - 4);
updName = updName + "_upd.exe";
if (!RenameFile(exeName, updName))
if (!RenameFile(exeName, updName, false))
return false;
if (!WriteFile(data, exeName))

View File

@ -10,7 +10,7 @@
#include <cmath>
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)
{

View File

@ -151,8 +151,7 @@ void FileBrowserActivity::RenameSave(SaveFile * file)
if (newName.length())
{
newName = ByteString::Build(directory, PATH_SEP_CHAR, newName, ".cps");
int ret = rename(file->GetName().c_str(), newName.c_str());
if (ret)
if (!Platform::RenameFile(file->GetName(), newName, false))
ErrorMessage::Blocking("Error", "Could not rename file");
else
loadDirectory(directory, "");

View File

@ -4050,7 +4050,8 @@ int LuaScriptInterface::fileSystem_move(lua_State * l)
{
auto filename = tpt_lua_checkByteString(l, 1);
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;
}