From 36ce7176c84395548ff091c00975a527a2a58072 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 1 Jul 2020 01:41:07 +0200 Subject: [PATCH] Make functions that create and return, remove the temporary dir. --- src/cgi.cpp | 9 ++------ src/files.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/files.hpp | 33 +++++++++++++++++++++++++++++ src/main.cpp | 13 +++++++++++- 4 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 src/files.cpp create mode 100644 src/files.hpp diff --git a/src/cgi.cpp b/src/cgi.cpp index 465cda0..bf9c528 100644 --- a/src/cgi.cpp +++ b/src/cgi.cpp @@ -15,6 +15,7 @@ */ #include "cgi.hpp" +#include "files.hpp" #include "fs-compat.hpp" #include @@ -61,14 +62,8 @@ entry_type parse_formdata() { throw runtime_error{"Filesize too big"}; } - string filepath{fs::temp_directory_path() - / "fediblock-backend-XXXXXX"}; - if (mkstemp(&filepath[0]) == -1) // mkstemp() modifies filepath. - { - throw runtime_error{"Could not open temporary file: " - + filepath}; - } + string filepath{get_tmpdir() / screenshot->getFilename()}; ofstream file{filepath, ios::binary}; if (file.good()) { diff --git a/src/files.cpp b/src/files.cpp new file mode 100644 index 0000000..1ac64dc --- /dev/null +++ b/src/files.cpp @@ -0,0 +1,58 @@ +/* This file is part of FediBlock-backend. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "files.hpp" +#include "fs-compat.hpp" + +#include +#include +#include +#include + +namespace FediBlock +{ + +using std::runtime_error; +using std::string; +using std::uintmax_t; + +string _tmpdir; + +fs::path get_tmpdir() +{ + if (_tmpdir.empty()) + { + _tmpdir = fs::temp_directory_path() / "fediblock-backend-XXXXXX"; + if (mkdtemp(&_tmpdir[0]) == nullptr) // mkdtemp() modifies _tmpdir. + { + throw runtime_error{"Could not create temporary directory: " + + _tmpdir}; + } + } + + return _tmpdir; +} + +void remove_tmpdir() +{ + if (fs::remove_all(get_tmpdir()) == static_cast(-1)) + { + throw runtime_error{"Couldn't remove temporary directory: " + + get_tmpdir().string()}; + } +} + +} // namespace FediBlock diff --git a/src/files.hpp b/src/files.hpp new file mode 100644 index 0000000..33a4872 --- /dev/null +++ b/src/files.hpp @@ -0,0 +1,33 @@ +/* This file is part of FediBlock-backend. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef FEDIBLOCK_BACKEND_FILES_HPP +#define FEDIBLOCK_BACKEND_FILES_HPP + +#include "fs-compat.hpp" + +namespace FediBlock +{ + +// Creates and returns the temporary directory. +[[nodiscard]] fs::path get_tmpdir(); + +// Remove the temporary directory. Throws runtime_error on error. +void remove_tmpdir(); + +} // namespace FediBlock + +#endif // FEDIBLOCK_BACKEND_FILES_HPP diff --git a/src/main.cpp b/src/main.cpp index d3b8972..9ed3a55 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,12 +15,16 @@ */ #include "cgi.hpp" +#include "files.hpp" #include "fs-compat.hpp" #include "json.hpp" +#include #include +using std::cerr; using std::cout; +using std::exception; using namespace FediBlock; @@ -57,7 +61,14 @@ int main() print_debug(entry); - fs::remove(entry.screenshot_filepath); + try + { + remove_tmpdir(); + } + catch (const exception &e) + { + cerr << e.what() << '\n'; + } return 0; }