xref: /freebsd/tests/sys/cddl/zfs/bin/file_trunc.c (revision faf25f48d601ae39f5752602f3020e2e92605625)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  * $FreeBSD$
21  */
22 
23 /*
24  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 #pragma ident	"@(#)file_trunc.c	1.2	07/05/25 SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <limits.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <sys/types.h>
36 #include <sys/fcntl.h>
37 #include <sys/stat.h>
38 #include <sys/statvfs.h>
39 #include <sys/errno.h>
40 #include <sys/time.h>
41 #include <sys/ioctl.h>
42 #include <sys/wait.h>
43 #include <sys/param.h>
44 #include <string.h>
45 
46 #define	FSIZE	256*1024*1024
47 #define	BSIZE	512
48 
49 /* Initialize Globals */
50 static long 	fsize = FSIZE;
51 static size_t 	bsize = BSIZE;
52 static int	count = 0;
53 static int	rflag = 0;
54 static int	seed = 0;
55 static int	vflag = 0;
56 static int	errflag = 0;
57 static off_t	offset = 0;
58 static char	*filename = NULL;
59 
60 static void usage(char *execname);
61 static void parse_options(int argc, char *argv[]);
62 static void do_write(int fd);
63 static void do_trunc(int fd);
64 
65 static void
66 usage(char *execname)
67 {
68 	(void) fprintf(stderr,
69 	    "usage: %s [-b blocksize] [-c count] [-f filesize]"
70 	    " [-o offset] [-s seed] [-r] [-v] filename\n", execname);
71 	(void) exit(1);
72 }
73 
74 int
75 main(int argc, char *argv[])
76 {
77 	int i = 0;
78 	int fd = -1;
79 
80 	parse_options(argc, argv);
81 
82 	fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
83 	if (fd < 0) {
84 		perror("open");
85 		exit(3);
86 	}
87 
88 	while (i < count) {
89 		(void) do_write(fd);
90 		(void) do_trunc(fd);
91 
92 		i++;
93 	}
94 
95 	(void) close(fd);
96 	return (0);
97 }
98 
99 static void
100 parse_options(int argc, char *argv[])
101 {
102 	int c;
103 
104 	extern char *optarg;
105 	extern int optind, optopt;
106 
107 	count = fsize / bsize;
108 	seed = time(NULL);
109 	while ((c = getopt(argc, argv, "b:c:f:o:rs:v")) != -1) {
110 		switch (c) {
111 			case 'b':
112 				bsize = atoi(optarg);
113 				break;
114 
115 			case 'c':
116 				count = atoi(optarg);
117 				break;
118 
119 			case 'f':
120 				fsize = atoi(optarg);
121 				break;
122 
123 			case 'o':
124 				offset = atoi(optarg);
125 				break;
126 
127 			case 'r':
128 				rflag++;
129 				break;
130 
131 			case 's':
132 				seed = atoi(optarg);
133 				break;
134 
135 			case 'v':
136 				vflag++;
137 				break;
138 
139 			case ':':
140 				(void) fprintf(stderr,
141 				    "Option -%c requires an operand\n", optopt);
142 				errflag++;
143 				break;
144 
145 			case '?':
146 				(void) fprintf(stderr,
147 				    "Unrecognized option: -%c\n", optopt);
148 				errflag++;
149 				break;
150 		}
151 
152 		if (errflag) {
153 			(void) usage(argv[0]);
154 		}
155 	}
156 	if (argc <= optind) {
157 		(void) fprintf(stderr,
158 		    "No filename specified\n");
159 		usage(argv[0]);
160 	}
161 	filename = argv[optind];
162 
163 	if (vflag) {
164 		(void) fprintf(stderr, "Seed = %d\n", seed);
165 	}
166 	srandom(seed);
167 }
168 
169 static void
170 do_write(int fd)
171 {
172 	off_t	roffset = 0;
173 	char 	*buf = NULL;
174 	char 	*rbuf = NULL;
175 
176 	buf = (char *)calloc(1, bsize);
177 	rbuf = (char *)calloc(1, bsize);
178 	if (buf == NULL || rbuf == NULL) {
179 		perror("malloc");
180 		exit(4);
181 	}
182 
183 	roffset = random() % fsize;
184 	if (lseek(fd, (offset + roffset), SEEK_SET) < 0) {
185 		perror("lseek");
186 		exit(5);
187 	}
188 
189 	strcpy(buf, "ZFS Test Suite Truncation Test");
190 	if (write(fd, buf, bsize) < bsize) {
191 		perror("write");
192 		exit(6);
193 	}
194 
195 	if (rflag) {
196 		if (lseek(fd, (offset + roffset), SEEK_SET) < 0) {
197 			perror("lseek");
198 			exit(7);
199 		}
200 
201 		if (read(fd, rbuf, bsize) < bsize) {
202 			perror("read");
203 			exit(8);
204 		}
205 
206 		if (memcmp(buf, rbuf, bsize) != 0) {
207 			perror("memcmp");
208 			exit(9);
209 		}
210 	}
211 	if (vflag) {
212 		(void) fprintf(stderr,
213 		    "Wrote to offset %ld\n", (offset + roffset));
214 		if (rflag) {
215 			(void) fprintf(stderr,
216 			    "Read back from offset %ld\n", (offset + roffset));
217 		}
218 	}
219 
220 	(void) free(buf);
221 	(void) free(rbuf);
222 }
223 
224 static void
225 do_trunc(int fd)
226 {
227 	off_t   roffset = 0;
228 
229 	roffset = random() % fsize;
230 	if (ftruncate(fd, (offset + roffset))  < 0) {
231 		perror("truncate");
232 		exit(7);
233 	}
234 
235 	if (vflag) {
236 		(void) fprintf(stderr,
237 		    "Truncated at offset %ld\n",
238 		    (offset + roffset));
239 	}
240 }
241