commit 6f98ef09e31768e3356598ef30e60fe028a0e70c (tree)
parent d2a4e5e226f2041e4a21e9353e1323b84017b3b1
Author: Andrew Kelley <andrew@ziglang.org>
Date: Wed, 1 Jul 2020 23:20:50 +0000
Merge pull request #5717 from squeek502/fs-dir-file-ops
Add tests for using file operations on directories
Diffstat:
2 files changed, 60 insertions(+), 4 deletions(-)
diff --git a/lib/std/fs.zig b/lib/std/fs.zig
@@ -1111,10 +1111,18 @@ pub const Dir = struct {
/// Delete a file name and possibly the file it refers to, based on an open directory handle.
/// Asserts that the path parameter has no null bytes.
pub fn deleteFile(self: Dir, sub_path: []const u8) DeleteFileError!void {
- os.unlinkat(self.fd, sub_path, 0) catch |err| switch (err) {
- error.DirNotEmpty => unreachable, // not passing AT_REMOVEDIR
- else => |e| return e,
- };
+ if (builtin.os.tag == .windows) {
+ const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
+ return self.deleteFileW(sub_path_w.span().ptr);
+ } else if (builtin.os.tag == .wasi) {
+ os.unlinkatWasi(self.fd, sub_path, 0) catch |err| switch (err) {
+ error.DirNotEmpty => unreachable, // not passing AT_REMOVEDIR
+ else => |e| return e,
+ };
+ } else {
+ const sub_path_c = try os.toPosixPath(sub_path);
+ return self.deleteFileZ(&sub_path_c);
+ }
}
pub const deleteFileC = @compileError("deprecated: renamed to deleteFileZ");
@@ -1123,6 +1131,17 @@ pub const Dir = struct {
pub fn deleteFileZ(self: Dir, sub_path_c: [*:0]const u8) DeleteFileError!void {
os.unlinkatZ(self.fd, sub_path_c, 0) catch |err| switch (err) {
error.DirNotEmpty => unreachable, // not passing AT_REMOVEDIR
+ error.AccessDenied => |e| switch (builtin.os.tag) {
+ // non-Linux POSIX systems return EPERM when trying to delete a directory, so
+ // we need to handle that case specifically and translate the error
+ .macosx, .ios, .freebsd, .netbsd, .dragonfly => {
+ // Don't follow symlinks to match unlinkat (which acts on symlinks rather than follows them)
+ const fstat = os.fstatatZ(self.fd, sub_path_c, os.AT_SYMLINK_NOFOLLOW) catch return e;
+ const is_dir = fstat.mode & os.S_IFMT == os.S_IFDIR;
+ return if (is_dir) error.IsDir else e;
+ },
+ else => return e,
+ },
else => |e| return e,
};
}
diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig
@@ -73,6 +73,43 @@ test "directory operations on files" {
file.close();
}
+test "file operations on directories" {
+ var tmp_dir = tmpDir(.{});
+ defer tmp_dir.cleanup();
+
+ const test_dir_name = "test_dir";
+
+ try tmp_dir.dir.makeDir(test_dir_name);
+
+ testing.expectError(error.IsDir, tmp_dir.dir.createFile(test_dir_name, .{}));
+ testing.expectError(error.IsDir, tmp_dir.dir.deleteFile(test_dir_name));
+ // Currently, WASI will return error.Unexpected (via ENOTCAPABLE) when attempting fd_read on a directory handle.
+ // TODO: Re-enable on WASI once https://github.com/bytecodealliance/wasmtime/issues/1935 is resolved.
+ if (builtin.os.tag != .wasi) {
+ testing.expectError(error.IsDir, tmp_dir.dir.readFileAlloc(testing.allocator, test_dir_name, std.math.maxInt(usize)));
+ }
+ // Note: The `.write = true` is necessary to ensure the error occurs on all platforms.
+ // TODO: Add a read-only test as well, see https://github.com/ziglang/zig/issues/5732
+ testing.expectError(error.IsDir, tmp_dir.dir.openFile(test_dir_name, .{ .write = true }));
+
+ if (builtin.os.tag != .wasi) {
+ // TODO: use Dir's realpath function once that exists
+ const absolute_path = blk: {
+ const relative_path = try fs.path.join(testing.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp_dir.sub_path[0..], test_dir_name });
+ defer testing.allocator.free(relative_path);
+ break :blk try fs.realpathAlloc(testing.allocator, relative_path);
+ };
+ defer testing.allocator.free(absolute_path);
+
+ testing.expectError(error.IsDir, fs.createFileAbsolute(absolute_path, .{}));
+ testing.expectError(error.IsDir, fs.deleteFileAbsolute(absolute_path));
+ }
+
+ // ensure the directory still exists as a sanity check
+ var dir = try tmp_dir.dir.openDir(test_dir_name, .{});
+ dir.close();
+}
+
test "openSelfExe" {
if (builtin.os.tag == .wasi) return error.SkipZigTest;