1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * http://www.illumos.org/license/CDDL.
11 */
12
13 /*
14 * Copyright 2021 iXsystems, Inc.
15 */
16
17 /*
18 * FreeBSD and macOS expose file generation number through stat(2) and stat(1).
19 * Linux exposes it instead through an ioctl.
20 */
21
22 #include <sys/ioctl.h>
23 #ifdef _KERNEL
24 #include <sys/fcntl.h>
25 #else
26 #include <fcntl.h>
27 #endif
28 #include <linux/fs.h>
29 #include <err.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 int
main(int argc,const char * const argv[])35 main(int argc, const char * const argv[])
36 {
37 if (argc != 2)
38 errx(EXIT_FAILURE, "usage: %s filename", argv[0]);
39
40 int fd = open(argv[1], O_RDONLY);
41 if (fd == -1)
42 err(EXIT_FAILURE, "failed to open %s", argv[1]);
43
44 int gen = 0;
45 if (ioctl(fd, FS_IOC_GETVERSION, &gen) == -1)
46 err(EXIT_FAILURE, "FS_IOC_GETVERSION failed");
47
48 (void) close(fd);
49
50 (void) printf("%d\n", gen);
51
52 return (EXIT_SUCCESS);
53 }
54