Update copyright annotations and license boilerplates to correspond with SGI Legals...
[xfstests-dev.git] / src / makeextents.c
1 /*
2  * Copyright (c) 2000-2004 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /*
20  * Write a bunch of holes to create a bunch of extents.
21  */
22  
23 #include "global.h"
24
25 char *progname;
26 __uint64_t num_holes = 1000;
27 int verbose_opt = 0;
28 char *filename;
29 int status_num = 100;
30 int wsync;
31 int preserve;
32 unsigned int blocksize;
33 __uint64_t fileoffset;
34
35 void
36 usage(void)
37 {
38         fprintf(stderr, "%s [-b blocksize] [-n num-holes] [-s status-num]"
39                         " [-o start-offset] [-vwp] file\n", progname);
40         exit(1);
41 }
42
43 int
44 main(int argc, char *argv[])
45 {
46         int c;
47         int fd;
48         int oflags;
49         __uint64_t i;
50         __uint64_t offset;
51         int blocksize = 512;
52         unsigned char *buffer = NULL;
53
54
55         progname = argv[0];
56
57         while ((c = getopt(argc, argv, "b:n:o:ps:vw")) != -1) {
58                 switch (c) {
59                 case 'b':
60                         blocksize = atoi(optarg);
61                         break;
62                 case 'n':
63                         num_holes = atoll(optarg);
64                         break;
65                 case 'v':
66                         verbose_opt = 1;
67                         break;
68                 case 'w':
69                         wsync = 1;
70                         break;
71                 case 'p':
72                         preserve = 1;
73                         break;
74                 case 's':
75                         status_num = atoi(optarg);
76                         break;
77                 case 'o':
78                         fileoffset = strtoull(optarg, NULL, 16);
79                         break;
80                 case '?':
81                         usage();
82                 }
83         }
84         if (optind == argc-1)
85                 filename = argv[optind];
86         else
87                 usage();
88
89         buffer = malloc(blocksize);
90         if (buffer == NULL) {
91             fprintf(stderr, "%s: blocksize to big to allocate buffer\n", progname);
92             return 1;
93         }
94
95         oflags = O_RDWR | O_CREAT;
96         oflags |=   (preserve ? 0 : O_TRUNC) |
97                     (wsync ? O_SYNC : 0);
98         
99         if ((fd = open(filename, oflags, 0666)) < 0) {
100                 perror("open");
101                 return 1;
102         }
103
104         for (i = 0; i < num_holes; i++) {
105
106                 offset = i * 128 * 1024 + fileoffset;
107
108                 if (lseek64(fd, offset, SEEK_SET) < 0) {
109                         perror("lseek");
110                         return 1;
111                 }
112
113                 if (write(fd, buffer, blocksize) < blocksize) {
114                         perror("write");
115                         return 1;
116                 }
117
118                 if (verbose_opt && ((i+1) % status_num == 0)) {
119                         printf("seeked and wrote %llu times\n", i+1);
120                 }
121         }
122
123         close(fd);
124
125         return 0;
126 }