mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-23 01:32:14 -05:00
d43a7eae54
As usual, this removes many unused includes and moves used includes further down the chain.
58 lines
1.3 KiB
C++
58 lines
1.3 KiB
C++
/*
|
|
* Copyright (c) 2020-2021, the SerenityOS developers.
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include "TempFile.h"
|
|
#include <AK/Random.h>
|
|
#include <LibCore/DeprecatedFile.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));
|
|
}
|
|
|
|
DeprecatedString 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 DeprecatedString { name_template };
|
|
}
|
|
|
|
TempFile::TempFile(Type type)
|
|
: m_type(type)
|
|
, m_path(create_temp(type))
|
|
{
|
|
}
|
|
|
|
TempFile::~TempFile()
|
|
{
|
|
DeprecatedFile::RecursionMode recursion_allowed { DeprecatedFile::RecursionMode::Disallowed };
|
|
if (m_type == Type::Directory)
|
|
recursion_allowed = DeprecatedFile::RecursionMode::Allowed;
|
|
|
|
auto rc = DeprecatedFile::remove(m_path, recursion_allowed);
|
|
if (rc.is_error()) {
|
|
warnln("File::remove failed: {}", rc.error().string_literal());
|
|
}
|
|
}
|
|
|
|
}
|