mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:47:45 +00:00
LibFileSystem: Move TempFile
from LibCore
to LibFileSystem
As suggested in commit de18485
This commit is contained in:
parent
492e5c3c14
commit
752f06f228
6 changed files with 22 additions and 23 deletions
|
@ -1,5 +1,6 @@
|
|||
set(SOURCES
|
||||
FileSystem.cpp
|
||||
TempFile.cpp
|
||||
)
|
||||
|
||||
serenity_lib(LibFileSystem filesystem)
|
||||
|
|
43
Userland/Libraries/LibFileSystem/TempFile.cpp
Normal file
43
Userland/Libraries/LibFileSystem/TempFile.cpp
Normal file
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
41
Userland/Libraries/LibFileSystem/TempFile.h
Normal file
41
Userland/Libraries/LibFileSystem/TempFile.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2020-2023, the SerenityOS developers.
|
||||
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Forward.h>
|
||||
#include <AK/String.h>
|
||||
|
||||
namespace FileSystem {
|
||||
|
||||
class TempFile {
|
||||
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<TempFile>> create_temp_directory();
|
||||
static ErrorOr<NonnullOwnPtr<TempFile>> create_temp_file();
|
||||
|
||||
~TempFile();
|
||||
|
||||
String const& path() const { return m_path; }
|
||||
|
||||
private:
|
||||
enum class Type {
|
||||
Directory,
|
||||
File
|
||||
};
|
||||
|
||||
TempFile(Type type, String path)
|
||||
: m_type(type)
|
||||
, m_path(move(path))
|
||||
{
|
||||
}
|
||||
|
||||
Type m_type;
|
||||
String m_path;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue