xref: /freebsd/tools/test/stress2/tools/lsholes.c (revision 16038816)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/stat.h>
30 
31 #include <err.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 int
39 main(int argc, char *argv[])
40 {
41 	struct stat st;
42 	off_t data, hole, pos;
43 	long mx;
44 	int fd, n;
45 	char *name;
46 
47         if (argc != 2) {
48                 fprintf(stderr, "Usage: %s <file>\n", argv[0]);
49                 exit(1);
50         }
51 
52         name = argv[1];
53         if ((fd = open(name, O_RDONLY)) == -1)
54                 err(1, "open(%s)", name);
55         if (fstat(fd, &st))
56                 err(1, "fstat()");
57 	if ((mx = fpathconf(fd, _PC_MIN_HOLE_SIZE)) == -1)
58 		err(1, "fpathconf()");
59 	fprintf(stderr, "file \"%s\" size = %jd, _PC_MIN_HOLE_SIZE = %ld\n",
60 	    name, (intmax_t)st.st_size, mx);
61 	n = 1;
62 	pos = 0;
63 	while (pos < st.st_size) {
64 		if ((hole = lseek(fd, pos, SEEK_HOLE)) == -1)
65 			err(1, "lseek(SEEK_HOLE)");
66 		if ((data = lseek(fd, hole, SEEK_DATA)) == -1) {
67 			if (errno == ENXIO) {
68 				if (hole == st.st_size)
69 					break;
70 				fprintf(stderr,
71 				    "No data after hole @ %jd\n",
72 				    (intmax_t)hole);
73 				break;
74 			}
75 			err(1, "lseek(SEEK_DATA)");
76 		}
77 		pos = data;
78 		printf("hole #%d @ %jd (0x%jx), size=%jd (0x%jx)\n",
79 		    n, (intmax_t)hole, (intmax_t)hole, (intmax_t)(data - hole),
80 		    (intmax_t)(data - hole));
81 		n++;
82         }
83         close(fd);
84 	if (hole != st.st_size)
85 		errx(1, "No implicit hole at EOF");
86 }
87