Skip to content

Commit 613a142

Browse files
committed
io: use struct FileAt instead of separate FileDescriptor/path parameters
1 parent 86e1489 commit 613a142

File tree

5 files changed

+47
-34
lines changed

5 files changed

+47
-34
lines changed

src/io/FileDescriptor.cxx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// author: Max Kellermann <max.kellermann@gmail.com>
33

44
#include "FileDescriptor.hxx"
5+
#include "FileAt.hxx"
56
#include "UniqueFileDescriptor.hxx"
67
#include "system/Error.hxx"
78

@@ -65,10 +66,9 @@ FileDescriptor::IsSocket() const noexcept
6566
#ifdef __linux__
6667

6768
bool
68-
FileDescriptor::Open(FileDescriptor dir, const char *pathname,
69-
int flags, mode_t mode) noexcept
69+
FileDescriptor::Open(FileAt file, int flags, mode_t mode) noexcept
7070
{
71-
fd = ::openat(dir.Get(), pathname, flags | O_NOCTTY | O_CLOEXEC | O_NONBLOCK, mode);
71+
fd = ::openat(file.directory.Get(), file.name, flags | O_NOCTTY | O_CLOEXEC | O_NONBLOCK, mode);
7272
return IsDefined();
7373
}
7474

@@ -101,9 +101,9 @@ FileDescriptor::OpenReadOnly(const char *pathname) noexcept
101101
#ifdef __linux__
102102

103103
bool
104-
FileDescriptor::OpenReadOnly(FileDescriptor dir, const char *pathname) noexcept
104+
FileDescriptor::OpenReadOnly(FileAt file) noexcept
105105
{
106-
return Open(dir, pathname, O_RDONLY);
106+
return Open(file, O_RDONLY);
107107
}
108108

109109
bool

src/io/FileDescriptor.hxx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
struct iovec;
1818
class UniqueFileDescriptor;
19+
struct FileAt;
1920

2021
/**
2122
* An OO wrapper for a UNIX file descriptor.
@@ -96,8 +97,7 @@ public:
9697

9798
#ifdef __linux__
9899
[[nodiscard]]
99-
bool Open(FileDescriptor dir, const char *pathname,
100-
int flags, mode_t mode=0666) noexcept;
100+
bool Open(FileAt file, int flags, mode_t mode=0666) noexcept;
101101
#endif
102102

103103
[[nodiscard]]
@@ -113,8 +113,7 @@ public:
113113

114114
#ifdef __linux__
115115
[[nodiscard]]
116-
bool OpenReadOnly(FileDescriptor dir,
117-
const char *pathname) noexcept;
116+
bool OpenReadOnly(FileAt file) noexcept;
118117

119118
[[nodiscard]]
120119
static bool CreatePipe(FileDescriptor &r, FileDescriptor &w,

src/io/FileOutputStream.cxx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#endif
1111

1212
#ifdef __linux__
13+
#include "FileAt.hxx"
1314
#include "io/linux/ProcPath.hxx"
1415
#include <fcntl.h>
1516
#endif
@@ -175,7 +176,7 @@ OpenTempFile(FileDescriptor directory_fd,
175176
FileDescriptor &fd, Path path) noexcept
176177
{
177178
if (directory_fd != FileDescriptor(AT_FDCWD))
178-
return fd.Open(directory_fd, ".", O_TMPFILE|O_WRONLY, 0666);
179+
return fd.Open({directory_fd, "."}, O_TMPFILE|O_WRONLY, 0666);
179180

180181
const auto directory = path.GetDirectoryName();
181182
if (directory.IsNull())
@@ -204,9 +205,13 @@ FileOutputStream::OpenCreate(bool visible)
204205

205206
if (fd.Open(
206207
#ifdef __linux__
207-
directory_fd,
208+
{
209+
directory_fd,
210+
#endif
211+
tmp_path.c_str(),
212+
#ifdef __linux__
213+
},
208214
#endif
209-
tmp_path.c_str(),
210215
O_WRONLY|O_CREAT|O_EXCL,
211216
0666))
212217
return;
@@ -216,9 +221,13 @@ FileOutputStream::OpenCreate(bool visible)
216221
/* fall back to plain POSIX */
217222
if (!fd.Open(
218223
#ifdef __linux__
219-
directory_fd,
224+
{
225+
directory_fd,
226+
#endif
227+
GetPath().c_str(),
228+
#ifdef __linux__
229+
},
220230
#endif
221-
GetPath().c_str(),
222231
O_WRONLY|O_CREAT|O_TRUNC,
223232
0666))
224233
throw FmtErrno("Failed to create {}", GetPath());
@@ -233,9 +242,14 @@ FileOutputStream::OpenAppend(bool create)
233242

