]> git.proxmox.com Git - mirror_zfs.git/blob - tests/zfs-tests/cmd/largest_file/largest_file.c
5e6a1866059b1aed0a2dbe41c8625e69695b5155
[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] = "aaaa\0";
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 err = errno;
82 perror("Failed to create testfile");
83 free(testfile);
84 return (err);
85 }
86
87 llseek_ret = lseek64(fd, offset, SEEK_SET);
88 if (llseek_ret < 0) {
89 err = errno;
90 perror("Failed to seek to end of testfile");
91 goto out;
92 }
93
94 write_ret = write(fd, mybuf, 1);
95 if (write_ret < 0) {
96 err = errno;
97 perror("Failed to write to end of file");
98 goto out;
99 }
100
101 offset = 0;
102 llseek_ret = lseek64(fd, offset, SEEK_CUR);
103 if (llseek_ret < 0) {
104 err = errno;
105 perror("Failed to seek to end of file");
106 goto out;
107 }
108
109 write_ret = write(fd, mybuf, 1);
110 if (write_ret < 0) {
111 if (errno == EFBIG || errno == EINVAL) {
112 (void) printf("write errno=EFBIG|EINVAL: success\n");
113 err = 0;
114 } else {
115 err = errno;
116 perror("Did not receive EFBIG");
117 }
118 } else {
119 (void) printf("write completed successfully, test failed\n");
120 err = 1;
121 }
122
123 out:
124 (void) unlink(testfile);
125 free(testfile);
126 close(fd);
127 return (err);
128 }
129
130 static void
131 usage(char *name)
132 {
133 (void) printf("%s <testfile>\n", name);
134 exit(1);
135 }
136
137 /* ARGSUSED */
138 static void
139 sigxfsz(int signo)
140 {
141 (void) printf("\nlargest_file: sigxfsz() caught SIGXFSZ\n");
142 }