mirror of
https://github.com/ONLYOFFICE/desktop-apps.git
synced 2026-04-07 14:09:22 +08:00
[win-linux] updatesvc: refactoring utils
This commit is contained in:
@ -33,13 +33,13 @@
|
||||
#include "platform_linux/utils.h"
|
||||
#include "version.h"
|
||||
#include <fstream>
|
||||
#include <stack>
|
||||
#include <algorithm>
|
||||
#include <sys/stat.h>
|
||||
#include <gtk/gtk.h>
|
||||
#include <string.h>
|
||||
//#include <openssl/md5.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/limits.h>
|
||||
#include "../../src/defines.h"
|
||||
#include "../../src/prop/defines_p.h"
|
||||
|
||||
@ -97,10 +97,11 @@ static bool moving_folder_content(const string &from, const string &to, bool use
|
||||
}
|
||||
|
||||
const size_t sourceLength = from.length();
|
||||
const size_t rootOffset = NS_File::parentPath(to).length() + 1;
|
||||
for (const string &sourcePath : filesList) {
|
||||
if (!sourcePath.empty()) {
|
||||
string dest = to + sourcePath.substr(sourceLength);
|
||||
if (!NS_File::dirExists(NS_File::parentPath(dest)) && !NS_File::makePath(NS_File::parentPath(dest))) {
|
||||
if (!NS_File::dirExists(NS_File::parentPath(dest)) && !NS_File::makePath(NS_File::parentPath(dest), rootOffset)) {
|
||||
NS_Logger::WriteLog("Can't create path: " + NS_File::parentPath(dest));
|
||||
return false;
|
||||
}
|
||||
@ -320,18 +321,31 @@ namespace NS_File
|
||||
return (count == 0);
|
||||
}
|
||||
|
||||
bool makePath(const string &path)
|
||||
{
|
||||
std::stack<string> pathsList;
|
||||
string last_path(path);
|
||||
while (!last_path.empty() && !dirExists(last_path)) {
|
||||
pathsList.push(last_path);
|
||||
last_path = parentPath(last_path);
|
||||
}
|
||||
while(!pathsList.empty()) {
|
||||
if (mkdir(pathsList.top().c_str(), S_IRWXU | S_IRWXG | S_IRWXO) != 0)
|
||||
bool makePath(const string &path, size_t root_offset) {
|
||||
size_t len = path.length();
|
||||
if (len == 0)
|
||||
return false;
|
||||
if (mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0 || errno == EEXIST)
|
||||
return true;
|
||||
if (len >= PATH_MAX || root_offset >= len)
|
||||
return false;
|
||||
char buf[PATH_MAX];
|
||||
strcpy(buf, path.c_str());
|
||||
if (buf[len - 1] == '/')
|
||||
buf[len - 1] = '\0';
|
||||
char *it = buf + root_offset;
|
||||
while (1) {
|
||||
while (*it != '\0' && *it != '/')
|
||||
it++;
|
||||
char tmp = *it;
|
||||
*it = '\0';
|
||||
if (mkdir(buf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0 && errno != EEXIST) {
|
||||
*it = tmp;
|
||||
return false;
|
||||
pathsList.pop();
|
||||
}
|
||||
if (tmp == '\0')
|
||||
break;
|
||||
*it++ = tmp;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -66,7 +66,7 @@ bool isProcessRunning(const string &fileName);
|
||||
bool fileExists(const string &filePath);
|
||||
bool dirExists(const string &dirName);
|
||||
bool dirIsEmpty(const string &dirName);
|
||||
bool makePath(const string &path);
|
||||
bool makePath(const string &path, size_t root_offset = 1);
|
||||
bool replaceFile(const string &oldFilePath, const string &newFilePath);
|
||||
bool replaceFolder(const string &from, const string &to, bool remove_existing = false);
|
||||
bool removeFile(const string &filePath);
|
||||
|
||||
@ -44,7 +44,6 @@
|
||||
#include <TlHelp32.h>
|
||||
#include <userenv.h>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <sstream>
|
||||
#include "../../src/defines.h"
|
||||
#include "../../src/prop/defines_p.h"
|
||||
@ -91,8 +90,11 @@ namespace NS_Utils
|
||||
LPWSTR msgBuff = NULL;
|
||||
size_t size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
NULL, errID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&msgBuff, 0, NULL);
|
||||
wstring msg(msgBuff, size);
|
||||
LocalFree(msgBuff);
|
||||
wstring msg;
|
||||
if (size > 0) {
|
||||
msg.assign(msgBuff, size);
|
||||
LocalFree(msgBuff);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
@ -370,18 +372,31 @@ namespace NS_File
|
||||
return PathIsDirectoryEmpty(dirName.c_str());
|
||||
}
|
||||
|
||||
bool makePath(const wstring &path)
|
||||
{
|
||||
std::stack<wstring> pathsList;
|
||||
wstring last_path(path);
|
||||
while (!last_path.empty() && !dirExists(last_path)) {
|
||||
pathsList.push(last_path);
|
||||
last_path = parentPath(last_path);
|
||||
}
|
||||
while(!pathsList.empty()) {
|
||||
if (::CreateDirectory(pathsList.top().c_str(), NULL) == 0)
|
||||
bool makePath(const wstring &path, size_t root_offset) {
|
||||
size_t len = path.length();
|
||||
if (len == 0)
|
||||
return false;
|
||||
if (CreateDirectoryW(path.c_str(), NULL) != 0 || GetLastError() == ERROR_ALREADY_EXISTS)
|
||||
return true;
|
||||
if (len >= MAX_PATH || root_offset >= len)
|
||||
return false;
|
||||
wchar_t buf[MAX_PATH];
|
||||
wcscpy(buf, path.c_str());
|
||||
if (buf[len - 1] == '/' || buf[len - 1] == '\\')
|
||||
buf[len - 1] = '\0';
|
||||
wchar_t *it = buf + root_offset;
|
||||
while (1) {
|
||||
while (*it != '\0' && *it != '/' && *it != '\\')
|
||||
it++;
|
||||
wchar_t tmp = *it;
|
||||
*it = '\0';
|
||||
if (CreateDirectoryW(buf, NULL) == 0 && GetLastError() != ERROR_ALREADY_EXISTS) {
|
||||
*it = tmp;
|
||||
return false;
|
||||
pathsList.pop();
|
||||
}
|
||||
if (tmp == '\0')
|
||||
break;
|
||||
*it++ = tmp;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -425,10 +440,11 @@ namespace NS_File
|
||||
}
|
||||
|
||||
const size_t sourceLength = from.length();
|
||||
const size_t rootOffset = parentPath(to).length() + 1;
|
||||
for (const wstring &sourcePath : filesList) {
|
||||
if (!sourcePath.empty()) {
|
||||
wstring dest = to + sourcePath.substr(sourceLength);
|
||||
if (!NS_File::dirExists(NS_File::parentPath(dest)) && !NS_File::makePath(NS_File::parentPath(dest))) {
|
||||
if (!NS_File::dirExists(NS_File::parentPath(dest)) && !NS_File::makePath(NS_File::parentPath(dest), rootOffset)) {
|
||||
NS_Logger::WriteLog(L"Can't create path: " + NS_File::parentPath(dest));
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -69,7 +69,7 @@ bool isProcessRunning(const wstring &fileName);
|
||||
bool fileExists(const wstring &filePath);
|
||||
bool dirExists(const wstring &dirName);
|
||||
bool dirIsEmpty(const wstring &dirName);
|
||||
bool makePath(const wstring &path);
|
||||
bool makePath(const wstring &path, size_t root_offset = 3);
|
||||
bool replaceFile(const wstring &oldFilePath, const wstring &newFilePath);
|
||||
bool replaceFolder(const wstring &from, const wstring &to, bool remove_existing = false);
|
||||
bool removeFile(const wstring &filePath);
|
||||
|
||||
Reference in New Issue
Block a user