]>
Commit | Line | Data |
---|---|---|
558d2836 A |
1 | #include <TargetConditionals.h> |
2 | ||
de8ee011 | 3 | #if !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) |
558d2836 A |
4 | |
5 | #include <sys/time.h> | |
6 | #include <fcntl.h> | |
7 | #include <unistd.h> | |
8 | #include <spawn.h> | |
9 | ||
10 | #import <Foundation/Foundation.h> | |
11 | ||
12 | #include "hfs-tests.h" | |
13 | #include "test-utils.h" | |
14 | #include "disk-image.h" | |
15 | ||
16 | TEST(file_too_big) | |
17 | ||
18 | #define DISK_IMAGE "/tmp/file_too_big.sparseimage" | |
19 | ||
20 | int run_file_too_big(__unused test_ctx_t *ctx) | |
21 | { | |
22 | int fd; | |
23 | ||
24 | disk_image_t *di = disk_image_create(DISK_IMAGE, &(disk_image_opts_t){ | |
25 | .size = 16 TB | |
26 | }); | |
27 | ||
28 | struct timeval start, end, elapsed; | |
29 | ||
30 | char *file; | |
31 | asprintf(&file, "%s/file-too-big", di->mount_point); | |
32 | assert_with_errno((fd = open(file, O_RDWR | O_CREAT, 0666)) >= 0); | |
33 | ||
34 | assert_no_err(gettimeofday(&start, NULL)); | |
35 | ||
36 | assert(pwrite(fd, &fd, 4, 128 * 1024ull * 1024 * 1024 * 1024) == -1 | |
37 | && errno == ENOSPC); | |
38 | ||
39 | assert_no_err(close(fd)); | |
40 | ||
41 | assert_no_err(gettimeofday(&end, NULL)); | |
42 | ||
43 | timersub(&end, &start, &elapsed); | |
44 | assert(elapsed.tv_sec < 1); | |
45 | ||
46 | // Check truncate | |
47 | ||
48 | assert_no_err(gettimeofday(&start, NULL)); | |
49 | ||
50 | assert(truncate(file, 128 * 1024ull * 1024 * 1024 * 1024) == -1); | |
51 | assert_with_errno(errno == ENOSPC); | |
52 | ||
53 | assert_no_err(gettimeofday(&end, NULL)); | |
54 | ||
55 | timersub(&end, &start, &elapsed); | |
56 | assert(elapsed.tv_sec < 1); | |
57 | ||
58 | // Check preallocate | |
59 | ||
60 | assert_no_err(gettimeofday(&start, NULL)); | |
61 | ||
62 | assert((fd = open(file, O_RDWR)) >= 0); | |
63 | ||
64 | fstore_t fst = { | |
65 | .fst_flags = F_ALLOCATEALL, | |
66 | .fst_posmode = F_PEOFPOSMODE, | |
67 | .fst_length = 128 * 1024ull * 1024 * 1024 * 1024, | |
68 | }; | |
69 | ||
70 | assert(fcntl(fd, F_PREALLOCATE, &fst) == -1 && errno == ENOSPC); | |
71 | assert(fst.fst_bytesalloc == 0); | |
72 | ||
73 | assert_no_err(close(fd)); | |
74 | ||
75 | assert_no_err(gettimeofday(&end, NULL)); | |
76 | ||
77 | timersub(&end, &start, &elapsed); | |
78 | assert(elapsed.tv_sec < 1); | |
79 | ||
80 | // And check preallocate works without the F_ALLOCATEALL flag | |
81 | ||
82 | assert((fd = open(file, O_RDWR)) >= 0); | |
83 | ||
84 | fst.fst_flags = 0; | |
85 | ||
86 | assert(fcntl(fd, F_PREALLOCATE, &fst) == -1 && errno == ENOSPC); | |
87 | ||
88 | // It should have allocated at least 32 MB | |
89 | assert(fst.fst_bytesalloc > 32 * 1024 * 1024); | |
90 | ||
91 | assert_no_err(close(fd)); | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
de8ee011 | 96 | #endif // !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) |