234243
if (!fd.Open(
235244
#ifdef __linux__
236-
directory_fd,
245+
{
246+
directory_fd,
247+
#endif
248+
path.c_str(),
249+
#ifdef __linux__
250+
},
237251
#endif
238-
path.c_str(), flags))
252+
flags))
239253
throw FmtErrno("Failed to append to {}", path);
240254
}
241255

src/io/Open.cxx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
#include "lib/fmt/SystemError.hxx"
77

88
#ifdef __linux__
9+
#include "FileAt.hxx"
910
#include "system/linux/Features.h"
1011
#ifdef HAVE_OPENAT2
11-
#include "FileAt.hxx"
1212
#include "system/linux/openat2.h"
1313
#endif // HAVE_OPENAT2
1414
#endif // __linux__
@@ -62,41 +62,41 @@ OpenPath(const char *path, int flags)
6262
}
6363

6464
UniqueFileDescriptor
65-
OpenPath(FileDescriptor directory, const char *name, int flags)
65+
OpenPath(FileAt file, int flags)
6666
{
6767
UniqueFileDescriptor fd;
68-
if (!fd.Open(directory, name, O_PATH|flags))
69-
throw FmtErrno("Failed to open {:?}", name);
68+
if (!fd.Open(file, O_PATH|flags))
69+
throw FmtErrno("Failed to open {:?}", file.name);
7070

7171
return fd;
7272
}
7373

7474
UniqueFileDescriptor
75-
OpenReadOnly(FileDescriptor directory, const char *name, int flags)
75+
OpenReadOnly(FileAt file, int flags)
7676
{
7777
UniqueFileDescriptor fd;
78-
if (!fd.Open(directory, name, O_RDONLY|flags))
79-
throw FmtErrno("Failed to open {:?}", name);
78+
if (!fd.Open(file, O_RDONLY|flags))
79+
throw FmtErrno("Failed to open {:?}", file.name);
8080

8181
return fd;
8282
}
8383

8484
UniqueFileDescriptor
85-
OpenWriteOnly(FileDescriptor directory, const char *name, int flags)
85+
OpenWriteOnly(FileAt file, int flags)
8686
{
8787
UniqueFileDescriptor fd;
88-
if (!fd.Open(directory, name, O_WRONLY|flags))
89-
throw FmtErrno("Failed to open {:?}", name);
88+
if (!fd.Open(file, O_WRONLY|flags))
89+
throw FmtErrno("Failed to open {:?}", file.name);
9090

9191
return fd;
9292
}
9393

9494
UniqueFileDescriptor
95-
OpenDirectory(FileDescriptor directory, const char *name, int flags)
95+
OpenDirectory(FileAt file, int flags)
9696
{
9797
UniqueFileDescriptor fd;
98-
if (!fd.Open(directory, name, O_DIRECTORY|O_RDONLY|flags))
99-
throw FmtErrno("Failed to open {:?}", name);
98+
if (!fd.Open(file, O_DIRECTORY|O_RDONLY|flags))
99+
throw FmtErrno("Failed to open {:?}", file.name);
100100

101101
return fd;
102102
}

src/io/Open.hxx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
class FileDescriptor;
77
class UniqueFileDescriptor;
8+
struct FileAt;
89

910
UniqueFileDescriptor
1011
OpenReadOnly(const char *path, int flags=0);
@@ -25,19 +26,18 @@ UniqueFileDescriptor
2526
OpenPath(const char *path, int flags=0);
2627

2728
UniqueFileDescriptor
28-
OpenPath(FileDescriptor directory, const char *name, int flags=0);
29+
OpenPath(FileAt file, int flags=0);
2930

3031
UniqueFileDescriptor
31-
OpenReadOnly(FileDescriptor directory, const char *name, int flags=0);
32+
OpenReadOnly(FileAt file, int flags=0);
3233

3334
UniqueFileDescriptor
34-
OpenWriteOnly(FileDescriptor directory, const char *name, int flags=0);
35+
OpenWriteOnly(FileAt file, int flags=0);
3536

3637
UniqueFileDescriptor
37-
OpenDirectory(FileDescriptor directory, const char *name, int flags=0);
38+
OpenDirectory(FileAt file, int flags=0);
3839

3940
struct opwn_how;
40-
struct FileAt;
4141

4242
/**
4343
* Wrapper for openat2() which converts the returned file descriptor

0 commit comments

Comments
 (0)