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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2010 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.opensource.org/licenses/cpl1.0.txt *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* Glenn Fowler
* AT&T Research
*
* Tv_t conversion support
*/
#if defined(__STDPP__directive) && defined(__STDPP__hide)
__STDPP__directive pragma pp:hide utime
#else
#define utime ______utime
#endif
#ifndef _ATFILE_SOURCE
#define _ATFILE_SOURCE 1
#endif
#include <ast.h>
#include <ls.h>
#include <tv.h>
#include <times.h>
#include <error.h>
#include "FEATURE/tvlib"
#if _hdr_utime && _lib_utime
#include <utime.h>
#endif
#if defined(__STDPP__directive) && defined(__STDPP__hide)
__STDPP__directive pragma pp:nohide utime
#else
#undef utime
#endif
#if _lib_utime
#if _hdr_utime
extern int utime(const char*, const struct utimbuf*);
#else
extern int utime(const char*, const time_t*);
#endif
#endif
#define NS(n) (((uint32_t)(n))<1000000000L?(n):0)
/*
* touch path <atime,mtime,ctime>
* Tv_t==0 uses current time
* Tv_t==TV_TOUCH_RETAIN retains path value if it exists, current time otherwise
* otherwise it is exact time
* file created if it doesn't exist and (flags&TV_TOUCH_CREATE)
* symlink not followed if (flags&TV_TOUCH_PHYSICAL)
* cv most likely ignored on most implementations
*
* NOTE: when *at() calls are integrated TV_TOUCH_* should be advertized!
*/
#define TV_TOUCH_CREATE 1
#define TV_TOUCH_PHYSICAL 2
#if !defined(UTIME_NOW) || !defined(UTIME_OMIT) || defined(__stub_utimensat)
#undef _lib_utimensat
#endif
int
tvtouch(const char* path, register const Tv_t* av, register const Tv_t* mv, const Tv_t* cv, int flags)
{
int fd;
int mode;
int oerrno;
struct stat st;
Tv_t now;
#if _lib_utimets || _lib_utimensat
struct timespec ts[2];
#endif
#if _lib_utimes
struct timeval am[2];
#else
#if _hdr_utime
struct utimbuf am;
#else
time_t am[2];
#endif
#endif
oerrno = errno;
#if _lib_utimensat
if (!av)
{
ts[0].tv_sec = 0;
ts[0].tv_nsec = UTIME_NOW;
}
else if (av == TV_TOUCH_RETAIN)
{
ts[0].tv_sec = 0;
ts[0].tv_nsec = UTIME_OMIT;
}
else
{
ts[0].tv_sec = av->tv_sec;
ts[0].tv_nsec = NS(av->tv_nsec);
}
if (!mv)
{
ts[1].tv_sec = 0;
ts[1].tv_nsec = UTIME_NOW;
}
else if (mv == TV_TOUCH_RETAIN)
{
ts[1].tv_sec = 0;
ts[1].tv_nsec = UTIME_OMIT;
}
else
{
ts[1].tv_sec = mv->tv_sec;
ts[1].tv_nsec = NS(mv->tv_nsec);
}
if (!cv && av == TV_TOUCH_RETAIN && mv == TV_TOUCH_RETAIN && !stat(path, &st) && !chmod(path, st.st_mode & S_IPERM))
return 0;
if (!utimensat(AT_FDCWD, path, ts[0].tv_nsec == UTIME_NOW && ts[1].tv_nsec == UTIME_NOW ? (struct timespec*)0 : ts, (flags & TV_TOUCH_PHYSICAL) ? AT_SYMLINK_NOFOLLOW : 0))
return 0;
if (errno != ENOSYS)
{
if (errno != ENOENT || !(flags & TV_TOUCH_CREATE))
return -1;
umask(mode = umask(0));
mode = (~mode) & (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) < 0)
return -1;
close(fd);
errno = oerrno;
if ((ts[0].tv_nsec != UTIME_NOW || ts[1].tv_nsec != UTIME_NOW) && utimensat(AT_FDCWD, path, ts, (flags & TV_TOUCH_PHYSICAL) ? AT_SYMLINK_NOFOLLOW : 0))
return -1;
return 0;
}
#endif
if ((av == TV_TOUCH_RETAIN || mv == TV_TOUCH_RETAIN) && stat(path, &st))
{
errno = oerrno;
if (av == TV_TOUCH_RETAIN)
av = 0;
if (mv == TV_TOUCH_RETAIN)
mv = 0;
}
if (!av || !mv)
{
tvgettime(&now);
if (!av)
av = (const Tv_t*)&now;
if (!mv)
mv = (const Tv_t*)&now;
}
#if _lib_utimets
if (av == TV_TOUCH_RETAIN)
{
ts[0].tv_sec = st.st_atime;
ts[0].tv_nsec = ST_ATIME_NSEC_GET(&st);
}
else
{
ts[0].tv_sec = av->tv_sec;
ts[0].tv_nsec = NS(av->tv_nsec);
}
if (mv == TV_TOUCH_RETAIN)
{
ts[1].tv_sec = st.st_mtime;
ts[1].tv_nsec = ST_MTIME_NSEC_GET(&st);
}
else
{
ts[1].tv_sec = mv->tv_sec;
ts[1].tv_nsec = NS(mv->tv_nsec);
}
if (!utimets(path, ts))
return 0;
if (errno != ENOENT && av == (const Tv_t*)&now && mv == (const Tv_t*)&now && !utimets(path, NiL))
{
errno = oerrno;
return 0;
}
#else
#if _lib_utimes
if (av == TV_TOUCH_RETAIN)
{
am[0].tv_sec = st.st_atime;
am[0].tv_usec = ST_ATIME_NSEC_GET(&st) / 1000;
}
else
{
am[0].tv_sec = av->tv_sec;
am[0].tv_usec = NS(av->tv_nsec) / 1000;
}
if (mv == TV_TOUCH_RETAIN)
{
am[1].tv_sec = st.st_mtime;
am[1].tv_usec = ST_MTIME_NSEC_GET(&st) / 1000;
}
else
{
am[1].tv_sec = mv->tv_sec;
am[1].tv_usec = NS(mv->tv_nsec) / 1000;
}
if (!utimes(path, am))
return 0;
if (errno != ENOENT && av == (const Tv_t*)&now && mv == (const Tv_t*)&now && !utimes(path, NiL))
{
errno = oerrno;
return 0;
}
#else
#if _lib_utime
am.actime = (av == TV_TOUCH_RETAIN) ? st.st_atime : av->tv_sec;
am.modtime = (mv == TV_TOUCH_RETAIN) ? st.st_mtime : mv->tv_sec;
if (!utime(path, &am))
return 0;
#if _lib_utime_now
if (errno != ENOENT && av == (const Tv_t*)&now && mv == (const Tv_t*)&now && !utime(path, NiL))
{
errno = oerrno;
return 0;
}
#endif
#endif
#endif
if (!access(path, F_OK))
{
if (av != (const Tv_t*)&now || mv != (const Tv_t*)&now)
{
errno = EINVAL;
return -1;
}
if ((fd = open(path, O_RDWR)) >= 0)
{
char c;
if (read(fd, &c, 1) == 1)
{
if (c = (lseek(fd, 0L, 0) == 0L && write(fd, &c, 1) == 1))
errno = oerrno;
close(fd);
if (c)
return 0;
}
close(fd);
}
}
#endif
if (errno != ENOENT || !(flags & TV_TOUCH_CREATE))
return -1;
umask(mode = umask(0));
mode = (~mode) & (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) < 0)
return -1;
close(fd);
errno = oerrno;
if (av == (const Tv_t*)&now && mv == (const Tv_t*)&now)
return 0;
#if _lib_utimets
return utimets(path, am);
#else
#if _lib_utimes
return utimes(path, am);
#else
#if _lib_utime
return utime(path, &am);
#else
errno = EINVAL;
return -1;
#endif
#endif
#endif
}
|