2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2018-08-21 18:16:52 +02:00
|
|
|
|
|
|
|
#include "Open.hxx"
|
|
|
|
#include "UniqueFileDescriptor.hxx"
|
2022-11-28 23:05:15 +01:00
|
|
|
#include "lib/fmt/SystemError.hxx"
|
2018-08-21 18:16:52 +02:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
UniqueFileDescriptor
|
2020-04-30 21:30:57 +02:00
|
|
|
OpenReadOnly(const char *path, int flags)
|
2018-08-21 18:16:52 +02:00
|
|
|
{
|
|
|
|
UniqueFileDescriptor fd;
|
2020-04-30 21:30:57 +02:00
|
|
|
if (!fd.Open(path, O_RDONLY|flags))
|
2022-11-28 18:49:35 +01:00
|
|
|
throw FmtErrno("Failed to open '{}'", path);
|
2018-08-21 18:16:52 +02:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2019-02-19 12:16:41 +01:00
|
|
|
UniqueFileDescriptor
|
|
|
|
OpenWriteOnly(const char *path, int flags)
|
|
|
|
{
|
|
|
|
UniqueFileDescriptor fd;
|
|
|
|
if (!fd.Open(path, O_WRONLY|flags))
|
2022-11-28 18:49:35 +01:00
|
|
|
throw FmtErrno("Failed to open '{}'", path);
|
2019-02-19 12:16:41 +01:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
UniqueFileDescriptor
|
|
|
|
OpenDirectory(const char *path, int flags)
|
|
|
|
{
|
|
|
|
UniqueFileDescriptor fd;
|
|
|
|
if (!fd.Open(path, O_DIRECTORY|O_RDONLY|flags))
|
2022-11-28 18:49:35 +01:00
|
|
|
throw FmtErrno("Failed to open '{}'", path);
|
2019-02-19 12:16:41 +01:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-08-21 18:16:52 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
|
|
|
|
UniqueFileDescriptor
|
|
|
|
OpenPath(const char *path, int flags)
|
|
|
|
{
|
|
|
|
UniqueFileDescriptor fd;
|
|
|
|
if (!fd.Open(path, O_PATH|flags))
|
2022-11-28 18:49:35 +01:00
|
|
|
throw FmtErrno("Failed to open '{}'", path);
|
2018-08-21 18:16:52 +02:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniqueFileDescriptor
|
|
|
|
OpenPath(FileDescriptor directory, const char *name, int flags)
|
|
|
|
{
|
|
|
|
UniqueFileDescriptor fd;
|
|
|
|
if (!fd.Open(directory, name, O_PATH|flags))
|
2022-11-28 18:49:35 +01:00
|
|
|
throw FmtErrno("Failed to open '{}'", name);
|
2018-08-21 18:16:52 +02:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniqueFileDescriptor
|
|
|
|
OpenReadOnly(FileDescriptor directory, const char *name, int flags)
|
|
|
|
{
|
|
|
|
UniqueFileDescriptor fd;
|
|
|
|
if (!fd.Open(directory, name, O_RDONLY|flags))
|
2022-11-28 18:49:35 +01:00
|
|
|
throw FmtErrno("Failed to open '{}'", name);
|
2018-08-21 18:16:52 +02:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2019-02-19 12:16:41 +01:00
|
|
|
UniqueFileDescriptor
|
|
|
|
OpenWriteOnly(FileDescriptor directory, const char *name, int flags)
|
|
|
|
{
|
|
|
|
UniqueFileDescriptor fd;
|
|
|
|
if (!fd.Open(directory, name, O_WRONLY|flags))
|
2022-11-28 18:49:35 +01:00
|
|
|
throw FmtErrno("Failed to open '{}'", name);
|
2019-02-19 12:16:41 +01:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2018-08-21 18:16:52 +02:00
|
|
|
UniqueFileDescriptor
|
|
|
|
OpenDirectory(FileDescriptor directory, const char *name, int flags)
|
|
|
|
{
|
|
|
|
UniqueFileDescriptor fd;
|
|
|
|
if (!fd.Open(directory, name, O_DIRECTORY|O_RDONLY|flags))
|
2022-11-28 18:49:35 +01:00
|
|
|
throw FmtErrno("Failed to open '{}'", name);
|
2018-08-21 18:16:52 +02:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|