diff --git a/src/system/libroot/posix/sys/utimes.c b/src/system/libroot/posix/sys/utimes.c index 00b442b48c..717ea0c49d 100644 --- a/src/system/libroot/posix/sys/utimes.c +++ b/src/system/libroot/posix/sys/utimes.c @@ -1,25 +1,47 @@ /* - * Copyright 2006, Axel Dörfler, axeld@pinc-software.de. + * Copyright 2006-2009, Axel Dörfler, axeld@pinc-software.de. * Distributed under the terms of the MIT License. */ #include -#include + +#include + +#include + +#include + + +#define RETURN_AND_SET_ERRNO(err) \ + if (err < 0) { \ + errno = err; \ + return -1; \ + } \ + return err; int -utimes(const char *file, const struct timeval times[2]) +utimes(const char* path, const struct timeval times[2]) { - struct utimbuf buffer, *timeBuffer; + struct stat stat; + status_t status; if (times != NULL) { - timeBuffer = &buffer; - buffer.actime = times[0].tv_sec + times[0].tv_usec / 1000000LL; - buffer.modtime = times[1].tv_sec + times[1].tv_usec / 1000000LL; - } else - timeBuffer = NULL; + stat.st_atim.tv_sec = times[0].tv_sec; + stat.st_atim.tv_nsec = times[0].tv_usec * 1000; - return utime(file, timeBuffer); + stat.st_mtim.tv_sec = times[1].tv_sec; + stat.st_mtim.tv_nsec = times[1].tv_usec * 1000; + } else { + bigtime_t now = real_time_clock_usecs(); + stat.st_atim.tv_sec = stat.st_mtim.tv_sec = now / 1000000; + stat.st_atim.tv_nsec = stat.st_mtim.tv_nsec = (now % 1000000) * 1000; + } + + status = _kern_write_stat(-1, path, true, &stat, sizeof(struct stat), + B_STAT_MODIFICATION_TIME | B_STAT_ACCESS_TIME); + + RETURN_AND_SET_ERRNO(status); } diff --git a/src/system/libroot/posix/utime.c b/src/system/libroot/posix/utime.c index e06ddb51ae..5223b4b7f1 100644 --- a/src/system/libroot/posix/utime.c +++ b/src/system/libroot/posix/utime.c @@ -1,14 +1,16 @@ /* - * Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de. * Distributed under the terms of the MIT License. */ +#include + +#include +#include + #include -#include -#include -#include #include @@ -27,10 +29,14 @@ utime(const char *path, const struct utimbuf *times) status_t status; if (times != NULL) { - stat.st_atime = times->actime; - stat.st_mtime = times->modtime; - } else - stat.st_atime = stat.st_mtime = time(NULL); + stat.st_atim.tv_sec = times->actime; + stat.st_mtim.tv_sec = times->modtime; + stat.st_atim.tv_nsec = stat.st_mtim.tv_nsec = 0; + } else { + bigtime_t now = real_time_clock_usecs(); + stat.st_atim.tv_sec = stat.st_mtim.tv_sec = now / 1000000; + stat.st_atim.tv_nsec = stat.st_mtim.tv_nsec = (now % 1000000) * 1000; + } status = _kern_write_stat(-1, path, true, &stat, sizeof(struct stat), B_STAT_MODIFICATION_TIME | B_STAT_ACCESS_TIME);