Update the access/modification times of the existing file, if there is one, in FileUtils::touch()

This changes FileUtils::touch() to match the documentation for the 'touch' tool
on Unix.
This commit is contained in:
Robert Knight 2011-08-29 22:44:52 +01:00
parent d7e3cc8d63
commit c20c452e98

View file

@ -258,14 +258,22 @@ void FileUtils::touch(const char* path) throw (IOException)
{
#ifdef PLATFORM_UNIX
// see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html
int fd = creat(path,S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fd != -1)
if (fileExists(path))
{
close(fd);
utimensat(AT_FDCWD,path,0 /* use current date/time */,0);
}
else
{
throw IOException("Unable to touch file " + std::string(path));
int fd = creat(path,S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fd != -1)
{
futimens(fd,0 /* use current date/time */);
close(fd);
}
else
{
throw IOException("Unable to touch file " + std::string(path));
}
}
#else
HANDLE result = CreateFile(path,GENERIC_WRITE,