@@ -27,7 +27,6 @@ pub const File = struct {
27
27
28
28
pub const OpenError = os .WindowsOpenError || os .PosixOpenError ;
29
29
30
- /// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
31
30
/// Call close to clean up.
32
31
pub fn openRead (path : []const u8 ) OpenError ! File {
33
32
if (is_posix ) {
@@ -49,15 +48,14 @@ pub const File = struct {
49
48
}
50
49
51
50
/// 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 );
54
53
}
55
54
56
55
/// If the path does not exist it will be created.
57
56
/// 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.
59
57
/// 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 {
61
59
if (is_posix ) {
62
60
const flags = posix .O_LARGEFILE | posix .O_WRONLY | posix .O_CREAT | posix .O_CLOEXEC | posix .O_TRUNC ;
63
61
const fd = try os .posixOpen (path , flags , file_mode );
@@ -78,16 +76,14 @@ pub const File = struct {
78
76
79
77
/// If the path does not exist it will be created.
80
78
/// 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.
82
79
/// 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 {
84
81
if (is_posix ) {
85
82
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 );
87
84
return openHandle (fd );
88
85
} else if (is_windows ) {
89
86
const handle = try os .windowsOpen (
90
- allocator ,
91
87
path ,
92
88
windows .GENERIC_WRITE ,
93
89
windows .FILE_SHARE_WRITE | windows .FILE_SHARE_READ | windows .FILE_SHARE_DELETE ,
@@ -117,12 +113,13 @@ pub const File = struct {
117
113
Unexpected ,
118
114
};
119
115
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
+ }
124
121
if (is_posix ) {
125
- const result = posix .access (path_with_null . ptr , posix .F_OK );
122
+ const result = posix .access (path , posix .F_OK );
126
123
const err = posix .getErrno (result );
127
124
switch (err ) {
128
125
0 = > return ,
@@ -141,7 +138,7 @@ pub const File = struct {
141
138
else = > return os .unexpectedErrorPosix (err ),
142
139
}
143
140
} 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 ) {
145
142
return ;
146
143
}
147
144
@@ -158,6 +155,21 @@ pub const File = struct {
158
155
}
159
156
}
160
157
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
+
161
173
/// Upon success, the stream is in an uninitialized state. To continue using it,
162
174
/// you must use the open() function.
163
175
pub fn close (self : * File ) void {
0 commit comments