xfs: test fallocate ops when rt extent size is and isn't a power of 2
[xfstests-dev.git] / dmapi / src / suite1 / cmd / security_hole.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 #include <string.h>
13
14 /*
15         To read unallocated disk blocks in a filesystem, do
16                 cc -o test thisfile.c
17                 ./test myfile
18                 cat myfile
19 */
20
21 #ifndef linux
22 extern  char    *sys_errlist[];
23 #endif
24 extern  int     optind;
25 extern  char    *optarg;
26
27 char    *Progname;
28
29
30 static void
31 usage(void)
32 {
33         int     i;
34
35         fprintf(stderr, "usage:\t%s [-s size] pathname\n", Progname);
36         fprintf(stderr, "\t-s size\t\tsize of file (default 10,000,000 bytes)\n");
37         exit(1);
38 }
39
40
41 int
42 main(
43         int     argc, 
44         char    **argv)
45 {
46         char    *pathname = NULL;
47         off_t   size = 10000000;
48         char    buff[1];
49         int     method = F_RESVSP;
50         flock_t flock;
51         int     opt;
52         int     fd;
53
54         Progname = strrchr(argv[0], '/');
55         if (Progname) {
56                 Progname++;
57         } else {
58                 Progname = argv[0];
59         }
60
61         /* Crack and validate the command line options. */
62
63         while ((opt = getopt(argc, argv, "s:")) != EOF) {
64                 switch (opt) {
65                 case 's':
66                         size = atol(optarg);
67                         break;
68                 case '?':
69                         usage();
70                 }
71         }
72         if (optind + 1 != argc)
73                 usage();
74         pathname = argv[optind];
75
76         /* Create the file and write one byte at a large offset to create a
77            big hole in the middle of the file.
78         */
79
80         if ((fd = open(pathname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) {
81                 perror("open failed");
82                 exit(1);
83         }
84         if (lseek(fd, size, 0) < 0) {
85                 perror("lseek failed");
86                 exit(1);
87         }
88         buff[0] = '\0';
89         if (write(fd, buff, 1) != 1) {
90                 perror("write failed");
91                 exit(1);
92         }
93
94         /* Now fill in the hole with uninitialized blocks. */
95
96         flock.l_whence = 0;
97         flock.l_start = 0;
98         flock.l_len = size;
99
100         if (fcntl(fd, method, &flock) < 0) {
101                 perror("fcntl failed");
102                 exit(1);
103         }
104         printf("%s created\n", pathname);
105 }