D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10174 - std.file claims files which are symlinks to non-existant files don't exist
Summary: std.file claims files which are symlinks to non-existant files don't exist
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-05-26 05:21 UTC by 76878357
Modified: 2019-11-14 13:29 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description 76878357 2013-05-26 05:21:42 UTC
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 tyler-dev 2013-06-17 23:35:41 UTC
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 76878357 2013-06-20 11:03:08 UTC
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 berni44 2019-11-14 13:29:41 UTC
Seems to have been fixed before the bug was reported... At least: It works now.