Utilities: add temp file management functions.
This commit is contained in:
parent
12995367eb
commit
8ecd1922f1
5 changed files with 138 additions and 0 deletions
|
@ -201,6 +201,8 @@ add_executable(MassBuilderSaveTool
|
||||||
UpdateChecker/UpdateChecker.cpp
|
UpdateChecker/UpdateChecker.cpp
|
||||||
Utilities/Crc32.h
|
Utilities/Crc32.h
|
||||||
Utilities/Crc32.cpp
|
Utilities/Crc32.cpp
|
||||||
|
Utilities/Temp.h
|
||||||
|
Utilities/Temp.cpp
|
||||||
Version/Version.h
|
Version/Version.h
|
||||||
Version/Version.cpp
|
Version/Version.cpp
|
||||||
${Logger_SOURCES}
|
${Logger_SOURCES}
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#include <shlobj.h>
|
#include <shlobj.h>
|
||||||
|
|
||||||
#include "../Logger/Logger.h"
|
#include "../Logger/Logger.h"
|
||||||
|
#include "../Utilities/Temp.h"
|
||||||
|
|
||||||
#include "Configuration.h"
|
#include "Configuration.h"
|
||||||
|
|
||||||
|
@ -123,6 +124,7 @@ Configuration::Configuration() {
|
||||||
_directories.armours = Utility::Path::join(armoury_dir, "armours");
|
_directories.armours = Utility::Path::join(armoury_dir, "armours");
|
||||||
_directories.weapons = Utility::Path::join(armoury_dir, "weapons");
|
_directories.weapons = Utility::Path::join(armoury_dir, "weapons");
|
||||||
_directories.styles = Utility::Path::join(armoury_dir, "styles");
|
_directories.styles = Utility::Path::join(armoury_dir, "styles");
|
||||||
|
_directories.temp = Utility::Path::join(executable_location, "temp");
|
||||||
|
|
||||||
if(!Utility::Path::exists(_directories.backups)) {
|
if(!Utility::Path::exists(_directories.backups)) {
|
||||||
LOG_WARNING("Backups directory not found, creating...");
|
LOG_WARNING("Backups directory not found, creating...");
|
||||||
|
@ -160,6 +162,17 @@ Configuration::Configuration() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!Utility::Path::exists(_directories.temp)) {
|
||||||
|
LOG_WARNING("Temporary directory not found, creating...");
|
||||||
|
if(!Utility::Path::make(_directories.temp)) {
|
||||||
|
LOG_ERROR(_lastError = "Couldn't create the temporary directory.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Utilities::emptyTempDir();
|
||||||
|
}
|
||||||
|
|
||||||
_valid = true;
|
_valid = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -64,6 +64,7 @@ class Configuration {
|
||||||
Containers::String armours;
|
Containers::String armours;
|
||||||
Containers::String weapons;
|
Containers::String weapons;
|
||||||
Containers::String styles;
|
Containers::String styles;
|
||||||
|
Containers::String temp;
|
||||||
};
|
};
|
||||||
auto directories() const -> Directories const&;
|
auto directories() const -> Directories const&;
|
||||||
|
|
||||||
|
|
88
src/Utilities/Temp.cpp
Normal file
88
src/Utilities/Temp.cpp
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
// MassBuilderSaveTool
|
||||||
|
// Copyright (C) 2021-2024 Guillaume Jacquemin
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <Corrade/Containers/Array.h>
|
||||||
|
#include <Corrade/Containers/Pair.h>
|
||||||
|
#include <Corrade/Utility/Path.h>
|
||||||
|
|
||||||
|
#include "../Configuration/Configuration.h"
|
||||||
|
#include "../Logger/Logger.h"
|
||||||
|
|
||||||
|
#include "Temp.h"
|
||||||
|
|
||||||
|
namespace mbst { namespace Utilities {
|
||||||
|
|
||||||
|
Containers::String
|
||||||
|
getTempPath(Containers::StringView filename) {
|
||||||
|
return Utility::Path::join(conf().directories().temp, filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
Containers::Optional<Containers::String>
|
||||||
|
copyToTemp(Containers::StringView path) {
|
||||||
|
auto filename = Utility::Path::split(path).first();
|
||||||
|
auto dest = Utility::Path::join(conf().directories().temp, filename);
|
||||||
|
|
||||||
|
if(!Utility::Path::copy(path, dest)) {
|
||||||
|
LOG_ERROR_FORMAT("Couldn't copy {} to {}.", path, conf().directories().temp);
|
||||||
|
return Containers::NullOpt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Utility::move(dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
Containers::Optional<Containers::String>
|
||||||
|
moveToTemp(Containers::StringView path) {
|
||||||
|
auto filename = Utility::Path::split(path).first();
|
||||||
|
auto dest = Utility::Path::join(conf().directories().temp, filename);
|
||||||
|
|
||||||
|
if(!Utility::Path::move(path, dest)) {
|
||||||
|
LOG_ERROR_FORMAT("Couldn't move {} to {}.", path, conf().directories().temp);
|
||||||
|
return Containers::NullOpt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Utility::move(dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
moveFromTemp(Containers::StringView filename, Containers::StringView destination) {
|
||||||
|
auto source = Utility::Path::join(conf().directories().temp, filename);
|
||||||
|
auto dest = Utility::Path::join(destination, filename);
|
||||||
|
|
||||||
|
if(!Utility::Path::move(source, dest)) {
|
||||||
|
LOG_ERROR_FORMAT("Couldn't move {} to {}.", filename, destination);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
deleteTempFile(Containers::StringView filename) {
|
||||||
|
return Utility::Path::remove(Utility::Path::join(conf().directories().temp, filename));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
emptyTempDir() {
|
||||||
|
using Flag = Utility::Path::ListFlag;
|
||||||
|
auto files = Utility::Path::list(conf().directories().temp, Flag::SkipDirectories|Flag::SkipSpecial);
|
||||||
|
CORRADE_INTERNAL_ASSERT(files);
|
||||||
|
|
||||||
|
for(auto& filename : *files) {
|
||||||
|
deleteTempFile(filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}}
|
34
src/Utilities/Temp.h
Normal file
34
src/Utilities/Temp.h
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
// MassBuilderSaveTool
|
||||||
|
// Copyright (C) 2021-2024 Guillaume Jacquemin
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <Corrade/Containers/Optional.h>
|
||||||
|
#include <Corrade/Containers/String.h>
|
||||||
|
#include <Corrade/Containers/StringView.h>
|
||||||
|
|
||||||
|
using namespace Corrade;
|
||||||
|
|
||||||
|
namespace mbst { namespace Utilities {
|
||||||
|
|
||||||
|
auto getTempPath(Containers::StringView filename) -> Containers::String;
|
||||||
|
auto copyToTemp(Containers::StringView path) -> Containers::Optional<Containers::String>;
|
||||||
|
auto moveToTemp(Containers::StringView path) -> Containers::Optional<Containers::String>;
|
||||||
|
bool moveFromTemp(Containers::StringView filename, Containers::StringView destination);
|
||||||
|
bool deleteTempFile(Containers::StringView filename);
|
||||||
|
void emptyTempDir();
|
||||||
|
|
||||||
|
}}
|
Loading…
Reference in a new issue