xref: /haiku/src/system/libroot/posix/utime.c (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <utime.h>
8 
9 #include <errno.h>
10 #include <time.h>
11 
12 #include <NodeMonitor.h>
13 
14 #include <errno_private.h>
15 #include <syscalls.h>
16 
17 
18 #define RETURN_AND_SET_ERRNO(err) \
19 	if (err < 0) { \
20 		__set_errno(err); \
21 		return -1; \
22 	} \
23 	return err;
24 
25 
26 int
27 utime(const char *path, const struct utimbuf *times)
28 {
29 	struct stat stat;
30 	status_t status;
31 
32 	if (times != NULL) {
33 		stat.st_atim.tv_sec = times->actime;
34 		stat.st_mtim.tv_sec = times->modtime;
35 		stat.st_atim.tv_nsec = stat.st_mtim.tv_nsec = 0;
36 	} else {
37 		bigtime_t now = real_time_clock_usecs();
38 		stat.st_atim.tv_sec = stat.st_mtim.tv_sec = now / 1000000;
39 		stat.st_atim.tv_nsec = stat.st_mtim.tv_nsec = (now % 1000000) * 1000;
40 	}
41 
42 	status = _kern_write_stat(-1, path, true, &stat, sizeof(struct stat),
43 		B_STAT_MODIFICATION_TIME | B_STAT_ACCESS_TIME);
44 
45 	RETURN_AND_SET_ERRNO(status);
46 }
47 
48