]>
Commit | Line | Data |
---|---|---|
fe8ab488 A |
1 | .\" Copyright (c) 2013 Apple Computer, Inc. All rights reserved. |
2 | .\" | |
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. | |
8 | .\" | |
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. | |
16 | .\" | |
17 | .\" @(#)getattrlistbulk.2 | |
18 | . | |
19 | .Dd November 15, 2013 | |
20 | .Dt GETATTRLISTBULK 2 | |
21 | .Os Darwin | |
22 | .Sh NAME | |
23 | .Nm getattrlistbulk | |
24 | .Nd get file system attributes for multiple directory entries | |
25 | .Sh SYNOPSIS | |
26 | .Fd #include <sys/attr.h> | |
27 | .Fd #include <unistd.h> | |
28 | .Pp | |
29 | .Ft int | |
30 | .Fn getattrlistbulk "int dirfd" "struct attrlist * attrList" "void * attrBuf" "size_t attrBufSize" "uint64_t options" | |
31 | . | |
32 | . | |
33 | .Sh DESCRIPTION | |
34 | The | |
35 | .Fn getattrlistbulk | |
36 | function iterates over the items in a directory and returns information about | |
37 | each directory entry like | |
38 | .Xr getattrlist 2 . | |
39 | Note: when | |
40 | .Fn getattrlistbulk | |
41 | returns information about a symbolic link, the information returned is about the link itself, not the target of the link. | |
42 | .Pp | |
43 | The function reads directory entries from the directory referenced by the file | |
44 | descriptor | |
45 | .Fa dirfd . | |
46 | The | |
47 | .Fa attrList | |
48 | parameter determines what attributes are returned for each entry. | |
49 | Attributes of those directory entries are placed into the buffer specified by | |
50 | .Fa attrBuf | |
51 | and | |
52 | .Fa attrBufSize . | |
53 | The | |
54 | .Fa options | |
55 | parameter allows you to modify the behaviour of the call. | |
56 | .Pp | |
57 | . | |
58 | .Pp | |
59 | . | |
60 | .\" dirfd parameter | |
61 | . | |
62 | The | |
63 | .Fa dirfd | |
64 | parameter must be a file descriptor that references a directory that you have opened for reading. | |
65 | .Pp | |
66 | . | |
67 | .\" attrList parameter | |
68 | . | |
69 | The | |
70 | .Fa attrList | |
71 | parameter is a pointer to an | |
72 | .Vt attrlist | |
73 | structure. | |
74 | All fields of this structure must be filled before calling the function. | |
75 | See the discussion of the | |
76 | .Xr getattrlist 2 | |
77 | function for a detailed description of this structure. | |
78 | To get an attribute, the corresponding bit in the appropriate | |
79 | .Vt attrgroup_t | |
80 | field of the | |
81 | .Vt attrlist | |
82 | structure must be set. | |
83 | Volume attributes cannot be requested but all other supported getattrlist attributes can be used. For this function, | |
84 | .Dv ATTR_CMN_NAME | |
85 | and | |
86 | .Dv ATRR_CMN_RETURNED_ATTRS | |
87 | are required and the absence of these attributes in the attrList parameter results in an error. | |
88 | .Pp | |
89 | . | |
90 | .\" attrBuf and attrBufSize parameters | |
91 | . | |
92 | The | |
93 | .Fa attrBuf | |
94 | and | |
95 | .Fa attrBufSize | |
96 | parameters specify a buffer into which the function places attribute values. | |
97 | The attributes for any given directory entry are grouped together and | |
98 | packed in exactly the same way as they are returned from | |
99 | .Xr getattrlist 2 | |
100 | and are subject to exactly the same alignment specifications | |
101 | and restrictions. These groups are then placed into the buffer, one after another. | |
102 | .Xr getattrlist 2 should be consulted on details of the attributes that can be | |
103 | requested for and returned. The name of the entry itself is provided by the | |
104 | .Dv ATTR_CMN_NAME | |
105 | attribute. Each group starts with a leading | |
106 | .Vt uint32_t | |
107 | , which will always be 8-byte aligned that contains the overall length of the group. | |
108 | You can step from one group to the next by simply adding this length to your pointer. | |
109 | The sample code (below) shows how to do this. | |
110 | The initial contents of this buffer are ignored. | |
111 | .Pp | |
112 | . | |
113 | .\" options parameter | |
114 | . | |
115 | The | |
116 | .Fa options | |
117 | parameter is a bit set that controls the behaviour of | |
118 | .Fn getattrlistbulk . | |
119 | The following option bits are defined. | |
120 | . | |
121 | .Bl -tag -width FSOPT_PACK_INVAL_ATTRS | |
122 | . | |
123 | .It FSOPT_PACK_INVAL_ATTRS | |
124 | If this is bit is set, then all requested attributes, | |
125 | even ones that are not supported by the object or file | |
126 | file system, will be returned the attrBuf. The attributes | |
127 | actually returned can be determined by looking at the | |
128 | attribute_set_t structure returned for the | |
129 | .Dv ATTR_CMN_RETURNED_ATTRS | |
130 | attribute. Default values will be returned for invalid | |
131 | attributes and should be ignored. | |
132 | .Pp | |
133 | Please see the discussion of this flag in | |
134 | .Xr getattrlist 2 | |
135 | . | |
136 | .El | |
137 | .Pp | |
138 | If | |
139 | .Dv ATTR_CMN_ERROR | |
140 | has been requested and an error specific to a directory entry occurs, | |
141 | an error will be reported. The | |
142 | .Dv ATTR_CMN_ERROR | |
143 | attribute is a uint32_t which, if non-zero, specifies the error code | |
144 | that was encountered during the processing of that directory entry. The | |
145 | .Dv ATTR_CMN_ERROR | |
146 | attribute will be after | |
147 | .Dv ATTR_CMN_RETURNED_ATTRS | |
148 | attribute in the returned buffer. | |
149 | .Pp | |
150 | It is typical to ask for a combination of common, file, and directory | |
151 | attributes and then use the value of the | |
152 | .Dv ATTR_CMN_OBJTYPE | |
153 | attribute to parse the resulting attribute buffer. | |
154 | . | |
155 | .Sh RETURN VALUES | |
156 | Upon successful completion the numbers of entries successfully read | |
bb59bff1 A |
157 | is returned. A value of 0 indicates there are no more entries. Once 0 is returned, |
158 | no further entries are returned even if new entries are added to the directory. | |
159 | Directory iteration should be restarted either by repostioning the offset to 0 by | |
160 | .Fn lseek | |
161 | or by closing the file descriptor and opening the directory again. On error, | |
fe8ab488 A |
162 | a value of -1 is returned and |
163 | .Va errno | |
164 | is set to indicate the error. | |
165 | .Pp | |
166 | When iterating all entries in a directory, | |
167 | .Fn getattrlistbulk | |
168 | is called repeatedly until a 0 is returned. In such a case if | |
169 | .Fn readdir | |
170 | and | |
171 | .Fn getattrlistbulk | |
172 | calls on the same fd are mixed, the behavior is undefined. | |
173 | ||
174 | .Pp | |
175 | .Sh ERRORS | |
176 | .Fn getattrlistbulk | |
177 | will fail if: | |
178 | .Bl -tag -width Er | |
179 | . | |
180 | .It Bq Er EBADF | |
181 | .Fa dirfd | |
182 | is not a valid file descriptor for a directory open for reading. | |
183 | . | |
184 | .It Bq Er ENOTDIR | |
185 | The File descriptor | |
186 | .Fa dirfd | |
187 | is not a directory. | |
188 | . | |
189 | .It Bq Er EACCES | |
190 | Search permission is denied on the directory whose descriptor is given | |
191 | as input. | |
192 | . | |
193 | .It Bq Er EFAULT | |
194 | .Fa attrList | |
195 | or | |
196 | .Em attrBuf | |
197 | points to an invalid address. | |
198 | . | |
199 | .It Bq Er ERANGE | |
200 | The buffer was too small. | |
201 | . | |
202 | .It Bq Er EINVAL | |
203 | The | |
204 | .Fa bitmapcount | |
205 | field of | |
206 | .Fa attrList | |
207 | is not | |
208 | .Dv ATTR_BIT_MAP_COUNT . | |
209 | . | |
210 | .It Bq Er EINVAL | |
211 | An invalid attribute was requested. | |
212 | . | |
213 | .It Bq Er EINVAL | |
214 | Volume attributes were requested. | |
215 | . | |
216 | .It Bq Er EINVAL | |
217 | .Dv ATTR_CMN_NAME | |
218 | or | |
219 | .Dv ATTR_CMN_RETURNED_ATTRS | |
220 | was not requested in the attrList parameter. | |
221 | . | |
222 | .It Bq Er EIO | |
223 | An I/O error occurred while reading from or writing to the file system. | |
224 | .El | |
225 | .Pp | |
226 | . | |
227 | .Sh EXAMPLES | |
228 | . | |
229 | The following code lists the contents of a directory using | |
230 | .Fn getattrlistbulk . | |
231 | The listing includes the file type. | |
232 | . | |
233 | .Bd -literal | |
234 | #include <sys/syscall.h> | |
235 | #include <sys/attr.h> | |
236 | #include <sys/errno.h> | |
237 | #include <sys/vnode.h> | |
238 | #include <unistd.h> | |
239 | #include <fcntl.h> | |
240 | #include <stdio.h> | |
241 | #include <assert.h> | |
242 | #include <stddef.h> | |
243 | #include <string.h> | |
244 | #include <stdbool.h> | |
245 | ||
246 | typedef struct val_attrs { | |
247 | uint32_t length; | |
248 | attribute_set_t returned; | |
249 | uint32_t error; | |
250 | attrreference_t name_info; | |
251 | char *name; | |
252 | fsobj_type_t obj_type; | |
253 | } val_attrs_t; | |
254 | ||
255 | ||
256 | void demo(const char *dirpath) | |
257 | { | |
258 | int error; | |
259 | int dirfd; | |
260 | struct attrlist attrList; | |
261 | char *entry_start; | |
262 | char attrBuf[256]; | |
263 | ||
264 | memset(&attrList, 0, sizeof(attrList)); | |
265 | attrList.bitmapcount = ATTR_BIT_MAP_COUNT; | |
266 | attrList.commonattr = ATTR_CMN_RETURNED_ATTRS | | |
267 | ATTR_CMN_NAME | | |
268 | ATTR_CMN_ERROR | | |
269 | ATTR_CMN_OBJTYPE; | |
270 | ||
271 | error = 0; | |
272 | dirfd = open(dirpath, O_RDONLY, 0); | |
273 | if (dirfd < 0) { | |
274 | error = errno; | |
275 | printf("Could not open directory %s", dirpath); | |
276 | perror("Error was "); | |
277 | } else { | |
278 | for (;;) { | |
279 | int retcount; | |
280 | ||
281 | retcount = getattrlistbulk(dirfd, &attrList, &attrBuf[0], | |
282 | sizeof(attrBuf), 0); | |
283 | printf("\engetattrlistbulk returned %d", retcount); | |
284 | if (retcount == -1) { | |
285 | error = errno; | |
286 | perror("Error returned : "); | |
287 | printf("\en"); | |
288 | break; | |
289 | } else if (retcount == 0) { | |
290 | /* No more entries in directory */ | |
291 | error = 0; | |
292 | break; | |
293 | } else { | |
294 | int index; | |
295 | uint32_t total_length; | |
296 | char *field; | |
297 | ||
298 | entry_start = &attrBuf[0]; | |
299 | total_length = 0; | |
300 | printf(" -> entries returned"); | |
301 | for (index = 0; index < retcount; index++) { | |
302 | val_attrs_t attrs = {0}; | |
303 | ||
304 | printf("\en Entry %d", index); | |
305 | printf(" -- "); | |
306 | field = entry_start; | |
307 | attrs.length = *(uint32_t *)field; | |
308 | printf(" Length %d ", attrs.length); | |
309 | total_length += attrs.length; | |
310 | printf(" Total Length %d ", total_length); | |
311 | field += sizeof(uint32_t); | |
312 | printf(" -- "); | |
313 | ||
314 | /* set starting point for next entry */ | |
315 | entry_start += attrs.length; | |
316 | ||
317 | attrs.returned = *(attribute_set_t *)field; | |
318 | field += sizeof(attribute_set_t); | |
319 | ||
320 | if (attrs.returned.commonattr & ATTR_CMN_ERROR) { | |
321 | attrs.error = *(uint32_t *)field; | |
322 | field += sizeof(uint32_t); | |
323 | } | |
324 | ||
325 | if (attrs.returned.commonattr & ATTR_CMN_NAME) { | |
326 | attrs.name = field; | |
327 | attrs.name_info = *(attrreference_t *)field; | |
328 | field += sizeof(attrreference_t); | |
329 | printf(" %s ", (attrs.name + | |
330 | attrs.name_info.attr_dataoffset)); | |
331 | } | |
332 | ||
333 | /* Check for error for this entry */ | |
334 | if (attrs.error) { | |
335 | /* | |
336 | * Print error and move on to next | |
337 | * entry | |
338 | */ | |
339 | printf("Error in reading attributes for directory \ | |
340 | entry %d", attrs.error); | |
341 | continue; | |
342 | } | |
343 | ||
344 | printf(" -- "); | |
345 | if (attrs.returned.commonattr & ATTR_CMN_OBJTYPE) { | |
346 | attrs.obj_type = *(fsobj_type_t *)field; | |
347 | field += sizeof(fsobj_type_t); | |
348 | ||
349 | switch (attrs.obj_type) { | |
350 | case VREG: | |
351 | printf("file "); | |
352 | break; | |
353 | case VDIR: | |
354 | printf("directory "); | |
355 | break; | |
356 | default: | |
357 | printf("obj_type = %-2d ", attrs.obj_type); | |
358 | break; | |
359 | } | |
360 | } | |
361 | printf(" -- "); | |
362 | } | |
363 | } | |
364 | } | |
365 | (void)close(dirfd); | |
366 | } | |
367 | } | |
368 | .Ed | |
369 | .Pp | |
370 | . | |
371 | .Sh SEE ALSO | |
372 | . | |
373 | .Xr getattrlist 2 , | |
374 | .Xr lseek 2 | |
375 | . | |
376 | .Sh HISTORY | |
377 | A | |
378 | .Fn getattrlistbulk | |
379 | function call appeared in OS X version 10.10 | |
380 | . |