Skip to content

Commit bda5539

Browse files
committed
*WIP* std.os assumes comptime-known max path size
this allows us to remove the requirement of allocators for a lot of functions See #1392
1 parent 3029363 commit bda5539

File tree

8 files changed

+254
-251
lines changed

8 files changed

+254
-251
lines changed

std/debug/index.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace {
340340
}
341341
}
342342

343-
fn printLineFromFile(allocator: *mem.Allocator, out_stream: var, line_info: *const LineInfo) !void {
343+
fn printLineFromFile(out_stream: var, line_info: *const LineInfo) !void {
344344
var f = try os.File.openRead(line_info.file_name);
345345
defer f.close();
346346
// TODO fstat and make sure that the file has the correct size

std/io_test.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test "write a file, read it, then delete it" {
1616
prng.random.bytes(data[0..]);
1717
const tmp_file_name = "temp_test_file.txt";
1818
{
19-
var file = try os.File.openWrite(allocator, tmp_file_name);
19+
var file = try os.File.openWrite(tmp_file_name);
2020
defer file.close();
2121

2222
var file_out_stream = io.FileOutStream.init(&file);
@@ -63,7 +63,7 @@ test "BufferOutStream" {
6363
}
6464

6565
test "SliceInStream" {
66-
const bytes = []const u8 { 1, 2, 3, 4, 5, 6, 7 };
66+
const bytes = []const u8{ 1, 2, 3, 4, 5, 6, 7 };
6767
var ss = io.SliceInStream.init(bytes);
6868

6969
var dest: [4]u8 = undefined;
@@ -81,7 +81,7 @@ test "SliceInStream" {
8181
}
8282

8383
test "PeekStream" {
84-
const bytes = []const u8 { 1, 2, 3, 4, 5, 6, 7, 8 };
84+
const bytes = []const u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
8585
var ss = io.SliceInStream.init(bytes);
8686
var ps = io.PeekStream(2, io.SliceInStream.Error).init(&ss.stream);
8787

std/os/file.zig

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub const File = struct {
2727

2828
pub const OpenError = os.WindowsOpenError || os.PosixOpenError;
2929

30-
/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
3130
/// Call close to clean up.
3231
pub fn openRead(path: []const u8) OpenError!File {
3332
if (is_posix) {
@@ -49,15 +48,14 @@ pub const File = struct {
4948
}
5049

5150
/// Calls `openWriteMode` with os.File.default_mode for the mode.
52-
pub fn openWrite(allocator: *mem.Allocator, path: []const u8) OpenError!File {
53-
return openWriteMode(allocator, path, os.File.default_mode);
51+
pub fn openWrite(path: []const u8) OpenError!File {
52+
return openWriteMode(path, os.File.default_mode);
5453
}
5554

5655
/// If the path does not exist it will be created.
5756
/// If a file already exists in the destination it will be truncated.
58-
/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
5957
/// Call close to clean up.
60-
pub fn openWriteMode(allocator: *mem.Allocator, path: []const u8, file_mode: Mode) OpenError!File {
58+
pub fn openWriteMode(path: []const u8, file_mode: Mode) OpenError!File {
6159
if (is_posix) {
6260
const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC;
6361
const fd = try os.posixOpen(path, flags, file_mode);
@@ -78,16 +76,14 @@ pub const File = struct {
7876

7977
/// If the path does not exist it will be created.
8078
/// If a file already exists in the destination this returns OpenError.PathAlreadyExists
81-
/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
8279
/// Call close to clean up.
83-
pub fn openWriteNoClobber(allocator: *mem.Allocator, path: []const u8, file_mode: Mode) OpenError!File {
80+
pub fn openWriteNoClobber(path: []const u8, file_mode: Mode) OpenError!File {
8481
if (is_posix) {
8582
const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_EXCL;
86-
const fd = try os.posixOpen(allocator, path, flags, file_mode);
83+
const fd = try os.posixOpen(path, flags, file_mode);
8784
return openHandle(fd);
8885
} else if (is_windows) {
8986
const handle = try os.windowsOpen(
90-
allocator,
9187
path,
9288
windows.GENERIC_WRITE,
9389
windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE,
@@ -117,12 +113,13 @@ pub const File = struct {
117113
Unexpected,
118114
};
119115

120-
pub fn access(allocator: *mem.Allocator, path: []const u8) AccessError!void {
121-
const path_with_null = try std.cstr.addNullByte(allocator, path);
122-
defer allocator.free(path_with_null);
123-
116+
pub fn accessC(path: [*]const u8) AccessError!void {
117+
if (is_windows) {
118+
// this needs to convert to UTF-16LE and call accessW
119+
@compileError("TODO support windows");
120+
}
124121
if (is_posix) {
125-
const result = posix.access(path_with_null.ptr, posix.F_OK);
122+
const result = posix.access(path, posix.F_OK);
126123
const err = posix.getErrno(result);
127124
switch (err) {
128125
0 => return,
@@ -141,7 +138,7 @@ pub const File = struct {
141138
else => return os.unexpectedErrorPosix(err),
142139
}
143140
} else if (is_windows) {
144-
if (os.windows.GetFileAttributesA(path_with_null.ptr) != os.windows.INVALID_FILE_ATTRIBUTES) {
141+
if (os.windows.GetFileAttributesA(path) != os.windows.INVALID_FILE_ATTRIBUTES) {
145142
return;
146143
}
147144

@@ -158,6 +155,21 @@ pub const File = struct {
158155
}
159156
}
160157

158+
pub fn access(path: []const u8) AccessError!void {
159+
if (is_windows) {
160+
// this needs to convert to UTF-16LE and call accessW
161+
@compileError("TODO support windows");
162+
}
163+
if (is_posix) {
164+
var path_with_null: [posix.PATH_MAX]u8 = undefined;
165+
if (path.len >= posix.PATH_MAX) return error.NameTooLong;
166+
mem.copy(u8, path_with_null[0..], path);
167+
path_with_null[path.len] = 0;
168+
return accessC(&path_with_null);
169+
}
170+
@compileError("TODO implement access for this OS");
171+
}
172+
161173
/// Upon success, the stream is in an uninitialized state. To continue using it,
162174
/// you must use the open() function.
163175
pub fn close(self: *File) void {

0 commit comments

Comments
 (0)