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
Comment #1 by shove — 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. ");
}
}
Comment #2 by robert.schadek — 2024-12-01T16:35:21Z