The OpenD Programming Language

1 /**
2  * D header file for Darwin
3  *
4  * $(LINK2 https://opensource.apple.com/source/xnu/xnu-2422.115.4/bsd/sys/attr.h.auto.html, Apple sys/attr.h)
5  */
6 module core.sys.darwin.sys.attr;
7 
8 version (OSX)
9     version = Darwin;
10 else version (iOS)
11     version = Darwin;
12 else version (TVOS)
13     version = Darwin;
14 else version (WatchOS)
15     version = Darwin;
16 
17 version (Darwin):
18 extern (C):
19 nothrow:
20 @nogc:
21 
22 import core.internal.attributes : betterC;
23 import core.sys.darwin.sys.cdefs : c_ulong;
24 import core.sys.posix.sys.time : timeval;
25 
26 // These functions aren't actually declared in attr.h but in unistd.h.
27 @system
28 {
29     void getattrlist(scope const char* path, scope attrlist* attrList, scope void* attrBuf,
30         size_t attrBufSize, c_ulong options);
31 
32     void setattrlist(scope const char* path, scope attrlist* attrList, scope void* attrBuf,
33         size_t attrBufSize, c_ulong options);
34 
35     version (TVOS) {}
36     else version (WatchOS) {}
37     else
38         int searchfs(scope const char* path, scope fssearchblock* searchBlock,
39             scope c_ulong* numMatches, uint scriptCode, uint options, scope searchstate* state);
40 }
41 
42 enum
43 {
44     FSOPT_NOFOLLOW                     = 0x00000001,
45     FSOPT_NOINMEMUPDATE                = 0x00000002,
46     FSOPT_REPORT_FULLSIZE              = 0x00000004,
47     FSOPT_PACK_INVAL_ATTRS             = 0x00000008,
48     FSOPT_ATTR_CMN_EXTENDED            = 0x00000020, // macOS 10.10
49 }
50 
51 enum SEARCHFS_MAX_SEARCHPARMS = 4096;
52 
53 alias uint text_encoding_t, fsobj_type_t, fsobj_tag_t, fsfile_type_t, fsvolid_t, attrgroup_t;
54 
55 struct attrlist
56 {
57     ushort bitmapcount, reserved;
58     attrgroup_t commonattr, volattr, dirattr, fileattr, forkattr;
59 }
60 enum ATTR_BIT_MAP_COUNT = 5;
61 
62 struct attribute_set_t
63 {
64     attrgroup_t commonattr, volattr, dirattr, fileattr, forkattr;
65 }
66 
67 struct attrreference_t
68 {
69     int     attr_dataoffset;
70     uint    attr_length;
71 }
72 
73 struct diskextent
74 {
75     uint startblock, blockcount;
76 }
77 
78 alias extentrecord = diskextent[8];
79 
80 alias vol_capabilities_set_t = uint[4];
81 
82 enum
83 {
84     VOL_CAPABILITIES_FORMAT            = 0,
85     VOL_CAPABILITIES_INTERFACES        = 1,
86     VOL_CAPABILITIES_RESERVED1         = 2,
87     VOL_CAPABILITIES_RESERVED2         = 3,
88 }
89 
90 struct vol_capabilities_attr_t
91 {
92     vol_capabilities_set_t capabilities, valid;
93 }
94 
95 enum ATTR_MAX_BUFFER = 8192;
96 
97 enum
98 {
99     VOL_CAP_FMT_PERSISTENTOBJECTIDS    = 0x00000001,
100     VOL_CAP_FMT_SYMBOLICLINKS          = 0x00000002,
101     VOL_CAP_FMT_HARDLINKS              = 0x00000004,
102     VOL_CAP_FMT_JOURNAL                = 0x00000008,
103     VOL_CAP_FMT_JOURNAL_ACTIVE         = 0x00000010,
104     VOL_CAP_FMT_NO_ROOT_TIMES          = 0x00000020,
105     VOL_CAP_FMT_SPARSE_FILES           = 0x00000040,
106     VOL_CAP_FMT_ZERO_RUNS              = 0x00000080,
107     VOL_CAP_FMT_CASE_SENSITIVE         = 0x00000100,
108     VOL_CAP_FMT_CASE_PRESERVING        = 0x00000200,
109     VOL_CAP_FMT_FAST_STATFS            = 0x00000400,
110     VOL_CAP_FMT_2TB_FILESIZE           = 0x00000800,
111     VOL_CAP_FMT_OPENDENYMODES          = 0x00001000,
112     VOL_CAP_FMT_HIDDEN_FILES           = 0x00002000,
113     VOL_CAP_FMT_PATH_FROM_ID           = 0x00004000,
114     VOL_CAP_FMT_NO_VOLUME_SIZES        = 0x00008000,
115     VOL_CAP_FMT_DECMPFS_COMPRESSION    = 0x00010000,
116     VOL_CAP_FMT_64BIT_OBJECT_IDS       = 0x00020000,
117     VOL_CAP_FMT_DIR_HARDLINKS          = 0x00040000, // macOS 10.12
118     VOL_CAP_FMT_DOCUMENT_ID            = 0x00080000, // macOS 10.12
119     VOL_CAP_FMT_WRITE_GENERATION_COUNT = 0x00100000, // macOS 10.12
120     VOL_CAP_FMT_NO_IMMUTABLE_FILES     = 0x00200000, // macOS 10.12.4
121     VOL_CAP_FMT_NO_PERMISSIONS         = 0x00400000, // macOS 10.12.4
122     VOL_CAP_FMT_SHARED_SPACE           = 0x00800000, // macOS 10.15
123     VOL_CAP_FMT_VOL_GROUPS             = 0x01000000, // macOS 10.15
124 }
125 
126 enum
127 {
128     VOL_CAP_INT_SEARCHFS               = 0x00000001,
129     VOL_CAP_INT_ATTRLIST               = 0x00000002,
130     VOL_CAP_INT_NFSEXPORT              = 0x00000004,
131     VOL_CAP_INT_READDIRATTR            = 0x00000008,
132     VOL_CAP_INT_EXCHANGEDATA           = 0x00000010,
133     VOL_CAP_INT_COPYFILE               = 0x00000020,
134     VOL_CAP_INT_ALLOCATE               = 0x00000040,
135     VOL_CAP_INT_VOL_RENAME             = 0x00000080,
136     VOL_CAP_INT_ADVLOCK                = 0x00000100,
137     VOL_CAP_INT_FLOCK                  = 0x00000200,
138     VOL_CAP_INT_EXTENDED_SECURITY      = 0x00000400,
139     VOL_CAP_INT_USERACCESS             = 0x00000800,
140     VOL_CAP_INT_MANLOCK                = 0x00001000,
141     VOL_CAP_INT_NAMEDSTREAMS           = 0x00002000,
142     VOL_CAP_INT_EXTENDED_ATTR          = 0x00004000,
143     VOL_CAP_INT_CLONE                  = 0x00010000, // macOS 10.12
144     VOL_CAP_INT_SNAPSHOT               = 0x00020000, // macOS 10.12
145     VOL_CAP_INT_RENAME_SWAP            = 0x00040000, // macOS 10.12
146     VOL_CAP_INT_RENAME_EXCL            = 0x00080000, // macOS 10.12
147     VOL_CAP_INT_RENAME_OPENFAIL        = 0x00100000, // macOS 10.15
148 }
149 
150 struct vol_attributes_attr_t
151 {
152     attribute_set_t validattr, nativeattr;
153 }
154 
155 enum
156 {
157     ATTR_CMN_NAME                      = 0x00000001,
158     ATTR_CMN_DEVID                     = 0x00000002,
159     ATTR_CMN_FSID                      = 0x00000004,
160     ATTR_CMN_OBJTYPE                   = 0x00000008,
161     ATTR_CMN_OBJTAG                    = 0x00000010,
162     ATTR_CMN_OBJID                     = 0x00000020,
163     ATTR_CMN_OBJPERMANENTID            = 0x00000040,
164     ATTR_CMN_PAROBJID                  = 0x00000080,
165     ATTR_CMN_SCRIPT                    = 0x00000100,
166     ATTR_CMN_CRTIME                    = 0x00000200,
167     ATTR_CMN_MODTIME                   = 0x00000400,
168     ATTR_CMN_CHGTIME                   = 0x00000800,
169     ATTR_CMN_ACCTIME                   = 0x00001000,
170     ATTR_CMN_BKUPTIME                  = 0x00002000,
171     ATTR_CMN_FNDRINFO                  = 0x00004000,
172     ATTR_CMN_OWNERID                   = 0x00008000,
173     ATTR_CMN_GRPID                     = 0x00010000,
174     ATTR_CMN_ACCESSMASK                = 0x00020000,
175     ATTR_CMN_FLAGS                     = 0x00040000,
176 
177     ATTR_CMN_GEN_COUNT                 = 0x00080000,
178     ATTR_CMN_DOCUMENT_ID               = 0x00100000,
179 
180     ATTR_CMN_USERACCESS                = 0x00200000,
181     ATTR_CMN_EXTENDED_SECURITY         = 0x00400000,
182     ATTR_CMN_UUID                      = 0x00800000,
183     ATTR_CMN_GRPUUID                   = 0x01000000,
184     ATTR_CMN_FILEID                    = 0x02000000,
185     ATTR_CMN_PARENTID                  = 0x04000000,
186     ATTR_CMN_FULLPATH                  = 0x08000000,
187     ATTR_CMN_ADDEDTIME                 = 0x10000000,
188     ATTR_CMN_ERROR                     = 0x20000000, // macOS 10.10
189     ATTR_CMN_DATA_PROTECT_FLAGS        = 0x40000000, // macOS 10.10
190 }
191 
192 enum ATTR_CMN_RETURNED_ATTRS           = 0x80000000;
193 enum ATTR_CMN_VALIDMASK                = 0xFFFFFFFF;
194 enum ATTR_CMN_SETMASK                  = 0x51C7FF00;
195 enum ATTR_CMN_VOLSETMASK               = 0x00006700;
196 
197 enum
198 {
199     ATTR_VOL_FSTYPE                    = 0x00000001,
200     ATTR_VOL_SIGNATURE                 = 0x00000002,
201     ATTR_VOL_SIZE                      = 0x00000004,
202     ATTR_VOL_SPACEFREE                 = 0x00000008,
203     ATTR_VOL_SPACEAVAIL                = 0x00000010,
204     ATTR_VOL_MINALLOCATION             = 0x00000020,
205     ATTR_VOL_ALLOCATIONCLUMP           = 0x00000040,
206     ATTR_VOL_IOBLOCKSIZE               = 0x00000080,
207     ATTR_VOL_OBJCOUNT                  = 0x00000100,
208     ATTR_VOL_FILECOUNT                 = 0x00000200,
209     ATTR_VOL_DIRCOUNT                  = 0x00000400,
210     ATTR_VOL_MAXOBJCOUNT               = 0x00000800,
211     ATTR_VOL_MOUNTPOINT                = 0x00001000,
212     ATTR_VOL_NAME                      = 0x00002000,
213     ATTR_VOL_MOUNTFLAGS                = 0x00004000,
214     ATTR_VOL_MOUNTEDDEVICE             = 0x00008000,
215     ATTR_VOL_ENCODINGSUSED             = 0x00010000,
216     ATTR_VOL_CAPABILITIES              = 0x00020000,
217     ATTR_VOL_UUID                      = 0x00040000,
218     ATTR_VOL_QUOTA_SIZE                = 0x10000000, // macOS 10.12.4
219     ATTR_VOL_RESERVED_SIZE             = 0x20000000, // macOS 10.12.4
220     ATTR_VOL_ATTRIBUTES                = 0x40000000,
221     ATTR_VOL_INFO                      = 0x80000000,
222 }
223 
224 enum ATTR_VOL_VALIDMASK                = 0xF007FFFF;
225 enum ATTR_VOL_SETMASK                  = 0x80002000;
226 
227 enum
228 {
229     ATTR_DIR_LINKCOUNT                 = 0x00000001,
230     ATTR_DIR_ENTRYCOUNT                = 0x00000002,
231     ATTR_DIR_MOUNTSTATUS               = 0x00000004,
232     ATTR_DIR_ALLOCSIZE                 = 0x00000008, // macOS 10.12.4
233     ATTR_DIR_IOBLOCKSIZE               = 0x00000010, // macOS 10.12.4
234     ATTR_DIR_DATALENGTH                = 0x00000020, // macOS 10.12.4
235 }
236 
237 enum
238 {
239     DIR_MNTSTATUS_MNTPOINT             = 0x00000001,
240     DIR_MNTSTATUS_TRIGGER              = 0x00000002,
241 }
242 
243 enum ATTR_DIR_VALIDMASK                = 0x0000003f;
244 enum ATTR_DIR_SETMASK                  = 0x00000000;
245 
246 enum
247 {
248     ATTR_FILE_LINKCOUNT                = 0x00000001,
249     ATTR_FILE_TOTALSIZE                = 0x00000002,
250     ATTR_FILE_ALLOCSIZE                = 0x00000004,
251     ATTR_FILE_IOBLOCKSIZE              = 0x00000008,
252     ATTR_FILE_DEVTYPE                  = 0x00000020,
253     ATTR_FILE_FORKCOUNT                = 0x00000080,
254     ATTR_FILE_FORKLIST                 = 0x00000100,
255     ATTR_FILE_DATALENGTH               = 0x00000200,
256     ATTR_FILE_DATAALLOCSIZE            = 0x00000400,
257     ATTR_FILE_RSRCLENGTH               = 0x00001000,
258     ATTR_FILE_RSRCALLOCSIZE            = 0x00002000,
259 }
260 
261 enum ATTR_FILE_VALIDMASK               = 0x000037FF;
262 enum ATTR_FILE_SETMASK                 = 0x00000020;
263 
264 enum
265 {
266     ATTR_CMNEXT_RELPATH                = 0x00000004, // macOS 10.12.4
267     ATTR_CMNEXT_PRIVATESIZE            = 0x00000008, // macOS 10.12.4
268     ATTR_CMNEXT_NOFIRMLINKPATH         = 0x00000020, // macOS 10.15
269     ATTR_CMNEXT_REALDEVID              = 0x00000040, // macOS 10.15
270     ATTR_CMNEXT_REALFSID               = 0x00000080, // macOS 10.15
271 }
272 
273 enum ATTR_CMNEXT_VALIDMASK             = 0x000000fc;
274 enum ATTR_CMNEXT_SETMASK               = 0x00000000;
275 
276 enum ATTR_BULK_REQUIRED = ATTR_CMN_NAME | ATTR_CMN_RETURNED_ATTRS;
277 
278 enum
279 {
280     SRCHFS_START                       = 0x00000001,
281     SRCHFS_MATCHPARTIALNAMES           = 0x00000002,
282     SRCHFS_MATCHDIRS                   = 0x00000004,
283     SRCHFS_MATCHFILES                  = 0x00000008,
284     SRCHFS_SKIPLINKS                   = 0x00000010,
285     SRCHFS_SKIPINVISIBLE               = 0x00000020,
286     SRCHFS_SKIPPACKAGES                = 0x00000040,
287     SRCHFS_SKIPINAPPROPRIATE           = 0x00000080,
288 
289     SRCHFS_NEGATEPARAMS                = 0x80000000,
290     SRCHFS_VALIDOPTIONSMASK            = 0x800000FF,
291 }
292 
293 struct fssearchblock
294 {
295     attrlist*        returnattrs;
296     void*            returnbuffer;
297     size_t           returnbuffersize;
298     c_ulong          maxmatches;
299     timeval          timelimit;
300     void*            searchparams1;
301     size_t           sizeofsearchparams1;
302     void*            searchparams2;
303     size_t           sizeofsearchparams2;
304     attrlist         searchattrs;
305 }
306 
307 struct searchstate
308 {
309     uint                        ss_union_flags;
310     uint                        ss_union_layer;
311     ubyte[548]                  ss_fsstate;
312 }
313 static assert(searchstate.sizeof == uint.sizeof * 2 + searchstate.ss_fsstate.sizeof,
314     "searchstate struct must be packed");
315 
316 enum FST_EOF = -1;
317 
318 @betterC @nogc nothrow pure @safe unittest
319 {
320     // Use an enum instead of `version (Darwin)` so it works with the betterc test extractor.
321     version (OSX) enum isDarwin = true;
322     else version (iOS) enum isDarwin = true;
323     else version (TVOS) enum isDarwin = true;
324     else version (WatchOS) enum isDarwin = true;
325     else enum isDarwin = false;
326     static if (isDarwin)
327     {
328         // Verify that these types don't need __initZ and so can be used in betterC.
329         attrlist al;
330         attribute_set_t as;
331         attrreference_t ar;
332         diskextent de;
333         vol_capabilities_attr_t vca;
334         vol_attributes_attr_t vaa;
335         fssearchblock fsb;
336         searchstate ss;
337     }
338 }