Skip to content
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

boost::filesystem::file_size returns 0 for symbolic links on Windows #313

Closed
TorstenHauska opened this issue Jun 18, 2024 · 2 comments
Closed

Comments

@TorstenHauska
Copy link

Hello,

when executing boost::filesystem::file_size on a symbolic link on Windows the functions returns a size of 0. The same call on Linux returns the size of the file the symbolic link is pointing to. I'm unsure what the correct outcome should be but I would vote for the Linux version.

I used boost version 1.85.0 on both systems.

Here the code of a simple command line tool to reproduce the problem:

#include <iostream>

#include <boost/filesystem.hpp>

int main(int argc, char *argv[])
{
    if (argc < 2)
        return -1;

    std::uint64_t size = boost::filesystem::file_size(argv[1]);

    std::cout << size << '\n';

    return 0;
}

Kind regards,
Torsten Hauska

Lastique added a commit that referenced this issue Jun 18, 2024
GetFileAttributesExW that was used to implement file_size and is_empty
on Windows returns information about the symlink rather than the file
the symlink refers to. Fix this by opening the file and using
GetFileInformationByHandle to obtain the file size and attributes.

Additionally, reworked is_empty implementation to reuse the file handle
(and fd on POSIX systems) to create the directory iterator if the
operation is invoked on a directory. On POSIX systems, implement a
more lightweight version of is_empty_directory when readdir is safe
to use. Reusing the file handle/fd improves protection against
filesystem races, when the file that is being tested by is_empty
is initially a directory and then, when we create a directory
iterator, it is not.

Fixes #313.
@Lastique
Copy link
Member

Thanks for the report.

@TorstenHauska
Copy link
Author

Thanks a lot for your fast fix. I am really impressed.

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

No branches or pull requests

2 participants