Path: blob/main/tests/sys/cddl/zfs/bin/file_trunc.c
39536 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/2021/*22* Copyright 2007 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#include <stdio.h>27#include <stdlib.h>28#include <unistd.h>29#include <limits.h>30#include <errno.h>31#include <fcntl.h>32#include <sys/types.h>33#include <sys/fcntl.h>34#include <sys/stat.h>35#include <sys/statvfs.h>36#include <sys/errno.h>37#include <sys/time.h>38#include <sys/ioctl.h>39#include <sys/wait.h>40#include <sys/param.h>41#include <string.h>4243#define FSIZE 256*1024*102444#define BSIZE 5124546/* Initialize Globals */47static long fsize = FSIZE;48static size_t bsize = BSIZE;49static int count = 0;50static int rflag = 0;51static int seed = 0;52static int vflag = 0;53static int errflag = 0;54static off_t offset = 0;55static char *filename = NULL;5657static void usage(char *execname);58static void parse_options(int argc, char *argv[]);59static void do_write(int fd);60static void do_trunc(int fd);6162static void63usage(char *execname)64{65(void) fprintf(stderr,66"usage: %s [-b blocksize] [-c count] [-f filesize]"67" [-o offset] [-s seed] [-r] [-v] filename\n", execname);68(void) exit(1);69}7071int72main(int argc, char *argv[])73{74int i = 0;75int fd = -1;7677parse_options(argc, argv);7879fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);80if (fd < 0) {81perror("open");82exit(3);83}8485while (i < count) {86(void) do_write(fd);87(void) do_trunc(fd);8889i++;90}9192(void) close(fd);93return (0);94}9596static void97parse_options(int argc, char *argv[])98{99int c;100101extern char *optarg;102extern int optind, optopt;103104count = fsize / bsize;105seed = time(NULL);106while ((c = getopt(argc, argv, "b:c:f:o:rs:v")) != -1) {107switch (c) {108case 'b':109bsize = atoi(optarg);110break;111112case 'c':113count = atoi(optarg);114break;115116case 'f':117fsize = atoi(optarg);118break;119120case 'o':121offset = atoi(optarg);122break;123124case 'r':125rflag++;126break;127128case 's':129seed = atoi(optarg);130break;131132case 'v':133vflag++;134break;135136case ':':137(void) fprintf(stderr,138"Option -%c requires an operand\n", optopt);139errflag++;140break;141142case '?':143(void) fprintf(stderr,144"Unrecognized option: -%c\n", optopt);145errflag++;146break;147}148149if (errflag) {150(void) usage(argv[0]);151}152}153if (argc <= optind) {154(void) fprintf(stderr,155"No filename specified\n");156usage(argv[0]);157}158filename = argv[optind];159160if (vflag) {161(void) fprintf(stderr, "Seed = %d\n", seed);162}163srandom(seed);164}165166static void167do_write(int fd)168{169off_t roffset = 0;170char *buf = NULL;171char *rbuf = NULL;172173buf = (char *)calloc(1, bsize);174rbuf = (char *)calloc(1, bsize);175if (buf == NULL || rbuf == NULL) {176perror("malloc");177exit(4);178}179180roffset = random() % fsize;181if (lseek(fd, (offset + roffset), SEEK_SET) < 0) {182perror("lseek");183exit(5);184}185186strcpy(buf, "ZFS Test Suite Truncation Test");187if (write(fd, buf, bsize) < bsize) {188perror("write");189exit(6);190}191192if (rflag) {193if (lseek(fd, (offset + roffset), SEEK_SET) < 0) {194perror("lseek");195exit(7);196}197198if (read(fd, rbuf, bsize) < bsize) {199perror("read");200exit(8);201}202203if (memcmp(buf, rbuf, bsize) != 0) {204perror("memcmp");205exit(9);206}207}208if (vflag) {209(void) fprintf(stderr,210"Wrote to offset %ld\n", (offset + roffset));211if (rflag) {212(void) fprintf(stderr,213"Read back from offset %ld\n", (offset + roffset));214}215}216217(void) free(buf);218(void) free(rbuf);219}220221static void222do_trunc(int fd)223{224off_t roffset = 0;225226roffset = random() % fsize;227if (ftruncate(fd, (offset + roffset)) < 0) {228perror("truncate");229exit(7);230}231232if (vflag) {233(void) fprintf(stderr,234"Truncated at offset %ld\n",235(offset + roffset));236}237}238239240