zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 0bc80411f6a87a76edd0e3f04f697a0eab8ef9c0 (tree)
parent 8d3eaab8717381023ba8b6837fc5840ad8be1004
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Sat, 14 Oct 2017 15:31:24 -0400

implement os.makeDir for windows

Diffstat:
Mstd/os/index.zig | 25++++++++++++++++++++++---
Mstd/os/windows/index.zig | 3+++
2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/std/os/index.zig b/std/os/index.zig @@ -761,11 +761,30 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8) } pub fn makeDir(allocator: &Allocator, dir_path: []const u8) -> %void { - const path_buf = %return allocator.alloc(u8, dir_path.len + 1); + if (is_windows) { + return makeDirWindows(allocator, dir_path); + } else { + return makeDirPosix(allocator, dir_path); + } +} + +pub fn makeDirWindows(allocator: &Allocator, dir_path: []const u8) -> %void { + const path_buf = %return cstr.addNullByte(allocator, dir_path); defer allocator.free(path_buf); - mem.copy(u8, path_buf, dir_path); - path_buf[dir_path.len] = 0; + if (!windows.CreateDirectoryA(path_buf.ptr, null)) { + const err = windows.GetLastError(); + return switch (err) { + windows.ERROR.ALREADY_EXISTS => error.PathAlreadyExists, + windows.ERROR.PATH_NOT_FOUND => error.FileNotFound, + else => error.Unexpected, + }; + } +} + +pub fn makeDirPosix(allocator: &Allocator, dir_path: []const u8) -> %void { + const path_buf = cstr.addNullByte(allocator, dir_path); + defer allocator.free(path_buf); const err = posix.getErrno(posix.mkdir(path_buf.ptr, 0o755)); if (err > 0) { diff --git a/std/os/windows/index.zig b/std/os/windows/index.zig @@ -10,6 +10,9 @@ pub extern "advapi32" stdcallcc fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWOR pub extern "kernel32" stdcallcc fn CloseHandle(hObject: HANDLE) -> BOOL; +pub extern "kernel32" stdcallcc fn CreateDirectoryA(lpPathName: LPCSTR, + lpSecurityAttributes: ?&SECURITY_ATTRIBUTES) -> BOOL; + pub extern "kernel32" stdcallcc fn CreateFileA(lpFileName: LPCSTR, dwDesiredAccess: DWORD, dwShareMode: DWORD, lpSecurityAttributes: ?LPSECURITY_ATTRIBUTES, dwCreationDisposition: DWORD, dwFlagsAndAttributes: DWORD, hTemplateFile: ?HANDLE) -> HANDLE;