Skip to content

Windows: Fix std.fs.realpath/os.realpathW for directories #4697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2020

Conversation

squeek502
Copy link
Collaborator

Fixes one of the cases in #4658. The rest are unaffected (still behave the same as before).

From the CreateFileW docs:

Directories

To open a directory using CreateFile, specify the FILE_FLAG_BACKUP_SEMANTICS flag as part of dwFlagsAndAttributes. Appropriate security checks still apply when this flag is used without SE_BACKUP_NAME and SE_RESTORE_NAME privileges.

Before (using the test code/setup found in #4658):

C:\tmp>.\realpath.exe dir
path=dir
error: AccessDenied
C:\Users\Ryan\Programming\Zig\bin\lib\zig\std\os\windows.zig:81:31: 0x13fb88907 in std.os.windows.CreateFileW (realpath.obj)
            .ACCESS_DENIED => return error.AccessDenied,
                              ^
C:\Users\Ryan\Programming\Zig\bin\lib\zig\std\os.zig:3004:20: 0x13fb83e4f in std.os.realpathW (realpath.obj)
    const h_file = try windows.CreateFileW(
                   ^
C:\Users\Ryan\Programming\Zig\bin\lib\zig\std\os.zig:2965:9: 0x13fb83d97 in std.os.realpath (realpath.obj)
        return realpathW(&pathname_w, out_buffer);
        ^
C:\Users\Ryan\Programming\Zig\bin\lib\zig\std\fs.zig:1664:36: 0x13fb78861 in std.fs.realpathAlloc (realpath.obj)
    return mem.dupe(allocator, u8, try os.realpath(pathname, &buf));
                                   ^
C:\tmp\realpath.zig:7:21: 0x13fb62f62 in main (realpath.obj)
    var real_path = try std.fs.realpathAlloc(std.heap.page_allocator, args[1]);
C:\tmp>.\realpath.exe file
path=file
realpath=C:\tmp\file

After:

C:\tmp>.\realpath.exe dir
path=dir
realpath=C:\tmp\dir
C:\tmp>.\realpath.exe file
path=file
realpath=C:\tmp\file

@squeek502
Copy link
Collaborator Author

squeek502 commented Mar 9, 2020

About FILE_ATTRIBUTE_NORMAL no longer being used: it is ignored when using OPEN_EXISTING, so it was unnecessary:

From the CreateFileW docs:

Note When CreateFile opens an existing file, it generally combines the file flags with the file attributes of the existing file, and ignores any file attributes supplied as part of dwFlagsAndAttributes. Special cases are detailed in Creating and Opening Files.

From Creating and Opening Files:

When opening an existing file (dwCreationDisposition set to either OPEN_EXISTING, OPEN_ALWAYS, or TRUNCATE_EXISTING), the CreateFile function performs the following actions:

  • Combines the file flags (FILE_FLAG_*) specified by dwFlagsAndAttributes with existing file attributes, and ignores any file attributes (FILE_ATTRIBUTE_*) specified by dwFlagsAndAttributes.

@andrewrk andrewrk merged commit 9b1b44b into ziglang:master Mar 9, 2020
@andrewrk
Copy link
Member

andrewrk commented Mar 9, 2020

Great! Thanks for the detailed explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants