diff --git a/std/os/index.zig b/std/os/index.zig index d4681eaa15..40f2ba2a91 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -577,6 +577,35 @@ pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path: } pub fn deleteFile(allocator: &Allocator, file_path: []const u8) -> %void { + if (builtin.os == Os.windows) { + return deleteFileWindows(allocator, file_path); + } else { + return deleteFilePosix(allocator, file_path); + } +} + +error FileNotFound; +error AccessDenied; + +pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) -> %void { + const buf = %return allocator.alloc(u8, file_path.len + 1); + defer allocator.free(buf); + + mem.copy(u8, buf, file_path); + buf[file_path.len] = 0; + + if (!windows.DeleteFileA(buf.ptr)) { + const err = windows.GetLastError(); + return switch (err) { + windows.ERROR.FILE_NOT_FOUND => error.FileNotFound, + windows.ERROR.ACCESS_DENIED => error.AccessDenied, + windows.ERROR.FILENAME_EXCED_RANGE, windows.ERROR.INVALID_PARAMETER => error.NameTooLong, + else => error.Unexpected, + } + } +} + +pub fn deleteFilePosix(allocator: &Allocator, file_path: []const u8) -> %void { const buf = %return allocator.alloc(u8, file_path.len + 1); defer allocator.free(buf); diff --git a/std/os/windows/index.zig b/std/os/windows/index.zig index 83e96d6777..498170a9b8 100644 --- a/std/os/windows/index.zig +++ b/std/os/windows/index.zig @@ -7,13 +7,15 @@ pub extern "kernel32" stdcallcc fn CryptReleaseContext(hProv: HCRYPTPROV, dwFlag pub extern "kernel32" stdcallcc fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWORD, pbBuffer: &BYTE) -> bool; +pub extern "kernel32" stdcallcc fn DeleteFileA(lpFileName: LPCSTR) -> bool; + pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: UINT) -> noreturn; pub extern "kernel32" stdcallcc fn GetCommandLine() -> LPTSTR; pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: &DWORD) -> bool; -pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPTSTR) -> DWORD; +pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPSTR) -> DWORD; /// Retrieves the calling thread's last-error code value. The last-error code is maintained on a per-thread basis. /// Multiple threads do not overwrite each other's last-error code. @@ -68,6 +70,7 @@ pub const HANDLE = &c_void; pub const HINSTANCE = &@OpaqueType(); pub const HCRYPTPROV = ULONG_PTR; pub const LPCTSTR = &const TCHAR; +pub const LPCSTR = &const CHAR; pub const LPDWORD = &DWORD; pub const LPVOID = &c_void; pub const PVOID = &c_void;