summaryrefslogtreecommitdiffstats
path: root/tests/xtouch.c
blob: b1daec72e38c6108d71d0fcc00fb171dd6cd8a58 (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
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
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD

#include "bfstd.h"
#include "config.h"
#include "sanity.h"
#include "xtime.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

/** Parsed xtouch arguments. */
struct args {
	/** Simple flags. */
	enum {
		/** Don't create nonexistent files (-c). */
		NO_CREATE = 1 << 0,
		/** Don't follow symlinks (-h). */
		NO_FOLLOW = 1 << 1,
		/** Create any missing parent directories (-p). */
		CREATE_PARENTS = 1 << 2,
	} flags;

	/** Timestamps (-r|-t|-d). */
	struct timespec times[2];

	/** File creation mode (-M; default 0666 & ~umask). */
	mode_t fmode;
	/** Directory creation mode (-M; default 0777 & ~umask). */
	mode_t dmode;
	/** Parent directory creation mode (0777 & ~umask). */
	mode_t pmode;
};

/** Open (and maybe create) a single directory. */
static int open_dir(const struct args *args, int dfd, const char *path) {
	int ret = openat(dfd, path, O_SEARCH | O_DIRECTORY);

	if (ret < 0 && errno == ENOENT && (args->flags & CREATE_PARENTS)) {
		if (mkdirat(dfd, path, args->pmode) == 0 || errno == EEXIST) {
			ret = openat(dfd, path, O_SEARCH | O_DIRECTORY);
		}
	}

	return ret;
}

/** Open (and maybe create) the parent directory of the path. */
static int open_parent(const struct args *args, const char **path) {
	size_t max = xbaseoff(*path);
	if (max == 0) {
		return AT_FDCWD;
	}

	char *dir = strndup(*path, max);
	if (!dir) {
		return -1;
	}

	// Optimistically try the whole path first
	int dfd = open_dir(args, AT_FDCWD, dir);
	if (dfd >= 0) {
		goto done;
	}

	if (errno == ENOENT) {
		if (!(args->flags & CREATE_PARENTS)) {
			goto err;
		}
	} else if (!errno_is_like(ENAMETOOLONG)) {
		goto err;
	}

	// Open the parents one-at-a-time
	dfd = AT_FDCWD;
	char *cur = dir;
	while (*cur) {
		char *next = cur;
		next += strcspn(next, "/");
		next += strspn(next, "/");

		char c = *next;
		*next = '\0';

		int parent = dfd;
		dfd = open_dir(args, parent, cur);
		if (parent >= 0) {
			close_quietly(parent);
		}
		if (dfd < 0) {
			goto err;
		}

		*next = c;
		cur = next;
	}

done:
	*path += max;
err:
	free(dir);
	return dfd;
}

/** Compute flags for fstatat()/utimensat(). */
static int at_flags(const struct args *args) {
	if (args->flags & NO_FOLLOW) {
		return AT_SYMLINK_NOFOLLOW;
	} else {
		return 0;
	}
}

/** Touch one path. */
static int xtouch(const struct args *args, const char *path) {
	int dfd = open_parent(args, &path);
	if (dfd < 0 && dfd != AT_FDCWD) {
		return -1;
	}

	int ret = utimensat(dfd, path, args->times, at_flags(args));
	if (ret == 0 || errno != ENOENT) {
		goto done;
	}

	if (args->flags & NO_CREATE) {
		ret = 0;
		goto done;
	}

	size_t len = strlen(path);
	if (len > 0 && path[len - 1] == '/') {
		if (mkdirat(dfd, path, args->dmode) == 0) {
			ret = utimensat(dfd, path, args->times, at_flags(args));
		}
	} else {
		int fd = openat(dfd, path, O_WRONLY | O_CREAT, args->fmode);
		if (fd >= 0) {
			if (futimens(fd, args->times) == 0) {
				ret = xclose(fd);
			} else {
				close_quietly(fd);
			}
		}
	}

done:
	if (dfd >= 0) {
		close_quietly(dfd);
	}
	return ret;
}

int main(int argc, char *argv[]) {
	tzset();

	mode_t mask = umask(0);

	struct args args = {
		.flags = 0,
		.times = {
			{ .tv_nsec = UTIME_OMIT },
			{ .tv_nsec = UTIME_OMIT },
		},
		.fmode = 0666 & ~mask,
		.dmode = 0777 & ~mask,
		.pmode = 0777 & ~mask,
	};

	bool atime = false, mtime = false;
	const char *darg = NULL;
	const char *marg = NULL;
	const char *rarg = NULL;

	const char *cmd = argc > 0 ? argv[0] : "xtouch";
	int c;
	while (c = getopt(argc, argv, ":M:acd:hmpr:t:"), c != -1) {
		switch (c) {
		case 'M':
			marg = optarg;
			break;
		case 'a':
			atime = true;
			break;
		case 'c':
			args.flags |= NO_CREATE;
			break;
		case 'd':
		case 't':
			darg = optarg;
			break;
		case 'h':
			args.flags |= NO_FOLLOW;
			break;
		case 'm':
			mtime = true;
			break;
		case 'p':
			args.flags |= CREATE_PARENTS;
			break;
		case 'r':
			rarg = optarg;
			break;
		case ':':
			fprintf(stderr, "%s: Missing argument to -%c\n", cmd, optopt);
			return EXIT_FAILURE;
		case '?':
			fprintf(stderr, "%s: Unrecognized option -%c\n", cmd, optopt);
			return EXIT_FAILURE;
		}
	}

	if (marg) {
		char *end;
		long mode = strtol(marg, &end, 8);
		// https://github.com/llvm/llvm-project/issues/64946
		sanitize_init(&end);
		if (*marg && !*end && mode >= 0 && mode < 01000) {
			args.fmode = args.dmode = mode;
		} else {
			fprintf(stderr, "%s: Invalid mode '%s'\n", cmd, marg);
			return EXIT_FAILURE;
		}
	}

	struct timespec times[2];

	if (rarg) {
		struct stat buf;
		if (fstatat(AT_FDCWD, rarg, &buf, at_flags(&args)) != 0) {
			fprintf(stderr, "%s: '%s': %s\n", cmd, rarg, xstrerror(errno));
			return EXIT_FAILURE;
		}
		times[0] = buf.st_atim;
		times[1] = buf.st_mtim;
	} else if (darg) {
		if (xgetdate(darg, &times[0]) != 0) {
			fprintf(stderr, "%s: Parsing time '%s' failed: %s\n", cmd, darg, xstrerror(errno));
			return EXIT_FAILURE;
		}
		times[1] = times[0];
	} else {
		// Don't use UTIME_NOW, so that multiple paths all get the same timestamp
		if (xgettime(&times[0]) != 0) {
			perror("xgettime()");
			return EXIT_FAILURE;
		}
		times[1] = times[0];
	}

	if (!atime && !mtime) {
		atime = true;
		mtime = true;
	}
	if (atime) {
		args.times[0] = times[0];
	}
	if (mtime) {
		args.times[1] = times[1];
	}

	if (optind >= argc) {
		fprintf(stderr, "%s: No files to touch\n", cmd);
		return EXIT_FAILURE;
	}

	int ret = EXIT_SUCCESS;
	for (; optind < argc; ++optind) {
		const char *path = argv[optind];
		if (xtouch(&args, path) != 0) {
			fprintf(stderr, "%s: '%s': %s\n", cmd, path, xstrerror(errno));
			ret = EXIT_FAILURE;
		}
	}
	return ret;
}