mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-24 13:35:12 +00:00
LibCore: Improve the TempFile
wrapper
- We were using primitive versions of mkstemp and mkdtemp, they have been converted to use LibCore::System. - If an error occurred whilst creating a temporary directory or file, it was thrown and the program would crash. Now, we use ErrorOr<T> so that the caller can handle the error accordingly - The `Type` enumeration has been made private, and `create_temp` has been "split" (although rewritten) into create_temp_directory and create_temp_file. The old pattern of TempFile::create_temp(Type::File) felt a bit awkward, and TempFile::create_temp_file() feels a bit nicer to use! :^) Once the Core::Filesystem PR is merged (#17789), it would be better for this helper to be merged in with that. But until then, this is a nice improvement.
This commit is contained in:
parent
5072a2280d
commit
de18485a2f
Notes:
sideshowbarker
2024-07-16 23:34:44 +09:00
Author: https://github.com/caoimhebyrne Commit: https://github.com/SerenityOS/serenity/commit/de18485a2f Pull-request: https://github.com/SerenityOS/serenity/pull/17905 Reviewed-by: https://github.com/linusg ✅
2 changed files with 37 additions and 48 deletions
|
@ -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 <AK/Random.h>
|
||||
#include <LibCore/DeprecatedFile.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <LibCore/System.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
NonnullOwnPtr<TempFile> TempFile::create(Type type)
|
||||
ErrorOr<NonnullOwnPtr<TempFile>> 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<NonnullOwnPtr<TempFile>> 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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 <AK/DeprecatedString.h>
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/String.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
class TempFile {
|
||||
AK_MAKE_NONCOPYABLE(TempFile);
|
||||
AK_MAKE_NONMOVABLE(TempFile);
|
||||
|
||||
public:
|
||||
enum class Type {
|
||||
File,
|
||||
Directory
|
||||
};
|
||||
static ErrorOr<NonnullOwnPtr<TempFile>> create_temp_directory();
|
||||
static ErrorOr<NonnullOwnPtr<TempFile>> create_temp_file();
|
||||
|
||||
static NonnullOwnPtr<TempFile> 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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue