1 .\" Copyright (c) 2003 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.
24 .Nd search a volume quickly
26 .Fd #include <sys/attr.h>
27 .Fd #include <unistd.h>
29 .Fn searchfs "const char* path" "struct fssearchblock* searchBlock" "unsigned long* numMatches" "unsigned int scriptCode" "unsigned int options" "struct searchstate* state"
34 function searches the volume (that is, mounted file system) specified by
36 for file system objects matching the criteria specified by
43 parameter returns the number of matching file system objects found.
44 The function also returns attributes of those file system objects in a buffer
49 parameter allows you search the volume using multiple calls to
51 resuming the search where it left off.
52 The routine will only return objects to which you have access (that is, you
53 have execute permissions on the directories leading to this object from the root).
60 parameter must reference a valid file system object on the volume to be searched.
61 Typically the path is to the volume's root directory.
62 The entire volume is always searched.
63 All directories listed in the path name leading to this object must be
67 .\" searchBlock parameter
71 parameter is a pointer to an
73 structure, as defined by
76 You are responsible for filling out all fields of this structure before calling the function.
78 struct fssearchblock {
79 struct attrlist * returnattrs;
81 size_t returnbuffersize;
82 unsigned int maxmatches;
83 struct timeval timelimit;
85 size_t sizeofsearchparams1;
87 size_t sizeofsearchparams2;
88 struct attrlist searchattrs;
93 For information about the
95 structure, see the discussion of
99 .\" searchBlock elements
103 structure are defined as follows.
104 .Bl -tag -width sizeofsearchparams1
108 can return arbitrary attributes of the file system objects that meet the designated
109 search criteria passed in via
113 This field must point to an
115 structure that specifies the attributes that you want returned.
116 To request an attribute you must set the corresponding bit in the appropriate
121 You are responsible for filling out all fields of this structure before calling the function.
122 You must not request volume attributes.
126 places attributes of the matching file system objects into this returned attributes buffer.
127 The attributes for any given object are grouped together and
128 packed in exactly the same way as they would be returned from
129 .Xr getdirentriesattr 2 .
130 The initial contents of this buffer are ignored.
133 Set this field to the size, in bytes, of the buffer pointed to by
137 Specifies the maximum number of matches that you want this call to
142 Specifies the maximum time that you want this call to
147 If you're implementing a volume format, you should impose your own internal
148 limit on the duration of this call to prevent a malicious user program
149 from monopolizing kernel resources.
153 Specifies the lower bound of the search criteria.
154 This is discussed in detail below.
155 You must place attribute values into the buffer in the same
156 way as they would be returned by
160 field determines the exact layout of the attribute values.
162 .It sizeofsearchparams1
163 Set this field to the size, in bytes, of the buffer pointed to by
167 Specifies the upper bound of the search criteria.
168 This is discussed in detail below.
169 You must place attribute values into the buffer in the same
170 way as they would be returned by
174 field determines the exact layout of the attribute values.
176 .It sizeofsearchparams2
177 Set this field to the size, in bytes, of the buffer pointed to by
181 Specifies the attributes that you want you use for your search criteria.
182 You are responsible for filling out all fields of this structure before calling the function.
183 To search for an attribute you must set the corresponding bit in the appropriate
187 structure, and place the appropriate values into the
192 The attributes specified here determine the format of those buffers.
193 This is discussed in detail below.
198 .\" numMatches parameter
202 parameter points to an
205 The initial value of this variable is ignored.
206 On return, this variable contains the number of matching file system objects found.
207 The is always less than or equal to the
212 The attributes for the matching objects have been placed into the returned attributes buffer.
215 .\" scriptCode parameter
219 parameter is currently ignored.
220 You should always pass in the value 0x08000103, which corresponds to the
221 UTF-8 text encoding value defined by
222 .Aq Pa CarbonCore/TextCommon.h .
225 .\" options parameter
229 parameter is a bit set that controls the behaviour of
231 The following option bits are defined.
233 .Bl -tag -width SRCHFS_MATCHPARTIALNAMES
240 parameter and start a new search.
245 is valid and attempts to resume a previous search based on that state.
247 .It SRCHFS_MATCHPARTIALNAMES
250 will consider substrings to be successful matches when evaluating the
257 will search for directories that match the search criteria.
258 To get meaningful results you must specify either this bit or
259 .Dv SRCHFS_MATCHFILES ,
262 .It SRCHFS_MATCHFILES
265 will search for files that match the search criteria.
266 To get meaningful results you must specify either this bit or
267 .Dv SRCHFS_MATCHDIRS ,
273 will only return one reference for a hard linked file, rather than a reference
274 for each hard link to the file.
276 This option is not recommended for general development.
277 Its primary client is the
279 utility. Note that not all filesystems that support
281 support this option and may return EINVAL if it is requested.
284 This option is privileged (the caller's effective UID must be 0) and cannot
285 be used if you request the
288 .Dv ATTR_CMN_PAROBJID
291 Introduced with Darwin 7.0 (Mac OS X version 10.3).
293 .It SRCHFS_SKIPINVISIBLE
296 will not match any invisible file system objects (that is, objects whose
297 .Dv ATTR_CMN_FNDRINFO
298 attribute has bit 6 set in the ninth byte) or any objects within
299 invisible directories.
301 Introduced with Darwin 7.0 (Mac OS X version 10.3).
303 .It SRCHFS_SKIPPACKAGES
306 will not match any file system objects that are inside a package.
307 A package is defined as a directory whose extension matches one
308 of the extensions that are configured into the kernel by Launch Services.
310 Introduced with Darwin 7.0 (Mac OS X version 10.3).
312 .It SRCHFS_SKIPINAPPROPRIATE
315 will not match any file system objects that are within an inappropriate directory.
316 The current list of inappropriate directories contains one item: /System.
318 Introduced with Darwin 7.0 (Mac OS X version 10.3).
320 .It SRCHFS_NEGATEPARAMS
323 will return all the file system objects that do not match the search criteria.
325 Introduced with Darwin 7.0 (Mac OS X version 10.3).
334 parameter is a pointer to an opaque data structure that
336 uses to maintain the state of a search between successive calls.
337 In your first call to
346 that the search state is invalid and that it should start a new search.
347 When this call completes, it may have only returned partial results;
348 in that case, it will have updated the structure pointed to by
352 again, this time without specifying the
356 parameter, it will resume the search where it left off, using the search state
357 that it previously stored in the state structure.
358 You do not need to explicitly dispose of this state.
363 function returns significant errors in the followings cases.
368 If it has found as many objects as you requested in the
372 parameter, it will return
376 If there is not enough space in the returned attributes buffer for the first match,
379 You should allocate a larger returned attributes buffer and try again.
381 will be zero in this case.
384 If the timeout expires it will return
388 If you attempt to resume a search (that is,
390 is not specified in the
392 parameter) and the catalog has changed since the last search,
393 the function will return
395 You must start your search again from the beginning.
406 may be greater than zero.
407 This is known as a partial result.
408 You should be sure to process these matches before calling
414 You specify the search criteria using a combination of the
417 .Fa sizeofsearchparams1,
420 .Fa sizeofsearchparams2
423 parameter, and various flags in the
428 field determines the attributes considered when comparing a file system object to
430 You can specify that an attribute should be considered by setting the corresponding
431 bit in the appropriate
436 See the discussion of
438 for a detailed description of this structure.
443 .Fa sizeofsearchparams1 ,
446 .Fa sizeofsearchparams2
447 fields specify the attribute values that must be matched.
448 The format of each of these buffers is determined by the attributes that you're searching for.
449 The values are packed in exactly the same way as they would be returned from
451 including the leading
453 length value. Note that the size of these buffers must be bounded by SEARCHFS_MAX_SEARCHPARMS bytes,
454 which is defined in <sys/attr.h>.
457 The attribute values in the first and second search buffers form a lower and upper bound for
458 the search, respectively.
459 These have different meanings depending on the type of attribute.
464 For string attributes (specifically
466 the object name), the value in the first search
467 buffer is significant and the value in the second search buffer is ignored.
468 The string comparison is either an exact match or a substring match depending on
470 .Dv SRCHFS_MATCHPARTIALNAMES
476 For structured attributes (specifically
477 .Dv ATTR_CMN_FNDRINFO ,
478 the Finder information), the value from the
479 file system object is masked (logical AND) with the value in the second search buffer and then
480 compared, byte for byte, against the value in the first search buffer.
481 If it is equal, the object is a match.
484 For scalar attributes (all other attributes, for example,
485 .Dv ATTR_CMN_MODTIME ,
486 the modification date), the values in the first and second search
487 buffers are literally a lower and upper bound.
488 An object matches the criteria if its value is greater than or equal to the value in
489 the first buffer and less than or equal to the value in the second.
494 Upon successful completion, a value of 0 is returned.
495 This means that the entire volume has been searched and all matches returned.
496 Otherwise, a value of -1 is returned and
498 is set to indicate the error.
501 See the discussion of the
509 Not all volumes support
511 You can test whether a volume supports
515 to get the volume capabilities attribute
516 .Dv ATTR_VOL_CAPABILITIES ,
518 .Dv VOL_CAP_INT_SEARCHFS
524 function has been undocumented for more than two years.
525 In that time a number of volume format implementations have been created without
526 a proper specification for the behaviour of this routine.
527 You may encounter volume format implementations with slightly different
528 behaviour than what is described here.
529 Your program is expected to be tolerant of this variant behaviour.
532 If you're implementing a volume format that supports
534 you should be careful to support the behaviour specified by this document.
537 A bug in systems prior to Darwin 7.0 (Mac OS X version 10.3) makes searching for the
538 .Dv ATTR_CMN_BKUPTIME
540 The bug causes the attribute to consume two items in the search attribute buffers, the
541 first in the proper place and the second between
542 .Dv ATTR_CMN_FNDRINFO
544 .Dv ATTR_CMN_OWNERID .
552 The volume does not support
556 A component of the path prefix is not a directory.
558 .It Bq Er ENAMETOOLONG
559 A component of a path name exceeded
561 characters, or an entire path name exceeded
566 The file system object does not exist.
569 Search permission is denied for a component of the path prefix.
572 Too many symbolic links were encountered in translating the pathname.
575 One of the pointer parameters points to an invalid address.
580 parameter contains an invalid flag or sizeofsearchparams1/2 is greater than
581 SEARCHFS_MAX_SEARCHPARMS (see attr.h). Additionally, filesystems that do
582 not support SRCHFS_SKIPLINKS may return EINVAL if this search option
583 is requested. EINVAL may also be returned if you request attributes for either
584 searching or to be returned for matched entries if the filesystem does not support
585 vending that particular attribute.
588 The search terminated with partial results, either because
590 has hit the limit specified by
592 or because the timeout expired.
593 Process the matches returned so far and then call
595 again to look for more.
599 The returned attributes buffer is too small for the first match.
600 You should allocate a larger returned attributes buffer and try again.
602 will be zero in this case.
605 The search could not be resumed because the volume has changed.
608 An I/O error occurred while reading from or writing to the file system.
614 The list of attributes valid for searching and returning to the caller may
615 be substantially smaller than that of the
617 system call. See the following lists for the currently available search criteria.
618 In general, a filesystem that supports
620 will typically supply per-item attributes for matched objects that are also
623 system call. This varies from filesystem to filesystem.
626 .Sh SEARCH ATTRIBUTES
628 The list of attributes that are valid as search criteria currently includes the
629 following list of attributes for a particular filesystem object.
673 ATTR_FILE_DATAALLOCSIZE
677 ATTR_FILE_RSRCALLOCSIZE
681 .Sh RETURN ATTRIBUTES
683 As mentioned above, the list of attributes that are available to be returned to the caller
684 vary by filesystem, but should include the following attributes, in the following order.
685 The buffer should be assumed to be packed similar to the output buffer of the
687 system call. Note that again, this list may be substantially smaller than what is available via
706 ATTR_CMN_OBJPERMANENTID
754 ATTR_FILE_IOBLOCKSIZE
762 ATTR_FILE_DATAALLOCSIZE
766 ATTR_FILE_RSRCALLOCSIZE
773 The following code searches a volume for files of the specified type and creator.
780 #include <sys/attr.h>
781 #include <sys/errno.h>
785 typedef struct attrlist attrlist_t;
786 typedef struct fssearchblock fssearchblock_t;
787 typedef struct searchstate searchstate_t;
790 struct SearchAttrBuf {
794 typedef struct SearchAttrBuf SearchAttrBuf;
797 struct ResultAttrBuf {
799 attrreference_t name;
802 typedef struct ResultAttrBuf ResultAttrBuf;
810 static int SearchFSDemo(
817 fssearchblock_t searchBlock;
820 static const unsigned char kAllOnes[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
821 unsigned long matchCount;
822 unsigned long matchIndex;
823 unsigned int options;
825 ResultAttrBuf * thisEntry;
826 attrlist_t returnAttrList;
827 char resultAttrBuf[ kMatchesPerCall
828 * (sizeof(ResultAttrBuf) + 64)];
831 // resultAttrBuf is big enough for kMatchesPerCall entries,
832 // assuming that the average name length is less than 64.
835 assert(strlen(type) == 4);
836 assert(strlen(creator) == 4);
839 memset(&searchBlock, 0, sizeof(searchBlock));
840 searchBlock.searchattrs.bitmapcount = ATTR_BIT_MAP_COUNT;
841 searchBlock.searchattrs.commonattr = ATTR_CMN_FNDRINFO;
844 memset(&lower, 0, sizeof(lower));
845 memset(&upper, 0, sizeof(upper));
846 lower.length = sizeof(lower);
847 upper.length = sizeof(upper);
848 memcpy(&lower.finderInfo[0], type, 4);
849 memcpy(&lower.finderInfo[4], creator, 4);
850 memcpy(&upper.finderInfo[0], kAllOnes, 4);
851 memcpy(&upper.finderInfo[4], kAllOnes, 4);
852 searchBlock.searchparams1 = &lower;
853 searchBlock.sizeofsearchparams1 = sizeof(lower);
854 searchBlock.searchparams2 = &upper;
855 searchBlock.sizeofsearchparams2 = sizeof(lower);
858 searchBlock.timelimit.tv_sec = 0;
859 searchBlock.timelimit.tv_usec = 100 * 1000;
862 searchBlock.maxmatches = kMatchesPerCall;
865 memset(&returnAttrList, 0, sizeof(returnAttrList));
866 returnAttrList.bitmapcount = ATTR_BIT_MAP_COUNT;
867 returnAttrList.commonattr = ATTR_CMN_NAME | ATTR_CMN_PAROBJID;
870 searchBlock.returnattrs = &returnAttrList;
871 searchBlock.returnbuffer = resultAttrBuf;
872 searchBlock.returnbuffersize = sizeof(resultAttrBuf);
875 options = SRCHFS_START | SRCHFS_MATCHFILES;
890 if ( (err == 0) || (err == EAGAIN) ) {
891 thisEntry = (ResultAttrBuf *) resultAttrBuf;
894 for (matchIndex = 0; matchIndex < matchCount; matchIndex++) {
895 printf("%08x ", thisEntry->parObjID.fid_objno);
898 ((char *) &thisEntry->name)
899 + thisEntry->name.attr_dataoffset
902 // Advance to the next entry.
904 ((char *) thisEntry) += thisEntry->length;
909 options &= ~SRCHFS_START;
910 } while (err == EAGAIN);
924 function call appeared in Darwin 1.3.1 (Mac OS X version 10.0).