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

DirEntries() fails on symbolic links to non-existent file #10381

Open
dlangBugzillaToGithub opened this issue Aug 5, 2019 · 1 comment
Open
Labels
Arch:x86_64 Issues specific to x86_64 OS:Linux Issues specific to Linux Severity:Enhancement

Comments

@dlangBugzillaToGithub
Copy link

ckatko reported this on 2019-08-05T23:52:22Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=20109

CC List

  • shove

Description

DirEntries() fails on symbolic links to non-existent file. I just had this bug show up.

 - I'm making a program that scans all the files in a path and computes their hashes. DirEntries keeps exploding on a random symlink on my hard drive.

 - I passed "false" to "followSymlink" in DirEntries(), but it still fails regardless and throws an exception. 

 - The reason I believe, is that this random spurious file (cmt_drv.la) I have is a symlink to "../cmt_drv.la", and that file doesn't exist. 

output:
-----

commandline args:
0 ./dup
1 /home/novous/Downloads/DOWNLOADS BUFFER/xf86-input-cmt/src/.libs/

object.Exception@std/file.d(3219): Failed to stat file `/home/novous/Downloads/DOWNLOADS BUFFER/xf86-input-cmt/src/.libs/cmt_drv.la'
----------------
??:? pure @safe void std.exception.bailOut!(Exception).bailOut(immutable(char)[], ulong, const(char[])) [0x6cdb691e]
??:? pure @safe bool std.exception.enforce!(Exception, bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong) [0x6cd9f9e1]
??:? @trusted void std.file.DirEntry._ensureStatDone() [0x6cd772ee]
??:? @property @safe ulong std.file.DirEntry.size() [0x6cd7713c]
??:? _Dmain [0x6cd58c81]

on github here:

https://github.com/dlang/phobos/blob/master/std/file.d#L3979

It appears "stat" is failing.

This might be key: I'm trying to run sort!("a.size > b.size") on the returned DirEntries, and while sorting it calls .size() which then invokes the stat command.

This program works fine until I ran into this edge-case. I can provide code snippets and clarification as needed. 

Thanks,
--Chris
@dlangBugzillaToGithub
Copy link
Author

shove commented on 2019-08-06T06:11:15Z

I don't think there is any problem here. It is correct to throw an exception to a non-existent file getSize.

When traversing files, folders and symlinks, we should judge them and decide whether to do some operations.


    import std.file;
    import std.stdio;

    string path = "...";
    foreach (DirEntry e; dirEntries(path, SpanMode.shallow))
    {
        if (e.isSymlink)
        {
            string origin = readLink(e.name);
            if (origin.exists)
                writeln(origin, ", size: ", e.size);
            else
                writeln(origin, " not exists. ");
        }
    }

@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arch:x86_64 Issues specific to x86_64 OS:Linux Issues specific to Linux Severity:Enhancement
Projects
None yet
Development

No branches or pull requests

2 participants