1 .\" Copyright (c) 2013 Apple Computer, Inc. All rights reserved.
3 .\" The contents of this file constitute Original Code as defined in and
4 .\" are subject to the Apple Public Source License Version 1.1 (the
5 .\" "License"). You may not use this file except in compliance with the
6 .\" License. Please obtain a copy of the License at
7 .\" http://www.apple.com/publicsource and read it before using this file.
9 .\" This Original Code and all software distributed under the License are
10 .\" distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
11 .\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
12 .\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
13 .\" FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
14 .\" License for the specific language governing rights and limitations
15 .\" under the License.
17 .\" @(#)getattrlistbulk.2
24 .Nd get file system attributes for multiple directory entries
26 .Fd #include <sys/attr.h>
27 .Fd #include <unistd.h>
30 .Fn getattrlistbulk "int dirfd" "struct attrlist * attrList" "void * attrBuf" "size_t attrBufSize" "uint64_t options"
36 function iterates over the items in a directory and returns information about
37 each directory entry like
41 returns information about a symbolic link, the information returned is about the link itself, not the target of the link.
43 The function reads directory entries from the directory referenced by the file
48 parameter determines what attributes are returned for each entry.
49 Attributes of those directory entries are placed into the buffer specified by
55 parameter allows you to modify the behaviour of the call.
64 parameter must be a file descriptor that references a directory that you have opened for reading.
67 .\" attrList parameter
71 parameter is a pointer to an
74 All fields of this structure must be filled before calling the function.
75 See the discussion of the
77 function for a detailed description of this structure.
78 To get an attribute, the corresponding bit in the appropriate
82 structure must be set.
83 Volume attributes cannot be requested but all other supported getattrlist attributes can be used. For this function,
86 .Dv ATRR_CMN_RETURNED_ATTRS
87 are required and the absence of these attributes in the attrList parameter results in an error. Note that
88 not all attributes supported by
90 may be vended back by this call, which is why the aforementioned flag must be supplied. In particular
92 may not be valid on all directory entries whose information is requested by this call.
95 .\" attrBuf and attrBufSize parameters
101 parameters specify a buffer into which the function places attribute values.
102 The attributes for any given directory entry are grouped together and
103 packed in exactly the same way as they are returned from
105 and are subject to exactly the same alignment specifications
106 and restrictions. These groups are then placed into the buffer, one after another.
107 .Xr getattrlist 2 should be consulted on details of the attributes that can be
108 requested for and returned. The name of the entry itself is provided by the
110 attribute. Each group starts with a leading
112 , which will always be 8-byte aligned that contains the overall length of the group.
113 You can step from one group to the next by simply adding this length to your pointer.
114 The sample code (below) shows how to do this.
115 The initial contents of this buffer are ignored.
118 .\" options parameter
122 parameter is a bit set that controls the behaviour of
123 .Fn getattrlistbulk .
124 The following option bits are defined.
126 .Bl -tag -width FSOPT_PACK_INVAL_ATTRS
128 .It FSOPT_PACK_INVAL_ATTRS
129 If this is bit is set, then all requested attributes,
130 even ones that are not supported by the object or file
131 file system, will be returned the attrBuf. The attributes
132 actually returned can be determined by looking at the
133 attribute_set_t structure returned for the
134 .Dv ATTR_CMN_RETURNED_ATTRS
135 attribute. Default values will be returned for invalid
136 attributes and should be ignored.
138 Please see the discussion of this flag in
145 has been requested and an error specific to a directory entry occurs,
146 an error will be reported. The
148 attribute is a uint32_t which, if non-zero, specifies the error code
149 that was encountered during the processing of that directory entry. The
151 attribute will be after
152 .Dv ATTR_CMN_RETURNED_ATTRS
153 attribute in the returned buffer.
155 It is typical to ask for a combination of common, file, and directory
156 attributes and then use the value of the
158 attribute to parse the resulting attribute buffer.
160 A directory which is a mount point for a file system, will have a value of "DIR_MNTSTATUS_MNTPOINT" set for it's the
161 ATTR_DIR_MOUNTSTATUS attribute entry. However the attributes for the mount point will be those from the (underlying) file system. The only way to get the attributes of mounted root directory is to call getattrlist(2) on the mount point.
164 Upon successful completion the numbers of entries successfully read
165 is returned. A value of 0 indicates there are no more entries. Once 0 is returned,
166 no further entries are returned even if new entries are added to the directory.
167 Directory iteration should be restarted either by repostioning the offset to 0 by
169 or by closing the file descriptor and opening the directory again. On error,
170 a value of -1 is returned and
172 is set to indicate the error.
174 When iterating all entries in a directory,
176 is called repeatedly until a 0 is returned. In such a case if
180 calls on the same fd are mixed, the behavior is undefined.
190 is not a valid file descriptor for a directory open for reading.
198 Search permission is denied on the directory whose descriptor is given
205 points to an invalid address.
208 The buffer was too small.
216 .Dv ATTR_BIT_MAP_COUNT .
219 An invalid attribute was requested.
222 Volume attributes were requested.
227 .Dv ATTR_CMN_RETURNED_ATTRS
228 was not requested in the attrList parameter.
231 An I/O error occurred while reading from or writing to the file system.
237 The following code lists the contents of a directory using
238 .Fn getattrlistbulk .
239 The listing includes the file type.
242 #include <sys/syscall.h>
243 #include <sys/attr.h>
244 #include <sys/errno.h>
245 #include <sys/vnode.h>
254 typedef struct val_attrs {
256 attribute_set_t returned;
258 attrreference_t name_info;
260 fsobj_type_t obj_type;
264 void demo(const char *dirpath)
268 struct attrlist attrList;
272 memset(&attrList, 0, sizeof(attrList));
273 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
274 attrList.commonattr = ATTR_CMN_RETURNED_ATTRS |
280 dirfd = open(dirpath, O_RDONLY, 0);
283 printf("Could not open directory %s", dirpath);
284 perror("Error was ");
289 retcount = getattrlistbulk(dirfd, &attrList, &attrBuf[0],
291 printf("\engetattrlistbulk returned %d", retcount);
292 if (retcount == -1) {
294 perror("Error returned : ");
297 } else if (retcount == 0) {
298 /* No more entries in directory */
303 uint32_t total_length;
306 entry_start = &attrBuf[0];
308 printf(" -> entries returned");
309 for (index = 0; index < retcount; index++) {
310 val_attrs_t attrs = {0};
312 printf("\en Entry %d", index);
315 attrs.length = *(uint32_t *)field;
316 printf(" Length %d ", attrs.length);
317 total_length += attrs.length;
318 printf(" Total Length %d ", total_length);
319 field += sizeof(uint32_t);
322 /* set starting point for next entry */
323 entry_start += attrs.length;
325 attrs.returned = *(attribute_set_t *)field;
326 field += sizeof(attribute_set_t);
328 if (attrs.returned.commonattr & ATTR_CMN_ERROR) {
329 attrs.error = *(uint32_t *)field;
330 field += sizeof(uint32_t);
333 if (attrs.returned.commonattr & ATTR_CMN_NAME) {
335 attrs.name_info = *(attrreference_t *)field;
336 field += sizeof(attrreference_t);
337 printf(" %s ", (attrs.name +
338 attrs.name_info.attr_dataoffset));
341 /* Check for error for this entry */
344 * Print error and move on to next
347 printf("Error in reading attributes for directory \
348 entry %d", attrs.error);
353 if (attrs.returned.commonattr & ATTR_CMN_OBJTYPE) {
354 attrs.obj_type = *(fsobj_type_t *)field;
355 field += sizeof(fsobj_type_t);
357 switch (attrs.obj_type) {
362 printf("directory ");
365 printf("obj_type = %-2d ", attrs.obj_type);
387 function call appeared in OS X version 10.10