]>
git.saurik.com Git - apple/hfs.git/blob - tests/cases/test-cas-bsdflags.c
9 #include <sys/sysctl.h>
11 #include <sys/xattr.h>
12 #include <sys/mount.h>
13 #include <sys/param.h>
15 #include <System/sys/fsctl.h>
17 #include "hfs-tests.h"
18 #include "test-utils.h"
19 #include "disk-image.h"
22 #define AFSCUTIL "/usr/local/bin/afscutil"
27 cas_bsd_flags(int fd
, uint32_t expected_flags
, uint32_t new_flags
, int expected_error
)
29 struct fsioc_cas_bsdflags cas
;
31 cas
.expected_flags
= expected_flags
;
32 cas
.new_flags
= new_flags
;
33 cas
.actual_flags
= ~0; /* poison */
35 if (expected_error
!= 0) {
36 // no assert_call_fail() in test_hfs
37 assert(ffsctl(fd
, FSIOC_CAS_BSDFLAGS
, &cas
, 0) == -1);
38 assert(errno
== EPERM
);
39 return true; // as expected - flags were not changed
41 assert_no_err(ffsctl(fd
, FSIOC_CAS_BSDFLAGS
, &cas
, 0));
44 return (cas
.expected_flags
== cas
.actual_flags
);
48 write_compressible_data(int fd
)
50 // adapted from test_clonefile in apfs
53 // write some easily compressable data
54 memset(dbuf
+ 0*(sizeof(dbuf
)/4), 'A', sizeof(dbuf
)/4);
55 memset(dbuf
+ 1*(sizeof(dbuf
)/4), 'B', sizeof(dbuf
)/4);
56 memset(dbuf
+ 2*(sizeof(dbuf
)/4), 'C', sizeof(dbuf
)/4);
57 memset(dbuf
+ 3*(sizeof(dbuf
)/4), 'D', sizeof(dbuf
)/4);
58 for (int idx
= 0; idx
< 32; idx
++) {
59 check_io(write(fd
, dbuf
, sizeof(dbuf
)), sizeof(dbuf
));
63 int run_cas_bsdflags(__unused test_ctx_t
*ctx
)
65 disk_image_t
*di
= disk_image_get();
70 asprintf(&file
, "%s/cas_bsdflags.data", di
->mount_point
);
72 assert_with_errno((fd
= open(file
,
73 O_CREAT
| O_RDWR
| O_TRUNC
, 0666)) >= 0);
75 assert_no_err(fchflags(fd
, UF_HIDDEN
));
76 assert_no_err(fstat(fd
, &sb
));
77 assert_equal_int(sb
.st_flags
, UF_HIDDEN
);
79 assert(cas_bsd_flags(fd
, 0, UF_NODUMP
, 0) == false);
80 assert_no_err(fstat(fd
, &sb
));
81 assert_equal_int(sb
.st_flags
, UF_HIDDEN
);
83 assert(cas_bsd_flags(fd
, UF_HIDDEN
, UF_NODUMP
, 0) == true);
84 assert_no_err(fstat(fd
, &sb
));
85 assert_equal_int(sb
.st_flags
, UF_NODUMP
);
87 assert(cas_bsd_flags(fd
, UF_NODUMP
, 0, 0) == true);
88 assert_no_err(fstat(fd
, &sb
));
89 assert_equal_int(sb
.st_flags
, 0);
91 // Add some data to our (non-compressed) file,
92 // mark it with UF_COMPRESSED,
93 // and check that UF_COMPRESSED is *not* set -
94 // as there is no decmpfs xattr present.
95 check_io(write(fd
, "J", 1), 1);
96 assert_no_err(fstat(fd
, &sb
));
97 assert(sb
.st_size
> 0);
99 assert(cas_bsd_flags(fd
, 0, UF_COMPRESSED
, EPERM
) == true);
100 assert_no_err(fstat(fd
, &sb
));
101 assert_equal_int(sb
.st_flags
, 0);
103 // Now, add some compressible data to the file and compress it using afscutil.
104 write_compressible_data(fd
);
105 assert(!systemx(AFSCUTIL
, "-c", file
, NULL
));
106 assert_no_err(fstat(fd
, &sb
));
107 assert_equal_int(sb
.st_flags
, UF_COMPRESSED
);
109 // Now, remove UF_COMPRESSED from our file and
110 // check that the file is 0-length.
111 assert(cas_bsd_flags(fd
, UF_COMPRESSED
, 0, 0) == true);
112 assert_no_err(fstat(fd
, &sb
));
113 assert_equal_ll(sb
.st_size
, 0);
116 assert_no_err(unlink(file
));