authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-03 17:02:42-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-04 00:27:09-08:00
log0317e95aade4f5651e8f556ca19d1598030715da
tree8f229fe94c4c29221c9795e2761edb8392c21050
parent2b326d27d572156b534a096005182976b2ac3fe1

std.posix: delete some mkdir functions

These are handled by Io.Dir now. This is part of an effort to eliminate error.OperationCanceled from the std lib. Also an effort to delete all std.posix functions.

2 files changed, 1 insertions(+), 114 deletions(-)

lib/std/posix.zig-113
......@@ -832,123 +832,10 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
832832 }
833833}
834834
835/// On Windows, `sub_dir_path` should be encoded as [WTF-8](https://wtf-8.codeberg.page/).
836/// On WASI, `sub_dir_path` should be encoded as valid UTF-8.
837/// On other platforms, `sub_dir_path` is an opaque sequence of bytes with no particular encoding.
838pub fn mkdirat(dir_fd: fd_t, sub_dir_path: []const u8, mode: mode_t) MakeDirError!void {
839 if (native_os == .windows) {
840 @compileError("use std.Io instead");
841 } else if (native_os == .wasi and !builtin.link_libc) {
842 @compileError("use std.Io instead");
843 } else {
844 const sub_dir_path_c = try toPosixPath(sub_dir_path);
845 return mkdiratZ(dir_fd, &sub_dir_path_c, mode);
846 }
847}
848
849/// Same as `mkdirat` except the parameters are null-terminated.
850pub fn mkdiratZ(dir_fd: fd_t, sub_dir_path: [*:0]const u8, mode: mode_t) MakeDirError!void {
851 if (native_os == .windows) {
852 @compileError("use std.Io instead");
853 } else if (native_os == .wasi and !builtin.link_libc) {
854 @compileError("use std.Io instead");
855 }
856 switch (errno(system.mkdirat(dir_fd, sub_dir_path, mode))) {
857 .SUCCESS => return,
858 .ACCES => return error.AccessDenied,
859 .BADF => unreachable,
860 .PERM => return error.PermissionDenied,
861 .DQUOT => return error.DiskQuota,
862 .EXIST => return error.PathAlreadyExists,
863 .FAULT => unreachable,
864 .LOOP => return error.SymLinkLoop,
865 .MLINK => return error.LinkQuotaExceeded,
866 .NAMETOOLONG => return error.NameTooLong,
867 .NOENT => return error.FileNotFound,
868 .NOMEM => return error.SystemResources,
869 .NOSPC => return error.NoSpaceLeft,
870 .NOTDIR => return error.NotDir,
871 .ROFS => return error.ReadOnlyFileSystem,
872 // dragonfly: when dir_fd is unlinked from filesystem
873 .NOTCONN => return error.FileNotFound,
874 .ILSEQ => return error.BadPathName,
875 else => |err| return unexpectedErrno(err),
876 }
877}
878
879pub const MakeDirError = std.Io.Dir.CreateDirError;
880
881/// Create a directory.
882/// `mode` is ignored on Windows and WASI.
883/// On Windows, `dir_path` should be encoded as [WTF-8](https://wtf-8.codeberg.page/).
884/// On WASI, `dir_path` should be encoded as valid UTF-8.
885/// On other platforms, `dir_path` is an opaque sequence of bytes with no particular encoding.
886pub fn mkdir(dir_path: []const u8, mode: mode_t) MakeDirError!void {
887 if (native_os == .wasi and !builtin.link_libc) {
888 return mkdirat(AT.FDCWD, dir_path, mode);
889 } else if (native_os == .windows) {
890 const dir_path_w = try windows.sliceToPrefixedFileW(null, dir_path);
891 return mkdirW(dir_path_w.span(), mode);
892 } else {
893 const dir_path_c = try toPosixPath(dir_path);
894 return mkdirZ(&dir_path_c, mode);
895 }
896}
897
898835/// Same as `mkdir` but the parameter is null-terminated.
899836/// On Windows, `dir_path` should be encoded as [WTF-8](https://wtf-8.codeberg.page/).
900837/// On WASI, `dir_path` should be encoded as valid UTF-8.
901838/// On other platforms, `dir_path` is an opaque sequence of bytes with no particular encoding.
902pub fn mkdirZ(dir_path: [*:0]const u8, mode: mode_t) MakeDirError!void {
903 if (native_os == .windows) {
904 const dir_path_w = try windows.cStrToPrefixedFileW(null, dir_path);
905 return mkdirW(dir_path_w.span(), mode);
906 } else if (native_os == .wasi and !builtin.link_libc) {
907 return mkdir(mem.sliceTo(dir_path, 0), mode);
908 }
909 switch (errno(system.mkdir(dir_path, mode))) {
910 .SUCCESS => return,
911 .ACCES => return error.AccessDenied,
912 .PERM => return error.PermissionDenied,
913 .DQUOT => return error.DiskQuota,
914 .EXIST => return error.PathAlreadyExists,
915 .FAULT => unreachable,
916 .LOOP => return error.SymLinkLoop,
917 .MLINK => return error.LinkQuotaExceeded,
918 .NAMETOOLONG => return error.NameTooLong,
919 .NOENT => return error.FileNotFound,
920 .NOMEM => return error.SystemResources,
921 .NOSPC => return error.NoSpaceLeft,
922 .NOTDIR => return error.NotDir,
923 .ROFS => return error.ReadOnlyFileSystem,
924 .ILSEQ => return error.BadPathName,
925 else => |err| return unexpectedErrno(err),
926 }
927}
928
929/// Windows-only. Same as `mkdir` but the parameters is WTF16LE encoded.
930pub fn mkdirW(dir_path_w: []const u16, mode: mode_t) MakeDirError!void {
931 _ = mode;
932 const sub_dir_handle = windows.OpenFile(dir_path_w, .{
933 .dir = Io.Dir.cwd().handle,
934 .access_mask = .{
935 .STANDARD = .{ .SYNCHRONIZE = true },
936 .GENERIC = .{ .READ = true },
937 },
938 .creation = .CREATE,
939 .filter = .dir_only,
940 }) catch |err| switch (err) {
941 error.IsDir => return error.Unexpected,
942 error.PipeBusy => return error.Unexpected,
943 error.NoDevice => return error.Unexpected,
944 error.WouldBlock => return error.Unexpected,
945 error.AntivirusInterference => return error.Unexpected,
946 error.OperationCanceled => return error.Unexpected,
947 else => |e| return e,
948 };
949 windows.CloseHandle(sub_dir_handle);
950}
951
952839pub const ChangeCurDirError = error{
953840 AccessDenied,
954841 FileSystem,
lib/std/posix/test.zig+1-1
......@@ -527,7 +527,7 @@ test "rename smoke test" {
527527 // Create some directory
528528 const file_path = try Dir.path.join(gpa, &.{ base_path, "some_dir" });
529529 defer gpa.free(file_path);
530 try posix.mkdir(file_path, mode);
530 try Io.Dir.createDirAbsolute(io, file_path, .fromMode(mode));
531531
532532 // Rename the directory
533533 const new_file_path = try Dir.path.join(gpa, &.{ base_path, "some_other_dir" });