Skip to content

Commit

Permalink
std.file: Use utimensat when available
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberShadow committed Mar 17, 2016
1 parent f68fc7d commit 28211cb
Showing 1 changed file with 48 additions and 20 deletions.
68 changes: 48 additions & 20 deletions std/file.d
Original file line number Diff line number Diff line change
Expand Up @@ -1149,20 +1149,40 @@ void setTimes(R)(R name,
else version(Posix)
{
auto namez = name.tempCString!FSChar();
static auto trustedUtimes(const(FSChar)* namez, const ref timeval[2] times) @trusted
static if (is(typeof(&utimensat)))
{
return utimes(namez, times);
}
timeval[2] t = void;
static auto trustedUtimensat(int fd, const(FSChar)* namez, const ref timespec[2] times, int flags) @trusted
{
return utimensat(fd, namez, times, flags);
}
timespec[2] t = void;

t[0] = accessTime.toTimeVal();
t[1] = modificationTime.toTimeVal();
t[0] = accessTime.toTimeSpec();
t[1] = modificationTime.toTimeSpec();

static if (isNarrowString!R && is(Unqual!(ElementEncodingType!R) == char))
alias names = name;
static if (isNarrowString!R && is(Unqual!(ElementEncodingType!R) == char))
alias names = name;
else
string names = null;
cenforce(trustedUtimensat(AT_FDCWD, namez, t, 0) == 0, names, namez);
}
else
string names = null;
cenforce(trustedUtimes(namez, t) == 0, names, namez);
{
static auto trustedUtimes(const(FSChar)* namez, const ref timeval[2] times) @trusted
{
return utimes(namez, times);
}
timeval[2] t = void;

t[0] = accessTime.toTimeVal();
t[1] = modificationTime.toTimeVal();

static if (isNarrowString!R && is(Unqual!(ElementEncodingType!R) == char))
alias names = name;
else
string names = null;
cenforce(trustedUtimes(namez, t) == 0, names, namez);
}
}
}

Expand All @@ -1176,7 +1196,8 @@ void setTimes(R)(auto ref R name,

@safe unittest
{
static assert(__traits(compiles, setTimes(TestAliasedString("foo"), SysTime.init, SysTime.init)));
if (false) // Test instatiation
setTimes(TestAliasedString("foo"), SysTime.init, SysTime.init);
}

unittest
Expand All @@ -1189,19 +1210,26 @@ unittest
if (!exists(dir)) mkdirRecurse(dir);
{ auto f = File(file, "w"); }

foreach (path; [file, dir]) // test file and dir
void testTimes(int hnsecValue)
{
SysTime atime = SysTime(DateTime(2010, 10, 4, 0, 0, 30));
SysTime mtime = SysTime(DateTime(2011, 10, 4, 0, 0, 30));
setTimes(path, atime, mtime);
foreach (path; [file, dir]) // test file and dir
{
SysTime atime = SysTime(DateTime(2010, 10, 4, 0, 0, 30), hnsecs(hnsecValue));
SysTime mtime = SysTime(DateTime(2011, 10, 4, 0, 0, 30), hnsecs(hnsecValue));
setTimes(path, atime, mtime);

SysTime atime_res;
SysTime mtime_res;
getTimes(path, atime_res, mtime_res);
assert(atime == atime_res);
assert(mtime == mtime_res);
SysTime atime_res;
SysTime mtime_res;
getTimes(path, atime_res, mtime_res);
assert(atime == atime_res);
assert(mtime == mtime_res);
}
}

testTimes(0);
version (linux)
testTimes(123_456_7);

rmdirRecurse(newdir);
}

Expand Down

0 comments on commit 28211cb

Please sign in to comment.