@@ -28,6 +28,7 @@ pub const GetAppDataDirError = @import("fs/get_app_data_dir.zig").GetAppDataDirE
28
28
/// This represents the maximum size of a UTF-8 encoded file path.
29
29
/// All file system operations which return a path are guaranteed to
30
30
/// fit into a UTF-8 encoded array of this length.
31
+ /// The byte count includes room for a null sentinel byte.
31
32
pub const MAX_PATH_BYTES = switch (builtin .os ) {
32
33
.linux , .macosx , .ios , .freebsd , .netbsd , .dragonfly = > os .PATH_MAX ,
33
34
// Each UTF-16LE character may be expanded to 3 UTF-8 bytes.
@@ -227,7 +228,7 @@ pub const AtomicFile = struct {
227
228
try crypto .randomBytes (rand_buf [0.. ]);
228
229
b64_fs_encoder .encode (tmp_path_buf [dirname_component_len .. tmp_path_len ], rand_buf );
229
230
230
- const file = File .openWriteNoClobberC (& tmp_path_buf , mode ) catch | err | switch (err ) {
231
+ const file = File .openWriteNoClobberC (@ptrCast ([ * : 0 ] u8 , & tmp_path_buf ) , mode ) catch | err | switch (err ) {
231
232
error .PathAlreadyExists = > continue ,
232
233
// TODO zig should figure out that this error set does not include PathAlreadyExists since
233
234
// it is handled in the above switch
@@ -247,7 +248,7 @@ pub const AtomicFile = struct {
247
248
pub fn deinit (self : * AtomicFile ) void {
248
249
if (! self .finished ) {
249
250
self .file .close ();
250
- deleteFileC (& self .tmp_path_buf ) catch {};
251
+ deleteFileC (@ptrCast ([ * : 0 ] u8 , & self .tmp_path_buf ) ) catch {};
251
252
self .finished = true ;
252
253
}
253
254
}
@@ -258,11 +259,11 @@ pub const AtomicFile = struct {
258
259
self .finished = true ;
259
260
if (builtin .os == .windows ) {
260
261
const dest_path_w = try os .windows .sliceToPrefixedFileW (self .dest_path );
261
- const tmp_path_w = try os .windows .cStrToPrefixedFileW (& self .tmp_path_buf );
262
+ const tmp_path_w = try os .windows .cStrToPrefixedFileW (@ptrCast ([ * : 0 ] u8 , & self .tmp_path_buf ) );
262
263
return os .renameW (& tmp_path_w , & dest_path_w );
263
264
}
264
265
const dest_path_c = try os .toPosixPath (self .dest_path );
265
- return os .renameC (& self .tmp_path_buf , & dest_path_c );
266
+ return os .renameC (@ptrCast ([ * : 0 ] u8 , & self .tmp_path_buf ) , & dest_path_c );
266
267
}
267
268
};
268
269
@@ -274,12 +275,12 @@ pub fn makeDir(dir_path: []const u8) !void {
274
275
}
275
276
276
277
/// Same as `makeDir` except the parameter is a null-terminated UTF8-encoded string.
277
- pub fn makeDirC (dir_path : [* ]const u8 ) ! void {
278
+ pub fn makeDirC (dir_path : [* : 0 ]const u8 ) ! void {
278
279
return os .mkdirC (dir_path , default_new_dir_mode );
279
280
}
280
281
281
282
/// Same as `makeDir` except the parameter is a null-terminated UTF16LE-encoded string.
282
- pub fn makeDirW (dir_path : [* ]const u16 ) ! void {
283
+ pub fn makeDirW (dir_path : [* : 0 ]const u16 ) ! void {
283
284
return os .mkdirW (dir_path , default_new_dir_mode );
284
285
}
285
286
@@ -327,12 +328,12 @@ pub fn deleteDir(dir_path: []const u8) !void {
327
328
}
328
329
329
330
/// Same as `deleteDir` except the parameter is a null-terminated UTF8-encoded string.
330
- pub fn deleteDirC (dir_path : [* ]const u8 ) ! void {
331
+ pub fn deleteDirC (dir_path : [* : 0 ]const u8 ) ! void {
331
332
return os .rmdirC (dir_path );
332
333
}
333
334
334
335
/// Same as `deleteDir` except the parameter is a null-terminated UTF16LE-encoded string.
335
- pub fn deleteDirW (dir_path : [* ]const u16 ) ! void {
336
+ pub fn deleteDirW (dir_path : [* : 0 ]const u16 ) ! void {
336
337
return os .rmdirW (dir_path );
337
338
}
338
339
@@ -688,7 +689,7 @@ pub const Dir = struct {
688
689
}
689
690
690
691
/// Same as `open` except the parameter is null-terminated.
691
- pub fn openC (dir_path_c : [* ]const u8 ) OpenError ! Dir {
692
+ pub fn openC (dir_path_c : [* : 0 ]const u8 ) OpenError ! Dir {
692
693
return cwd ().openDirC (dir_path_c );
693
694
}
694
695
@@ -708,7 +709,7 @@ pub const Dir = struct {
708
709
}
709
710
710
711
/// Call `File.close` on the result when done.
711
- pub fn openReadC (self : Dir , sub_path : [* ]const u8 ) File.OpenError ! File {
712
+ pub fn openReadC (self : Dir , sub_path : [* : 0 ]const u8 ) File.OpenError ! File {
712
713
if (builtin .os == .windows ) {
713
714
const path_w = try os .windows .cStrToPrefixedFileW (sub_path );
714
715
return self .openReadW (& path_w );
@@ -719,7 +720,7 @@ pub const Dir = struct {
719
720
return File .openHandle (fd );
720
721
}
721
722
722
- pub fn openReadW (self : Dir , sub_path_w : [* ]const u16 ) File.OpenError ! File {
723
+ pub fn openReadW (self : Dir , sub_path_w : [* : 0 ]const u16 ) File.OpenError ! File {
723
724
const w = os .windows ;
724
725
725
726
var result = File { .handle = undefined };
@@ -786,7 +787,7 @@ pub const Dir = struct {
786
787
}
787
788
788
789
/// Same as `openDir` except the parameter is null-terminated.
789
- pub fn openDirC (self : Dir , sub_path_c : [* ]const u8 ) OpenError ! Dir {
790
+ pub fn openDirC (self : Dir , sub_path_c : [* : 0 ]const u8 ) OpenError ! Dir {
790
791
if (builtin .os == .windows ) {
791
792
const sub_path_w = try os .windows .cStrToPrefixedFileW (sub_path_c );
792
793
return self .openDirW (& sub_path_w );
@@ -805,7 +806,7 @@ pub const Dir = struct {
805
806
806
807
/// Same as `openDir` except the path parameter is UTF16LE, NT-prefixed.
807
808
/// This function is Windows-only.
808
- pub fn openDirW (self : Dir , sub_path_w : [* ]const u16 ) OpenError ! Dir {
809
+ pub fn openDirW (self : Dir , sub_path_w : [* : 0 ]const u16 ) OpenError ! Dir {
809
810
const w = os .windows ;
810
811
811
812
var result = Dir {
@@ -868,7 +869,7 @@ pub const Dir = struct {
868
869
}
869
870
870
871
/// Same as `deleteFile` except the parameter is null-terminated.
871
- pub fn deleteFileC (self : Dir , sub_path_c : [* ]const u8 ) DeleteFileError ! void {
872
+ pub fn deleteFileC (self : Dir , sub_path_c : [* : 0 ]const u8 ) DeleteFileError ! void {
872
873
os .unlinkatC (self .fd , sub_path_c , 0 ) catch | err | switch (err ) {
873
874
error .DirNotEmpty = > unreachable , // not passing AT_REMOVEDIR
874
875
else = > | e | return e ,
@@ -903,7 +904,7 @@ pub const Dir = struct {
903
904
}
904
905
905
906
/// Same as `deleteDir` except the parameter is null-terminated.
906
- pub fn deleteDirC (self : Dir , sub_path_c : [* ]const u8 ) DeleteDirError ! void {
907
+ pub fn deleteDirC (self : Dir , sub_path_c : [* : 0 ]const u8 ) DeleteDirError ! void {
907
908
os .unlinkatC (self .fd , sub_path_c , os .AT_REMOVEDIR ) catch | err | switch (err ) {
908
909
error .IsDir = > unreachable , // not possible since we pass AT_REMOVEDIR
909
910
else = > | e | return e ,
@@ -912,7 +913,7 @@ pub const Dir = struct {
912
913
913
914
/// Same as `deleteDir` except the parameter is UTF16LE, NT prefixed.
914
915
/// This function is Windows-only.
915
- pub fn deleteDirW (self : Dir , sub_path_w : [* ]const u16 ) DeleteDirError ! void {
916
+ pub fn deleteDirW (self : Dir , sub_path_w : [* : 0 ]const u16 ) DeleteDirError ! void {
916
917
os .unlinkatW (self .fd , sub_path_w , os .AT_REMOVEDIR ) catch | err | switch (err ) {
917
918
error .IsDir = > unreachable , // not possible since we pass AT_REMOVEDIR
918
919
else = > | e | return e ,
@@ -927,7 +928,7 @@ pub const Dir = struct {
927
928
}
928
929
929
930
/// Same as `readLink`, except the `pathname` parameter is null-terminated.
930
- pub fn readLinkC (self : Dir , sub_path_c : [* ]const u8 , buffer : * [MAX_PATH_BYTES ]u8 ) ! []u8 {
931
+ pub fn readLinkC (self : Dir , sub_path_c : [* : 0 ]const u8 , buffer : * [MAX_PATH_BYTES ]u8 ) ! []u8 {
931
932
return os .readlinkatC (self .fd , sub_path_c , buffer );
932
933
}
933
934
@@ -1250,7 +1251,8 @@ pub fn openSelfExe() OpenSelfExeError!File {
1250
1251
var buf : [MAX_PATH_BYTES ]u8 = undefined ;
1251
1252
const self_exe_path = try selfExePath (& buf );
1252
1253
buf [self_exe_path .len ] = 0 ;
1253
- return File .openReadC (self_exe_path .ptr );
1254
+ // TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3731
1255
+ return File .openReadC (@ptrCast ([* :0 ]u8 , self_exe_path .ptr ));
1254
1256
}
1255
1257
1256
1258
test "openSelfExe" {
@@ -1277,7 +1279,7 @@ pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]u8 {
1277
1279
var u32_len : u32 = out_buffer .len ;
1278
1280
const rc = std .c ._NSGetExecutablePath (out_buffer , & u32_len );
1279
1281
if (rc != 0 ) return error .NameTooLong ;
1280
- return mem .toSlice (u8 , out_buffer );
1282
+ return mem .toSlice (u8 , @ptrCast ([ * : 0 ] u8 , out_buffer ) );
1281
1283
}
1282
1284
switch (builtin .os ) {
1283
1285
.linux = > return os .readlinkC ("/proc/self/exe" , out_buffer ),
@@ -1306,9 +1308,9 @@ pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]u8 {
1306
1308
}
1307
1309
1308
1310
/// The result is UTF16LE-encoded.
1309
- pub fn selfExePathW () []const u16 {
1311
+ pub fn selfExePathW () [: 0 ]const u16 {
1310
1312
const image_path_name = & os .windows .peb ().ProcessParameters .ImagePathName ;
1311
- return mem .toSliceConst (u16 , image_path_name .Buffer );
1313
+ return mem .toSliceConst (u16 , @ptrCast ([ * : 0 ] const u16 , image_path_name .Buffer ) );
1312
1314
}
1313
1315
1314
1316
/// `selfExeDirPath` except allocates the result on the heap.
0 commit comments