diff options
author | Cameron Youell <cameronyouell@gmail.com> | 2023-03-22 02:36:18 +1100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-03-21 19:03:21 +0000 |
commit | 752f06f228119a70c59e844dcd2b9560e757b0ff (patch) | |
tree | 639eb770fb4db090adc69bfa77a19655fea4fa77 /Userland/Libraries/LibFileSystem/TempFile.cpp | |
parent | 492e5c3c14e4c15c2a655b876f4c77c1df30bb00 (diff) | |
download | serenity-752f06f228119a70c59e844dcd2b9560e757b0ff.zip |
LibFileSystem: Move `TempFile` from `LibCore` to `LibFileSystem`
As suggested in commit de18485
Diffstat (limited to 'Userland/Libraries/LibFileSystem/TempFile.cpp')
-rw-r--r-- | Userland/Libraries/LibFileSystem/TempFile.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Userland/Libraries/LibFileSystem/TempFile.cpp b/Userland/Libraries/LibFileSystem/TempFile.cpp new file mode 100644 index 0000000000..9f433acaea --- /dev/null +++ b/Userland/Libraries/LibFileSystem/TempFile.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2020-2023, the SerenityOS developers. + * Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibCore/System.h> +#include <LibFileSystem/FileSystem.h> +#include <LibFileSystem/TempFile.h> + +namespace FileSystem { + +TempFile::~TempFile() +{ + // Temporary files aren't removed by anyone else, so we must do it ourselves. + auto recursion_mode = RecursionMode::Disallowed; + if (m_type == Type::Directory) + recursion_mode = RecursionMode::Allowed; + + auto result = FileSystem::remove(m_path, recursion_mode); + if (result.is_error()) + warnln("Removal of temporary file failed '{}': {}", m_path, result.error().string_literal()); +} + +ErrorOr<NonnullOwnPtr<TempFile>> TempFile::create_temp_directory() +{ + char pattern[] = "/tmp/tmp.XXXXXX"; + + auto path = TRY(Core::System::mkdtemp(pattern)); + return adopt_nonnull_own_or_enomem(new (nothrow) TempFile(Type::Directory, path)); +} + +ErrorOr<NonnullOwnPtr<TempFile>> TempFile::create_temp_file() +{ + char file_path[] = "/tmp/tmp.XXXXXX"; + TRY(Core::System::mkstemp(file_path)); + + auto string = TRY(String::from_utf8({ file_path, sizeof file_path })); + return adopt_nonnull_own_or_enomem(new (nothrow) TempFile(Type::File, string)); +} + +} |