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