mirror of
https://github.com/dlang/phobos.git
synced 2025-05-01 23:50:31 +03:00
Make std.file.getAttributes safe
This commit is contained in:
parent
828f44383c
commit
19cf705be5
1 changed files with 12 additions and 4 deletions
16
std/file.d
16
std/file.d
|
@ -1021,21 +1021,29 @@ bool exists(in char[] name) @trusted nothrow @nogc
|
||||||
|
|
||||||
Throws: $(D FileException) on error.
|
Throws: $(D FileException) on error.
|
||||||
+/
|
+/
|
||||||
uint getAttributes(in char[] name)
|
uint getAttributes(in char[] name) @safe
|
||||||
{
|
{
|
||||||
version(Windows)
|
version(Windows)
|
||||||
{
|
{
|
||||||
immutable result = GetFileAttributesW(name.tempCStringW());
|
static auto trustedGetFileAttributesW(in char[] fileName) @trusted
|
||||||
|
{
|
||||||
|
return GetFileAttributesW(fileName.tempCStringW());
|
||||||
|
}
|
||||||
|
immutable result = trustedGetFileAttributesW(name);
|
||||||
|
|
||||||
enforce(result != uint.max, new FileException(name.idup));
|
cenforce(result != INVALID_FILE_ATTRIBUTES, name);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
else version(Posix)
|
else version(Posix)
|
||||||
{
|
{
|
||||||
|
static auto trustedStat(in char[] path, ref stat_t buf) @trusted
|
||||||
|
{
|
||||||
|
return stat(path.tempCString(), &buf);
|
||||||
|
}
|
||||||
stat_t statbuf = void;
|
stat_t statbuf = void;
|
||||||
|
|
||||||
cenforce(stat(name.tempCString(), &statbuf) == 0, name);
|
cenforce(trustedStat(name, statbuf) == 0, name);
|
||||||
|
|
||||||
return statbuf.st_mode;
|
return statbuf.st_mode;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue