Skip to content

Commit

Permalink
Make std.file.getTimes safe
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-tan committed Aug 23, 2014
1 parent e2cd694 commit 01d8dd9
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions std/file.d
Expand Up @@ -570,21 +570,25 @@ unittest
+/
void getTimes(in char[] name,
out SysTime accessTime,
out SysTime modificationTime)
out SysTime modificationTime) @safe
{
version(Windows)
{
with (getFileAttributesWin(name))
{
accessTime = std.datetime.FILETIMEToSysTime(&ftLastAccessTime);
modificationTime = std.datetime.FILETIMEToSysTime(&ftLastWriteTime);
accessTime = FILETIMEToSysTime(&ftLastAccessTime);
modificationTime = FILETIMEToSysTime(&ftLastWriteTime);
}
}
else version(Posix)
{
static auto trustedStat(in char* path, ref stat_t buf) @trusted
{
return stat(path, &buf);
}
stat_t statbuf = void;

cenforce(stat(toStringz(name), &statbuf) == 0, name);
cenforce(trustedStat(toStringz(name), statbuf) == 0, name);

accessTime = SysTime(unixTimeToStdTime(statbuf.st_atime));
modificationTime = SysTime(unixTimeToStdTime(statbuf.st_mtime));
Expand Down

0 comments on commit 01d8dd9

Please sign in to comment.