Skip to content

Commit

Permalink
implement std.os.deleteFile for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Oct 9, 2017
1 parent 7f56744 commit a4310cf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
29 changes: 29 additions & 0 deletions std/os/index.zig
Expand Up @@ -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 {

This comment has been minimized.

Copy link
@RobinTD

RobinTD Oct 9, 2017

You could introduce a thread-local scratch buffer allocator into the standard library, then tons of methods like this would not have to take an allocator argument, and in addition would perform a really efficient pointer bump allocation (and subsequent pointer reset).

This comment has been minimized.

Copy link
@andrewrk

andrewrk Oct 9, 2017

Author Member

Sounds like a reasonable idea. It depends on #174 and setting up thread local variables - right now we don't initialize them.

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

Expand Down
5 changes: 4 additions & 1 deletion std/os/windows/index.zig
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a4310cf

Please sign in to comment.