MassBuilderSaveTool/src/Utilities/Temp.cpp

88 lines
2.7 KiB
C++

// 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);
}
}
}}