authorgravatar for nycex@cccp.orgnycex <nycex@cccp.org> 2020-05-02 10:19:07+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-02 04:19:07-04:00
log77376a54bf4a379135261f40f7f4025a79314897
treebc00d70937962d66bd5e423066b45cdd2bb269dd
parent428065da30cd957cadd9d1f2428933bab9d17637
signature Signed by PGP key 4AEE18F83AFDEB23

correct usages of std.fs.dir.DeleteFileError (#5058)

* correct usages of std.fs.dir.DeleteFileError * test std.fs.createFileAbsolute() and std.fs.deleteFileAbsolute()

2 files changed, 19 insertions(+), 3 deletions(-)

lib/std/fs.zig+3-3
...@@ -1442,7 +1442,7 @@ pub fn createFileAbsoluteW(absolute_path_w: [*:0]const u16, flags: File.CreateFl...@@ -1442,7 +1442,7 @@ pub fn createFileAbsoluteW(absolute_path_w: [*:0]const u16, flags: File.CreateFl
1442/// Asserts that the path is absolute. See `Dir.deleteFile` for a function that1442/// Asserts that the path is absolute. See `Dir.deleteFile` for a function that
1443/// operates on both absolute and relative paths.1443/// operates on both absolute and relative paths.
1444/// Asserts that the path parameter has no null bytes.1444/// Asserts that the path parameter has no null bytes.
1445pub fn deleteFileAbsolute(absolute_path: []const u8) DeleteFileError!void {1445pub fn deleteFileAbsolute(absolute_path: []const u8) Dir.DeleteFileError!void {
1446 assert(path.isAbsolute(absolute_path));1446 assert(path.isAbsolute(absolute_path));
1447 return cwd().deleteFile(absolute_path);1447 return cwd().deleteFile(absolute_path);
1448}1448}
...@@ -1450,13 +1450,13 @@ pub fn deleteFileAbsolute(absolute_path: []const u8) DeleteFileError!void {...@@ -1450,13 +1450,13 @@ pub fn deleteFileAbsolute(absolute_path: []const u8) DeleteFileError!void {
1450pub const deleteFileAbsoluteC = @compileError("deprecated: renamed to deleteFileAbsoluteZ");1450pub const deleteFileAbsoluteC = @compileError("deprecated: renamed to deleteFileAbsoluteZ");
14511451
1452/// Same as `deleteFileAbsolute` except the parameter is null-terminated.1452/// Same as `deleteFileAbsolute` except the parameter is null-terminated.
1453pub fn deleteFileAbsoluteZ(absolute_path_c: [*:0]const u8) DeleteFileError!void {1453pub fn deleteFileAbsoluteZ(absolute_path_c: [*:0]const u8) Dir.DeleteFileError!void {
1454 assert(path.isAbsoluteZ(absolute_path_c));1454 assert(path.isAbsoluteZ(absolute_path_c));
1455 return cwd().deleteFileZ(absolute_path_c);1455 return cwd().deleteFileZ(absolute_path_c);
1456}1456}
14571457
1458/// Same as `deleteFileAbsolute` except the parameter is WTF-16 encoded.1458/// Same as `deleteFileAbsolute` except the parameter is WTF-16 encoded.
1459pub fn deleteFileAbsoluteW(absolute_path_w: [*:0]const u16) DeleteFileError!void {1459pub fn deleteFileAbsoluteW(absolute_path_w: [*:0]const u16) Dir.DeleteFileError!void {
1460 assert(path.isAbsoluteWindowsW(absolute_path_w));1460 assert(path.isAbsoluteWindowsW(absolute_path_w));
1461 return cwd().deleteFileW(absolute_path_w);1461 return cwd().deleteFileW(absolute_path_w);
1462}1462}
lib/std/fs/test.zig+16
...@@ -100,6 +100,22 @@ test "create file, lock and read from multiple process at once" {...@@ -100,6 +100,22 @@ test "create file, lock and read from multiple process at once" {
100 };100 };
101}101}
102102
103test "open file with exclusive nonblocking lock twice (absolute paths)" {
104 const allocator = std.testing.allocator;
105
106 const file_paths: [1][]const u8 = .{"zig-test-absolute-paths.txt"};
107 const filename = try fs.path.resolve(allocator, &file_paths);
108 defer allocator.free(filename);
109
110 const file1 = try fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
111
112 const file2 = fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
113 file1.close();
114 std.testing.expectError(error.WouldBlock, file2);
115
116 try fs.deleteFileAbsolute(filename);
117}
118
103const FileLockTestContext = struct {119const FileLockTestContext = struct {
104 filename: []const u8,120 filename: []const u8,
105 pid: if (builtin.os.tag == .windows) ?void else ?std.os.pid_t = null,121 pid: if (builtin.os.tag == .windows) ?void else ?std.os.pid_t = null,