1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:57:34 +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,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;
};
}