authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-14 15:31:24-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-14 15:32:29-04:00
log0bc80411f6a87a76edd0e3f04f697a0eab8ef9c0
treea0f79b8dfd6174f3231699cfa3bc02e08c0da23c
parent8d3eaab8717381023ba8b6837fc5840ad8be1004

implement os.makeDir for windows


2 files changed, 25 insertions(+), 3 deletions(-)

std/os/index.zig+22-3
...@@ -761,11 +761,30 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8)...@@ -761,11 +761,30 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8)
761}761}
762762
763pub fn makeDir(allocator: &Allocator, dir_path: []const u8) -> %void {763pub fn makeDir(allocator: &Allocator, dir_path: []const u8) -> %void {
764 const path_buf = %return allocator.alloc(u8, dir_path.len + 1);764 if (is_windows) {
765 return makeDirWindows(allocator, dir_path);
766 } else {
767 return makeDirPosix(allocator, dir_path);
768 }
769}
770
771pub fn makeDirWindows(allocator: &Allocator, dir_path: []const u8) -> %void {
772 const path_buf = %return cstr.addNullByte(allocator, dir_path);
765 defer allocator.free(path_buf);773 defer allocator.free(path_buf);
766774
767 mem.copy(u8, path_buf, dir_path);775 if (!windows.CreateDirectoryA(path_buf.ptr, null)) {
768 path_buf[dir_path.len] = 0;776 const err = windows.GetLastError();
777 return switch (err) {
778 windows.ERROR.ALREADY_EXISTS => error.PathAlreadyExists,
779 windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,
780 else => error.Unexpected,
781 };
782 }
783}
784
785pub fn makeDirPosix(allocator: &Allocator, dir_path: []const u8) -> %void {
786 const path_buf = cstr.addNullByte(allocator, dir_path);
787 defer allocator.free(path_buf);
769788
770 const err = posix.getErrno(posix.mkdir(path_buf.ptr, 0o755));789 const err = posix.getErrno(posix.mkdir(path_buf.ptr, 0o755));
771 if (err > 0) {790 if (err > 0) {
std/os/windows/index.zig+3
...@@ -10,6 +10,9 @@ pub extern "advapi32" stdcallcc fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWOR...@@ -10,6 +10,9 @@ pub extern "advapi32" stdcallcc fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWOR
1010
11pub extern "kernel32" stdcallcc fn CloseHandle(hObject: HANDLE) -> BOOL;11pub extern "kernel32" stdcallcc fn CloseHandle(hObject: HANDLE) -> BOOL;
1212
13pub extern "kernel32" stdcallcc fn CreateDirectoryA(lpPathName: LPCSTR,
14 lpSecurityAttributes: ?&SECURITY_ATTRIBUTES) -> BOOL;
15
13pub extern "kernel32" stdcallcc fn CreateFileA(lpFileName: LPCSTR, dwDesiredAccess: DWORD,16pub extern "kernel32" stdcallcc fn CreateFileA(lpFileName: LPCSTR, dwDesiredAccess: DWORD,
14 dwShareMode: DWORD, lpSecurityAttributes: ?LPSECURITY_ATTRIBUTES, dwCreationDisposition: DWORD,17 dwShareMode: DWORD, lpSecurityAttributes: ?LPSECURITY_ATTRIBUTES, dwCreationDisposition: DWORD,
15 dwFlagsAndAttributes: DWORD, hTemplateFile: ?HANDLE) -> HANDLE;18 dwFlagsAndAttributes: DWORD, hTemplateFile: ?HANDLE) -> HANDLE;