diff --git a/Userland/Libraries/LibCore/TempFile.cpp b/Userland/Libraries/LibCore/TempFile.cpp index 527ad911e4..8558847e43 100644 --- a/Userland/Libraries/LibCore/TempFile.cpp +++ b/Userland/Libraries/LibCore/TempFile.cpp @@ -1,57 +1,42 @@ /* - * Copyright (c) 2020-2021, the SerenityOS developers. + * Copyright (c) 2020-2023, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ #include "TempFile.h" -#include #include -#include -#include -#include +#include namespace Core { -NonnullOwnPtr TempFile::create(Type type) +ErrorOr> TempFile::create_temp_directory() { - return adopt_own(*new TempFile(type)); + char pattern[] = "/tmp/tmp.XXXXXX"; + + auto path = TRY(Core::System::mkdtemp(pattern)); + return adopt_nonnull_own_or_enomem(new (nothrow) TempFile(Type::Directory, path)); } -DeprecatedString TempFile::create_temp(Type type) +ErrorOr> TempFile::create_temp_file() { - char name_template[] = "/tmp/tmp.XXXXXX"; - switch (type) { - case Type::File: { - auto fd = mkstemp(name_template); - VERIFY(fd >= 0); - close(fd); - break; - } - case Type::Directory: { - auto fd = mkdtemp(name_template); - VERIFY(fd != nullptr); - break; - } - } - return DeprecatedString { name_template }; -} + char file_path[] = "/tmp/tmp.XXXXXX"; + TRY(Core::System::mkstemp(file_path)); -TempFile::TempFile(Type type) - : m_type(type) - , m_path(create_temp(type)) -{ + auto string = TRY(String::from_utf8({ file_path, sizeof file_path })); + return adopt_nonnull_own_or_enomem(new (nothrow) TempFile(Type::File, string)); } TempFile::~TempFile() { - DeprecatedFile::RecursionMode recursion_allowed { DeprecatedFile::RecursionMode::Disallowed }; + // Temporary files aren't removed by anyone else, so we must do it ourselves. + auto recursion_mode = DeprecatedFile::RecursionMode::Disallowed; if (m_type == Type::Directory) - recursion_allowed = DeprecatedFile::RecursionMode::Allowed; + recursion_mode = DeprecatedFile::RecursionMode::Allowed; - auto rc = DeprecatedFile::remove(m_path, recursion_allowed); - if (rc.is_error()) { - warnln("File::remove failed: {}", rc.error().string_literal()); + auto result = DeprecatedFile::remove(m_path, recursion_mode); + if (result.is_error()) { + warnln("Removal of temporary file failed: {}", result.error().string_literal()); } } diff --git a/Userland/Libraries/LibCore/TempFile.h b/Userland/Libraries/LibCore/TempFile.h index 014b36866b..ac1743a165 100644 --- a/Userland/Libraries/LibCore/TempFile.h +++ b/Userland/Libraries/LibCore/TempFile.h @@ -1,37 +1,41 @@ /* - * Copyright (c) 2020-2021, the SerenityOS developers. + * Copyright (c) 2020-2023, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once -#include -#include +#include +#include +#include namespace Core { class TempFile { - AK_MAKE_NONCOPYABLE(TempFile); - AK_MAKE_NONMOVABLE(TempFile); public: - enum class Type { - File, - Directory - }; + static ErrorOr> create_temp_directory(); + static ErrorOr> create_temp_file(); - static NonnullOwnPtr create(Type = Type::File); ~TempFile(); - DeprecatedString path() const { return m_path; } + String const& path() const { return m_path; } private: - TempFile(Type); - static DeprecatedString create_temp(Type); + enum class Type { + Directory, + File + }; - Type m_type { Type::File }; - DeprecatedString m_path; + TempFile(Type type, String path) + : m_type(type) + , m_path(move(path)) + { + } + + Type m_type; + String m_path; }; }