implement os.makeDir for windows

master
Andrew Kelley 2017-10-14 15:31:24 -04:00
parent 8d3eaab871
commit 0bc80411f6
2 changed files with 25 additions and 3 deletions

View File

@ -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) {

View File

@ -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;