]> git.proxmox.com Git - mirror_zfs.git/blob - tests/zfs-tests/cmd/largest_file/largest_file.c
Add the ZFS Test Suite
[mirror_zfs.git] / tests / zfs-tests / cmd / largest_file / largest_file.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * Copyright (c) 2012 by Delphix. All rights reserved.
29 */
30
31 #include "../file_common.h"
32 #include <sys/param.h>
33 #include <signal.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38
39 typedef long long offset_t;
40 #define MAXOFFSET_T LLONG_MAX
41
42 /*
43 * --------------------------------------------------------------
44 *
45 * Assertion:
46 * The last byte of the largest file size can be
47 * accessed without any errors. Also, the writing
48 * beyond the last byte of the largest file size
49 * will produce an errno of EFBIG.
50 *
51 * --------------------------------------------------------------
52 * If the write() system call below returns a "1",
53 * then the last byte can be accessed.
54 * --------------------------------------------------------------
55 */
56 static void sigxfsz(int);
57 static void usage(char *);
58
59 int
60 main(int argc, char **argv)
61 {
62 int fd = 0;
63 offset_t offset = (MAXOFFSET_T - 1);
64 offset_t llseek_ret = 0;
65 int write_ret = 0;
66 int err = 0;
67 char mybuf[5];
68 char *testfile;
69 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
70
71 if (argc != 2) {
72 usage(argv[0]);
73 }
74
75 (void) sigset(SIGXFSZ, sigxfsz);
76
77 testfile = strdup(argv[1]);
78
79 fd = open(testfile, O_CREAT | O_RDWR, mode);
80 if (fd < 0) {
81 perror("Failed to create testfile");
82 err = errno;
83 goto out;
84 }
85
86 llseek_ret = lseek64(fd, offset, SEEK_SET);
87 if (llseek_ret < 0) {
88 perror("Failed to seek to end of testfile");
89 err = errno;
90 goto out;
91 }
92
93 write_ret = write(fd, mybuf, 1);
94 if (write_ret < 0) {
95 perror("Failed to write to end of file");
96 err = errno;
97 goto out;
98 }
99
100 offset = 0;
101 llseek_ret = lseek64(fd, offset, SEEK_CUR);
102 if (llseek_ret < 0) {
103 perror("Failed to seek to end of file");
104 err = errno;
105 goto out;
106 }
107
108 write_ret = write(fd, mybuf, 1);
109 if (write_ret < 0) {
110 if (errno == EFBIG) {
111 (void) printf("write errno=EFBIG: success\n");
112 err = 0;
113 } else {
114 perror("Did not receive EFBIG");
115 err = errno;
116 }
117 } else {
118 (void) printf("write completed successfully, test failed\n");
119 err = 1;
120 }
121
122 out:
123 (void) unlink(testfile);
124 free(testfile);
125 return (err);
126 }
127
128 static void
129 usage(char *name)
130 {
131 (void) printf("%s <testfile>\n", name);
132 exit(1);
133 }
134
135 /* ARGSUSED */
136 static void
137 sigxfsz(int signo)
138 {
139 (void) printf("\nlargest_file: sigxfsz() caught SIGXFSZ\n");
140 }