summaryrefslogtreecommitdiff
path: root/compat/utimensat.c
blob: e4c8e8d0b687ec6a59b2163b0265f3a6b71f5551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "../git-compat-util.h"

int git_utimensat(int fd, const char *path, const struct timespec times[2], int flag)
{
	struct timeval tv[2];
	struct timeval *tvp = NULL;

	if (fd != AT_FDCWD) {
		errno = ENOSYS;
		return -1;
	}

	if (flag) {
		errno = ENOSYS;
		return -1;
	}

	if (times) {
		for (int i = 0; i < 2; i++) {
			if (times[i].tv_nsec == UTIME_NOW) {
				struct timeval now;
				gettimeofday(&now, NULL);
				tv[i] = now;
			} else if (times[i].tv_nsec == UTIME_OMIT) {
				struct stat st;
				if (stat(path, &st) < 0)
					return -1;
				tv[i].tv_sec = (i == 0) ? st.st_atime : st.st_mtime;
				tv[i].tv_usec = (i == 0) ? ST_ATIME_NSEC(st) / 1000 : ST_MTIME_NSEC(st) / 1000;
			} else {
				tv[i].tv_sec = times[i].tv_sec;
				tv[i].tv_usec = times[i].tv_nsec / 1000;
			}
		}
		tvp = tv;
	}

	return utimes(path, tvp);
}