Skip to content

Commit a4310cf

Browse files
committedOct 9, 2017
implement std.os.deleteFile for windows
1 parent 7f56744 commit a4310cf

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
 

‎std/os/index.zig

+29
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,35 @@ pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path:
577577
}
578578

579579
pub 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+
587+
error FileNotFound;
588+
error AccessDenied;
589+
590+
pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) -> %void {
Has conversations. Original line has conversations.
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+
608+
pub 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
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ pub extern "kernel32" stdcallcc fn CryptReleaseContext(hProv: HCRYPTPROV, dwFlag
77

88
pub extern "kernel32" stdcallcc fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWORD, pbBuffer: &BYTE) -> bool;
99

10+
pub extern "kernel32" stdcallcc fn DeleteFileA(lpFileName: LPCSTR) -> bool;
11+
1012
pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: UINT) -> noreturn;
1113

1214
pub extern "kernel32" stdcallcc fn GetCommandLine() -> LPTSTR;
1315

1416
pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: &DWORD) -> bool;
1517

16-
pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPTSTR) -> DWORD;
18+
pub 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;
6870
pub const HINSTANCE = &@OpaqueType();
6971
pub const HCRYPTPROV = ULONG_PTR;
7072
pub const LPCTSTR = &const TCHAR;
73+
pub const LPCSTR = &const CHAR;
7174
pub const LPDWORD = &DWORD;
7275
pub const LPVOID = &c_void;
7376
pub const PVOID = &c_void;

0 commit comments

Comments
 (0)
Please sign in to comment.