Skip to content

Commit

Permalink
ziglang#12474: Code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
QusaiHroub committed Mar 10, 2023
1 parent 0c9e26a commit 74962d5
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/std/fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1465,15 +1465,15 @@ pub const Dir = struct {
}
}

fn openDirAccessMaskWWrapper (self: Dir, sub_path: []const u8, access_mask: u32, no_follow: bool) OpenError!Dir {
fn openDirAccessMaskW(self: Dir, sub_path: []const u8, access_mask: u32, no_follow: bool) OpenError!Dir {
const w = os.windows;
var end_index: usize = sub_path.len;

return while (true) {
const sub_path_w = try w.sliceToPrefixedFileW(sub_path[0..end_index]);
const result = self.openDirAccessMaskW(sub_path_w.span().ptr, access_mask, .{
const result = self.openDirWithAccessAndCreationW(sub_path_w.span().ptr, access_mask, .{
.no_follow = no_follow,
.create = true,
.create_disposition = if (end_index == sub_path.len) w.FILE_OPEN_IF else w.FILE_CREATE,
}) catch |err| switch (err) {
error.FileNotFound => {
// march end_index backward until next path component
Expand Down Expand Up @@ -1506,7 +1506,7 @@ pub const Dir = struct {
const base_flags = w.STANDARD_RIGHTS_READ | w.FILE_READ_ATTRIBUTES | w.FILE_READ_EA |
w.SYNCHRONIZE | w.FILE_TRAVERSE;

return self.openDirAccessMaskWWrapper(sub_path, base_flags, open_dir_options.no_follow);
return self.openDirAccessMaskW(sub_path, base_flags, open_dir_options.no_follow);
},
else => {
return self.openDir(sub_path, open_dir_options) catch {
Expand All @@ -1528,7 +1528,7 @@ pub const Dir = struct {
w.SYNCHRONIZE | w.FILE_TRAVERSE | w.FILE_LIST_DIRECTORY;

return IterableDir{
.dir = try self.openDirAccessMaskWWrapper(sub_path, base_flags, open_dir_options.no_follow)
.dir = try self.openDirAccessMaskW(sub_path, base_flags, open_dir_options.no_follow)
};
},
else => {
Expand Down Expand Up @@ -1791,9 +1791,9 @@ pub const Dir = struct {
const base_flags = w.STANDARD_RIGHTS_READ | w.FILE_READ_ATTRIBUTES | w.FILE_READ_EA |
w.SYNCHRONIZE | w.FILE_TRAVERSE;
const flags: u32 = if (iterable) base_flags | w.FILE_LIST_DIRECTORY else base_flags;
var dir = try self.openDirAccessMaskW(sub_path_w, flags, .{
var dir = try self.openDirWithAccessAndCreationW(sub_path_w, flags, .{
.no_follow = args.no_follow,
.create = false,
.create_disposition = w.FILE_OPEN
});
return dir;
}
Expand All @@ -1817,12 +1817,17 @@ pub const Dir = struct {
return Dir{ .fd = fd };
}

const OpenDirAccessMaskWOptions = struct {
const OpenDirWithAccessAndCreationWOptions = struct {
no_follow: bool,
create: bool,
create_disposition: u32
};

fn openDirAccessMaskW(self: Dir, sub_path_w: [*:0]const u16, access_mask: u32, flags: OpenDirAccessMaskWOptions) OpenError!Dir {
fn openDirWithAccessAndCreationW(
self: Dir,
sub_path_w: [*:0]const u16,
access_mask: u32,
flags: OpenDirWithAccessAndCreationWOptions
) OpenError!Dir {
const w = os.windows;

var result = Dir{
Expand Down Expand Up @@ -1853,7 +1858,7 @@ pub const Dir = struct {
null,
w.FILE_ATTRIBUTE_NORMAL,
w.FILE_SHARE_READ | w.FILE_SHARE_WRITE,
if (flags.create) w.FILE_OPEN_IF else w.FILE_OPEN,
flags.create_disposition,
w.FILE_DIRECTORY_FILE | w.FILE_SYNCHRONOUS_IO_NONALERT | w.FILE_OPEN_FOR_BACKUP_INTENT | open_reparse_point,
null,
0,
Expand Down

0 comments on commit 74962d5

Please sign in to comment.