pub const Reader = struct A length for buffer that allows all implementations to function.
pub fn init(dir: Dir, buffer: []align(@alignOf(usize)) u8) ReaderAsserts that buffer has length at least min_buffer_len.
pub fn read(r: *Reader, io: Io, buffer: []Entry) Error!usizeAll Entry.name are invalidated with the next call to read or next.
pub fn next(r: *Reader, io: Io) Error!?EntryEntry.name is invalidated with the next call to read or next.
pub fn reset(r: *Reader) void97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170pub const Reader = struct {
dir: Dir,
state: State,
/// Stores I/O implementation specific data.
buffer: []align(@alignOf(usize)) u8,
/// Index of next entry in `buffer`.
index: usize,
/// Fill position of `buffer`.
end: usize,
/// A length for `buffer` that allows all implementations to function.
pub const min_buffer_len = switch (native_os) {
.linux => std.mem.alignForward(usize, @sizeOf(std.os.linux.dirent64), 8) +
std.mem.alignForward(usize, max_name_bytes, 8),
.windows => len: {
const max_info_len = @sizeOf(std.os.windows.FILE_BOTH_DIR_INFORMATION) + std.os.windows.NAME_MAX * 2;
const info_align = @alignOf(std.os.windows.FILE_BOTH_DIR_INFORMATION);
const reserved_len = std.mem.alignForward(usize, max_name_bytes, info_align) - max_info_len;
break :len std.mem.alignForward(usize, reserved_len, info_align) + max_info_len;
},
.wasi => @sizeOf(std.os.wasi.dirent_t) +
std.mem.alignForward(usize, max_name_bytes, @alignOf(std.os.wasi.dirent_t)),
.openbsd => std.c.S.BLKSIZE,
else => if (builtin.link_libc) @sizeOf(std.c.dirent) else std.mem.alignForward(usize, max_name_bytes, @alignOf(usize)),
};
pub const State = enum {
/// Indicates the next call to `read` should rewind and start over the
/// directory listing.
reset,
reading,
finished,
};
pub const Error = error{
AccessDenied,
PermissionDenied,
SystemResources,
} || Io.UnexpectedError || Io.Cancelable;
/// Asserts that `buffer` has length at least `min_buffer_len`.
pub fn init(dir: Dir, buffer: []align(@alignOf(usize)) u8) Reader {
assert(buffer.len >= min_buffer_len);
return .{
.dir = dir,
.state = .reset,
.index = 0,
.end = 0,
.buffer = buffer,
};
}
/// All `Entry.name` are invalidated with the next call to `read` or
/// `next`.
pub fn read(r: *Reader, io: Io, buffer: []Entry) Error!usize {
return io.vtable.dirRead(io.userdata, r, buffer);
}
/// `Entry.name` is invalidated with the next call to `read` or `next`.
pub fn next(r: *Reader, io: Io) Error!?Entry {
var buffer: [1]Entry = undefined;
while (true) {
const n = try read(r, io, &buffer);
if (n == 1) return buffer[0];
if (r.state == .finished) return null;
}
}
pub fn reset(r: *Reader) void {
r.state = .reset;
r.index = 0;
r.end = 0;
}
}