4 #include <darwintest.h>
5 #include <darwintest_utils.h>
8 #include <TargetConditionals.h>
10 #if !TARGET_OS_SIMULATOR
15 #include <apfs/apfs_fsctl.h>
18 T_DECL(dirstat
, "Runs dirstat(3)")
20 bool fast_only
= false;
21 bool force_fallback
= false;
22 char *path
= "/System/Library/Frameworks";
24 // <rdar://problem/26400444> Libdarwintest passes argv without progname and flags starting in argv[0]
28 while ((ch
= getopt(argc
, argv
, "fup:")) != -1){
34 force_fallback
= true;
40 T_ASSERT_FAIL("Usage: [-f] [-p <path>]");
44 T_LOG("Path: %s", path
);
48 T_LOG("Using DIRSTAT_FAST_ONLY");
49 flags
|= DIRSTAT_FAST_ONLY
;
52 T_LOG("Using DIRSTAT_FORCE_FALLBACK");
53 flags
|= DIRSTAT_FORCE_FALLBACK
;
56 struct dirstat ds
= {0};
58 T_ASSERT_POSIX_SUCCESS(dirstat_np(path
, flags
, &ds
, sizeof(ds
)), NULL
);
60 T_LOG("Size: %zd bytes", ds
.total_size
);
61 T_LOG("Descendants: %llu objects", ds
.descendants
);
64 T_DECL(dirstat_fallback
, "Tests dirstat(3) fallback")
66 char *path
= "/System/Library/Frameworks";
68 struct dirstat ds
= {0};
70 off_t native_size
= 0;
71 off_t fallback_size
= 0;
73 T_LOG("dirstat_np(\"%s\", 0, ...)", path
);
74 T_EXPECT_POSIX_SUCCESS(dirstat_np(path
, 0, &ds
, sizeof(ds
)), NULL
);
75 T_LOG("Size: %zd bytes", ds
.total_size
);
76 T_LOG("Descendants: %llu objects", ds
.descendants
);
77 native_size
= ds
.total_size
;
79 T_LOG("dirstat_np(\"%s\", DIRSTAT_FORCE_FALLBACK, ...)", path
);
80 T_EXPECT_POSIX_SUCCESS(dirstat_np(path
, DIRSTAT_FORCE_FALLBACK
, &ds
, sizeof(ds
)), NULL
);
81 T_LOG("Size: %zd bytes", ds
.total_size
);
82 T_LOG("Descendants: %llu objects", ds
.descendants
);
83 fallback_size
= ds
.total_size
;
85 T_EXPECT_EQ(native_size
, fallback_size
, "Native and fallback sizes match");
88 T_DECL(dirstat_flags
, "Tests dirstat(3)'s behavior flags")
91 struct dirstat ds
= {0};
93 char *not_fast
= "/System/Library/Frameworks";
95 T_LOG("dirstat_np(\"%s\", DIRSTAT_FAST_ONLY, ...)", not_fast
);
96 T_EXPECT_EQ(dirstat_np(not_fast
, DIRSTAT_FAST_ONLY
, &ds
, sizeof(ds
)), -1, "Fast-only fails on non-fast-enabled directory");
103 T_ASSERT_NE(asprintf(&fast_tmpdir
, "%s/%s", dt_tmpdir(), "fast-XXXXXX"), -1, "Generate fast dir stats directory name");
104 T_ASSERT_EQ((void *)mkdtemp(fast_tmpdir
), (void *)fast_tmpdir
, "Make fast dir stats directory");
105 err
= fsctl(fast_tmpdir
, APFSIOC_MAINTAIN_DIR_STATS
, &flags
, 0);
109 T_SKIP("Couldn't enable fast dir stats for directory (not on APFS?)");
113 T_LOG("dirstat_np(\"%s\", DIRSTAT_FAST_ONLY, ...)", fast_tmpdir
);
115 T_EXPECT_POSIX_SUCCESS(dirstat_np(fast_tmpdir
, DIRSTAT_FAST_ONLY
, &ds
, sizeof(ds
)), "Fast-only works on fast-enabled directory");
117 T_ASSERT_POSIX_SUCCESS(rmdir(fast_tmpdir
), NULL
);