1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:54:57 +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:
Caoimhe 2023-03-18 00:05:35 +00:00 committed by Linus Groh
parent 5072a2280d
commit de18485a2f
2 changed files with 37 additions and 48 deletions

View file

@ -1,57 +1,42 @@
/* /*
* Copyright (c) 2020-2021, the SerenityOS developers. * Copyright (c) 2020-2023, the SerenityOS developers.
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include "TempFile.h" #include "TempFile.h"
#include <AK/Random.h>
#include <LibCore/DeprecatedFile.h> #include <LibCore/DeprecatedFile.h>
#include <fcntl.h> #include <LibCore/System.h>
#include <stdlib.h>
#include <sys/stat.h>
namespace Core { 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"; char file_path[] = "/tmp/tmp.XXXXXX";
switch (type) { TRY(Core::System::mkstemp(file_path));
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 };
}
TempFile::TempFile(Type type) auto string = TRY(String::from_utf8({ file_path, sizeof file_path }));
: m_type(type) return adopt_nonnull_own_or_enomem(new (nothrow) TempFile(Type::File, string));
, m_path(create_temp(type))
{
} }
TempFile::~TempFile() 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) if (m_type == Type::Directory)
recursion_allowed = DeprecatedFile::RecursionMode::Allowed; recursion_mode = DeprecatedFile::RecursionMode::Allowed;
auto rc = DeprecatedFile::remove(m_path, recursion_allowed); auto result = DeprecatedFile::remove(m_path, recursion_mode);
if (rc.is_error()) { if (result.is_error()) {
warnln("File::remove failed: {}", rc.error().string_literal()); warnln("Removal of temporary file failed: {}", result.error().string_literal());
} }
} }

View file

@ -1,37 +1,41 @@
/* /*
* Copyright (c) 2020-2021, the SerenityOS developers. * Copyright (c) 2020-2023, the SerenityOS developers.
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#pragma once #pragma once
#include <AK/DeprecatedString.h> #include <AK/Error.h>
#include <AK/Noncopyable.h> #include <AK/NonnullOwnPtr.h>
#include <AK/String.h>
namespace Core { namespace Core {
class TempFile { class TempFile {
AK_MAKE_NONCOPYABLE(TempFile);
AK_MAKE_NONMOVABLE(TempFile);
public: public:
enum class Type { static ErrorOr<NonnullOwnPtr<TempFile>> create_temp_directory();
File, static ErrorOr<NonnullOwnPtr<TempFile>> create_temp_file();
Directory
};
static NonnullOwnPtr<TempFile> create(Type = Type::File);
~TempFile(); ~TempFile();
DeprecatedString path() const { return m_path; } String const& path() const { return m_path; }
private: private:
TempFile(Type); enum class Type {
static DeprecatedString create_temp(Type); Directory,
File
};
Type m_type { Type::File }; TempFile(Type type, String path)
DeprecatedString m_path; : m_type(type)
, m_path(move(path))
{
}
Type m_type;
String m_path;
}; };
} }