fix openWriteNoClobber and add test

This commit is contained in:
Quetzal Bradley 2019-02-17 13:08:08 -08:00 committed by Andrew Kelley
parent de18ece294
commit 7e54954052
2 changed files with 12 additions and 1 deletions

View File

@ -29,6 +29,17 @@ test "write a file, read it, then delete it" {
try st.print("end");
try buf_stream.flush();
}
{
// make sure openWriteNoClobber doesn't harm the file
if (os.File.openWriteNoClobber(tmp_file_name, os.File.default_mode)) |file| {
unreachable;
}
else |err| {
std.debug.assert(err == os.File.OpenError.PathAlreadyExists);
}
}
{
var file = try os.File.openRead(tmp_file_name);
defer file.close();

View File

@ -105,7 +105,7 @@ pub const File = struct {
pub fn openWriteNoClobber(path: []const u8, file_mode: Mode) OpenError!File {
if (is_posix) {
const path_c = try os.toPosixPath(path);
return openWriteNoClobberC(path_c, file_mode);
return openWriteNoClobberC(&path_c, file_mode);
} else if (is_windows) {
const path_w = try windows_util.sliceToPrefixedFileW(path);
return openWriteNoClobberW(&path_w, file_mode);