Bug 10174 – std.file claims files which are symlinks to non-existant files don't exist
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
Linux
Creation time
2013-05-26T05:21:42Z
Last change time
2019-11-14T13:29:41Z
Assigned to
No Owner
Creator
76878357
Comments
Comment #0 by 76878357 — 2013-05-26T05:21:42Z
The functions exists() and isSymlink() of std.file interact with symlinks in a way that does not make sense to me. It seems the exists() function checks whether the file pointed to by the symlink exists instead of the symlink itself. I however think this is quite counter-intuitive as it leads to situations where exists() returns false while isSymlink() returns true on the same file if the symlink is broken. This behaviour is also not documented.
Here is a simple test case:
import std.file, std.stdio;
void main()
{
auto file = "testfile";
symlink("DOES-NOT-EXIST", file);
writefln("exists: %b, isSymlink: %b", exists(file), isSymlink(file));
}
Which outputs:
exists: 0, isSymlink: 1
But should output:
exists: 1, isSymlink: 1
Maybe I am overlooking something here, but if not, I suggest the behaviour of exists() to be changed to check the file itself and not the symlink target (i.e. use lstat instead of stat) and maybe offer a function that explicitly checks the referenced file in case of a symbolic link. Or, preserving backwards-compatibility, introduce a new function that explicitly checks only the file itself and properly document exists' behaviour.
Comment #1 by beatgammit — 2013-06-17T23:35:41Z
I like the behavior of exists() because most of the time you only care that the file can be read. Since a symlink is just a link to a file, the link itself often doesn't mean that much.
I would, however, like a way to get at lstat() like the current dirEntry(). Perhaps I overlooked this as well?
Comment #2 by 76878357 — 2013-06-20T11:03:08Z
Yeah I can see how you would want symlinks to behave transparently by default. Still getting this behaviour documented and a function that does lstat would be nice.
Comment #3 by bugzilla — 2019-11-14T13:29:41Z
Seems to have been fixed before the bug was reported... At least: It works now.