Commit 77376a54bf

nycex <nycex@cccp.org>
2020-05-02 10:19:07
correct usages of std.fs.dir.DeleteFileError (#5058)
* correct usages of std.fs.dir.DeleteFileError * test std.fs.createFileAbsolute() and std.fs.deleteFileAbsolute()
1 parent 428065d
Changed files (2)
lib
lib/std/fs/test.zig
@@ -100,6 +100,22 @@ test "create file, lock and read from multiple process at once" {
     };
 }
 
+test "open file with exclusive nonblocking lock twice (absolute paths)" {
+    const allocator = std.testing.allocator;
+
+    const file_paths: [1][]const u8 = .{"zig-test-absolute-paths.txt"};
+    const filename = try fs.path.resolve(allocator, &file_paths);
+    defer allocator.free(filename);
+
+    const file1 = try fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
+
+    const file2 = fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
+    file1.close();
+    std.testing.expectError(error.WouldBlock, file2);
+
+    try fs.deleteFileAbsolute(filename);
+}
+
 const FileLockTestContext = struct {
     filename: []const u8,
     pid: if (builtin.os.tag == .windows) ?void else ?std.os.pid_t = null,
lib/std/fs.zig
@@ -1442,7 +1442,7 @@ pub fn createFileAbsoluteW(absolute_path_w: [*:0]const u16, flags: File.CreateFl
 /// Asserts that the path is absolute. See `Dir.deleteFile` for a function that
 /// operates on both absolute and relative paths.
 /// Asserts that the path parameter has no null bytes.
-pub fn deleteFileAbsolute(absolute_path: []const u8) DeleteFileError!void {
+pub fn deleteFileAbsolute(absolute_path: []const u8) Dir.DeleteFileError!void {
     assert(path.isAbsolute(absolute_path));
     return cwd().deleteFile(absolute_path);
 }
@@ -1450,13 +1450,13 @@ pub fn deleteFileAbsolute(absolute_path: []const u8) DeleteFileError!void {
 pub const deleteFileAbsoluteC = @compileError("deprecated: renamed to deleteFileAbsoluteZ");
 
 /// Same as `deleteFileAbsolute` except the parameter is null-terminated.
-pub fn deleteFileAbsoluteZ(absolute_path_c: [*:0]const u8) DeleteFileError!void {
+pub fn deleteFileAbsoluteZ(absolute_path_c: [*:0]const u8) Dir.DeleteFileError!void {
     assert(path.isAbsoluteZ(absolute_path_c));
     return cwd().deleteFileZ(absolute_path_c);
 }
 
 /// Same as `deleteFileAbsolute` except the parameter is WTF-16 encoded.
-pub fn deleteFileAbsoluteW(absolute_path_w: [*:0]const u16) DeleteFileError!void {
+pub fn deleteFileAbsoluteW(absolute_path_w: [*:0]const u16) Dir.DeleteFileError!void {
     assert(path.isAbsoluteWindowsW(absolute_path_w));
     return cwd().deleteFileW(absolute_path_w);
 }