summaryrefslogtreecommitdiffstats
path: root/src/stat.c
blob: 8da1466db2bb51f1de240844ad358a701e50892f (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD

#include "stat.h"
#include "atomic.h"
#include "bfstd.h"
#include "config.h"
#include "diag.h"
#include "sanity.h"
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#if BFS_USE_STATX && !BFS_HAS_LIBC_STATX
#  include <linux/stat.h>
#  include <sys/syscall.h>
#  include <unistd.h>
#endif

const char *bfs_stat_field_name(enum bfs_stat_field field) {
	switch (field) {
	case BFS_STAT_DEV:
		return "device number";
	case BFS_STAT_INO:
		return "inode nunmber";
	case BFS_STAT_TYPE:
		return "type";
	case BFS_STAT_MODE:
		return "mode";
	case BFS_STAT_NLINK:
		return "link count";
	case BFS_STAT_GID:
		return "group ID";
	case BFS_STAT_UID:
		return "user ID";
	case BFS_STAT_SIZE:
		return "size";
	case BFS_STAT_BLOCKS:
		return "block count";
	case BFS_STAT_RDEV:
		return "underlying device";
	case BFS_STAT_ATTRS:
		return "attributes";
	case BFS_STAT_ATIME:
		return "access time";
	case BFS_STAT_BTIME:
		return "birth time";
	case BFS_STAT_CTIME:
		return "change time";
	case BFS_STAT_MTIME:
		return "modification time";
	}

	bfs_bug("Unrecognized stat field");
	return "???";
}

void bfs_stat_convert(struct bfs_stat *dest, const struct stat *src) {
	dest->mask = 0;

	dest->dev = src->st_dev;
	dest->mask |= BFS_STAT_DEV;

	dest->ino = src->st_ino;
	dest->mask |= BFS_STAT_INO;

	dest->mode = src->st_mode;
	dest->mask |= BFS_STAT_TYPE | BFS_STAT_MODE;

	dest->nlink = src->st_nlink;
	dest->mask |= BFS_STAT_NLINK;

	dest->gid = src->st_gid;
	dest->mask |= BFS_STAT_GID;

	dest->uid = src->st_uid;
	dest->mask |= BFS_STAT_UID;

	dest->size = src->st_size;
	dest->mask |= BFS_STAT_SIZE;

	dest->blocks = src->st_blocks;
	dest->mask |= BFS_STAT_BLOCKS;

	dest->rdev = src->st_rdev;
	dest->mask |= BFS_STAT_RDEV;

#if BSD
	dest->attrs = src->st_flags;
	dest->mask |= BFS_STAT_ATTRS;
#endif

	dest->atime = src->st_atim;
	dest->mask |= BFS_STAT_ATIME;

	dest->ctime = src->st_ctim;
	dest->mask |= BFS_STAT_CTIME;

	dest->mtime = src->st_mtim;
	dest->mask |= BFS_STAT_MTIME;

#if __APPLE__ || __FreeBSD__ || __NetBSD__
	dest->btime = src->st_birthtim;
	dest->mask |= BFS_STAT_BTIME;
#endif
}

/**
 * bfs_stat() implementation backed by stat().
 */
static int bfs_stat_impl(int at_fd, const char *at_path, int at_flags, struct bfs_stat *buf) {
	struct stat statbuf;
	int ret = fstatat(at_fd, at_path, &statbuf, at_flags);
	if (ret == 0) {
		bfs_stat_convert(buf, &statbuf);
	}
	return ret;
}

#if BFS_USE_STATX

/**
 * Wrapper for the statx() system call, which had no glibc wrapper prior to 2.28.
 */
static int bfs_statx(int at_fd, const char *at_path, int at_flags, unsigned int mask, struct statx *buf) {
#if BFS_HAS_LIBC_STATX
	int ret = statx(at_fd, at_path, at_flags, mask, buf);
#else
	int ret = syscall(SYS_statx, at_fd, at_path, at_flags, mask, buf);
#endif

	if (ret == 0) {
		// -fsanitize=memory doesn't know about statx()
		sanitize_init(buf);
	}

	return ret;
}

int bfs_statx_convert(struct bfs_stat *dest, const struct statx *src) {
	// Callers shouldn't have to check anything except the times
	const unsigned int guaranteed = STATX_BASIC_STATS & ~(STATX_ATIME | STATX_CTIME | STATX_MTIME);
	if ((src->stx_mask & guaranteed) != guaranteed) {
		errno = ENOTSUP;
		return -1;
	}

	dest->mask = 0;

	dest->dev = xmakedev(src->stx_dev_major, src->stx_dev_minor);
	dest->mask |= BFS_STAT_DEV;

	dest->ino = src->stx_ino;
	dest->mask |= BFS_STAT_INO;

	dest->mode = src->stx_mode;
	dest->mask |= BFS_STAT_TYPE;
	dest->mask |= BFS_STAT_MODE;

	dest->nlink = src->stx_nlink;
	dest->mask |= BFS_STAT_NLINK;

	dest->gid = src->stx_gid;
	dest->mask |= BFS_STAT_GID;

	dest->uid = src->stx_uid;
	dest->mask |= BFS_STAT_UID;

	dest->size = src->stx_size;
	dest->mask |= BFS_STAT_SIZE;

	dest->blocks = src->stx_blocks;
	dest->mask |= BFS_STAT_BLOCKS;

	dest->rdev = xmakedev(src->stx_rdev_major, src->stx_rdev_minor);
	dest->mask |= BFS_STAT_RDEV;

	dest->attrs = src->stx_attributes;
	dest->mask |= BFS_STAT_ATTRS;

	if (src->stx_mask & STATX_ATIME) {
		dest->atime.tv_sec = src->stx_atime.tv_sec;
		dest->atime.tv_nsec = src->stx_atime.tv_nsec;
		dest->mask |= BFS_STAT_ATIME;
	}

	if (src->stx_mask & STATX_BTIME) {
		dest->btime.tv_sec = src->stx_btime.tv_sec;
		dest->btime.tv_nsec = src->stx_btime.tv_nsec;
		dest->mask |= BFS_STAT_BTIME;
	}

	if (src->stx_mask & STATX_CTIME) {
		dest->ctime.tv_sec = src->stx_ctime.tv_sec;
		dest->ctime.tv_nsec = src->stx_ctime.tv_nsec;
		dest->mask |= BFS_STAT_CTIME;
	}

	if (src->stx_mask & STATX_MTIME) {
		dest->mtime.tv_sec = src->stx_mtime.tv_sec;
		dest->mtime.tv_nsec = src->stx_mtime.tv_nsec;
		dest->mask |= BFS_STAT_MTIME;
	}

	return 0;
}

/**
 * bfs_stat() implementation backed by statx().
 */
static int bfs_statx_impl(int at_fd, const char *at_path, int at_flags, struct bfs_stat *buf) {
	unsigned int mask = STATX_BASIC_STATS | STATX_BTIME;
	struct statx xbuf;
	int ret = bfs_statx(at_fd, at_path, at_flags, mask, &xbuf);
	if (ret != 0) {
		return ret;
	}

	return bfs_statx_convert(buf, &xbuf);
}

#endif // BFS_USE_STATX

/**
 * Calls the stat() implementation with explicit flags.
 */
static int bfs_stat_explicit(int at_fd, const char *at_path, int at_flags, int x_flags, struct bfs_stat *buf) {
#if BFS_USE_STATX
	static atomic bool has_statx = true;

	if (load(&has_statx, relaxed)) {
		int ret = bfs_statx_impl(at_fd, at_path, at_flags | x_flags, buf);
		// EPERM is commonly returned in a seccomp() sandbox that does
		// not allow statx()
		if (ret != 0 && (errno == ENOSYS || errno == EPERM)) {
			store(&has_statx, false, relaxed);
		} else {
			return ret;
		}
	}
#endif

	return bfs_stat_impl(at_fd, at_path, at_flags, buf);
}

/**
 * Implements the BFS_STAT_TRYFOLLOW retry logic.
 */
static int bfs_stat_tryfollow(int at_fd, const char *at_path, int at_flags, int x_flags, enum bfs_stat_flags bfs_flags, struct bfs_stat *buf) {
	int ret = bfs_stat_explicit(at_fd, at_path, at_flags, x_flags, buf);

	if (ret != 0
	    && (bfs_flags & (BFS_STAT_NOFOLLOW | BFS_STAT_TRYFOLLOW)) == BFS_STAT_TRYFOLLOW
	    && errno_is_like(ENOENT))
	{
		at_flags |= AT_SYMLINK_NOFOLLOW;
		ret = bfs_stat_explicit(at_fd, at_path, at_flags, x_flags, buf);
	}

	return ret;
}

int bfs_stat(int at_fd, const char *at_path, enum bfs_stat_flags flags, struct bfs_stat *buf) {
	int at_flags = 0;
	if (flags & BFS_STAT_NOFOLLOW) {
		at_flags |= AT_SYMLINK_NOFOLLOW;
	}

#if defined(AT_NO_AUTOMOUNT) && (!__GNU__ || __GLIBC_PREREQ(2, 35))
	at_flags |= AT_NO_AUTOMOUNT;
#endif

	int x_flags = 0;
#ifdef AT_STATX_DONT_SYNC
	if (flags & BFS_STAT_NOSYNC) {
		x_flags |= AT_STATX_DONT_SYNC;
	}
#endif

	if (at_path) {
		return bfs_stat_tryfollow(at_fd, at_path, at_flags, x_flags, flags, buf);
	}

	// Check __GNU__ to work around https://lists.gnu.org/archive/html/bug-hurd/2021-12/msg00001.html
#if defined(AT_EMPTY_PATH) && !__GNU__
	static atomic bool has_at_ep = true;
	if (load(&has_at_ep, relaxed)) {
		at_flags |= AT_EMPTY_PATH;
		int ret = bfs_stat_explicit(at_fd, "", at_flags, x_flags, buf);
		if (ret != 0 && errno == EINVAL) {
			store(&has_at_ep, false, relaxed);
		} else {
			return ret;
		}
	}
#endif

	struct stat statbuf;
	if (fstat(at_fd, &statbuf) == 0) {
		bfs_stat_convert(buf, &statbuf);
		return 0;
	} else {
		return -1;
	}
}

const struct timespec *bfs_stat_time(const struct bfs_stat *buf, enum bfs_stat_field field) {
	if (!(buf->mask & field)) {
		errno = ENOTSUP;
		return NULL;
	}

	switch (field) {
	case BFS_STAT_ATIME:
		return &buf->atime;
	case BFS_STAT_BTIME:
		return &buf->btime;
	case BFS_STAT_CTIME:
		return &buf->ctime;
	case BFS_STAT_MTIME:
		return &buf->mtime;
	default:
		bfs_bug("Invalid stat field for time");
		errno = EINVAL;
		return NULL;
	}
}

void bfs_stat_id(const struct bfs_stat *buf, bfs_file_id *id) {
	memcpy(*id, &buf->dev, sizeof(buf->dev));
	memcpy(*id + sizeof(buf->dev), &buf->ino, sizeof(buf->ino));
}