2 * Copyright (c) 1997-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
25 #include <sys/param.h>
26 #include <sys/systm.h>
27 #include <sys/kernel.h>
31 #include <mach/machine/vm_types.h>
32 #include <sys/vnode.h>
33 #include <sys/malloc.h>
34 #include <sys/signalvar.h>
36 #include <sys/utfconv.h>
37 #include <sys/kauth.h>
38 #include <sys/vnode_internal.h>
42 #include "hfs_catalog.h"
43 #include "hfs_attrlist.h"
44 #include "hfs_endian.h"
46 #include "hfscommon/headers/FileMgrInternal.h"
47 #include "hfscommon/headers/HFSUnicodeWrappers.h"
48 #include "hfscommon/headers/BTreesPrivate.h"
49 #include "hfscommon/headers/BTreeScanner.h"
50 #include "hfscommon/headers/CatalogPrivate.h"
52 /* Search criterea. */
53 struct directoryInfoSpec
60 off_t dataLogicalLength
;
61 off_t dataPhysicalLength
;
62 off_t resourceLogicalLength
;
63 off_t resourcePhysicalLength
;
68 u_char name
[kHFSPlusMaxFileNameBytes
];
70 char attributes
; // see IM:Files 2-100
73 struct timespec creationDate
;
74 struct timespec modificationDate
;
75 struct timespec changeDate
;
76 struct timespec accessDate
;
77 struct timespec lastBackupDate
;
78 uint8_t finderInfo
[32];
82 struct fileInfoSpec f
;
83 struct directoryInfoSpec d
;
85 typedef struct searchinfospec searchinfospec_t
;
87 static void ResolveHardlink(ExtendedVCB
*vcb
, HFSPlusCatalogFile
*recp
);
90 static int UnpackSearchAttributeBlock(struct hfsmount
*hfsmp
, struct attrlist
*alist
,
91 searchinfospec_t
*searchInfo
, void *attributeBuffer
);
93 static int CheckCriteria( ExtendedVCB
*vcb
,
95 struct attrlist
*attrList
,
98 searchinfospec_t
*searchInfo1
,
99 searchinfospec_t
*searchInfo2
,
100 Boolean lookForDup
);
102 static int CheckAccess(ExtendedVCB
*vcb
, u_long searchBits
, CatalogKey
*key
, struct proc
*p
);
104 static int InsertMatch(struct hfsmount
*hfsmp
, uio_t a_uio
, CatalogRecord
*rec
,
105 CatalogKey
*key
, struct attrlist
*returnAttrList
,
106 void *attributesBuffer
, void *variableBuffer
,
107 u_long
* nummatches
);
109 static Boolean
CompareRange(u_long val
, u_long low
, u_long high
);
110 static Boolean
CompareWideRange(u_int64_t val
, u_int64_t low
, u_int64_t high
);
112 static Boolean
CompareRange( u_long val
, u_long low
, u_long high
)
114 return( (val
>= low
) && (val
<= high
) );
117 static Boolean
CompareWideRange( u_int64_t val
, u_int64_t low
, u_int64_t high
)
119 return( (val
>= low
) && (val
<= high
) );
121 //#define CompareRange(val, low, high) ((val >= low) && (val <= high))
123 #if 1 // Installer workaround (2940423)
124 static Boolean
IsTargetName( searchinfospec_t
* searchInfoPtr
, Boolean isHFSPlus
);
125 #endif // Installer workaround
127 __private_extern__
int hfs_vnop_search(struct vnop_searchfs_args
*ap
);
130 /************************************************************************/
131 /* Entry for searchfs() */
132 /************************************************************************/
134 #define errSearchBufferFull 101 /* Internal search errors */
143 IN kauth_cred_t cred;
151 struct vnop_searchfs_args
*ap
; /*
152 struct vnodeop_desc *a_desc;
154 void *a_searchparams1;
155 void *a_searchparams2;
156 struct attrlist *a_searchattrs;
158 struct timeval *a_timelimit;
159 struct attrlist *a_returnattrs;
160 u_long *a_nummatches;
164 struct searchstate *a_searchstate;
165 vfs_context_t a_context;
168 ExtendedVCB
*vcb
= VTOVCB(ap
->a_vp
);
169 struct hfsmount
*hfsmp
;
171 searchinfospec_t searchInfo1
;
172 searchinfospec_t searchInfo2
;
173 void *attributesBuffer
;
174 void *variableBuffer
;
175 u_long fixedBlockSize
;
176 u_long eachReturnBufferSize
;
177 struct proc
*p
= proc_self();
180 int timerExpired
= false;
181 int doQuickExit
= false;
182 CatalogKey
* myCurrentKeyPtr
;
183 CatalogRecord
* myCurrentDataPtr
;
184 CatPosition
* myCatPositionPtr
;
185 BTScanState myBTScanState
;
186 user_addr_t user_start
= 0;
187 user_size_t user_len
= 0;
191 /* XXX Parameter check a_searchattrs? */
193 *(ap
->a_nummatches
) = 0;
195 if (ap
->a_options
& ~SRCHFS_VALIDOPTIONSMASK
)
198 /* SRCHFS_SKIPLINKS requires root access.
199 * This option cannot be used with either
200 * the ATTR_CMN_NAME or ATTR_CMN_PAROBJID
203 if (ap
->a_options
& SRCHFS_SKIPLINKS
) {
206 attrs
= ap
->a_searchattrs
->commonattr
| ap
->a_returnattrs
->commonattr
;
207 if (attrs
& (ATTR_CMN_NAME
| ATTR_CMN_PAROBJID
))
209 if ((err
= suser(kauth_cred_get(), 0)))
213 if (uio_resid(ap
->a_uio
) <= 0)
216 isHFSPlus
= (vcb
->vcbSigWord
== kHFSPlusSigWord
);
217 hfsmp
= VTOHFS(ap
->a_vp
);
219 searchTime
= kMaxMicroSecsInKernel
;
220 if (ap
->a_timelimit
->tv_sec
== 0 &&
221 ap
->a_timelimit
->tv_usec
> 0 &&
222 ap
->a_timelimit
->tv_usec
< kMaxMicroSecsInKernel
) {
223 searchTime
= ap
->a_timelimit
->tv_usec
;
226 /* UnPack the search boundries, searchInfo1, searchInfo2 */
227 err
= UnpackSearchAttributeBlock(hfsmp
, ap
->a_searchattrs
,
228 &searchInfo1
, ap
->a_searchparams1
);
230 err
= UnpackSearchAttributeBlock(hfsmp
, ap
->a_searchattrs
,
231 &searchInfo2
, ap
->a_searchparams2
);
234 fixedBlockSize
= sizeof(uint32_t) + hfs_attrblksize(ap
->a_returnattrs
); /* uint32_t for length word */
236 eachReturnBufferSize
= fixedBlockSize
;
238 if ( ap
->a_returnattrs
->commonattr
& ATTR_CMN_NAME
) /* XXX should be more robust! */
239 eachReturnBufferSize
+= kHFSPlusMaxFileNameBytes
+ 1;
241 MALLOC( attributesBuffer
, void *, eachReturnBufferSize
, M_TEMP
, M_WAITOK
);
242 variableBuffer
= (void*)((char*) attributesBuffer
+ fixedBlockSize
);
244 // XXXdbg - have to lock the user's buffer so we don't fault
245 // while holding the shared catalog file lock. see the comment
246 // in hfs_readdir() for more details.
248 if (hfsmp
->jnl
&& uio_isuserspace(ap
->a_uio
)) {
249 user_start
= uio_curriovbase(ap
->a_uio
);
250 user_len
= uio_curriovlen(ap
->a_uio
);
252 if ((err
= vslock(user_start
, user_len
)) != 0) {
254 goto ExitThisRoutine
;
258 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
260 catalogFCB
= GetFileControlBlock(vcb
->catalogRefNum
);
261 myCurrentKeyPtr
= NULL
;
262 myCurrentDataPtr
= NULL
;
263 myCatPositionPtr
= (CatPosition
*)ap
->a_searchstate
;
265 if (ap
->a_options
& SRCHFS_START
) {
266 /* Starting a new search. */
267 /* Make sure the on-disk Catalog file is current */
268 (void) hfs_fsync(vcb
->catalogRefNum
, MNT_WAIT
, 0, p
);
270 hfs_systemfile_unlock(hfsmp
, lockflags
);
271 journal_flush(hfsmp
->jnl
);
272 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
275 ap
->a_options
&= ~SRCHFS_START
;
276 bzero( (caddr_t
)myCatPositionPtr
, sizeof( *myCatPositionPtr
) );
277 err
= BTScanInitialize(catalogFCB
, 0, 0, 0, kCatSearchBufferSize
, &myBTScanState
);
279 #if 1 // Installer workaround (2940423)
280 // hack to get around installer problems when the installer expects search results
281 // to be in key order. At this point the problem appears to be limited to
282 // searches for "Library". The idea here is to go get the "Library" at root
283 // and return it first to the caller then continue the search as normal with
284 // the exception of taking care not to return a duplicate hit (see CheckCriteria)
285 if ( err
== E_NONE
&&
286 (ap
->a_searchattrs
->commonattr
& ATTR_CMN_NAME
) != 0 &&
287 IsTargetName( &searchInfo1
, isHFSPlus
) )
290 BTreeIterator iterator
;
291 FSBufferDescriptor btrec
;
296 bzero( (caddr_t
)&iterator
, sizeof( iterator
) );
297 keyp
= (CatalogKey
*) &iterator
.key
;
298 (void) BuildCatalogKeyUTF8(vcb
, kRootDirID
, "Library", kUndefinedStrLen
, keyp
, NULL
);
300 btrec
.bufferAddress
= &rec
;
302 btrec
.itemSize
= sizeof( rec
);
304 result
= BTSearchRecord( catalogFCB
, &iterator
, &btrec
, &reclen
, &iterator
);
305 if ( result
== E_NONE
) {
306 // need to unlock since CheckAccess assumes no lock held
307 hfs_systemfile_unlock(hfsmp
, lockflags
);
308 if (CheckCriteria(vcb
, ap
->a_options
, ap
->a_searchattrs
, &rec
,
309 keyp
, &searchInfo1
, &searchInfo2
, false) &&
310 CheckAccess(vcb
, ap
->a_options
, keyp
, p
)) {
312 result
= InsertMatch(hfsmp
, ap
->a_uio
, &rec
,
313 keyp
, ap
->a_returnattrs
,
314 attributesBuffer
, variableBuffer
,
316 if (result
== E_NONE
&& *(ap
->a_nummatches
) >= ap
->a_maxmatches
)
319 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
322 #endif // Installer workaround
324 /* Resuming a search. */
325 err
= BTScanInitialize(catalogFCB
, myCatPositionPtr
->nextNode
,
326 myCatPositionPtr
->nextRecord
,
327 myCatPositionPtr
->recordsFound
,
328 kCatSearchBufferSize
,
330 /* Make sure Catalog hasn't changed. */
332 && myCatPositionPtr
->writeCount
!= myBTScanState
.btcb
->writeCount
) {
333 myCatPositionPtr
->writeCount
= myBTScanState
.btcb
->writeCount
;
334 err
= EBUSY
; /* catChangedErr */
337 hfs_systemfile_unlock(hfsmp
, lockflags
);
340 goto ExitThisRoutine
;
341 #if 1 // Installer workaround (2940423)
344 #endif // Installer workaround
347 * Check all the catalog btree records...
348 * return the attributes for matching items
351 struct timeval myCurrentTime
;
352 struct timeval myElapsedTime
;
354 err
= BTScanNextRecord(&myBTScanState
, timerExpired
,
355 (void **)&myCurrentKeyPtr
, (void **)&myCurrentDataPtr
,
360 /* Resolve any hardlinks */
361 if (isHFSPlus
&& (ap
->a_options
& SRCHFS_SKIPLINKS
) == 0) {
362 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
363 ResolveHardlink(vcb
, (HFSPlusCatalogFile
*) myCurrentDataPtr
);
364 hfs_systemfile_unlock(hfsmp
, lockflags
);
366 if (CheckCriteria( vcb
, ap
->a_options
, ap
->a_searchattrs
, myCurrentDataPtr
,
367 myCurrentKeyPtr
, &searchInfo1
, &searchInfo2
, true )
368 && CheckAccess(vcb
, ap
->a_options
, myCurrentKeyPtr
, p
)) {
369 err
= InsertMatch(hfsmp
, ap
->a_uio
, myCurrentDataPtr
,
370 myCurrentKeyPtr
, ap
->a_returnattrs
,
371 attributesBuffer
, variableBuffer
, ap
->a_nummatches
);
374 * The last match didn't fit so come back
375 * to this record on the next trip.
377 --myBTScanState
.recordsFound
;
378 --myBTScanState
.recordNum
;
382 if (*(ap
->a_nummatches
) >= ap
->a_maxmatches
)
387 * Check our elapsed time and bail if we've hit the max.
388 * The idea here is to throttle the amount of time we
389 * spend in the kernel.
391 microuptime(&myCurrentTime
);
392 timersub(&myCurrentTime
, &myBTScanState
.startTime
, &myElapsedTime
);
393 /* Note: assumes kMaxMicroSecsInKernel is less than 1,000,000 */
394 if (myElapsedTime
.tv_sec
> 0
395 || myElapsedTime
.tv_usec
>= searchTime
) {
401 /* Update catalog position */
402 myCatPositionPtr
->writeCount
= myBTScanState
.btcb
->writeCount
;
404 BTScanTerminate(&myBTScanState
, &myCatPositionPtr
->nextNode
,
405 &myCatPositionPtr
->nextRecord
,
406 &myCatPositionPtr
->recordsFound
);
408 if ( err
== E_NONE
) {
409 err
= EAGAIN
; /* signal to the user to call searchfs again */
410 } else if ( err
== errSearchBufferFull
) {
411 if ( *(ap
->a_nummatches
) > 0 )
415 } else if ( err
== btNotFound
) {
416 err
= E_NONE
; /* the entire disk has been searched */
417 } else if ( err
== fsBTTimeOutErr
) {
422 FREE( attributesBuffer
, M_TEMP
);
424 if (hfsmp
->jnl
&& user_start
) {
425 vsunlock(user_start
, user_len
, TRUE
);
428 return (MacToVFSError(err
));
433 ResolveHardlink(ExtendedVCB
*vcb
, HFSPlusCatalogFile
*recp
)
435 if ((recp
->recordType
== kHFSPlusFileRecord
)
436 && (SWAP_BE32(recp
->userInfo
.fdType
) == kHardLinkFileType
)
437 && (SWAP_BE32(recp
->userInfo
.fdCreator
) == kHFSPlusCreator
)
438 && ((to_bsd_time(recp
->createDate
) == vcb
->vcbCrDate
) ||
439 (to_bsd_time(recp
->createDate
) == VCBTOHFS(vcb
)->hfs_metadata_createdate
))) {
442 /* Export link's cnid (a unique value) instead of inode's cnid */
443 saved_cnid
= recp
->fileID
;
444 (void) resolvelink(VCBTOHFS(vcb
), recp
->bsdInfo
.special
.iNodeNum
, recp
);
445 recp
->fileID
= saved_cnid
;
451 CompareMasked(const UInt32
*thisValue
, const UInt32
*compareData
,
452 const UInt32
*compareMask
, UInt32 count
)
457 matched
= true; /* Assume it will all match */
459 for (i
=0; i
<count
; i
++) {
460 if (((*thisValue
++ ^ *compareData
++) & *compareMask
++) != 0) {
471 ComparePartialUnicodeName (register ConstUniCharArrayPtr str
, register ItemCount s_len
,
472 register ConstUniCharArrayPtr find
, register ItemCount f_len
)
474 if (f_len
== 0 || s_len
== 0)
480 } while (FastUnicodeCompare(str
++, f_len
, find
, f_len
) != 0);
487 ComparePartialPascalName ( register ConstStr31Param str
, register ConstStr31Param find
)
489 register u_char s_len
= str
[0];
490 register u_char f_len
= find
[0];
491 register u_char
*tsp
;
494 if (f_len
== 0 || s_len
== 0)
497 bcopy(str
, tmpstr
, s_len
+ 1);
500 while (s_len
-- >= f_len
) {
503 if (FastRelString(tsp
++, find
) == 0)
512 // Determine if a name is "inappropriate" where the definition
513 // of "inappropriate" is up to higher level execs. Currently
514 // that's limited to /System.
517 is_inappropriate_name(char *name
, int len
)
519 const char *bad_names
[] = { "System" };
520 int bad_len
[] = { 6 };
523 for(i
=0; i
< (int) (sizeof(bad_names
) / sizeof(bad_names
[0])); i
++) {
524 if (len
== bad_len
[i
] && strcmp(name
, bad_names
[i
]) == 0) {
529 // if we get here, no name matched
536 * Check to see if caller has access rights to this item
540 CheckAccess(ExtendedVCB
*theVCBPtr
, u_long searchBits
, CatalogKey
*theKeyPtr
, struct proc
*theProcPtr
)
545 HFSCatalogNodeID myNodeID
;
547 struct FndrDirInfo
*finfop
;
548 struct vnode
* vp
= NULL
;
549 struct vfs_context my_context
;
551 myResult
= 0; /* default to "no access" */
552 my_context
.vc_proc
= theProcPtr
;
553 my_context
.vc_ucred
= kauth_cred_get();
555 if (!proc_suser(theProcPtr
)) {
556 myResult
= 1; /* allow access */
557 goto ExitThisRoutine
; /* root always has access */
560 hfsmp
= VCBTOHFS( theVCBPtr
);
561 isHFSPlus
= ( theVCBPtr
->vcbSigWord
== kHFSPlusSigWord
);
563 myNodeID
= theKeyPtr
->hfsPlus
.parentID
;
565 myNodeID
= theKeyPtr
->hfs
.parentID
;
567 while ( myNodeID
>= kRootDirID
) {
570 /* now go get catalog data for this directory */
571 myErr
= hfs_vget(hfsmp
, myNodeID
, &vp
, 0);
573 goto ExitThisRoutine
; /* no access */
577 finfop
= (struct FndrDirInfo
*)&cp
->c_attr
.ca_finderinfo
[0];
579 if ( searchBits
& SRCHFS_SKIPPACKAGES
) {
580 if ( (SWAP_BE16(finfop
->frFlags
) & kHasBundle
)
581 || (cp
->c_desc
.cd_nameptr
!= NULL
582 && is_package_name(cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
)) ) {
584 goto ExitThisRoutine
;
588 if ( searchBits
& SRCHFS_SKIPINAPPROPRIATE
) {
589 if ( cp
->c_parentcnid
== kRootDirID
&& cp
->c_desc
.cd_nameptr
!= NULL
&&
590 is_inappropriate_name(cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
) ) {
592 goto ExitThisRoutine
;
596 if ( (searchBits
& SRCHFS_SKIPINVISIBLE
) &&
597 (SWAP_BE16(finfop
->frFlags
) & kIsInvisible
) ) {
599 goto ExitThisRoutine
;
602 myNodeID
= cp
->c_parentcnid
; /* move up the hierarchy */
603 hfs_unlock(VTOC(vp
));
604 if (vp
->v_type
== VDIR
) {
605 myErr
= vnode_authorize(vp
, NULL
, (KAUTH_VNODE_SEARCH
| KAUTH_VNODE_LIST_DIRECTORY
), &my_context
);
607 myErr
= vnode_authorize(vp
, NULL
, (KAUTH_VNODE_SEARCH
), &my_context
);
612 goto ExitThisRoutine
; /* no access */
615 myResult
= 1; /* allow access */
619 hfs_unlock(VTOC(vp
));
627 CheckCriteria( ExtendedVCB
*vcb
,
629 struct attrlist
*attrList
,
632 searchinfospec_t
*searchInfo1
,
633 searchinfospec_t
*searchInfo2
,
636 Boolean matched
, atleastone
;
638 attrgroup_t searchAttributes
;
639 struct cat_attr c_attr
;
640 struct cat_fork datafork
;
641 struct cat_fork rsrcfork
;
643 bzero(&c_attr
, sizeof(c_attr
));
644 isHFSPlus
= (vcb
->vcbSigWord
== kHFSPlusSigWord
);
646 switch (rec
->recordType
) {
647 case kHFSFolderRecord
:
648 case kHFSPlusFolderRecord
:
649 if ( (searchBits
& SRCHFS_MATCHDIRS
) == 0 ) { /* If we are NOT searching folders */
656 if ( (searchBits
& SRCHFS_MATCHFILES
) == 0 ) { /* If we are NOT searching files */
662 case kHFSPlusFileRecord
:
663 /* Check if hardlink links should be skipped. */
664 if (searchBits
& SRCHFS_SKIPLINKS
) {
665 cnid_t parid
= key
->hfsPlus
.parentID
;
666 HFSPlusCatalogFile
*filep
= (HFSPlusCatalogFile
*)rec
;
668 if ((SWAP_BE32(filep
->userInfo
.fdType
) == kHardLinkFileType
) &&
669 (SWAP_BE32(filep
->userInfo
.fdCreator
) == kHFSPlusCreator
)) {
670 return (false); /* skip over link records */
671 } else if ((parid
== VCBTOHFS(vcb
)->hfs_privdir_desc
.cd_cnid
) &&
672 (filep
->bsdInfo
.special
.linkCount
== 0)) {
673 return (false); /* skip over unlinked files */
675 } else if (key
->hfsPlus
.parentID
== VCBTOHFS(vcb
)->hfs_privdir_desc
.cd_cnid
) {
676 return (false); /* skip over private files */
679 if ( (searchBits
& SRCHFS_MATCHFILES
) == 0 ) { /* If we are NOT searching files */
685 default: /* Never match a thread record or any other type. */
686 return( false ); /* Not a file or folder record, so can't search it */
689 matched
= true; /* Assume we got a match */
690 atleastone
= false; /* Dont insert unless we match at least one criteria */
692 /* First, attempt to match the name -- either partial or complete */
693 if ( attrList
->commonattr
& ATTR_CMN_NAME
) {
695 /* Check for partial/full HFS Plus name match */
697 if ( searchBits
& SRCHFS_MATCHPARTIALNAMES
) {
698 matched
= ComparePartialUnicodeName(key
->hfsPlus
.nodeName
.unicode
,
699 key
->hfsPlus
.nodeName
.length
,
700 (UniChar
*)searchInfo1
->name
,
701 searchInfo1
->nameLength
);
702 } else /* full HFS Plus name match */ {
703 matched
= (FastUnicodeCompare(key
->hfsPlus
.nodeName
.unicode
,
704 key
->hfsPlus
.nodeName
.length
,
705 (UniChar
*)searchInfo1
->name
,
706 searchInfo1
->nameLength
) == 0);
709 /* Check for partial/full HFS name match */
711 if ( searchBits
& SRCHFS_MATCHPARTIALNAMES
)
712 matched
= ComparePartialPascalName(key
->hfs
.nodeName
, (u_char
*)searchInfo1
->name
);
713 else /* full HFS name match */
714 matched
= (FastRelString(key
->hfs
.nodeName
, (u_char
*)searchInfo1
->name
) == 0);
717 #if 1 // Installer workaround (2940423)
719 HFSCatalogNodeID parentID
;
721 parentID
= key
->hfsPlus
.parentID
;
723 parentID
= key
->hfs
.parentID
;
725 if ( matched
&& parentID
== kRootDirID
&&
726 IsTargetName( searchInfo1
, isHFSPlus
) )
729 #endif // Installer workaround
731 if ( matched
== false || (searchBits
& ~SRCHFS_MATCHPARTIALNAMES
) == 0 )
732 goto TestDone
; /* no match, or nothing more to compare */
737 /* Convert catalog record into cat_attr format. */
738 cat_convertattr(VCBTOHFS(vcb
), rec
, &c_attr
, &datafork
, &rsrcfork
);
740 if (searchBits
& SRCHFS_SKIPINVISIBLE
) {
743 switch (rec
->recordType
) {
744 case kHFSFolderRecord
:
745 case kHFSPlusFolderRecord
: {
746 struct FndrDirInfo
*finder_info
;
748 finder_info
= (struct FndrDirInfo
*)&c_attr
.ca_finderinfo
[0];
749 flags
= SWAP_BE16(finder_info
->frFlags
);
754 case kHFSPlusFileRecord
: {
755 struct FndrFileInfo
*finder_info
;
757 finder_info
= (struct FndrFileInfo
*)&c_attr
.ca_finderinfo
[0];
758 flags
= SWAP_BE16(finder_info
->fdFlags
);
763 flags
= kIsInvisible
;
768 if (flags
& kIsInvisible
) {
776 /* Now that we have a record worth searching, see if it matches the search attributes */
777 if (rec
->recordType
== kHFSFileRecord
||
778 rec
->recordType
== kHFSPlusFileRecord
) {
779 if ((attrList
->fileattr
& ~ATTR_FILE_VALIDMASK
) != 0) { /* attr we do know about */
783 else if ((attrList
->fileattr
& ATTR_FILE_VALIDMASK
) != 0) {
784 searchAttributes
= attrList
->fileattr
;
786 /* File logical length (data fork) */
787 if ( searchAttributes
& ATTR_FILE_DATALENGTH
) {
788 matched
= CompareWideRange(
790 searchInfo1
->f
.dataLogicalLength
,
791 searchInfo2
->f
.dataLogicalLength
);
792 if (matched
== false) goto TestDone
;
796 /* File physical length (data fork) */
797 if ( searchAttributes
& ATTR_FILE_DATAALLOCSIZE
) {
798 matched
= CompareWideRange(
799 (u_int64_t
)datafork
.cf_blocks
* (u_int64_t
)vcb
->blockSize
,
800 searchInfo1
->f
.dataPhysicalLength
,
801 searchInfo2
->f
.dataPhysicalLength
);
802 if (matched
== false) goto TestDone
;
806 /* File logical length (resource fork) */
807 if ( searchAttributes
& ATTR_FILE_RSRCLENGTH
) {
808 matched
= CompareWideRange(
810 searchInfo1
->f
.resourceLogicalLength
,
811 searchInfo2
->f
.resourceLogicalLength
);
812 if (matched
== false) goto TestDone
;
816 /* File physical length (resource fork) */
817 if ( searchAttributes
& ATTR_FILE_RSRCALLOCSIZE
) {
818 matched
= CompareWideRange(
819 (u_int64_t
)rsrcfork
.cf_blocks
* (u_int64_t
)vcb
->blockSize
,
820 searchInfo1
->f
.resourcePhysicalLength
,
821 searchInfo2
->f
.resourcePhysicalLength
);
822 if (matched
== false) goto TestDone
;
827 atleastone
= true; /* to match SRCHFS_MATCHFILES */
831 * Check the directory attributes
833 else if (rec
->recordType
== kHFSFolderRecord
||
834 rec
->recordType
== kHFSPlusFolderRecord
) {
835 if ((attrList
->dirattr
& ~ATTR_DIR_VALIDMASK
) != 0) { /* attr we do know about */
839 else if ((attrList
->dirattr
& ATTR_DIR_VALIDMASK
) != 0) {
840 searchAttributes
= attrList
->dirattr
;
842 /* Directory valence */
843 if ( searchAttributes
& ATTR_DIR_ENTRYCOUNT
) {
844 matched
= CompareRange(c_attr
.ca_entries
,
845 searchInfo1
->d
.numFiles
,
846 searchInfo2
->d
.numFiles
);
847 if (matched
== false) goto TestDone
;
852 atleastone
= true; /* to match SRCHFS_MATCHDIRS */
857 * Check the common attributes
859 searchAttributes
= attrList
->commonattr
;
860 if ( (searchAttributes
& ATTR_CMN_VALIDMASK
) != 0 ) {
862 if ( searchAttributes
& ATTR_CMN_OBJID
) {
863 matched
= CompareRange(c_attr
.ca_fileid
,
865 searchInfo2
->nodeID
);
866 if (matched
== false) goto TestDone
;
871 if ( searchAttributes
& ATTR_CMN_PAROBJID
) {
872 HFSCatalogNodeID parentID
;
875 parentID
= key
->hfsPlus
.parentID
;
877 parentID
= key
->hfs
.parentID
;
879 matched
= CompareRange(parentID
, searchInfo1
->parentDirID
,
880 searchInfo2
->parentDirID
);
881 if (matched
== false) goto TestDone
;
885 /* Finder Info & Extended Finder Info where extFinderInfo is last 32 bytes */
886 if ( searchAttributes
& ATTR_CMN_FNDRINFO
) {
888 thisValue
= (UInt32
*) &c_attr
.ca_finderinfo
;
891 * Note: ioFlFndrInfo and ioDrUsrWds have the same offset in search info, so
892 * no need to test the object type here.
894 matched
= CompareMasked(thisValue
,
895 (UInt32
*)&searchInfo1
->finderInfo
,
896 (UInt32
*) &searchInfo2
->finderInfo
, 8);
897 if (matched
== false) goto TestDone
;
902 if ( searchAttributes
& ATTR_CMN_CRTIME
) {
903 matched
= CompareRange(c_attr
.ca_itime
,
904 searchInfo1
->creationDate
.tv_sec
,
905 searchInfo2
->creationDate
.tv_sec
);
906 if (matched
== false) goto TestDone
;
911 if ( searchAttributes
& ATTR_CMN_MODTIME
) {
912 matched
= CompareRange(c_attr
.ca_mtime
,
913 searchInfo1
->modificationDate
.tv_sec
,
914 searchInfo2
->modificationDate
.tv_sec
);
915 if (matched
== false) goto TestDone
;
920 if ( searchAttributes
& ATTR_CMN_CHGTIME
) {
921 matched
= CompareRange(c_attr
.ca_ctime
,
922 searchInfo1
->changeDate
.tv_sec
,
923 searchInfo2
->changeDate
.tv_sec
);
924 if (matched
== false) goto TestDone
;
929 if ( searchAttributes
& ATTR_CMN_ACCTIME
) {
930 matched
= CompareRange(c_attr
.ca_atime
,
931 searchInfo1
->accessDate
.tv_sec
,
932 searchInfo2
->accessDate
.tv_sec
);
933 if (matched
== false) goto TestDone
;
938 if ( searchAttributes
& ATTR_CMN_BKUPTIME
) {
939 matched
= CompareRange(c_attr
.ca_btime
,
940 searchInfo1
->lastBackupDate
.tv_sec
,
941 searchInfo2
->lastBackupDate
.tv_sec
);
942 if (matched
== false) goto TestDone
;
947 if ( searchAttributes
& ATTR_CMN_OWNERID
) {
948 matched
= CompareRange(c_attr
.ca_uid
,
949 searchInfo1
->uid
, searchInfo2
->uid
);
950 if (matched
== false) goto TestDone
;
955 if ( searchAttributes
& ATTR_CMN_GRPID
) {
956 matched
= CompareRange(c_attr
.ca_gid
,
957 searchInfo1
->gid
, searchInfo2
->gid
);
958 if (matched
== false) goto TestDone
;
963 if ( searchAttributes
& ATTR_CMN_ACCESSMASK
) {
964 matched
= CompareRange((uint32_t)c_attr
.ca_mode
,
965 (uint32_t)searchInfo1
->mask
,
966 (uint32_t)searchInfo2
->mask
);
967 if (matched
== false) goto TestDone
;
972 /* If we got here w/o matching any, then set to false */
978 * Finally, determine whether we need to negate the sense of the match
979 * (i.e. find all objects that DON'T match).
981 if ( searchBits
& SRCHFS_NEGATEPARAMS
)
989 * Adds another record to the packed array for output
992 InsertMatch(struct hfsmount
*hfsmp
, uio_t a_uio
, CatalogRecord
*rec
,
993 CatalogKey
*key
, struct attrlist
*returnAttrList
,
994 void *attributesBuffer
, void *variableBuffer
, u_long
* nummatches
)
997 void *rovingAttributesBuffer
;
998 void *rovingVariableBuffer
;
999 u_long packedBufferSize
;
1000 u_long privateDir
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
1001 struct attrblock attrblk
;
1002 struct cat_desc c_desc
;
1003 struct cat_attr c_attr
;
1004 struct cat_fork datafork
;
1005 struct cat_fork rsrcfork
;
1007 bzero(&c_desc
, sizeof(c_desc
));
1008 bzero(&c_attr
, sizeof(c_attr
));
1009 rovingAttributesBuffer
= (char*)attributesBuffer
+ sizeof(u_long
); /* Reserve space for length field */
1010 rovingVariableBuffer
= variableBuffer
;
1012 /* Convert catalog record into cat_attr format. */
1013 cat_convertattr(hfsmp
, rec
, &c_attr
, &datafork
, &rsrcfork
);
1015 /* hide our private meta data directory */
1016 if ((privateDir
!= 0) && (c_attr
.ca_fileid
== privateDir
)) {
1021 /* Hide the private journal files */
1023 ((c_attr
.ca_fileid
== hfsmp
->hfs_jnlfileid
) ||
1024 (c_attr
.ca_fileid
== hfsmp
->hfs_jnlinfoblkid
))) {
1029 if (returnAttrList
->commonattr
& ATTR_CMN_NAME
) {
1030 cat_convertkey(hfsmp
, key
, rec
, &c_desc
);
1032 c_desc
.cd_cnid
= c_attr
.ca_fileid
;
1033 if (hfsmp
->hfs_flags
& HFS_STANDARD
)
1034 c_desc
.cd_parentcnid
= key
->hfs
.parentID
;
1036 c_desc
.cd_parentcnid
= key
->hfsPlus
.parentID
;
1039 attrblk
.ab_attrlist
= returnAttrList
;
1040 attrblk
.ab_attrbufpp
= &rovingAttributesBuffer
;
1041 attrblk
.ab_varbufpp
= &rovingVariableBuffer
;
1042 attrblk
.ab_flags
= 0;
1043 attrblk
.ab_blocksize
= 0;
1045 hfs_packattrblk(&attrblk
, hfsmp
, NULL
, &c_desc
, &c_attr
, &datafork
, &rsrcfork
, current_proc());
1047 packedBufferSize
= (char*)rovingVariableBuffer
- (char*)attributesBuffer
;
1049 if ( packedBufferSize
> uio_resid(a_uio
) )
1050 return( errSearchBufferFull
);
1054 *((u_long
*)attributesBuffer
) = packedBufferSize
; /* Store length of fixed + var block */
1056 err
= uiomove( (caddr_t
)attributesBuffer
, packedBufferSize
, a_uio
); /* XXX should be packedBufferSize */
1058 cat_releasedesc(&c_desc
);
1065 UnpackSearchAttributeBlock( struct hfsmount
*hfsmp
, struct attrlist
*alist
, searchinfospec_t
*searchInfo
, void *attributeBuffer
)
1069 boolean_t is_64_bit
;
1071 DBG_ASSERT(searchInfo
!= NULL
);
1073 is_64_bit
= proc_is64bit(current_proc());
1075 bufferSize
= *((uint32_t *)attributeBuffer
);
1076 if (bufferSize
== 0)
1077 return (EINVAL
); /* XXX -DJB is a buffer size of zero ever valid for searchfs? */
1079 ++((uint32_t *)attributeBuffer
); /* advance past the size */
1082 * UnPack common attributes
1084 a
= alist
->commonattr
;
1086 if ( a
& ATTR_CMN_NAME
) {
1090 s
= (char*) attributeBuffer
+ ((attrreference_t
*) attributeBuffer
)->attr_dataoffset
;
1091 len
= ((attrreference_t
*) attributeBuffer
)->attr_length
;
1093 if (len
> sizeof(searchInfo
->name
))
1096 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1097 /* Convert name to pascal string to match HFS B-Tree names */
1100 if (utf8_to_hfs(HFSTOVCB(hfsmp
), len
-1, s
, (u_char
*)searchInfo
->name
) != 0)
1103 searchInfo
->nameLength
= searchInfo
->name
[0];
1105 searchInfo
->name
[0] = searchInfo
->nameLength
= 0;
1107 ++((attrreference_t
*)attributeBuffer
);
1110 /* Convert name to Unicode to match HFS Plus B-Tree names */
1113 if (utf8_decodestr(s
, len
-1, (UniChar
*)searchInfo
->name
, &ucslen
,
1114 sizeof(searchInfo
->name
), ':', UTF_DECOMPOSED
))
1117 searchInfo
->nameLength
= ucslen
/ sizeof(UniChar
);
1119 searchInfo
->nameLength
= 0;
1121 ++((attrreference_t
*)attributeBuffer
);
1124 if ( a
& ATTR_CMN_OBJID
) {
1125 searchInfo
->nodeID
= ((fsobj_id_t
*) attributeBuffer
)->fid_objno
; /* ignore fid_generation */
1126 ++((fsobj_id_t
*)attributeBuffer
);
1128 if ( a
& ATTR_CMN_PAROBJID
) {
1129 searchInfo
->parentDirID
= ((fsobj_id_t
*) attributeBuffer
)->fid_objno
; /* ignore fid_generation */
1130 ++((fsobj_id_t
*)attributeBuffer
);
1132 if ( a
& ATTR_CMN_CRTIME
) {
1134 struct user_timespec tmp
;
1135 tmp
= *((struct user_timespec
*)attributeBuffer
);
1136 searchInfo
->creationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1137 searchInfo
->creationDate
.tv_nsec
= tmp
.tv_nsec
;
1138 ++((struct user_timespec
*)attributeBuffer
);
1141 searchInfo
->creationDate
= *((struct timespec
*)attributeBuffer
);
1142 ++((struct timespec
*)attributeBuffer
);
1145 if ( a
& ATTR_CMN_MODTIME
) {
1147 struct user_timespec tmp
;
1148 tmp
= *((struct user_timespec
*)attributeBuffer
);
1149 searchInfo
->modificationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1150 searchInfo
->modificationDate
.tv_nsec
= tmp
.tv_nsec
;
1151 ++((struct user_timespec
*)attributeBuffer
);
1154 searchInfo
->modificationDate
= *((struct timespec
*)attributeBuffer
);
1155 ++((struct timespec
*)attributeBuffer
);
1158 if ( a
& ATTR_CMN_CHGTIME
) {
1160 struct user_timespec tmp
;
1161 tmp
= *((struct user_timespec
*)attributeBuffer
);
1162 searchInfo
->changeDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1163 searchInfo
->changeDate
.tv_nsec
= tmp
.tv_nsec
;
1164 ++((struct user_timespec
*)attributeBuffer
);
1167 searchInfo
->changeDate
= *((struct timespec
*)attributeBuffer
);
1168 ++((struct timespec
*)attributeBuffer
);
1171 if ( a
& ATTR_CMN_ACCTIME
) {
1173 struct user_timespec tmp
;
1174 tmp
= *((struct user_timespec
*)attributeBuffer
);
1175 searchInfo
->accessDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1176 searchInfo
->accessDate
.tv_nsec
= tmp
.tv_nsec
;
1177 ++((struct user_timespec
*)attributeBuffer
);
1180 searchInfo
->accessDate
= *((struct timespec
*)attributeBuffer
);
1181 ++((struct timespec
*)attributeBuffer
);
1184 if ( a
& ATTR_CMN_BKUPTIME
) {
1186 struct user_timespec tmp
;
1187 tmp
= *((struct user_timespec
*)attributeBuffer
);
1188 searchInfo
->lastBackupDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1189 searchInfo
->lastBackupDate
.tv_nsec
= tmp
.tv_nsec
;
1190 ++((struct user_timespec
*)attributeBuffer
);
1193 searchInfo
->lastBackupDate
= *((struct timespec
*)attributeBuffer
);
1194 ++((struct timespec
*)attributeBuffer
);
1197 if ( a
& ATTR_CMN_FNDRINFO
) {
1198 bcopy( attributeBuffer
, searchInfo
->finderInfo
, sizeof(searchInfo
->finderInfo
) );
1199 (uint8_t *)attributeBuffer
+= 32;
1201 if ( a
& ATTR_CMN_OWNERID
) {
1202 searchInfo
->uid
= *((uid_t
*)attributeBuffer
);
1203 ++((uid_t
*)attributeBuffer
);
1205 if ( a
& ATTR_CMN_GRPID
) {
1206 searchInfo
->gid
= *((gid_t
*)attributeBuffer
);
1207 ++((gid_t
*)attributeBuffer
);
1209 if ( a
& ATTR_CMN_ACCESSMASK
) {
1210 searchInfo
->mask
= *((mode_t
*)attributeBuffer
);
1211 ++((mode_t
*)attributeBuffer
);
1217 if ( a
& ATTR_DIR_ENTRYCOUNT
) {
1218 searchInfo
->d
.numFiles
= *((u_int32_t
*)attributeBuffer
);
1219 ++((u_int32_t
*)attributeBuffer
);
1223 a
= alist
->fileattr
;
1225 if ( a
& ATTR_FILE_DATALENGTH
) {
1226 searchInfo
->f
.dataLogicalLength
= *((off_t
*)attributeBuffer
);
1227 ++((off_t
*)attributeBuffer
);
1229 if ( a
& ATTR_FILE_DATAALLOCSIZE
) {
1230 searchInfo
->f
.dataPhysicalLength
= *((off_t
*)attributeBuffer
);
1231 ++((off_t
*)attributeBuffer
);
1233 if ( a
& ATTR_FILE_RSRCLENGTH
) {
1234 searchInfo
->f
.resourceLogicalLength
= *((off_t
*)attributeBuffer
);
1235 ++((off_t
*)attributeBuffer
);
1237 if ( a
& ATTR_FILE_RSRCALLOCSIZE
) {
1238 searchInfo
->f
.resourcePhysicalLength
= *((off_t
*)attributeBuffer
);
1239 ++((off_t
*)attributeBuffer
);
1247 #if 1 // Installer workaround (2940423)
1248 /* this routine was added as part of the work around where some installers would fail */
1249 /* because they incorrectly assumed search results were in some kind of order. */
1250 /* This routine is used to indentify the problematic target. At this point we */
1251 /* only know of one. This routine could be modified for more (I hope not). */
1252 static Boolean
IsTargetName( searchinfospec_t
* searchInfoPtr
, Boolean isHFSPlus
)
1254 if ( searchInfoPtr
->name
== NULL
)
1258 HFSUniStr255 myName
= {
1259 7, /* number of unicode characters */
1261 'L','i','b','r','a','r','y'
1264 if ( FastUnicodeCompare( myName
.unicode
, myName
.length
,
1265 (UniChar
*)searchInfoPtr
->name
,
1266 searchInfoPtr
->nameLength
) == 0 ) {
1271 u_char myName
[32] = {
1272 0x07,'L','i','b','r','a','r','y'
1274 if ( FastRelString(myName
, (u_char
*)searchInfoPtr
->name
) == 0 ) {
1280 } /* IsTargetName */
1281 #endif // Installer workaround