Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/file/file_trunc.c
48674 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* 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.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright 2007 Sun Microsystems, Inc. All rights reserved.24* Use is subject to license terms.25*/2627/*28* Copyright (c) 2012, 2014 by Delphix. All rights reserved.29*/3031#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/stat.h>39#include <sys/statvfs.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#include <time.h>46#include <inttypes.h>4748#define FSIZE 256*1024*102449#define BSIZE 5125051/* Initialize Globals */52static long fsize = FSIZE;53static size_t bsize = BSIZE;54static int count = 0;55static int rflag = 0;56static uint_t seed = 0;57static int vflag = 0;58static int errflag = 0;59static off_t offset = 0;60static char *filename = NULL;6162static void usage(char *execname);63static void parse_options(int argc, char *argv[]);64static void do_write(int fd);65static void do_trunc(int fd);6667static void68usage(char *execname)69{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);74}7576int77main(int argc, char *argv[])78{79int i = 0;80int fd = -1;8182parse_options(argc, argv);8384fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);85if (fd < 0) {86perror("open");87exit(3);88}8990for (i = 0; count == 0 || i < count; i++) {91(void) do_write(fd);92(void) do_trunc(fd);93}9495(void) close(fd);96return (0);97}9899static void100parse_options(int argc, char *argv[])101{102int c;103104extern char *optarg;105extern int optind, optopt;106107count = fsize / bsize;108seed = (uint_t)time(NULL);109while ((c = getopt(argc, argv, "b:c:f:o:rs:v")) != -1) {110switch (c) {111case 'b':112bsize = atoi(optarg);113break;114115case 'c':116count = atoi(optarg);117break;118119case 'f':120fsize = atoi(optarg);121break;122123case 'o':124offset = atoi(optarg);125break;126127case 'r':128rflag++;129break;130131case 's':132seed = atoi(optarg);133break;134135case 'v':136vflag++;137break;138139case ':':140(void) fprintf(stderr,141"Option -%c requires an operand\n", optopt);142errflag++;143break;144145case '?':146(void) fprintf(stderr,147"Unrecognized option: -%c\n", optopt);148errflag++;149break;150}151152if (errflag) {153(void) usage(argv[0]);154}155}156if (argc <= optind) {157(void) fprintf(stderr,158"No filename specified\n");159usage(argv[0]);160}161filename = argv[optind];162163if (vflag) {164(void) fprintf(stderr, "Seed = %d\n", seed);165}166srandom(seed);167}168169static void170do_write(int fd)171{172off_t roffset = 0;173char *buf = NULL;174char *rbuf = NULL;175176buf = (char *)calloc(1, bsize);177rbuf = (char *)calloc(1, bsize);178if (buf == NULL || rbuf == NULL) {179perror("malloc");180exit(4);181}182183roffset = random() % fsize;184if (lseek64(fd, (offset + roffset), SEEK_SET) < 0) {185perror("lseek");186exit(5);187}188189(void) strcpy(buf, "ZFS Test Suite Truncation Test");190if (write(fd, buf, bsize) < bsize) {191perror("write");192exit(6);193}194195if (rflag) {196if (lseek64(fd, (offset + roffset), SEEK_SET) < 0) {197perror("lseek");198exit(7);199}200201if (read(fd, rbuf, bsize) < bsize) {202perror("read");203exit(8);204}205206if (memcmp(buf, rbuf, bsize) != 0) {207perror("memcmp");208exit(9);209}210}211if (vflag) {212(void) fprintf(stderr,213"Wrote to offset %" PRId64 "\n", (offset + roffset));214if (rflag) {215(void) fprintf(stderr,216"Read back from offset %" PRId64 "\n",217(offset + roffset));218}219}220221(void) free(buf);222(void) free(rbuf);223}224225static void226do_trunc(int fd)227{228off_t roffset = 0;229230roffset = random() % fsize;231if (ftruncate64(fd, (offset + roffset)) < 0) {232perror("truncate");233exit(7);234}235236if (vflag) {237(void) fprintf(stderr, "Truncated at offset %" PRId64 "\n",238(offset + roffset));239}240}241242243