2 * Copyright (c) 1997-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
39 #include <mach/machine/vm_types.h>
40 #include <sys/vnode.h>
41 #include <sys/malloc.h>
42 #include <sys/signalvar.h>
44 #include <sys/utfconv.h>
45 #include <sys/kauth.h>
46 #include <sys/vnode_internal.h>
50 #include "hfs_catalog.h"
51 #include "hfs_attrlist.h"
52 #include "hfs_endian.h"
54 #include "hfscommon/headers/FileMgrInternal.h"
55 #include "hfscommon/headers/HFSUnicodeWrappers.h"
56 #include "hfscommon/headers/BTreesPrivate.h"
57 #include "hfscommon/headers/BTreeScanner.h"
58 #include "hfscommon/headers/CatalogPrivate.h"
60 /* Search criterea. */
61 struct directoryInfoSpec
68 off_t dataLogicalLength
;
69 off_t dataPhysicalLength
;
70 off_t resourceLogicalLength
;
71 off_t resourcePhysicalLength
;
76 u_char name
[kHFSPlusMaxFileNameBytes
];
78 char attributes
; // see IM:Files 2-100
81 struct timespec creationDate
;
82 struct timespec modificationDate
;
83 struct timespec changeDate
;
84 struct timespec accessDate
;
85 struct timespec lastBackupDate
;
86 uint8_t finderInfo
[32];
90 struct fileInfoSpec f
;
91 struct directoryInfoSpec d
;
93 typedef struct searchinfospec searchinfospec_t
;
95 static void ResolveHardlink(ExtendedVCB
*vcb
, HFSPlusCatalogFile
*recp
);
98 static int UnpackSearchAttributeBlock(struct hfsmount
*hfsmp
, struct attrlist
*alist
,
99 searchinfospec_t
*searchInfo
, void *attributeBuffer
);
101 static int CheckCriteria( ExtendedVCB
*vcb
,
103 struct attrlist
*attrList
,
106 searchinfospec_t
*searchInfo1
,
107 searchinfospec_t
*searchInfo2
,
108 Boolean lookForDup
);
110 static int CheckAccess(ExtendedVCB
*vcb
, u_long searchBits
, CatalogKey
*key
, struct vfs_context
*ctx
);
112 static int InsertMatch(struct hfsmount
*hfsmp
, uio_t a_uio
, CatalogRecord
*rec
,
113 CatalogKey
*key
, struct attrlist
*returnAttrList
,
114 void *attributesBuffer
, void *variableBuffer
,
115 u_long
* nummatches
);
117 static Boolean
CompareRange(u_long val
, u_long low
, u_long high
);
118 static Boolean
CompareWideRange(u_int64_t val
, u_int64_t low
, u_int64_t high
);
120 static Boolean
CompareRange( u_long val
, u_long low
, u_long high
)
122 return( (val
>= low
) && (val
<= high
) );
125 static Boolean
CompareWideRange( u_int64_t val
, u_int64_t low
, u_int64_t high
)
127 return( (val
>= low
) && (val
<= high
) );
129 //#define CompareRange(val, low, high) ((val >= low) && (val <= high))
131 #if 1 // Installer workaround (2940423)
132 static Boolean
IsTargetName( searchinfospec_t
* searchInfoPtr
, Boolean isHFSPlus
);
133 #endif // Installer workaround
135 __private_extern__
int hfs_vnop_search(struct vnop_searchfs_args
*ap
);
138 /************************************************************************/
139 /* Entry for searchfs() */
140 /************************************************************************/
142 #define errSearchBufferFull 101 /* Internal search errors */
151 IN kauth_cred_t cred;
159 struct vnop_searchfs_args
*ap
; /*
160 struct vnodeop_desc *a_desc;
162 void *a_searchparams1;
163 void *a_searchparams2;
164 struct attrlist *a_searchattrs;
166 struct timeval *a_timelimit;
167 struct attrlist *a_returnattrs;
168 u_long *a_nummatches;
172 struct searchstate *a_searchstate;
173 vfs_context_t a_context;
176 ExtendedVCB
*vcb
= VTOVCB(ap
->a_vp
);
177 struct hfsmount
*hfsmp
;
179 searchinfospec_t searchInfo1
;
180 searchinfospec_t searchInfo2
;
181 void *attributesBuffer
;
182 void *variableBuffer
;
183 u_long fixedBlockSize
;
184 u_long eachReturnBufferSize
;
185 struct proc
*p
= proc_self();
188 int timerExpired
= false;
189 int doQuickExit
= false;
190 CatalogKey
* myCurrentKeyPtr
;
191 CatalogRecord
* myCurrentDataPtr
;
192 CatPosition
* myCatPositionPtr
;
193 BTScanState myBTScanState
;
194 user_addr_t user_start
= 0;
195 user_size_t user_len
= 0;
199 /* XXX Parameter check a_searchattrs? */
201 *(ap
->a_nummatches
) = 0;
203 if (ap
->a_options
& ~SRCHFS_VALIDOPTIONSMASK
)
206 /* SRCHFS_SKIPLINKS requires root access.
207 * This option cannot be used with either
208 * the ATTR_CMN_NAME or ATTR_CMN_PAROBJID
211 if (ap
->a_options
& SRCHFS_SKIPLINKS
) {
214 attrs
= ap
->a_searchattrs
->commonattr
| ap
->a_returnattrs
->commonattr
;
215 if (attrs
& (ATTR_CMN_NAME
| ATTR_CMN_PAROBJID
))
217 if ((err
= vfs_context_suser(ap
->a_context
)))
221 if (uio_resid(ap
->a_uio
) <= 0)
224 isHFSPlus
= (vcb
->vcbSigWord
== kHFSPlusSigWord
);
225 hfsmp
= VTOHFS(ap
->a_vp
);
227 searchTime
= kMaxMicroSecsInKernel
;
228 if (ap
->a_timelimit
->tv_sec
== 0 &&
229 ap
->a_timelimit
->tv_usec
> 0 &&
230 ap
->a_timelimit
->tv_usec
< kMaxMicroSecsInKernel
) {
231 searchTime
= ap
->a_timelimit
->tv_usec
;
234 /* UnPack the search boundries, searchInfo1, searchInfo2 */
235 err
= UnpackSearchAttributeBlock(hfsmp
, ap
->a_searchattrs
,
236 &searchInfo1
, ap
->a_searchparams1
);
238 err
= UnpackSearchAttributeBlock(hfsmp
, ap
->a_searchattrs
,
239 &searchInfo2
, ap
->a_searchparams2
);
242 fixedBlockSize
= sizeof(uint32_t) + hfs_attrblksize(ap
->a_returnattrs
); /* uint32_t for length word */
244 eachReturnBufferSize
= fixedBlockSize
;
246 if ( ap
->a_returnattrs
->commonattr
& ATTR_CMN_NAME
) /* XXX should be more robust! */
247 eachReturnBufferSize
+= kHFSPlusMaxFileNameBytes
+ 1;
249 MALLOC( attributesBuffer
, void *, eachReturnBufferSize
, M_TEMP
, M_WAITOK
);
250 variableBuffer
= (void*)((char*) attributesBuffer
+ fixedBlockSize
);
252 // XXXdbg - have to lock the user's buffer so we don't fault
253 // while holding the shared catalog file lock. see the comment
254 // in hfs_readdir() for more details.
256 if (hfsmp
->jnl
&& uio_isuserspace(ap
->a_uio
)) {
257 user_start
= uio_curriovbase(ap
->a_uio
);
258 user_len
= uio_curriovlen(ap
->a_uio
);
260 if ((err
= vslock(user_start
, user_len
)) != 0) {
262 goto ExitThisRoutine
;
266 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
268 catalogFCB
= GetFileControlBlock(vcb
->catalogRefNum
);
269 myCurrentKeyPtr
= NULL
;
270 myCurrentDataPtr
= NULL
;
271 myCatPositionPtr
= (CatPosition
*)ap
->a_searchstate
;
273 if (ap
->a_options
& SRCHFS_START
) {
274 /* Starting a new search. */
275 /* Make sure the on-disk Catalog file is current */
276 (void) hfs_fsync(vcb
->catalogRefNum
, MNT_WAIT
, 0, p
);
278 hfs_systemfile_unlock(hfsmp
, lockflags
);
279 journal_flush(hfsmp
->jnl
);
280 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
283 ap
->a_options
&= ~SRCHFS_START
;
284 bzero( (caddr_t
)myCatPositionPtr
, sizeof( *myCatPositionPtr
) );
285 err
= BTScanInitialize(catalogFCB
, 0, 0, 0, kCatSearchBufferSize
, &myBTScanState
);
287 #if 1 // Installer workaround (2940423)
288 // hack to get around installer problems when the installer expects search results
289 // to be in key order. At this point the problem appears to be limited to
290 // searches for "Library". The idea here is to go get the "Library" at root
291 // and return it first to the caller then continue the search as normal with
292 // the exception of taking care not to return a duplicate hit (see CheckCriteria)
293 if ( err
== E_NONE
&&
294 (ap
->a_searchattrs
->commonattr
& ATTR_CMN_NAME
) != 0 &&
295 IsTargetName( &searchInfo1
, isHFSPlus
) )
298 BTreeIterator iterator
;
299 FSBufferDescriptor btrec
;
304 bzero( (caddr_t
)&iterator
, sizeof( iterator
) );
305 keyp
= (CatalogKey
*) &iterator
.key
;
306 (void) BuildCatalogKeyUTF8(vcb
, kRootDirID
, "Library", kUndefinedStrLen
, keyp
, NULL
);
308 btrec
.bufferAddress
= &rec
;
310 btrec
.itemSize
= sizeof( rec
);
312 result
= BTSearchRecord( catalogFCB
, &iterator
, &btrec
, &reclen
, &iterator
);
313 if ( result
== E_NONE
) {
314 // need to unlock since CheckAccess assumes no lock held
315 hfs_systemfile_unlock(hfsmp
, lockflags
);
316 if (CheckCriteria(vcb
, ap
->a_options
, ap
->a_searchattrs
, &rec
,
317 keyp
, &searchInfo1
, &searchInfo2
, false) &&
318 CheckAccess(vcb
, ap
->a_options
, keyp
, ap
->a_context
)) {
320 result
= InsertMatch(hfsmp
, ap
->a_uio
, &rec
,
321 keyp
, ap
->a_returnattrs
,
322 attributesBuffer
, variableBuffer
,
324 if (result
== E_NONE
&& *(ap
->a_nummatches
) >= ap
->a_maxmatches
)
327 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
330 #endif // Installer workaround
332 /* Resuming a search. */
333 err
= BTScanInitialize(catalogFCB
, myCatPositionPtr
->nextNode
,
334 myCatPositionPtr
->nextRecord
,
335 myCatPositionPtr
->recordsFound
,
336 kCatSearchBufferSize
,
338 /* Make sure Catalog hasn't changed. */
340 && myCatPositionPtr
->writeCount
!= myBTScanState
.btcb
->writeCount
) {
341 myCatPositionPtr
->writeCount
= myBTScanState
.btcb
->writeCount
;
342 err
= EBUSY
; /* catChangedErr */
345 hfs_systemfile_unlock(hfsmp
, lockflags
);
348 goto ExitThisRoutine
;
349 #if 1 // Installer workaround (2940423)
352 #endif // Installer workaround
355 * Check all the catalog btree records...
356 * return the attributes for matching items
359 struct timeval myCurrentTime
;
360 struct timeval myElapsedTime
;
362 err
= BTScanNextRecord(&myBTScanState
, timerExpired
,
363 (void **)&myCurrentKeyPtr
, (void **)&myCurrentDataPtr
,
368 /* Resolve any hardlinks */
369 if (isHFSPlus
&& (ap
->a_options
& SRCHFS_SKIPLINKS
) == 0) {
370 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
371 ResolveHardlink(vcb
, (HFSPlusCatalogFile
*) myCurrentDataPtr
);
372 hfs_systemfile_unlock(hfsmp
, lockflags
);
374 if (CheckCriteria( vcb
, ap
->a_options
, ap
->a_searchattrs
, myCurrentDataPtr
,
375 myCurrentKeyPtr
, &searchInfo1
, &searchInfo2
, true )
376 && CheckAccess(vcb
, ap
->a_options
, myCurrentKeyPtr
, ap
->a_context
)) {
377 err
= InsertMatch(hfsmp
, ap
->a_uio
, myCurrentDataPtr
,
378 myCurrentKeyPtr
, ap
->a_returnattrs
,
379 attributesBuffer
, variableBuffer
, ap
->a_nummatches
);
382 * The last match didn't fit so come back
383 * to this record on the next trip.
385 --myBTScanState
.recordsFound
;
386 --myBTScanState
.recordNum
;
390 if (*(ap
->a_nummatches
) >= ap
->a_maxmatches
)
395 * Check our elapsed time and bail if we've hit the max.
396 * The idea here is to throttle the amount of time we
397 * spend in the kernel.
399 microuptime(&myCurrentTime
);
400 timersub(&myCurrentTime
, &myBTScanState
.startTime
, &myElapsedTime
);
401 /* Note: assumes kMaxMicroSecsInKernel is less than 1,000,000 */
402 if (myElapsedTime
.tv_sec
> 0
403 || myElapsedTime
.tv_usec
>= searchTime
) {
409 /* Update catalog position */
410 myCatPositionPtr
->writeCount
= myBTScanState
.btcb
->writeCount
;
412 BTScanTerminate(&myBTScanState
, &myCatPositionPtr
->nextNode
,
413 &myCatPositionPtr
->nextRecord
,
414 &myCatPositionPtr
->recordsFound
);
416 if ( err
== E_NONE
) {
417 err
= EAGAIN
; /* signal to the user to call searchfs again */
418 } else if ( err
== errSearchBufferFull
) {
419 if ( *(ap
->a_nummatches
) > 0 )
423 } else if ( err
== btNotFound
) {
424 err
= E_NONE
; /* the entire disk has been searched */
425 } else if ( err
== fsBTTimeOutErr
) {
430 FREE( attributesBuffer
, M_TEMP
);
432 if (hfsmp
->jnl
&& user_start
) {
433 vsunlock(user_start
, user_len
, TRUE
);
436 return (MacToVFSError(err
));
441 ResolveHardlink(ExtendedVCB
*vcb
, HFSPlusCatalogFile
*recp
)
443 if ((recp
->recordType
== kHFSPlusFileRecord
)
444 && (SWAP_BE32(recp
->userInfo
.fdType
) == kHardLinkFileType
)
445 && (SWAP_BE32(recp
->userInfo
.fdCreator
) == kHFSPlusCreator
)
446 && ((to_bsd_time(recp
->createDate
) == vcb
->vcbCrDate
) ||
447 (to_bsd_time(recp
->createDate
) == VCBTOHFS(vcb
)->hfs_metadata_createdate
))) {
450 /* Export link's cnid (a unique value) instead of inode's cnid */
451 saved_cnid
= recp
->fileID
;
452 (void) resolvelink(VCBTOHFS(vcb
), recp
->bsdInfo
.special
.iNodeNum
, recp
);
453 recp
->fileID
= saved_cnid
;
459 CompareMasked(const UInt32
*thisValue
, const UInt32
*compareData
,
460 const UInt32
*compareMask
, UInt32 count
)
465 matched
= true; /* Assume it will all match */
467 for (i
=0; i
<count
; i
++) {
468 if (((*thisValue
++ ^ *compareData
++) & *compareMask
++) != 0) {
479 ComparePartialUnicodeName (register ConstUniCharArrayPtr str
, register ItemCount s_len
,
480 register ConstUniCharArrayPtr find
, register ItemCount f_len
)
482 if (f_len
== 0 || s_len
== 0)
488 } while (FastUnicodeCompare(str
++, f_len
, find
, f_len
) != 0);
495 ComparePartialPascalName ( register ConstStr31Param str
, register ConstStr31Param find
)
497 register u_char s_len
= str
[0];
498 register u_char f_len
= find
[0];
499 register u_char
*tsp
;
502 if (f_len
== 0 || s_len
== 0)
505 bcopy(str
, tmpstr
, s_len
+ 1);
508 while (s_len
-- >= f_len
) {
511 if (FastRelString(tsp
++, find
) == 0)
520 // Determine if a name is "inappropriate" where the definition
521 // of "inappropriate" is up to higher level execs. Currently
522 // that's limited to /System.
525 is_inappropriate_name(char *name
, int len
)
527 const char *bad_names
[] = { "System" };
528 int bad_len
[] = { 6 };
531 for(i
=0; i
< (int) (sizeof(bad_names
) / sizeof(bad_names
[0])); i
++) {
532 if (len
== bad_len
[i
] && strcmp(name
, bad_names
[i
]) == 0) {
537 // if we get here, no name matched
544 * Check to see if caller has access rights to this item
548 CheckAccess(ExtendedVCB
*theVCBPtr
, u_long searchBits
, CatalogKey
*theKeyPtr
, struct vfs_context
*ctx
)
553 HFSCatalogNodeID myNodeID
;
555 struct FndrDirInfo
*finfop
;
556 struct vnode
* vp
= NULL
;
558 myResult
= 0; /* default to "no access" */
560 if (!vfs_context_suser(ctx
)) {
561 myResult
= 1; /* allow access */
562 goto ExitThisRoutine
; /* root always has access */
565 hfsmp
= VCBTOHFS( theVCBPtr
);
566 isHFSPlus
= ( theVCBPtr
->vcbSigWord
== kHFSPlusSigWord
);
568 myNodeID
= theKeyPtr
->hfsPlus
.parentID
;
570 myNodeID
= theKeyPtr
->hfs
.parentID
;
572 while ( myNodeID
>= kRootDirID
) {
575 /* now go get catalog data for this directory */
576 myErr
= hfs_vget(hfsmp
, myNodeID
, &vp
, 0);
578 goto ExitThisRoutine
; /* no access */
582 finfop
= (struct FndrDirInfo
*)&cp
->c_attr
.ca_finderinfo
[0];
584 if ( searchBits
& SRCHFS_SKIPPACKAGES
) {
585 if ( (SWAP_BE16(finfop
->frFlags
) & kHasBundle
)
586 || (cp
->c_desc
.cd_nameptr
!= NULL
587 && is_package_name(cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
)) ) {
589 goto ExitThisRoutine
;
593 if ( searchBits
& SRCHFS_SKIPINAPPROPRIATE
) {
594 if ( cp
->c_parentcnid
== kRootDirID
&& cp
->c_desc
.cd_nameptr
!= NULL
&&
595 is_inappropriate_name(cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
) ) {
597 goto ExitThisRoutine
;
601 if ( (searchBits
& SRCHFS_SKIPINVISIBLE
) &&
602 (SWAP_BE16(finfop
->frFlags
) & kIsInvisible
) ) {
604 goto ExitThisRoutine
;
607 myNodeID
= cp
->c_parentcnid
; /* move up the hierarchy */
608 hfs_unlock(VTOC(vp
));
609 if (vp
->v_type
== VDIR
) {
610 myErr
= vnode_authorize(vp
, NULL
, (KAUTH_VNODE_SEARCH
| KAUTH_VNODE_LIST_DIRECTORY
), ctx
);
612 myErr
= vnode_authorize(vp
, NULL
, (KAUTH_VNODE_SEARCH
), ctx
);
617 goto ExitThisRoutine
; /* no access */
620 myResult
= 1; /* allow access */
624 hfs_unlock(VTOC(vp
));
632 CheckCriteria( ExtendedVCB
*vcb
,
634 struct attrlist
*attrList
,
637 searchinfospec_t
*searchInfo1
,
638 searchinfospec_t
*searchInfo2
,
641 Boolean matched
, atleastone
;
643 attrgroup_t searchAttributes
;
644 struct cat_attr c_attr
;
645 struct cat_fork datafork
;
646 struct cat_fork rsrcfork
;
648 bzero(&c_attr
, sizeof(c_attr
));
649 isHFSPlus
= (vcb
->vcbSigWord
== kHFSPlusSigWord
);
651 switch (rec
->recordType
) {
652 case kHFSFolderRecord
:
653 case kHFSPlusFolderRecord
:
654 if ( (searchBits
& SRCHFS_MATCHDIRS
) == 0 ) { /* If we are NOT searching folders */
661 if ( (searchBits
& SRCHFS_MATCHFILES
) == 0 ) { /* If we are NOT searching files */
667 case kHFSPlusFileRecord
:
668 /* Check if hardlink links should be skipped. */
669 if (searchBits
& SRCHFS_SKIPLINKS
) {
670 cnid_t parid
= key
->hfsPlus
.parentID
;
671 HFSPlusCatalogFile
*filep
= (HFSPlusCatalogFile
*)rec
;
673 if ((SWAP_BE32(filep
->userInfo
.fdType
) == kHardLinkFileType
) &&
674 (SWAP_BE32(filep
->userInfo
.fdCreator
) == kHFSPlusCreator
)) {
675 return (false); /* skip over link records */
676 } else if ((parid
== VCBTOHFS(vcb
)->hfs_privdir_desc
.cd_cnid
) &&
677 (filep
->bsdInfo
.special
.linkCount
== 0)) {
678 return (false); /* skip over unlinked files */
680 } else if (key
->hfsPlus
.parentID
== VCBTOHFS(vcb
)->hfs_privdir_desc
.cd_cnid
) {
681 return (false); /* skip over private files */
684 if ( (searchBits
& SRCHFS_MATCHFILES
) == 0 ) { /* If we are NOT searching files */
690 default: /* Never match a thread record or any other type. */
691 return( false ); /* Not a file or folder record, so can't search it */
694 matched
= true; /* Assume we got a match */
695 atleastone
= false; /* Dont insert unless we match at least one criteria */
697 /* First, attempt to match the name -- either partial or complete */
698 if ( attrList
->commonattr
& ATTR_CMN_NAME
) {
700 /* Check for partial/full HFS Plus name match */
702 if ( searchBits
& SRCHFS_MATCHPARTIALNAMES
) {
703 matched
= ComparePartialUnicodeName(key
->hfsPlus
.nodeName
.unicode
,
704 key
->hfsPlus
.nodeName
.length
,
705 (UniChar
*)searchInfo1
->name
,
706 searchInfo1
->nameLength
);
707 } else /* full HFS Plus name match */ {
708 matched
= (FastUnicodeCompare(key
->hfsPlus
.nodeName
.unicode
,
709 key
->hfsPlus
.nodeName
.length
,
710 (UniChar
*)searchInfo1
->name
,
711 searchInfo1
->nameLength
) == 0);
714 /* Check for partial/full HFS name match */
716 if ( searchBits
& SRCHFS_MATCHPARTIALNAMES
)
717 matched
= ComparePartialPascalName(key
->hfs
.nodeName
, (u_char
*)searchInfo1
->name
);
718 else /* full HFS name match */
719 matched
= (FastRelString(key
->hfs
.nodeName
, (u_char
*)searchInfo1
->name
) == 0);
722 #if 1 // Installer workaround (2940423)
724 HFSCatalogNodeID parentID
;
726 parentID
= key
->hfsPlus
.parentID
;
728 parentID
= key
->hfs
.parentID
;
730 if ( matched
&& parentID
== kRootDirID
&&
731 IsTargetName( searchInfo1
, isHFSPlus
) )
734 #endif // Installer workaround
736 if ( matched
== false || (searchBits
& ~SRCHFS_MATCHPARTIALNAMES
) == 0 )
737 goto TestDone
; /* no match, or nothing more to compare */
742 /* Convert catalog record into cat_attr format. */
743 cat_convertattr(VCBTOHFS(vcb
), rec
, &c_attr
, &datafork
, &rsrcfork
);
745 if (searchBits
& SRCHFS_SKIPINVISIBLE
) {
748 switch (rec
->recordType
) {
749 case kHFSFolderRecord
:
750 case kHFSPlusFolderRecord
: {
751 struct FndrDirInfo
*finder_info
;
753 finder_info
= (struct FndrDirInfo
*)&c_attr
.ca_finderinfo
[0];
754 flags
= SWAP_BE16(finder_info
->frFlags
);
759 case kHFSPlusFileRecord
: {
760 struct FndrFileInfo
*finder_info
;
762 finder_info
= (struct FndrFileInfo
*)&c_attr
.ca_finderinfo
[0];
763 flags
= SWAP_BE16(finder_info
->fdFlags
);
768 flags
= kIsInvisible
;
773 if (flags
& kIsInvisible
) {
781 /* Now that we have a record worth searching, see if it matches the search attributes */
782 if (rec
->recordType
== kHFSFileRecord
||
783 rec
->recordType
== kHFSPlusFileRecord
) {
784 if ((attrList
->fileattr
& ~ATTR_FILE_VALIDMASK
) != 0) { /* attr we do know about */
788 else if ((attrList
->fileattr
& ATTR_FILE_VALIDMASK
) != 0) {
789 searchAttributes
= attrList
->fileattr
;
791 /* File logical length (data fork) */
792 if ( searchAttributes
& ATTR_FILE_DATALENGTH
) {
793 matched
= CompareWideRange(
795 searchInfo1
->f
.dataLogicalLength
,
796 searchInfo2
->f
.dataLogicalLength
);
797 if (matched
== false) goto TestDone
;
801 /* File physical length (data fork) */
802 if ( searchAttributes
& ATTR_FILE_DATAALLOCSIZE
) {
803 matched
= CompareWideRange(
804 (u_int64_t
)datafork
.cf_blocks
* (u_int64_t
)vcb
->blockSize
,
805 searchInfo1
->f
.dataPhysicalLength
,
806 searchInfo2
->f
.dataPhysicalLength
);
807 if (matched
== false) goto TestDone
;
811 /* File logical length (resource fork) */
812 if ( searchAttributes
& ATTR_FILE_RSRCLENGTH
) {
813 matched
= CompareWideRange(
815 searchInfo1
->f
.resourceLogicalLength
,
816 searchInfo2
->f
.resourceLogicalLength
);
817 if (matched
== false) goto TestDone
;
821 /* File physical length (resource fork) */
822 if ( searchAttributes
& ATTR_FILE_RSRCALLOCSIZE
) {
823 matched
= CompareWideRange(
824 (u_int64_t
)rsrcfork
.cf_blocks
* (u_int64_t
)vcb
->blockSize
,
825 searchInfo1
->f
.resourcePhysicalLength
,
826 searchInfo2
->f
.resourcePhysicalLength
);
827 if (matched
== false) goto TestDone
;
832 atleastone
= true; /* to match SRCHFS_MATCHFILES */
836 * Check the directory attributes
838 else if (rec
->recordType
== kHFSFolderRecord
||
839 rec
->recordType
== kHFSPlusFolderRecord
) {
840 if ((attrList
->dirattr
& ~ATTR_DIR_VALIDMASK
) != 0) { /* attr we do know about */
844 else if ((attrList
->dirattr
& ATTR_DIR_VALIDMASK
) != 0) {
845 searchAttributes
= attrList
->dirattr
;
847 /* Directory valence */
848 if ( searchAttributes
& ATTR_DIR_ENTRYCOUNT
) {
849 matched
= CompareRange(c_attr
.ca_entries
,
850 searchInfo1
->d
.numFiles
,
851 searchInfo2
->d
.numFiles
);
852 if (matched
== false) goto TestDone
;
857 atleastone
= true; /* to match SRCHFS_MATCHDIRS */
862 * Check the common attributes
864 searchAttributes
= attrList
->commonattr
;
865 if ( (searchAttributes
& ATTR_CMN_VALIDMASK
) != 0 ) {
867 if ( searchAttributes
& ATTR_CMN_OBJID
) {
868 matched
= CompareRange(c_attr
.ca_fileid
,
870 searchInfo2
->nodeID
);
871 if (matched
== false) goto TestDone
;
876 if ( searchAttributes
& ATTR_CMN_PAROBJID
) {
877 HFSCatalogNodeID parentID
;
880 parentID
= key
->hfsPlus
.parentID
;
882 parentID
= key
->hfs
.parentID
;
884 matched
= CompareRange(parentID
, searchInfo1
->parentDirID
,
885 searchInfo2
->parentDirID
);
886 if (matched
== false) goto TestDone
;
890 /* Finder Info & Extended Finder Info where extFinderInfo is last 32 bytes */
891 if ( searchAttributes
& ATTR_CMN_FNDRINFO
) {
893 thisValue
= (UInt32
*) &c_attr
.ca_finderinfo
;
896 * Note: ioFlFndrInfo and ioDrUsrWds have the same offset in search info, so
897 * no need to test the object type here.
899 matched
= CompareMasked(thisValue
,
900 (UInt32
*)&searchInfo1
->finderInfo
,
901 (UInt32
*) &searchInfo2
->finderInfo
, 8);
902 if (matched
== false) goto TestDone
;
907 if ( searchAttributes
& ATTR_CMN_CRTIME
) {
908 matched
= CompareRange(c_attr
.ca_itime
,
909 searchInfo1
->creationDate
.tv_sec
,
910 searchInfo2
->creationDate
.tv_sec
);
911 if (matched
== false) goto TestDone
;
916 if ( searchAttributes
& ATTR_CMN_MODTIME
) {
917 matched
= CompareRange(c_attr
.ca_mtime
,
918 searchInfo1
->modificationDate
.tv_sec
,
919 searchInfo2
->modificationDate
.tv_sec
);
920 if (matched
== false) goto TestDone
;
925 if ( searchAttributes
& ATTR_CMN_CHGTIME
) {
926 matched
= CompareRange(c_attr
.ca_ctime
,
927 searchInfo1
->changeDate
.tv_sec
,
928 searchInfo2
->changeDate
.tv_sec
);
929 if (matched
== false) goto TestDone
;
934 if ( searchAttributes
& ATTR_CMN_ACCTIME
) {
935 matched
= CompareRange(c_attr
.ca_atime
,
936 searchInfo1
->accessDate
.tv_sec
,
937 searchInfo2
->accessDate
.tv_sec
);
938 if (matched
== false) goto TestDone
;
943 if ( searchAttributes
& ATTR_CMN_BKUPTIME
) {
944 matched
= CompareRange(c_attr
.ca_btime
,
945 searchInfo1
->lastBackupDate
.tv_sec
,
946 searchInfo2
->lastBackupDate
.tv_sec
);
947 if (matched
== false) goto TestDone
;
952 if ( searchAttributes
& ATTR_CMN_OWNERID
) {
953 matched
= CompareRange(c_attr
.ca_uid
,
954 searchInfo1
->uid
, searchInfo2
->uid
);
955 if (matched
== false) goto TestDone
;
960 if ( searchAttributes
& ATTR_CMN_GRPID
) {
961 matched
= CompareRange(c_attr
.ca_gid
,
962 searchInfo1
->gid
, searchInfo2
->gid
);
963 if (matched
== false) goto TestDone
;
968 if ( searchAttributes
& ATTR_CMN_ACCESSMASK
) {
969 matched
= CompareRange((uint32_t)c_attr
.ca_mode
,
970 (uint32_t)searchInfo1
->mask
,
971 (uint32_t)searchInfo2
->mask
);
972 if (matched
== false) goto TestDone
;
977 /* If we got here w/o matching any, then set to false */
983 * Finally, determine whether we need to negate the sense of the match
984 * (i.e. find all objects that DON'T match).
986 if ( searchBits
& SRCHFS_NEGATEPARAMS
)
994 * Adds another record to the packed array for output
997 InsertMatch(struct hfsmount
*hfsmp
, uio_t a_uio
, CatalogRecord
*rec
,
998 CatalogKey
*key
, struct attrlist
*returnAttrList
,
999 void *attributesBuffer
, void *variableBuffer
, u_long
* nummatches
)
1002 void *rovingAttributesBuffer
;
1003 void *rovingVariableBuffer
;
1004 u_long packedBufferSize
;
1005 u_long privateDir
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
1006 struct attrblock attrblk
;
1007 struct cat_desc c_desc
;
1008 struct cat_attr c_attr
;
1009 struct cat_fork datafork
;
1010 struct cat_fork rsrcfork
;
1012 bzero(&c_desc
, sizeof(c_desc
));
1013 bzero(&c_attr
, sizeof(c_attr
));
1014 rovingAttributesBuffer
= (char*)attributesBuffer
+ sizeof(u_long
); /* Reserve space for length field */
1015 rovingVariableBuffer
= variableBuffer
;
1017 /* Convert catalog record into cat_attr format. */
1018 cat_convertattr(hfsmp
, rec
, &c_attr
, &datafork
, &rsrcfork
);
1020 /* hide our private meta data directory */
1021 if ((privateDir
!= 0) && (c_attr
.ca_fileid
== privateDir
)) {
1026 /* Hide the private journal files */
1028 ((c_attr
.ca_fileid
== hfsmp
->hfs_jnlfileid
) ||
1029 (c_attr
.ca_fileid
== hfsmp
->hfs_jnlinfoblkid
))) {
1034 if (returnAttrList
->commonattr
& ATTR_CMN_NAME
) {
1035 cat_convertkey(hfsmp
, key
, rec
, &c_desc
);
1037 c_desc
.cd_cnid
= c_attr
.ca_fileid
;
1038 if (hfsmp
->hfs_flags
& HFS_STANDARD
)
1039 c_desc
.cd_parentcnid
= key
->hfs
.parentID
;
1041 c_desc
.cd_parentcnid
= key
->hfsPlus
.parentID
;
1044 attrblk
.ab_attrlist
= returnAttrList
;
1045 attrblk
.ab_attrbufpp
= &rovingAttributesBuffer
;
1046 attrblk
.ab_varbufpp
= &rovingVariableBuffer
;
1047 attrblk
.ab_flags
= 0;
1048 attrblk
.ab_blocksize
= 0;
1050 hfs_packattrblk(&attrblk
, hfsmp
, NULL
, &c_desc
, &c_attr
, &datafork
, &rsrcfork
, current_proc());
1052 packedBufferSize
= (char*)rovingVariableBuffer
- (char*)attributesBuffer
;
1054 if ( packedBufferSize
> uio_resid(a_uio
) )
1055 return( errSearchBufferFull
);
1059 *((u_long
*)attributesBuffer
) = packedBufferSize
; /* Store length of fixed + var block */
1061 err
= uiomove( (caddr_t
)attributesBuffer
, packedBufferSize
, a_uio
); /* XXX should be packedBufferSize */
1063 cat_releasedesc(&c_desc
);
1070 UnpackSearchAttributeBlock( struct hfsmount
*hfsmp
, struct attrlist
*alist
, searchinfospec_t
*searchInfo
, void *attributeBuffer
)
1074 boolean_t is_64_bit
;
1076 DBG_ASSERT(searchInfo
!= NULL
);
1078 is_64_bit
= proc_is64bit(current_proc());
1080 bufferSize
= *((uint32_t *)attributeBuffer
);
1081 if (bufferSize
== 0)
1082 return (EINVAL
); /* XXX -DJB is a buffer size of zero ever valid for searchfs? */
1084 ++((uint32_t *)attributeBuffer
); /* advance past the size */
1087 * UnPack common attributes
1089 a
= alist
->commonattr
;
1091 if ( a
& ATTR_CMN_NAME
) {
1095 s
= (char*) attributeBuffer
+ ((attrreference_t
*) attributeBuffer
)->attr_dataoffset
;
1096 len
= ((attrreference_t
*) attributeBuffer
)->attr_length
;
1098 if (len
> sizeof(searchInfo
->name
))
1101 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1102 /* Convert name to pascal string to match HFS B-Tree names */
1105 if (utf8_to_hfs(HFSTOVCB(hfsmp
), len
-1, s
, (u_char
*)searchInfo
->name
) != 0)
1108 searchInfo
->nameLength
= searchInfo
->name
[0];
1110 searchInfo
->name
[0] = searchInfo
->nameLength
= 0;
1112 ++((attrreference_t
*)attributeBuffer
);
1115 /* Convert name to Unicode to match HFS Plus B-Tree names */
1118 if (utf8_decodestr(s
, len
-1, (UniChar
*)searchInfo
->name
, &ucslen
,
1119 sizeof(searchInfo
->name
), ':', UTF_DECOMPOSED
))
1122 searchInfo
->nameLength
= ucslen
/ sizeof(UniChar
);
1124 searchInfo
->nameLength
= 0;
1126 ++((attrreference_t
*)attributeBuffer
);
1129 if ( a
& ATTR_CMN_OBJID
) {
1130 searchInfo
->nodeID
= ((fsobj_id_t
*) attributeBuffer
)->fid_objno
; /* ignore fid_generation */
1131 ++((fsobj_id_t
*)attributeBuffer
);
1133 if ( a
& ATTR_CMN_PAROBJID
) {
1134 searchInfo
->parentDirID
= ((fsobj_id_t
*) attributeBuffer
)->fid_objno
; /* ignore fid_generation */
1135 ++((fsobj_id_t
*)attributeBuffer
);
1137 if ( a
& ATTR_CMN_CRTIME
) {
1139 struct user_timespec tmp
;
1140 tmp
= *((struct user_timespec
*)attributeBuffer
);
1141 searchInfo
->creationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1142 searchInfo
->creationDate
.tv_nsec
= tmp
.tv_nsec
;
1143 ++((struct user_timespec
*)attributeBuffer
);
1146 searchInfo
->creationDate
= *((struct timespec
*)attributeBuffer
);
1147 ++((struct timespec
*)attributeBuffer
);
1150 if ( a
& ATTR_CMN_MODTIME
) {
1152 struct user_timespec tmp
;
1153 tmp
= *((struct user_timespec
*)attributeBuffer
);
1154 searchInfo
->modificationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1155 searchInfo
->modificationDate
.tv_nsec
= tmp
.tv_nsec
;
1156 ++((struct user_timespec
*)attributeBuffer
);
1159 searchInfo
->modificationDate
= *((struct timespec
*)attributeBuffer
);
1160 ++((struct timespec
*)attributeBuffer
);
1163 if ( a
& ATTR_CMN_CHGTIME
) {
1165 struct user_timespec tmp
;
1166 tmp
= *((struct user_timespec
*)attributeBuffer
);
1167 searchInfo
->changeDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1168 searchInfo
->changeDate
.tv_nsec
= tmp
.tv_nsec
;
1169 ++((struct user_timespec
*)attributeBuffer
);
1172 searchInfo
->changeDate
= *((struct timespec
*)attributeBuffer
);
1173 ++((struct timespec
*)attributeBuffer
);
1176 if ( a
& ATTR_CMN_ACCTIME
) {
1178 struct user_timespec tmp
;
1179 tmp
= *((struct user_timespec
*)attributeBuffer
);
1180 searchInfo
->accessDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1181 searchInfo
->accessDate
.tv_nsec
= tmp
.tv_nsec
;
1182 ++((struct user_timespec
*)attributeBuffer
);
1185 searchInfo
->accessDate
= *((struct timespec
*)attributeBuffer
);
1186 ++((struct timespec
*)attributeBuffer
);
1189 if ( a
& ATTR_CMN_BKUPTIME
) {
1191 struct user_timespec tmp
;
1192 tmp
= *((struct user_timespec
*)attributeBuffer
);
1193 searchInfo
->lastBackupDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1194 searchInfo
->lastBackupDate
.tv_nsec
= tmp
.tv_nsec
;
1195 ++((struct user_timespec
*)attributeBuffer
);
1198 searchInfo
->lastBackupDate
= *((struct timespec
*)attributeBuffer
);
1199 ++((struct timespec
*)attributeBuffer
);
1202 if ( a
& ATTR_CMN_FNDRINFO
) {
1203 bcopy( attributeBuffer
, searchInfo
->finderInfo
, sizeof(searchInfo
->finderInfo
) );
1204 (uint8_t *)attributeBuffer
+= 32;
1206 if ( a
& ATTR_CMN_OWNERID
) {
1207 searchInfo
->uid
= *((uid_t
*)attributeBuffer
);
1208 ++((uid_t
*)attributeBuffer
);
1210 if ( a
& ATTR_CMN_GRPID
) {
1211 searchInfo
->gid
= *((gid_t
*)attributeBuffer
);
1212 ++((gid_t
*)attributeBuffer
);
1214 if ( a
& ATTR_CMN_ACCESSMASK
) {
1215 searchInfo
->mask
= *((mode_t
*)attributeBuffer
);
1216 ++((mode_t
*)attributeBuffer
);
1222 if ( a
& ATTR_DIR_ENTRYCOUNT
) {
1223 searchInfo
->d
.numFiles
= *((u_int32_t
*)attributeBuffer
);
1224 ++((u_int32_t
*)attributeBuffer
);
1228 a
= alist
->fileattr
;
1230 if ( a
& ATTR_FILE_DATALENGTH
) {
1231 searchInfo
->f
.dataLogicalLength
= *((off_t
*)attributeBuffer
);
1232 ++((off_t
*)attributeBuffer
);
1234 if ( a
& ATTR_FILE_DATAALLOCSIZE
) {
1235 searchInfo
->f
.dataPhysicalLength
= *((off_t
*)attributeBuffer
);
1236 ++((off_t
*)attributeBuffer
);
1238 if ( a
& ATTR_FILE_RSRCLENGTH
) {
1239 searchInfo
->f
.resourceLogicalLength
= *((off_t
*)attributeBuffer
);
1240 ++((off_t
*)attributeBuffer
);
1242 if ( a
& ATTR_FILE_RSRCALLOCSIZE
) {
1243 searchInfo
->f
.resourcePhysicalLength
= *((off_t
*)attributeBuffer
);
1244 ++((off_t
*)attributeBuffer
);
1252 #if 1 // Installer workaround (2940423)
1253 /* this routine was added as part of the work around where some installers would fail */
1254 /* because they incorrectly assumed search results were in some kind of order. */
1255 /* This routine is used to indentify the problematic target. At this point we */
1256 /* only know of one. This routine could be modified for more (I hope not). */
1257 static Boolean
IsTargetName( searchinfospec_t
* searchInfoPtr
, Boolean isHFSPlus
)
1259 if ( searchInfoPtr
->name
== NULL
)
1263 HFSUniStr255 myName
= {
1264 7, /* number of unicode characters */
1266 'L','i','b','r','a','r','y'
1269 if ( FastUnicodeCompare( myName
.unicode
, myName
.length
,
1270 (UniChar
*)searchInfoPtr
->name
,
1271 searchInfoPtr
->nameLength
) == 0 ) {
1276 u_char myName
[32] = {
1277 0x07,'L','i','b','r','a','r','y'
1279 if ( FastRelString(myName
, (u_char
*)searchInfoPtr
->name
) == 0 ) {
1285 } /* IsTargetName */
1286 #endif // Installer workaround