- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement std.os.deleteFile for windows
Showing
2 changed files
with
33 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
andrewrk
Author
Member
|
||
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); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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).