]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/searchfs.2
xnu-792.tar.gz
[apple/xnu.git] / bsd / man / man2 / searchfs.2
CommitLineData
91447636
A
1.\" Copyright (c) 2003 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.\" @(#)searchfs.2
18.
19.Dd December 15, 2003
20.Dt SEARCHFS 2
21.Os Darwin
22.Sh NAME
23.Nm searchfs
24.Nd search a volume quickly
25.Sh SYNOPSIS
26.Fd #include <sys/attr.h>
27.Fd #include <unistd.h>
28.Ft int
29.Fn searchfs "const char * path" "struct fssearchblock * searchBlock" "unsigned long * numMatches" "unsigned long scriptCode" "unsigned long options" "struct searchstate * state"
30.
31.Sh DESCRIPTION
32The
33.Fn searchfs
34function searches the volume (that is, mounted file system) specified by
35.Fa path
36for file system objects matching the criteria specified by
37.Fa searchBlock ,
38.Fa scriptCode ,
39and
40.Fa options .
41The
42.Fa numMatches
43parameter returns the number of matching file system objects found.
44The function also returns attributes of those file system objects in a buffer
45specified by
46.Fa searchBlock .
47The
48.Fa searchState
49parameter allows you search the volume using multiple calls to
50.Fn searchfs ,
51resuming the search where it left off.
52The routine will only return objects to which you have access (that is, you
53have execute permissions on the directories leading to this object from the root).
54.Pp
55.
56.\" path parameter
57.
58The
59.Fa path
60parameter must reference a valid file system object on the volume to be searched.
61Typically the path is to the volume's root directory.
62The entire volume is always searched.
63All directories listed in the path name leading to this object must be
64searchable.
65.Pp
66.
67.\" searchBlock parameter
68.
69The
70.Fa searchBlock
71parameter is a pointer to an
72.Vt fssearchblock
73structure, as defined by
74.Aq Pa sys/attr.h
75(shown below).
76You are responsible for filling out all fields of this structure before calling the function.
77.Bd -literal
78struct fssearchblock {
79 struct attrlist * returnattrs;
80 void * returnbuffer;
81 size_t returnbuffersize;
82 unsigned long maxmatches;
83 struct timeval timelimit;
84 void * searchparams1;
85 size_t sizeofsearchparams1;
86 void * searchparams2;
87 size_t sizeofsearchparams2;
88 struct attrlist searchattrs;
89};
90.Ed
91.Pp
92.
93For information about the
94.Vt attrlist
95structure, see the discussion of
96.Xr getattrlist 2 .
97.Pp
98.
99.\" searchBlock elements
100.
101The fields of the
102.Vt fssearchblock
103structure are defined as follows.
104.Bl -tag -width sizeofsearchparams1
105.
106.It returnattrs
107.Fn searchfs
108can return arbitrary attributes of the file system objects that it finds.
109This field must point to an
110.Vt attrlist
111structure that specifies the attributes that you want returned.
112To request an attribute you must set the corresponding bit in the appropriate
113.Vt attrgroup_t
114field of the
115.Vt attrlist
116structure.
117You are responsible for filling out all fields of this structure before calling the function.
118You must not request volume attributes.
119.
120.It returnbuffer
121.Fn searchfs
122places attributes of the matching file system objects into this returned attributes buffer.
123The attributes for any given object are grouped together and
124packed in exactly the same way as they would be returned from
125.Xr getdirentriesattr 2 .
126The initial contents of this buffer are ignored.
127.
128.It returnbuffersize
129Set this field to the size, in bytes, of the buffer pointed to by
130.Fa returnbuffer .
131.
132.It maxmatches
133Specifies the maximum number of matches that you want this call to
134.Fn searchfs
135to return.
136.
137.It timelimit
138Specifies the maximum time that you want this call to
139.Fn searchfs
140to run.
141.Pp
142.
143If you're implementing a volume format, you should impose your own internal
144limit on the duration of this call to prevent a malicious user program
145from monopolising kernel resources.
146.Pp
147.
148.It searchparams1
149Specifies the lower bound of the search criteria.
150This is discussed in detail below.
151You must place attribute values into the buffer in the same
152way as they would be returned by
153.Xr getattrlist 2 ,
154where the
155.Fa searchattrs
156field determines the exact layout of the attribute values.
157.
158.It sizeofsearchparams1
159Set this field to the size, in bytes, of the buffer pointed to by
160.Fa searchparams1 .
161.
162.It searchparams2
163Specifies the upper bound of the search criteria.
164This is discussed in detail below.
165You must place attribute values into the buffer in the same
166way as they would be returned by
167.Xr getattrlist 2 ,
168where the
169.Fa searchattrs
170field determines the exact layout of the attribute values.
171.
172.It sizeofsearchparams2
173Set this field to the size, in bytes, of the buffer pointed to by
174.Fa searchparams2 .
175.
176.It searchattrs
177Specifies the attributes that you want you use for your search criteria.
178You are responsible for filling out all fields of this structure before calling the function.
179To search for an attribute you must set the corresponding bit in the appropriate
180.Vt attrgroup_t
181field of the
182.Vt attrlist
183structure, and place the appropriate values into the
184.Fa searchparam1
185and
186.Fa searchparam2
187buffers.
188The attributes specified here determine the format of those buffers.
189This is discussed in detail below.
190.
191.El
192.Pp
193.
194.\" numMatches parameter
195.
196The
197.Fa numMatches
198parameter points to an
199.Vt unsigned long
200variable.
201The initial value of this variable is ignored.
202On return, this variable contains the number of matching file system objects found.
203The is always less than or equal to the
204.Fa maxmatches
205field of the
206.Fa searchBlock
207parameter.
208The attributes for the matching objects have been placed into the returned attributes buffer.
209.Pp
210.
211.\" scriptCode parameter
212.
213The
214.Fa scriptCode
215parameter is currently ignored.
216You should always pass in the value 0x08000103, which corresponds to the
217UTF-8 text encoding value defined by
218.Aq Pa CarbonCore/TextCommon.h .
219.Pp
220.
221.\" options parameter
222.
223The
224.Fa options
225parameter is a bit set that controls the behaviour of
226.Fn searchfs .
227The following option bits are defined.
228.
229.Bl -tag -width SRCHFS_MATCHPARTIALNAMES
230.
231.It SRCHFS_START
232If this bit is set,
233.Fn searchfs
234will ignore the
235.Fa state
236parameter and start a new search.
237Otherwise
238.Fn searchfs
239assumes that
240.Fa searchstate
241is valid and attempts to resume a previous search based on that state.
242.
243.It SRCHFS_MATCHPARTIALNAMES
244If this bit is set,
245.Fn searchfs
246will consider substrings to be successful matches when evaluating the
247.Dv ATTR_CMN_NAME
248attribute.
249.
250.It SRCHFS_MATCHDIRS
251If this bit is set,
252.Fn searchfs
253will search for directories that match the search criteria.
254To get meaningful results you must specify either this bit or
255.Dv SRCHFS_MATCHFILES ,
256or both.
257.
258.It SRCHFS_MATCHFILES
259If this bit is set,
260.Fn searchfs
261will search for files that match the search criteria.
262To get meaningful results you must specify either this bit or
263.Dv SRCHFS_MATCHDIRS ,
264or both.
265.
266.It SRCHFS_SKIPLINKS
267If this bit is set,
268.Fn searchfs
269will only return one reference for a hard linked file, rather that a reference
270for each hard link to the file.
271.Pp
272This option is not recommended for general development.
273Its primary client is the
274.Xr quotacheck 2
275utility.
276.Pp
277.
278This option is privileged (the caller's effective UID must be 0) and cannot
279be used if you request the
280.Dv ATTR_CMN_NAME
281or
282.Dv ATTR_CMN_PAROBJID
283attributes.
284.Pp
285Introduced with Darwin 7.0 (Mac OS X version 10.3).
286.
287.It SRCHFS_SKIPINVISIBLE
288If this bit is set,
289.Fn searchfs
290will not match any invisible file system objects (that is, objects whose
291.Dv ATTR_CMN_FNDRINFO
292attribute has bit 6 set in the ninth byte) or any objects within
293invisible directories.
294.Pp
295Introduced with Darwin 7.0 (Mac OS X version 10.3).
296.
297.It SRCHFS_SKIPPACKAGES
298If this bit is set,
299.Fn searchfs
300will not match any file system objects that are inside a package.
301A package is defined as a directory whose extension matches one
302of the extensions that are configured into the kernel by Launch Services.
303.Pp
304Introduced with Darwin 7.0 (Mac OS X version 10.3).
305.
306.It SRCHFS_SKIPINAPPROPRIATE
307If this bit is set,
308.Fn searchfs
309will not match any file system objects that are within an inappropriate directory.
310The current list of inappropriate directories contains one item: /System.
311.Pp
312Introduced with Darwin 7.0 (Mac OS X version 10.3).
313.
314.It SRCHFS_NEGATEPARAMS
315If this bit is set,
316.Fn searchfs
317will return all the file system objects that do not match the search criteria.
318.Pp
319Introduced with Darwin 7.0 (Mac OS X version 10.3).
320.
321.El
322.Pp
323.
324.\" state parameter
325.
326The
327.Fa state
328parameter is a pointer to an opaque data structure that
329.Fn searchfs
330uses to maintain the state of a search between successive calls.
331In your first call to
332.Fn searchfs ,
333you specify the
334.Dv SRCHFS_START
335flag in the
336.Fa options
337parameter.
338This tells
339.Fn searchfs
340that the search state is invalid and that it should start a new search.
341When this call completes, it may have only returned partial results;
342in that case, it will have updated the structure pointed to by
343.Fa state .
344If you call
345.Fn searchfs
346again, this time without specifying the
347.Dv SRCHFS_START
348flag in the
349.Fa options
350parameter, it will resume the search where it left off, using the search state
351that it previously stored in the state structure.
352You do not need to explicitly dispose of this state.
353.Pp
354.
355The
356.Fn searchfs
357function returns significant errors in the followings cases.
358.
359.Bl -bullet
360.
361.It
362If it has found as many objects as you requested in the
363.Fa maxmatches
364field of the
365.Fa searchBlock
366parameter, it will return
367.Dv EAGAIN .
368.
369.It
370If there is not enough space in the returned attributes buffer for the first match,
371it will return
372.Dv ENOBUFS .
373You should allocate a larger returned attributes buffer and try again.
374.Fa numMatches
375will be zero in this case.
376.
377.It
378If the timeout expires it will return
379.Dv EAGAIN .
380.
381.It
382If you attempt to resume a search (that is,
383.Dv SRCHFS_START
384is not specified in the
385.Fa options
386parameter) and the catalog has changed since the last search,
387the function will return
388.Dv EBUSY .
389You must start your search again from the beginning.
390.
391.El
392.Pp
393.
394If
395.Fn searchfs
396returns
397.Dv EAGAIN ,
398the value in
399.Fa numMatches
400may be greater than zero.
401This is known as a partial result.
402You should be sure to process these matches before calling
403.Fn searchfs
404again.
405.
406.Sh SEARCH CRITERIA
407.
408You specify the search criteria using a combination of the
409.Fa searchattrs ,
410.Fa searchparams1 ,
411.Fa sizeofsearchparams1,
412.Fa searchparams2 ,
413and
414.Fa sizeofsearchparams2
415fields of the
416.Fa searchBlock
417parameter, and various flags in the
418.Fa options
419parameter.
420The
421.Fa searchattrs
422field determines the attributes considered when comparing a file system object to
423the search criteria.
424You can specify that an attribute should be considered by setting the corresponding
425bit in the appropriate
426.Vt attrgroup_t
427field of the
428.Vt attrlist
429structure.
430See the discussion of
431.Xr getattrlist 2
432for a detailed description of this structure.
433.Pp
434.
435The
436.Fa searchparams1 ,
437.Fa sizeofsearchparams1 ,
438.Fa searchparams2 ,
439and
440.Fa sizeofsearchparams2
441fields specify the attribute values that must be matched.
442The format of each of these buffers is determined by the attributes that you're searching for.
443The values are packed in exactly the same way as they would be returned from
444.Xr getattrlist 2 ,
445including the leading
446.Vt unsigned long
447length value.
448.Pp
449.
450The attribute values in the first and second search buffers form a lower and upper bound for
451the search, respectively.
452These have different meanings depending on the type of attribute.
453.
454.Bl -bullet
455.
456.It
457For string attributes (specifically
458.Dv ATTR_CMN_NAME ,
459the object name), the value in the first search
460buffer is significant and the value in the second search buffer is ignored.
461The string comparison is either an exact match or a substring match depending on
462the
463.Dv SRCHFS_MATCHPARTIALNAMES
464flag in the
465.Fa options
466parameter.
467.
468.It
469For structured attributes (specifically
470.Dv ATTR_CMN_FNDRINFO ,
471the Finder information), the value from the
472file system object is masked (logical AND) with the value in the second search buffer and then
473compared, byte for byte, against the value in the first search buffer.
474If it is equal, the object is a match.
475.
476.It
477For scalar attributes (all other attributes, for example,
478.Dv ATTR_CMN_MODTIME ,
479the modification date), the values in the first and second search
480buffers are literally a lower and upper bound.
481An object matches the criteria if its value is greater than or equal to the value in
482the first buffer and less than or equal to the value in the second.
483.
484.El
485.
486.Sh RETURN VALUES
487Upon successful completion, a value of 0 is returned.
488This means that the entire volume has been searched and all matches returned.
489Otherwise, a value of -1 is returned and
490.Va errno
491is set to indicate the error.
492.Pp
493.
494See the discussion of the
495.Dv EAGAIN ,
496.Dv ENOBUFS ,
497and
498.Dv EBUSY
499error codes above.
500.
501.Sh COMPATIBILITY
502Not all volumes support
503.Fn searchfs .
504You can test whether a volume supports
505.Fn searchfs
506by using
507.Xr getattrlist 2
508to get the volume capabilities attribute
509.Dv ATTR_VOL_CAPABILITIES ,
510and then testing the
511.Dv VOL_CAP_INT_SEARCHFS
512flag.
513.Pp
514.
515The
516.Fn searchfs
517function has been undocumented for more than two years.
518In that time a number of volume format implementations have been created without
519a proper specification for the behaviour of this routine.
520You may encounter volume format implementations with slightly different
521behaviour than what is described here.
522Your program is expected to be tolerant of this variant behaviour.
523.Pp
524.
525If you're implementing a volume format that supports
526.Fn searchfs ,
527you should be careful to support the behaviour specified by this document.
528.Pp
529.
530A bug in systems prior to Darwin 7.0 (Mac OS X version 10.3) makes searching for the
531.Dv ATTR_CMN_BKUPTIME
532attribute tricky.
533The bug causes the attribute to consume two items in the search attribute buffers, the
534first in the proper place and the second between
535.Dv ATTR_CMN_FNDRINFO
536and
537.Dv ATTR_CMN_OWNERID .
538.
539.Sh ERRORS
540.Fn searchfs
541will fail if:
542.Bl -tag -width Er
543.
544.It Bq Er ENOTSUP
545The volume does not support
546.Fn searchfs .
547.
548.It Bq Er ENOTDIR
549A component of the path prefix is not a directory.
550.
551.It Bq Er ENAMETOOLONG
552A component of a path name exceeded
553.Dv NAME_MAX
554characters, or an entire path name exceeded
555.Dv PATH_MAX
556characters.
557.
558.It Bq Er ENOENT
559The file system object does not exist.
560.
561.It Bq Er EACCES
562Search permission is denied for a component of the path prefix.
563.
564.It Bq Er ELOOP
565Too many symbolic links were encountered in translating the pathname.
566.
567.It Bq Er EFAULT
568One of the pointer parameters points to an invalid address.
569.
570.It Bq Er EINVAL
571The
572.Fa options
573parameter contains an invalid flag or sizeofsearchparams1/2 is greater than
574SEARCHFS_MAX_SEARCHPARMS (see attr.h).
575.
576.It Bq Er EAGAIN
577The search terminated with partial results, either because
578.Fa numMatches
579has hit the limit specified by
580.Fa maxmatches
581or because the timeout expired.
582Process the matches returned so far and then call
583.Fn searchfs
584again to look for more.
585.Pp
586.
587.It Bq Er ENOBUFS
588The returned attributes buffer is too small for the first match.
589You should allocate a larger returned attributes buffer and try again.
590.Fa numMatches
591will be zero in this case.
592.
593.It Bq Er EBUSY
594The search could not be resumed because the volume has changed.
595.
596.It Bq Er EIO
597An I/O error occurred while reading from or writing to the file system.
598.El
599.Pp
600.
601.Sh CAVEATS
602Not all attributes can be searched for using
603.Fn searchfs .
604The list currently includes:
605.Pp
606.
607.Bl -item -compact
608.It
609ATTR_CMN_NAME
610.It
611ATTR_CMN_OBJID
612.It
613ATTR_CMN_PAROBJID
614.It
615ATTR_CMN_CRTIME
616.It
617ATTR_CMN_MODTIME
618.It
619ATTR_CMN_CHGTIME
620.It
621ATTR_CMN_ACCTIME
622.It
623ATTR_CMN_BKUPTIME
624.It
625ATTR_CMN_FNDRINFO
626.It
627ATTR_CMN_BKUPTIME
628.It
629ATTR_CMN_OWNERID
630.It
631ATTR_CMN_GRPID
632.It
633ATTR_CMN_ACCESSMASK
634.Pp
635.
636.It
637ATTR_DIR_ENTRYCOUNT
638.Pp
639.
640.It
641ATTR_FILE_DATALENGTH
642.It
643ATTR_FILE_DATAALLOCSIZE
644.It
645ATTR_FILE_RSRCLENGTH
646.It
647ATTR_FILE_RSRCALLOCSIZE
648.El
649.
650.Sh EXAMPLES
651.
652The following code searches a volume for files of the specified type and creator.
653.
654.Bd -literal
655#include <assert.h>
656#include <stdio.h>
657#include <stddef.h>
658#include <string.h>
659#include <sys/attr.h>
660#include <sys/errno.h>
661#include <unistd.h>
662.Pp
663.
664typedef struct attrlist attrlist_t;
665typedef struct fssearchblock fssearchblock_t;
666typedef struct searchstate searchstate_t;
667.Pp
668.
669struct SearchAttrBuf {
670 unsigned long length;
671 char finderInfo[32];
672};
673typedef struct SearchAttrBuf SearchAttrBuf;
674.Pp
675.
676struct ResultAttrBuf {
677 unsigned long length;
678 attrreference_t name;
679 fsobj_id_t parObjID;
680};
681typedef struct ResultAttrBuf ResultAttrBuf;
682.Pp
683.
684enum {
685 kMatchesPerCall = 16
686};
687.Pp
688.
689static int SearchFSDemo(
690 const char *volPath,
691 const char *type,
692 const char *creator
693)
694{
695 int err;
696 fssearchblock_t searchBlock;
697 SearchAttrBuf lower;
698 SearchAttrBuf upper;
699 static const unsigned char kAllOnes[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
700 unsigned long matchCount;
701 unsigned long matchIndex;
702 unsigned long options;
703 searchstate_t state;
704 ResultAttrBuf * thisEntry;
705 attrlist_t returnAttrList;
706 char resultAttrBuf[ kMatchesPerCall
707 * (sizeof(ResultAttrBuf) + 64)];
708.Pp
709.
710 // resultAttrBuf is big enough for kMatchesPerCall entries,
711 // assuming that the average name length is less than 64.
712.Pp
713.
714 assert(strlen(type) == 4);
715 assert(strlen(creator) == 4);
716.Pp
717
718 memset(&searchBlock, 0, sizeof(searchBlock));
719 searchBlock.searchattrs.bitmapcount = ATTR_BIT_MAP_COUNT;
720 searchBlock.searchattrs.commonattr = ATTR_CMN_FNDRINFO;
721.Pp
722
723 memset(&lower, 0, sizeof(lower));
724 memset(&upper, 0, sizeof(upper));
725 lower.length = sizeof(lower);
726 upper.length = sizeof(upper);
727 memcpy(&lower.finderInfo[0], type, 4);
728 memcpy(&lower.finderInfo[4], creator, 4);
729 memcpy(&upper.finderInfo[0], kAllOnes, 4);
730 memcpy(&upper.finderInfo[4], kAllOnes, 4);
731 searchBlock.searchparams1 = &lower;
732 searchBlock.sizeofsearchparams1 = sizeof(lower);
733 searchBlock.searchparams2 = &upper;
734 searchBlock.sizeofsearchparams2 = sizeof(lower);
735.Pp
736
737 searchBlock.timelimit.tv_sec = 0;
738 searchBlock.timelimit.tv_usec = 100 * 1000;
739.Pp
740
741 searchBlock.maxmatches = kMatchesPerCall;
742.Pp
743
744 memset(&returnAttrList, 0, sizeof(returnAttrList));
745 returnAttrList.bitmapcount = ATTR_BIT_MAP_COUNT;
746 returnAttrList.commonattr = ATTR_CMN_NAME | ATTR_CMN_PAROBJID;
747.Pp
748.
749 searchBlock.returnattrs = &returnAttrList;
750 searchBlock.returnbuffer = resultAttrBuf;
751 searchBlock.returnbuffersize = sizeof(resultAttrBuf);
752.Pp
753
754 options = SRCHFS_START | SRCHFS_MATCHFILES;
755.Pp
756
757 do {
758 err = searchfs(
759 volPath,
760 &searchBlock,
761 &matchCount,
762 0x08000103,
763 options,
764 &state
765 );
766 if (err != 0) {
767 err = errno;
768 }
769 if ( (err == 0) || (err == EAGAIN) ) {
770 thisEntry = (ResultAttrBuf *) resultAttrBuf;
771.Pp
772
773 for (matchIndex = 0; matchIndex < matchCount; matchIndex++) {
774 printf("%08x ", thisEntry->parObjID.fid_objno);
775 printf(
776 "%s\en",
777 ((char *) &thisEntry->name)
778 + thisEntry->name.attr_dataoffset
779 );
780.
781 // Advance to the next entry.
782.
783 ((char *) thisEntry) += thisEntry->length;
784 }
785 }
786.Pp
787
788 options &= ~SRCHFS_START;
789 } while (err == EAGAIN);
790.Pp
791
792 return err;
793}
794.Ed
795.
796.Sh SEE ALSO
797.
798.Xr getattrlist 2
799.
800.Sh HISTORY
801A
802.Fn searchfs
803function call appeared in Darwin 1.3.1 (Mac OS X version 10.0).
804.