2018-09-28 07:36:10 +00:00
|
|
|
///
|
2022-01-21 19:05:54 +00:00
|
|
|
/// Copyright 2018-2022 Oliver Giles
|
2018-09-28 07:36:10 +00:00
|
|
|
///
|
|
|
|
/// This file is part of Laminar
|
|
|
|
///
|
|
|
|
/// Laminar 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.
|
|
|
|
///
|
|
|
|
/// Laminar 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 Laminar. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
///
|
|
|
|
#ifndef LAMINAR_TEMPDIR_H_
|
|
|
|
#define LAMINAR_TEMPDIR_H_
|
|
|
|
|
2020-07-03 05:31:44 +00:00
|
|
|
#include "log.h"
|
|
|
|
|
2018-09-28 07:36:10 +00:00
|
|
|
#include <kj/filesystem.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
class TempDir {
|
|
|
|
public:
|
|
|
|
TempDir() :
|
|
|
|
path(mkdtemp()),
|
2022-01-21 19:05:54 +00:00
|
|
|
fs(kj::newDiskFilesystem()->getRoot().openSubdir(path, kj::WriteMode::MODIFY))
|
2018-09-28 07:36:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~TempDir() noexcept {
|
|
|
|
kj::newDiskFilesystem()->getRoot().remove(path);
|
|
|
|
}
|
2022-01-21 19:05:54 +00:00
|
|
|
void init() {
|
|
|
|
// set up empty directory structure
|
|
|
|
fs->openSubdir(kj::Path{"cfg"}, kj::WriteMode::CREATE);
|
|
|
|
fs->openSubdir(kj::Path{"cfg", "jobs"}, kj::WriteMode::CREATE);
|
2022-01-21 19:07:41 +00:00
|
|
|
fs->openSubdir(kj::Path{"cfg", "contexts"}, kj::WriteMode::CREATE);
|
2022-01-21 19:05:54 +00:00
|
|
|
}
|
|
|
|
void clean() {
|
|
|
|
// rm -rf in config folder
|
|
|
|
for(kj::StringPtr name : fs->listNames()) {
|
|
|
|
fs->remove(kj::Path{name});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 07:36:10 +00:00
|
|
|
kj::Path path;
|
|
|
|
kj::Own<const kj::Directory> fs;
|
|
|
|
private:
|
|
|
|
static kj::Path mkdtemp() {
|
|
|
|
char dir[] = "/tmp/laminar-test-XXXXXX";
|
2020-07-03 05:31:44 +00:00
|
|
|
LASSERT(::mkdtemp(dir) != nullptr, "mkdtemp failed");
|
2018-09-28 07:36:10 +00:00
|
|
|
return kj::Path::parse(&dir[1]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // LAMINAR_TEMPDIR_H_
|