authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-08 23:06:56-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-08 23:06:56-04:00
loga4310cf8b4f104802360cb854e2dcc48802301a6
tree9835d055e7aee7923750a44bc881daf4e6f9a087
parent7f56744320139ef134658f9ee756050345a31d30

implement std.os.deleteFile for windows


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

std/os/index.zig+29
......@@ -577,6 +577,35 @@ pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path:
577577}
578578
579579pub fn deleteFile(allocator: &Allocator, file_path: []const u8) -> %void {
580 if (builtin.os == Os.windows) {
581 return deleteFileWindows(allocator, file_path);
582 } else {
583 return deleteFilePosix(allocator, file_path);
584 }
585}
586
587error FileNotFound;
588error AccessDenied;
589
590pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) -> %void {
591 const buf = %return allocator.alloc(u8, file_path.len + 1);
592 defer allocator.free(buf);
593
594 mem.copy(u8, buf, file_path);
595 buf[file_path.len] = 0;
596
597 if (!windows.DeleteFileA(buf.ptr)) {
598 const err = windows.GetLastError();
599 return switch (err) {
600 windows.ERROR.FILE_NOT_FOUND => error.FileNotFound,
601 windows.ERROR.ACCESS_DENIED => error.AccessDenied,
602 windows.ERROR.FILENAME_EXCED_RANGE, windows.ERROR.INVALID_PARAMETER => error.NameTooLong,
603 else => error.Unexpected,
604 }
605 }
606}
607
608pub fn deleteFilePosix(allocator: &Allocator, file_path: []const u8) -> %void {
580609 const buf = %return allocator.alloc(u8, file_path.len + 1);
581610 defer allocator.free(buf);
582611
std/os/windows/index.zig+4-1
......@@ -7,13 +7,15 @@ pub extern "kernel32" stdcallcc fn CryptReleaseContext(hProv: HCRYPTPROV, dwFlag
77
88pub extern "kernel32" stdcallcc fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWORD, pbBuffer: &BYTE) -> bool;
99
10pub extern "kernel32" stdcallcc fn DeleteFileA(lpFileName: LPCSTR) -> bool;
11
1012pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: UINT) -> noreturn;
1113
1214pub extern "kernel32" stdcallcc fn GetCommandLine() -> LPTSTR;
1315
1416pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: &DWORD) -> bool;
1517
16pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPTSTR) -> DWORD;
18pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPSTR) -> DWORD;
1719
1820/// Retrieves the calling thread's last-error code value. The last-error code is maintained on a per-thread basis.
1921/// Multiple threads do not overwrite each other's last-error code.
......@@ -68,6 +70,7 @@ pub const HANDLE = &c_void;
6870pub const HINSTANCE = &@OpaqueType();
6971pub const HCRYPTPROV = ULONG_PTR;
7072pub const LPCTSTR = &const TCHAR;
73pub const LPCSTR = &const CHAR;
7174pub const LPDWORD = &DWORD;
7275pub const LPVOID = &c_void;
7376pub const PVOID = &c_void;