ladybird/Libraries/LibIPC/File.cpp
Timothy Flynn 674075f79e Everywhere: Remove LibCore/System.h includes from header files
This reduces the number of compilation jobs when System.h changes from
about 750 to 60. (There are still a large number of linker jobs.)
2025-12-04 15:40:46 +00:00

71 lines
1.4 KiB
C++

/*
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2023-2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/File.h>
namespace IPC {
File File::adopt_file(NonnullOwnPtr<Core::File> file)
{
return File(file->leak_fd());
}
File File::adopt_fd(int fd)
{
return File(fd);
}
ErrorOr<File> File::clone_fd(int fd)
{
int new_fd = TRY(Core::System::dup(fd));
return File(new_fd);
}
File::File(int fd)
: m_fd(fd)
{
}
File::File(File&& other)
: m_fd(exchange(other.m_fd, -1))
{
}
File& File::operator=(File&& other)
{
if (this != &other) {
m_fd = exchange(other.m_fd, -1);
}
return *this;
}
File::~File()
{
if (m_fd != -1)
(void)Core::System::close(m_fd);
}
// FIXME: IPC::Files transferred over the wire always set O_CLOEXEC during decoding. Perhaps we should add an option to
// allow the receiver to decide whether to make it O_CLOEXEC or not. Or an attribute in the .ipc file?
ErrorOr<void> File::clear_close_on_exec()
{
return Core::System::set_close_on_exec(m_fd, false);
}
template<>
ErrorOr<File> decode(Decoder& decoder)
{
auto file = decoder.files().dequeue();
TRY(Core::System::set_close_on_exec(file.fd(), true));
return file;
}
}