Skip to content

Commit

Permalink
Make std.file.isFile, isSymlink and their unittests safe
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-tan committed Sep 16, 2014
1 parent a5cf543 commit 1ac1cb6
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions std/file.d
Expand Up @@ -1237,15 +1237,15 @@ assert("/etc/fonts/fonts.conf".isFile);
assert(!"/usr/share/include".isFile);
--------------------
+/
@property bool isFile(in char[] name)
@property bool isFile(in char[] name) @safe
{
version(Windows)
return !name.isDir;
else version(Posix)
return (getAttributes(name) & S_IFMT) == S_IFREG;
}

unittest
@safe unittest
{
version(Windows)
{
Expand Down Expand Up @@ -1346,15 +1346,15 @@ unittest
Throws:
$(D FileException) if the given file does not exist.
+/
@property bool isSymlink(C)(const(C)[] name)
@property bool isSymlink(C)(const(C)[] name) @safe
{
version(Windows)
return (getAttributes(name) & FILE_ATTRIBUTE_REPARSE_POINT) != 0;
else version(Posix)
return (getLinkAttributes(name) & S_IFMT) == S_IFLNK;
}

unittest
@safe unittest
{
version(Windows)
{
Expand Down Expand Up @@ -1383,14 +1383,18 @@ unittest
}
else version(Posix)
{
static auto trustedSymlink(string path1, string path2) @trusted
{
return core.sys.posix.unistd.symlink(path1.ptr, path2.ptr);
}
if(system_directory.exists)
{
assert(!system_directory.isSymlink);

immutable symfile = deleteme ~ "_slink\0";
scope(exit) if(symfile.exists) symfile.remove();

core.sys.posix.unistd.symlink(system_directory, symfile.ptr);
trustedSymlink(system_directory, symfile);

assert(symfile.isSymlink);
assert(!attrIsSymlink(getAttributes(symfile)));
Expand All @@ -1410,7 +1414,7 @@ unittest
immutable symfile = deleteme ~ "_slink\0";
scope(exit) if(symfile.exists) symfile.remove();

core.sys.posix.unistd.symlink(system_file, symfile.ptr);
trustedSymlink(system_file, symfile);

assert(symfile.isSymlink);
assert(!attrIsSymlink(getAttributes(symfile)));
Expand Down

0 comments on commit 1ac1cb6

Please sign in to comment.