]>
Commit | Line | Data |
---|---|---|
558d2836 A |
1 | #include <unistd.h> |
2 | #include <sys/stat.h> | |
3 | #include <stdio.h> | |
4 | #include <err.h> | |
5 | #include <time.h> | |
6 | #include <sys/time.h> | |
7 | #include <stdlib.h> | |
8 | #include <stdbool.h> | |
9 | #include <sys/param.h> | |
10 | #include <sys/mman.h> | |
11 | #include <sys/types.h> | |
12 | #include <sys/fcntl.h> | |
13 | #include <sys/resource.h> | |
14 | #include <TargetConditionals.h> | |
15 | ||
16 | #include "hfs-tests.h" | |
17 | #include "test-utils.h" | |
18 | #include "disk-image.h" | |
19 | ||
20 | TEST(mod_time) | |
21 | ||
22 | static disk_image_t *di; | |
23 | ||
24 | static void run_test(void) | |
25 | { | |
26 | char *file; | |
27 | int fd; | |
28 | ||
29 | asprintf(&file, "%s/tstfile", di->mount_point); | |
30 | assert((fd = open(file, O_CREAT | O_RDWR | O_TRUNC, 0666)) >= 0); | |
31 | ||
32 | char data[] = "The quick brown fox jumps over the lazy dog\n"; | |
33 | ||
34 | check_io(write(fd, data, sizeof (data) - 1), sizeof(data) - 1); | |
35 | ||
36 | struct timeval times[] = { | |
37 | { time(NULL), 0 }, | |
38 | { 1234567890, 0 } | |
39 | }; | |
40 | ||
41 | assert_no_err(futimes(fd, times)); | |
42 | ||
43 | /* Have to open file again because whether it's writable comes from | |
44 | the file descriptor, *not* how we map. */ | |
45 | int fd2; | |
46 | ||
47 | assert_with_errno((fd2 = open(file, O_RDONLY)) >= 0); | |
48 | void *p; | |
49 | ||
50 | assert((p = mmap(NULL, NBPG, PROT_READ, MAP_SHARED, fd2, 0)) != MAP_FAILED); | |
51 | ||
52 | assert_no_err(msync(p, NBPG, MS_INVALIDATE)); | |
53 | ||
54 | struct stat sb; | |
55 | assert_no_err(fstat(fd, &sb)); | |
56 | ||
57 | assert (sb.st_mtimespec.tv_sec == times[1].tv_sec); | |
58 | ||
59 | assert_no_err(close(fd)); | |
60 | assert_no_err(close(fd2)); | |
ec99dd30 | 61 | |
558d2836 | 62 | assert_no_err(unlink(file)); |
ec99dd30 | 63 | assert_no_err(munmap(p, NBPG)); |
558d2836 A |
64 | } |
65 | ||
66 | int run_mod_time(__unused test_ctx_t *ctx) | |
67 | { | |
68 | di = disk_image_get(); | |
69 | ||
70 | // We need to run the test twice because the sync runs every 30 secs or so | |
71 | run_test(); | |
72 | run_test(); | |
73 | ||
74 | return 0; | |
75 | } |