blob: 9f433acaea86adbba5cc68a4eba8d72654f8d30d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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));
}
}
|