5767 fix several problems with zfs test suite
[unleashed.git] / usr / src / test / zfs-tests / cmd / file_trunc / file_trunc.c
bloba6e78d153c260ced6a81c50b56e4c34331d92b52
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <limits.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <sys/types.h>
38 #include <sys/fcntl.h>
39 #include <sys/stat.h>
40 #include <sys/statvfs.h>
41 #include <sys/errno.h>
42 #include <sys/time.h>
43 #include <sys/ioctl.h>
44 #include <sys/wait.h>
45 #include <sys/param.h>
46 #include <string.h>
48 #define FSIZE 256*1024*1024
49 #define BSIZE 512
51 /* Initialize Globals */
52 static long fsize = FSIZE;
53 static size_t bsize = BSIZE;
54 static int count = 0;
55 static int rflag = 0;
56 static int seed = 0;
57 static int vflag = 0;
58 static int errflag = 0;
59 static off_t offset = 0;
60 static char *filename = NULL;
62 static void usage(char *execname);
63 static void parse_options(int argc, char *argv[]);
64 static void do_write(int fd);
65 static void do_trunc(int fd);
67 static void
68 usage(char *execname)
70 (void) fprintf(stderr,
71 "usage: %s [-b blocksize] [-c count] [-f filesize]"
72 " [-o offset] [-s seed] [-r] [-v] filename\n", execname);
73 (void) exit(1);
76 int
77 main(int argc, char *argv[])
79 int i;
80 int fd = -1;
82 parse_options(argc, argv);
84 fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
85 if (fd < 0) {
86 perror("open");
87 exit(3);
90 for (i = 0; count == 0 || i < count; i++) {
91 (void) do_write(fd);
92 (void) do_trunc(fd);
95 (void) close(fd);
96 return (0);
99 static void
100 parse_options(int argc, char *argv[])
102 int c;
104 extern char *optarg;
105 extern int optind, optopt;
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;
115 case 'c':
116 count = atoi(optarg);
117 break;
119 case 'f':
120 fsize = atoi(optarg);
121 break;
123 case 'o':
124 offset = atoi(optarg);
125 break;
127 case 'r':
128 rflag++;
129 break;
131 case 's':
132 seed = atoi(optarg);
133 break;
135 case 'v':
136 vflag++;
137 break;
139 case ':':
140 (void) fprintf(stderr,
141 "Option -%c requires an operand\n", optopt);
142 errflag++;
143 break;
145 case '?':
146 (void) fprintf(stderr,
147 "Unrecognized option: -%c\n", optopt);
148 errflag++;
149 break;
152 if (errflag) {
153 (void) usage(argv[0]);
156 if (argc <= optind) {
157 (void) fprintf(stderr,
158 "No filename specified\n");
159 usage(argv[0]);
161 filename = argv[optind];
163 if (vflag) {
164 (void) fprintf(stderr, "Seed = %d\n", seed);
166 srandom(seed);
169 static void
170 do_write(int fd)
172 off_t roffset = 0;
173 char *buf = NULL;
174 char *rbuf = NULL;
176 buf = (char *)calloc(1, bsize);
177 rbuf = (char *)calloc(1, bsize);
178 if (buf == NULL || rbuf == NULL) {
179 perror("malloc");
180 exit(4);
183 roffset = random() % fsize;
184 if (lseek64(fd, (offset + roffset), SEEK_SET) < 0) {
185 perror("lseek");
186 exit(5);
189 (void) strcpy(buf, "ZFS Test Suite Truncation Test");
190 if (write(fd, buf, bsize) < bsize) {
191 perror("write");
192 exit(6);
195 if (rflag) {
196 if (lseek64(fd, (offset + roffset), SEEK_SET) < 0) {
197 perror("lseek");
198 exit(7);
201 if (read(fd, rbuf, bsize) < bsize) {
202 perror("read");
203 exit(8);
206 if (memcmp(buf, rbuf, bsize) != 0) {
207 perror("memcmp");
208 exit(9);
211 if (vflag) {
212 (void) fprintf(stderr,
213 "Wrote to offset %lld\n", (offset + roffset));
214 if (rflag) {
215 (void) fprintf(stderr,
216 "Read back from offset %lld\n", (offset + roffset));
220 (void) free(buf);
221 (void) free(rbuf);
224 static void
225 do_trunc(int fd)
227 off_t roffset = 0;
229 roffset = random() % fsize;
230 if (ftruncate64(fd, (offset + roffset)) < 0) {
231 perror("truncate");
232 exit(7);
235 if (vflag) {
236 (void) fprintf(stderr,
237 "Truncated at offset %lld\n",
238 (offset + roffset));