LibCore: Add utility class for temporary files and directories
Core::TempFile is a RAII type that creates a temporary file or directory on construction and deletes it on destruction.
This commit is contained in:
parent
28a6686f2c
commit
95545648bd
Notes:
sideshowbarker
2024-07-17 21:05:00 +09:00
Author: https://github.com/itamar8910 Commit: https://github.com/SerenityOS/serenity/commit/95545648bdf Pull-request: https://github.com/SerenityOS/serenity/pull/11726 Reviewed-by: https://github.com/awesomekling
3 changed files with 97 additions and 0 deletions
|
@ -33,6 +33,7 @@ set(SOURCES
|
|||
System.cpp
|
||||
TCPServer.cpp
|
||||
TCPSocket.cpp
|
||||
TempFile.cpp
|
||||
Timer.cpp
|
||||
UDPServer.cpp
|
||||
UDPSocket.cpp
|
||||
|
|
59
Userland/Libraries/LibCore/TempFile.cpp
Normal file
59
Userland/Libraries/LibCore/TempFile.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Copyright (c) 2020-2021, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "TempFile.h"
|
||||
#include <AK/Random.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
NonnullOwnPtr<TempFile> TempFile::create(Type type)
|
||||
{
|
||||
return adopt_own(*new TempFile(type));
|
||||
}
|
||||
|
||||
String TempFile::create_temp(Type type)
|
||||
{
|
||||
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 String { name_template };
|
||||
}
|
||||
|
||||
TempFile::TempFile(Type type)
|
||||
: m_type(type)
|
||||
, m_path(create_temp(type))
|
||||
{
|
||||
}
|
||||
|
||||
TempFile::~TempFile()
|
||||
{
|
||||
File::RecursionMode recursion_allowed { File::RecursionMode::Disallowed };
|
||||
if (m_type == Type::Directory)
|
||||
recursion_allowed = File::RecursionMode::Allowed;
|
||||
|
||||
auto rc = File::remove(m_path.characters(), recursion_allowed, false);
|
||||
if (rc.is_error()) {
|
||||
warnln("File::remove failed: {}", rc.error().string_literal());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
37
Userland/Libraries/LibCore/TempFile.h
Normal file
37
Userland/Libraries/LibCore/TempFile.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright (c) 2020-2021, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <AK/String.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
class TempFile {
|
||||
AK_MAKE_NONCOPYABLE(TempFile);
|
||||
AK_MAKE_NONMOVABLE(TempFile);
|
||||
|
||||
public:
|
||||
enum class Type {
|
||||
File,
|
||||
Directory
|
||||
};
|
||||
|
||||
static NonnullOwnPtr<TempFile> create(Type = Type::File);
|
||||
~TempFile();
|
||||
|
||||
String path() const { return m_path; }
|
||||
|
||||
private:
|
||||
TempFile(Type);
|
||||
static String create_temp(Type);
|
||||
|
||||
Type m_type { Type::File };
|
||||
String m_path;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue