summaryrefslogtreecommitdiffstats
path: root/lib/util/utimens.c
blob: 579391f149f5ec6c1fe23b7a6214c99899a30165 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Copyright (c) 2015, 2018 Todd C. Miller <Todd.Miller@sudo.ws>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
 */

#include <config.h>

#if !defined(HAVE_FUTIMENS) || !defined(HAVE_UTIMENSAT)

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#if !defined(HAVE_UTIMES) || defined(HAVE_FUTIME)
# include <utime.h>
#endif

#include "sudo_compat.h"
#include "sudo_util.h"

#if !defined(HAVE_FUTIMES) && defined(HAVE_FUTIMESAT)
# define futimes(_f, _tv)	futimesat(_f, NULL, _tv)
# define HAVE_FUTIMES
#endif

#if defined(HAVE_ST_MTIM)
# ifdef HAVE_ST__TIM
#  define ATIME_TO_TIMEVAL(_x, _y)	TIMESPEC_TO_TIMEVAL((_x), &(_y)->st_atim.st__tim)
#  define MTIME_TO_TIMEVAL(_x, _y)	TIMESPEC_TO_TIMEVAL((_x), &(_y)->st_mtim.st__tim)
# else
#  define ATIME_TO_TIMEVAL(_x, _y)	TIMESPEC_TO_TIMEVAL((_x), &(_y)->st_atim)
#  define MTIME_TO_TIMEVAL(_x, _y)	TIMESPEC_TO_TIMEVAL((_x), &(_y)->st_mtim)
# endif
#elif defined(HAVE_ST_MTIMESPEC)
# define ATIME_TO_TIMEVAL(_x, _y)	TIMESPEC_TO_TIMEVAL((_x), &(_y)->st_atimespec)
# define MTIME_TO_TIMEVAL(_x, _y)	TIMESPEC_TO_TIMEVAL((_x), &(_y)->st_mtimespec)
#elif defined(HAVE_ST_NMTIME)
# define ATIME_TO_TIMEVAL(_x, _y)      do { (_x)->tv_sec = (_y)->st_atime; (_x)->tv_usec = (_y)->st_natime; } while (0)
# define MTIME_TO_TIMEVAL(_x, _y)      do { (_x)->tv_sec = (_y)->st_mtime; (_x)->tv_usec = (_y)->st_nmtime; } while (0)
#else
# define ATIME_TO_TIMEVAL(_x, _y)      do { (_x)->tv_sec = (_y)->st_atime; (_x)->tv_usec = 0; } while (0)
# define MTIME_TO_TIMEVAL(_x, _y)      do { (_x)->tv_sec = (_y)->st_mtime; (_x)->tv_usec = 0; } while (0)
#endif /* HAVE_ST_MTIM */

/*
 * Convert the pair of timespec structs passed to futimens() / utimensat()
 * to a pair of timeval structs, handling UTIME_OMIT and UTIME_NOW.
 * Returns 0 on success and -1 on failure (setting errno).
 */
static int
utimens_ts_to_tv(int fd, const char *file, const struct timespec *ts,
    struct timeval *tv)
{
    TIMESPEC_TO_TIMEVAL(&tv[0], &ts[0]);
    TIMESPEC_TO_TIMEVAL(&tv[1], &ts[1]);
    if (ts[0].tv_nsec == UTIME_OMIT || ts[1].tv_nsec == UTIME_OMIT) {
	struct stat sb;

	if (fd != -1) {
	    /* For futimens() */
	    if (fstat(fd, &sb) == -1)
		return -1;
	} else {
	    /* For utimensat() */
	    if (stat(file, &sb) == -1)
		return -1;
	}
	if (ts[0].tv_nsec == UTIME_OMIT)
	    ATIME_TO_TIMEVAL(&tv[0], &sb);
	if (ts[1].tv_nsec == UTIME_OMIT)
	    MTIME_TO_TIMEVAL(&tv[1], &sb);
    }
    if (ts[0].tv_nsec == UTIME_NOW || ts[1].tv_nsec == UTIME_NOW) {
	struct timeval now;

	if (gettimeofday(&now, NULL) == -1)
	    return -1;
	if (ts[0].tv_nsec == UTIME_NOW)
	    tv[0] = now;
	if (ts[1].tv_nsec == UTIME_NOW)
	    tv[1] = now;
    }
    return 0;
}

#if defined(HAVE_FUTIMES)
/*
 * Emulate futimens() via futimes()
 */
int
sudo_futimens(int fd, const struct timespec *ts)
{
    struct timeval tv[2], *times = NULL;

    if (ts != NULL) {
	if (utimens_ts_to_tv(fd, NULL, ts, tv) == -1)
	    return -1;
	times = tv;
    }
    return futimes(fd, times);
}
#elif defined(HAVE_FUTIME)
/*
 * Emulate futimens() via futime()
 */
int
sudo_futimens(int fd, const struct timespec *ts)
{
    struct utimbuf utb, *times = NULL;

    if (ts != NULL) {
	struct timeval tv[2];

	if (utimens_ts_to_tv(fd, NULL, ts, tv) == -1)
	    return -1;
	utb.actime = (time_t)(tv[0].tv_sec + tv[0].tv_usec / 1000000);
	utb.modtime = (time_t)(tv[1].tv_sec + tv[1].tv_usec / 1000000);
	times = &utb;
    }
    return futime(fd, times);
}
#else
/*
 * Nothing to do but fail.
 */
int
sudo_futimens(int fd, const struct timespec *ts)
{
    errno = ENOSYS;
    return -1;
}
#endif /* HAVE_FUTIMES */

#if defined(HAVE_UTIMES)
/*
 * Emulate utimensat() via utimes()
 */
int
sudo_utimensat(int fd, const char *file, const struct timespec *ts, int flag)
{
    struct timeval tv[2], *times = NULL;

    if (fd != AT_FDCWD || flag != 0) {
	errno = ENOTSUP;
	return -1;
    }

    if (ts != NULL) {
	if (utimens_ts_to_tv(-1, file, ts, tv) == -1)
	    return -1;
	times = tv;
    }
    return utimes(file, times);
}
#else
/*
 * Emulate utimensat() via utime()
 */
int
sudo_utimensat(int fd, const char *file, const struct timespec *ts, int flag)
{
    struct utimbuf utb, *times = NULL;

    if (fd != AT_FDCWD || flag != 0) {
	errno = ENOTSUP;
	return -1;
    }

    if (ts != NULL) {
	struct timeval tv[2];

	if (utimens_ts_to_tv(-1, file, ts, tv) == -1)
	    return -1;
	utb.actime = (time_t)(tv[0].tv_sec + tv[0].tv_usec / 1000000);
	utb.modtime = (time_t)(tv[1].tv_sec + tv[1].tv_usec / 1000000);
	times = &utb;
    }
    return utime(file, times);
}
#endif /* !HAVE_UTIMES */

#endif /* !HAVE_FUTIMENS && !HAVE_UTIMENSAT */