Path: blob/main/tests/sys/cddl/zfs/bin/randfree_file.c
39537 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*/252627#include "file_common.h"2829/*30* Create a file with assigned size and then free the specified31* section of the file32*/3334static void usage(char *progname);3536static void37usage(char *progname)38{39(void) fprintf(stderr,40"usage: %s [-l filesize] [-s start-offset]"41"[-n section-len] filename\n", progname);42exit(1);43}4445int46main(int argc, char *argv[])47{48char *filename, *buf;49size_t filesize;50off_t start_off, off_len;51int fd, ch;52struct flock fl;5354while ((ch = getopt(argc, argv, "l:s:n:")) != EOF) {55switch (ch) {56case 'l':57filesize = atoll(optarg);58break;59case 's':60start_off = atoll(optarg);61break;62case 'n':63off_len = atoll(optarg);64break;65default:66usage(argv[0]);67break;68}69}7071if (optind == argc - 1)72filename = argv[optind];73else74usage(argv[0]);7576buf = (char *)malloc(filesize);7778if ((fd = open(filename, O_RDWR|O_CREAT|O_TRUNC)) < 0) {79perror("open");80return (1);81}82if (write(fd, buf, filesize) < filesize) {83perror("write");84return (1);85}86#if UNSUPPORTED87fl.l_whence = SEEK_SET;88fl.l_start = start_off;89fl.l_len = off_len;90if (fcntl(fd, F_FREESP, &fl) != 0) {91perror("fcntl");92return (1);93}94#else95fprintf(stderr, "fcntl: F_FREESP not supported\n");96return (1);97#endif9899free(buf);100return (0);101}102103104