Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/file/largest_file.c
48676 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 by Delphix. All rights reserved.29*/3031#include "file_common.h"32#include <sys/param.h>33#include <signal.h>34#include <stdio.h>35#include <string.h>36#include <sys/stdtypes.h>37#include <unistd.h>3839/*40* --------------------------------------------------------------41*42* Assertion:43* The last byte of the largest file size can be44* accessed without any errors. Also, the writing45* beyond the last byte of the largest file size46* will produce an errno of EFBIG.47*48* --------------------------------------------------------------49* If the write() system call below returns a "1",50* then the last byte can be accessed.51* --------------------------------------------------------------52*/53static void sigxfsz(int);54static void usage(char *);5556int57main(int argc, char **argv)58{59int fd = 0;60offset_t offset = (MAXOFFSET_T - 1);61offset_t llseek_ret = 0;62int write_ret = 0;63int err = 0;64char mybuf[5] = "aaaa";65char *testfile;66mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;67struct sigaction sa;6869if (argc != 2) {70usage(argv[0]);71}7273if (sigemptyset(&sa.sa_mask) == -1)74return (errno);75sa.sa_flags = 0;76sa.sa_handler = sigxfsz;77if (sigaction(SIGXFSZ, &sa, NULL) == -1)78return (errno);7980testfile = strdup(argv[1]);81if (testfile == NULL)82return (errno);8384fd = open(testfile, O_CREAT | O_RDWR, mode);85if (fd < 0) {86err = errno;87perror("Failed to create testfile");88free(testfile);89return (err);90}9192llseek_ret = lseek64(fd, offset, SEEK_SET);93if (llseek_ret < 0) {94err = errno;95perror("Failed to seek to end of testfile");96goto out;97}9899write_ret = write(fd, mybuf, 1);100if (write_ret < 0) {101err = errno;102perror("Failed to write to end of file");103goto out;104}105106offset = 0;107llseek_ret = lseek64(fd, offset, SEEK_CUR);108if (llseek_ret < 0) {109err = errno;110perror("Failed to seek to end of file");111goto out;112}113114write_ret = write(fd, mybuf, 1);115if (write_ret < 0) {116if (errno == EFBIG || errno == EINVAL) {117(void) printf("write errno=EFBIG|EINVAL: success\n");118err = 0;119} else {120err = errno;121perror("Did not receive EFBIG");122}123} else {124(void) printf("write completed successfully, test failed\n");125err = 1;126}127128out:129(void) unlink(testfile);130free(testfile);131close(fd);132return (err);133}134135static void136usage(char *name)137{138(void) printf("%s <testfile>\n", name);139exit(1);140}141142static void143sigxfsz(int signo)144{145(void) signo;146(void) printf("\nlargest_file: sigxfsz() caught SIGXFSZ\n");147}148149150