[win-linux] updatesvc: refactoring replaceFile, parentPath

This commit is contained in:
SimplestStudio
2024-10-14 17:02:39 +03:00
parent 825a484d13
commit 2dd2f33a19
2 changed files with 44 additions and 18 deletions

View File

@ -349,21 +349,13 @@ namespace NS_File
bool replaceFile(const string &oldFilePath, const string &newFilePath)
{
struct stat src, dst;
if (stat(oldFilePath.c_str(), &src) != 0)
return false;
if (!S_ISREG(src.st_mode))
return false;
if (stat(parentPath(newFilePath).c_str(), &dst) != 0)
return false;
if (src.st_dev == dst.st_dev) {
if (rename(oldFilePath.c_str(), newFilePath.c_str()) != 0)
return false;
} else {
if (!copyFile(oldFilePath, newFilePath) || unlink(oldFilePath.c_str()) != 0)
return false;
if (rename(oldFilePath.c_str(), newFilePath.c_str()) == 0)
return true;
if (errno == EXDEV) {
errno = 0;
return copyFile(oldFilePath, newFilePath) && unlink(oldFilePath.c_str()) == 0;
}
return true;
return false;
}
bool replaceFolder(const string &from, const string &to, bool remove_existing)
@ -506,8 +498,25 @@ namespace NS_File
string parentPath(const string &path)
{
auto delim = (path.size() > 1) ? path.find_last_of('/', path.size() - 2) : string::npos;
return (delim == string::npos) ? "" : (delim == 0) ? "/" : path.substr(0, delim);
size_t len = path.length();
if (len > 1) {
const char *buf = path.c_str();
const char *it = buf + len - 1;
while (*it == '/') {
if (it == buf)
return "";
it--;
}
while (*it != '/') {
if (it == buf)
return "";
it--;
}
if (it == buf)
return "/";
return string(buf, it - buf);
}
return "";
}
string tempPath()

View File

@ -542,8 +542,25 @@ namespace NS_File
wstring parentPath(const wstring &path)
{
auto delim = (path.size() > 2) ? path.find_last_of(L"\\/", path.size() - 2) : wstring::npos;
return (delim == wstring::npos) ? L"" : path.substr(0, delim);
size_t len = path.length();
if (len > 1) {
const wchar_t *buf = path.c_str();
const wchar_t *it = buf + len - 1;
while (*it == '/' || *it == '\\') {
if (it == buf)
return L"";
it--;
}
while (*it != '/' && *it != '\\') {
if (it == buf)
return L"";
it--;
}
if (it == buf)
return L"";
return wstring(buf, it - buf);
}
return L"";
}
wstring fallbackTempPath()