ftruncate_test.c (160199) | ftruncate_test.c (160200) |
---|---|
1/*- 2 * Copyright (c) 2006 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 9 unchanged lines hidden (view full) --- 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * | 1/*- 2 * Copyright (c) 2006 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 9 unchanged lines hidden (view full) --- 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * |
26 * $FreeBSD: head/tools/regression/file/ftruncate/ftruncate.c 160199 2006-07-09 10:19:07Z rwatson $ | 26 * $FreeBSD: head/tools/regression/file/ftruncate/ftruncate.c 160200 2006-07-09 10:43:31Z rwatson $ |
27 */ 28 29/* | 27 */ 28 29/* |
30 * Very simple regression test. Open a temporary file, then proceed to 31 * ftruncate it to various values and check that fstat returns those values. 32 * First we run forwards through a set of sizes starting with zero, then back 33 * again. | 30 * Very simple regression test. |
34 * 35 * Future tests that might be of interest: 36 * 37 * - Make sure that files grown via ftruncate() return 0 bytes for data 38 * reads. 39 * 40 * - Make sure that we can't ftruncate on a read-only descriptor. 41 * 42 * - Make sure we get EISDIR on a directory. 43 */ 44 | 31 * 32 * Future tests that might be of interest: 33 * 34 * - Make sure that files grown via ftruncate() return 0 bytes for data 35 * reads. 36 * 37 * - Make sure that we can't ftruncate on a read-only descriptor. 38 * 39 * - Make sure we get EISDIR on a directory. 40 */ 41 |
42#include <sys/types.h> 43#include <sys/event.h> 44#include <sys/socket.h> |
|
45#include <sys/stat.h> 46 47#include <err.h> 48#include <errno.h> 49#include <limits.h> 50#include <stdio.h> 51#include <unistd.h> 52 --- 5 unchanged lines hidden (view full) --- 58 1024, 1025, 2047, 2048, 2049, 4095, 4096, 4097, 8191, 8192, 8193, 16383, 59 16384, 16385}; 60static int size_count = sizeof(sizes) / sizeof(off_t); 61 62int 63main(int argc, char *argv[]) 64{ 65 char path[PATH_MAX]; | 45#include <sys/stat.h> 46 47#include <err.h> 48#include <errno.h> 49#include <limits.h> 50#include <stdio.h> 51#include <unistd.h> 52 --- 5 unchanged lines hidden (view full) --- 58 1024, 1025, 2047, 2048, 2049, 4095, 4096, 4097, 8191, 8192, 8193, 16383, 59 16384, 16385}; 60static int size_count = sizeof(sizes) / sizeof(off_t); 61 62int 63main(int argc, char *argv[]) 64{ 65 char path[PATH_MAX]; |
66 int fd, fds[2], i; |
|
66 struct stat sb; | 67 struct stat sb; |
67 int fd, i; | |
68 | 68 |
69 /* 70 * Tests using a writable temporary file: grow and then shrink a file 71 * using ftruncate and various sizes. Make sure that a negative file 72 * size is rejected. 73 */ |
|
69 snprintf(path, PATH_MAX, "/tmp/ftruncate.XXXXXXXXXXXXX"); 70 fd = mkstemp(path); 71 if (fd < 0) 72 err(-1, "makestemp"); 73 (void)unlink(path); 74 75 if (ftruncate(fd, -1) == 0) 76 errx(-1, "ftruncate(fd, -1) succeeded"); --- 14 unchanged lines hidden (view full) --- 91 if (ftruncate(fd, sizes[i]) < 0) 92 err(-1, "ftruncate(%llu) down", sizes[i]); 93 if (fstat(fd, &sb) < 0) 94 err(-1, "stat"); 95 if (sb.st_size != sizes[i]) 96 errx(-1, "fstat(%llu) returned %llu down", sizes[i], 97 sb.st_size); 98 } | 74 snprintf(path, PATH_MAX, "/tmp/ftruncate.XXXXXXXXXXXXX"); 75 fd = mkstemp(path); 76 if (fd < 0) 77 err(-1, "makestemp"); 78 (void)unlink(path); 79 80 if (ftruncate(fd, -1) == 0) 81 errx(-1, "ftruncate(fd, -1) succeeded"); --- 14 unchanged lines hidden (view full) --- 96 if (ftruncate(fd, sizes[i]) < 0) 97 err(-1, "ftruncate(%llu) down", sizes[i]); 98 if (fstat(fd, &sb) < 0) 99 err(-1, "stat"); 100 if (sb.st_size != sizes[i]) 101 errx(-1, "fstat(%llu) returned %llu down", sizes[i], 102 sb.st_size); 103 } |
104 close(fd); |
|
99 | 105 |
106 /* 107 * Make sure that ftruncate on sockets doesn't work. 108 */ 109 fd = socket(PF_UNIX, SOCK_STREAM, 0); 110 if (fd < 0) 111 err(-1, "socket(PF_UNIX, SOCK_STREAM, 0)"); 112 if (ftruncate(fd, 0) == 0) 113 errx(-1, "ftruncate(socket) succeeded"); 114 if (errno != EINVAL) 115 err(-1, "ftruncate(socket) returned wrong error"); |
|
100 close(fd); | 116 close(fd); |
117 118 /* 119 * Make sure that ftruncate on pipes doesn't work. 120 */ 121 if (pipe(fds) < 0) 122 err(-1, "pipe"); 123 if (ftruncate(fds[0], 0) == 0) 124 errx(-1, "ftruncate(pipe) succeeded"); 125 if (errno != EINVAL) 126 err(-1, "ftruncate(pipe) returned wrong error"); 127 close(fds[0]); 128 close(fds[1]); 129 130 /* 131 * Make sure that ftruncate on kqueues doesn't work. 132 */ 133 fd = kqueue(); 134 if (fd < 0) 135 err(-1, "kqueue"); 136 if (ftruncate(fds[0], 0) == 0) 137 errx(-1, "ftruncate(kqueue) succeeded"); 138 if (errno != EINVAL) 139 err(-1, "ftruncate(kqueue) returned wrong error"); 140 close(fd); 141 |
|
101 return (0); 102} | 142 return (0); 143} |