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 vfs_context
*ctx
);
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
= vfs_context_suser(ap
->a_context
)))
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
, ap
->a_context
)) {
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
, ap
->a_context
)) {
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 vfs_context
*ctx
)
545 HFSCatalogNodeID myNodeID
;
547 struct FndrDirInfo
*finfop
;
548 struct vnode
* vp
= NULL
;
550 myResult
= 0; /* default to "no access" */
552 if (!vfs_context_suser(ctx
)) {
553 myResult
= 1; /* allow access */
554 goto ExitThisRoutine
; /* root always has access */
557 hfsmp
= VCBTOHFS( theVCBPtr
);
558 isHFSPlus
= ( theVCBPtr
->vcbSigWord
== kHFSPlusSigWord
);
560 myNodeID
= theKeyPtr
->hfsPlus
.parentID
;
562 myNodeID
= theKeyPtr
->hfs
.parentID
;
564 while ( myNodeID
>= kRootDirID
) {
567 /* now go get catalog data for this directory */
568 myErr
= hfs_vget(hfsmp
, myNodeID
, &vp
, 0);
570 goto ExitThisRoutine
; /* no access */
574 finfop
= (struct FndrDirInfo
*)&cp
->c_attr
.ca_finderinfo
[0];
576 if ( searchBits
& SRCHFS_SKIPPACKAGES
) {
577 if ( (SWAP_BE16(finfop
->frFlags
) & kHasBundle
)
578 || (cp
->c_desc
.cd_nameptr
!= NULL
579 && is_package_name(cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
)) ) {
581 goto ExitThisRoutine
;
585 if ( searchBits
& SRCHFS_SKIPINAPPROPRIATE
) {
586 if ( cp
->c_parentcnid
== kRootDirID
&& cp
->c_desc
.cd_nameptr
!= NULL
&&
587 is_inappropriate_name(cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
) ) {
589 goto ExitThisRoutine
;
593 if ( (searchBits
& SRCHFS_SKIPINVISIBLE
) &&
594 (SWAP_BE16(finfop
->frFlags
) & kIsInvisible
) ) {
596 goto ExitThisRoutine
;
599 myNodeID
= cp
->c_parentcnid
; /* move up the hierarchy */
600 hfs_unlock(VTOC(vp
));
601 if (vp
->v_type
== VDIR
) {
602 myErr
= vnode_authorize(vp
, NULL
, (KAUTH_VNODE_SEARCH
| KAUTH_VNODE_LIST_DIRECTORY
), ctx
);
604 myErr
= vnode_authorize(vp
, NULL
, (KAUTH_VNODE_SEARCH
), ctx
);
609 goto ExitThisRoutine
; /* no access */
612 myResult
= 1; /* allow access */
616 hfs_unlock(VTOC(vp
));
624 CheckCriteria( ExtendedVCB
*vcb
,
626 struct attrlist
*attrList
,
629 searchinfospec_t
*searchInfo1
,
630 searchinfospec_t
*searchInfo2
,
633 Boolean matched
, atleastone
;
635 attrgroup_t searchAttributes
;
636 struct cat_attr c_attr
;
637 struct cat_fork datafork
;
638 struct cat_fork rsrcfork
;
640 bzero(&c_attr
, sizeof(c_attr
));
641 isHFSPlus
= (vcb
->vcbSigWord
== kHFSPlusSigWord
);
643 switch (rec
->recordType
) {
644 case kHFSFolderRecord
:
645 case kHFSPlusFolderRecord
:
646 if ( (searchBits
& SRCHFS_MATCHDIRS
) == 0 ) { /* If we are NOT searching folders */
653 if ( (searchBits
& SRCHFS_MATCHFILES
) == 0 ) { /* If we are NOT searching files */
659 case kHFSPlusFileRecord
:
660 /* Check if hardlink links should be skipped. */
661 if (searchBits
& SRCHFS_SKIPLINKS
) {
662 cnid_t parid
= key
->hfsPlus
.parentID
;
663 HFSPlusCatalogFile
*filep
= (HFSPlusCatalogFile
*)rec
;
665 if ((SWAP_BE32(filep
->userInfo
.fdType
) == kHardLinkFileType
) &&
666 (SWAP_BE32(filep
->userInfo
.fdCreator
) == kHFSPlusCreator
)) {
667 return (false); /* skip over link records */
668 } else if ((parid
== VCBTOHFS(vcb
)->hfs_privdir_desc
.cd_cnid
) &&
669 (filep
->bsdInfo
.special
.linkCount
== 0)) {
670 return (false); /* skip over unlinked files */
672 } else if (key
->hfsPlus
.parentID
== VCBTOHFS(vcb
)->hfs_privdir_desc
.cd_cnid
) {
673 return (false); /* skip over private files */
676 if ( (searchBits
& SRCHFS_MATCHFILES
) == 0 ) { /* If we are NOT searching files */
682 default: /* Never match a thread record or any other type. */
683 return( false ); /* Not a file or folder record, so can't search it */
686 matched
= true; /* Assume we got a match */
687 atleastone
= false; /* Dont insert unless we match at least one criteria */
689 /* First, attempt to match the name -- either partial or complete */
690 if ( attrList
->commonattr
& ATTR_CMN_NAME
) {
692 /* Check for partial/full HFS Plus name match */
694 if ( searchBits
& SRCHFS_MATCHPARTIALNAMES
) {
695 matched
= ComparePartialUnicodeName(key
->hfsPlus
.nodeName
.unicode
,
696 key
->hfsPlus
.nodeName
.length
,
697 (UniChar
*)searchInfo1
->name
,
698 searchInfo1
->nameLength
);
699 } else /* full HFS Plus name match */ {
700 matched
= (FastUnicodeCompare(key
->hfsPlus
.nodeName
.unicode
,
701 key
->hfsPlus
.nodeName
.length
,
702 (UniChar
*)searchInfo1
->name
,
703 searchInfo1
->nameLength
) == 0);
706 /* Check for partial/full HFS name match */
708 if ( searchBits
& SRCHFS_MATCHPARTIALNAMES
)
709 matched
= ComparePartialPascalName(key
->hfs
.nodeName
, (u_char
*)searchInfo1
->name
);
710 else /* full HFS name match */
711 matched
= (FastRelString(key
->hfs
.nodeName
, (u_char
*)searchInfo1
->name
) == 0);
714 #if 1 // Installer workaround (2940423)
716 HFSCatalogNodeID parentID
;
718 parentID
= key
->hfsPlus
.parentID
;
720 parentID
= key
->hfs
.parentID
;
722 if ( matched
&& parentID
== kRootDirID
&&
723 IsTargetName( searchInfo1
, isHFSPlus
) )
726 #endif // Installer workaround
728 if ( matched
== false || (searchBits
& ~SRCHFS_MATCHPARTIALNAMES
) == 0 )
729 goto TestDone
; /* no match, or nothing more to compare */
734 /* Convert catalog record into cat_attr format. */
735 cat_convertattr(VCBTOHFS(vcb
), rec
, &c_attr
, &datafork
, &rsrcfork
);
737 if (searchBits
& SRCHFS_SKIPINVISIBLE
) {
740 switch (rec
->recordType
) {
741 case kHFSFolderRecord
:
742 case kHFSPlusFolderRecord
: {
743 struct FndrDirInfo
*finder_info
;
745 finder_info
= (struct FndrDirInfo
*)&c_attr
.ca_finderinfo
[0];
746 flags
= SWAP_BE16(finder_info
->frFlags
);
751 case kHFSPlusFileRecord
: {
752 struct FndrFileInfo
*finder_info
;
754 finder_info
= (struct FndrFileInfo
*)&c_attr
.ca_finderinfo
[0];
755 flags
= SWAP_BE16(finder_info
->fdFlags
);
760 flags
= kIsInvisible
;
765 if (flags
& kIsInvisible
) {
773 /* Now that we have a record worth searching, see if it matches the search attributes */
774 if (rec
->recordType
== kHFSFileRecord
||
775 rec
->recordType
== kHFSPlusFileRecord
) {
776 if ((attrList
->fileattr
& ~ATTR_FILE_VALIDMASK
) != 0) { /* attr we do know about */
780 else if ((attrList
->fileattr
& ATTR_FILE_VALIDMASK
) != 0) {
781 searchAttributes
= attrList
->fileattr
;
783 /* File logical length (data fork) */
784 if ( searchAttributes
& ATTR_FILE_DATALENGTH
) {
785 matched
= CompareWideRange(
787 searchInfo1
->f
.dataLogicalLength
,
788 searchInfo2
->f
.dataLogicalLength
);
789 if (matched
== false) goto TestDone
;
793 /* File physical length (data fork) */
794 if ( searchAttributes
& ATTR_FILE_DATAALLOCSIZE
) {
795 matched
= CompareWideRange(
796 (u_int64_t
)datafork
.cf_blocks
* (u_int64_t
)vcb
->blockSize
,
797 searchInfo1
->f
.dataPhysicalLength
,
798 searchInfo2
->f
.dataPhysicalLength
);
799 if (matched
== false) goto TestDone
;
803 /* File logical length (resource fork) */
804 if ( searchAttributes
& ATTR_FILE_RSRCLENGTH
) {
805 matched
= CompareWideRange(
807 searchInfo1
->f
.resourceLogicalLength
,
808 searchInfo2
->f
.resourceLogicalLength
);
809 if (matched
== false) goto TestDone
;
813 /* File physical length (resource fork) */
814 if ( searchAttributes
& ATTR_FILE_RSRCALLOCSIZE
) {
815 matched
= CompareWideRange(
816 (u_int64_t
)rsrcfork
.cf_blocks
* (u_int64_t
)vcb
->blockSize
,
817 searchInfo1
->f
.resourcePhysicalLength
,
818 searchInfo2
->f
.resourcePhysicalLength
);
819 if (matched
== false) goto TestDone
;
824 atleastone
= true; /* to match SRCHFS_MATCHFILES */
828 * Check the directory attributes
830 else if (rec
->recordType
== kHFSFolderRecord
||
831 rec
->recordType
== kHFSPlusFolderRecord
) {
832 if ((attrList
->dirattr
& ~ATTR_DIR_VALIDMASK
) != 0) { /* attr we do know about */
836 else if ((attrList
->dirattr
& ATTR_DIR_VALIDMASK
) != 0) {
837 searchAttributes
= attrList
->dirattr
;
839 /* Directory valence */
840 if ( searchAttributes
& ATTR_DIR_ENTRYCOUNT
) {
841 matched
= CompareRange(c_attr
.ca_entries
,
842 searchInfo1
->d
.numFiles
,
843 searchInfo2
->d
.numFiles
);
844 if (matched
== false) goto TestDone
;
849 atleastone
= true; /* to match SRCHFS_MATCHDIRS */
854 * Check the common attributes
856 searchAttributes
= attrList
->commonattr
;
857 if ( (searchAttributes
& ATTR_CMN_VALIDMASK
) != 0 ) {
859 if ( searchAttributes
& ATTR_CMN_OBJID
) {
860 matched
= CompareRange(c_attr
.ca_fileid
,
862 searchInfo2
->nodeID
);
863 if (matched
== false) goto TestDone
;
868 if ( searchAttributes
& ATTR_CMN_PAROBJID
) {
869 HFSCatalogNodeID parentID
;
872 parentID
= key
->hfsPlus
.parentID
;
874 parentID
= key
->hfs
.parentID
;
876 matched
= CompareRange(parentID
, searchInfo1
->parentDirID
,
877 searchInfo2
->parentDirID
);
878 if (matched
== false) goto TestDone
;
882 /* Finder Info & Extended Finder Info where extFinderInfo is last 32 bytes */
883 if ( searchAttributes
& ATTR_CMN_FNDRINFO
) {
885 thisValue
= (UInt32
*) &c_attr
.ca_finderinfo
;
888 * Note: ioFlFndrInfo and ioDrUsrWds have the same offset in search info, so
889 * no need to test the object type here.
891 matched
= CompareMasked(thisValue
,
892 (UInt32
*)&searchInfo1
->finderInfo
,
893 (UInt32
*) &searchInfo2
->finderInfo
, 8);
894 if (matched
== false) goto TestDone
;
899 if ( searchAttributes
& ATTR_CMN_CRTIME
) {
900 matched
= CompareRange(c_attr
.ca_itime
,
901 searchInfo1
->creationDate
.tv_sec
,
902 searchInfo2
->creationDate
.tv_sec
);
903 if (matched
== false) goto TestDone
;
908 if ( searchAttributes
& ATTR_CMN_MODTIME
) {
909 matched
= CompareRange(c_attr
.ca_mtime
,
910 searchInfo1
->modificationDate
.tv_sec
,
911 searchInfo2
->modificationDate
.tv_sec
);
912 if (matched
== false) goto TestDone
;
917 if ( searchAttributes
& ATTR_CMN_CHGTIME
) {
918 matched
= CompareRange(c_attr
.ca_ctime
,
919 searchInfo1
->changeDate
.tv_sec
,
920 searchInfo2
->changeDate
.tv_sec
);
921 if (matched
== false) goto TestDone
;
926 if ( searchAttributes
& ATTR_CMN_ACCTIME
) {
927 matched
= CompareRange(c_attr
.ca_atime
,
928 searchInfo1
->accessDate
.tv_sec
,
929 searchInfo2
->accessDate
.tv_sec
);
930 if (matched
== false) goto TestDone
;
935 if ( searchAttributes
& ATTR_CMN_BKUPTIME
) {
936 matched
= CompareRange(c_attr
.ca_btime
,
937 searchInfo1
->lastBackupDate
.tv_sec
,
938 searchInfo2
->lastBackupDate
.tv_sec
);
939 if (matched
== false) goto TestDone
;
944 if ( searchAttributes
& ATTR_CMN_OWNERID
) {
945 matched
= CompareRange(c_attr
.ca_uid
,
946 searchInfo1
->uid
, searchInfo2
->uid
);
947 if (matched
== false) goto TestDone
;
952 if ( searchAttributes
& ATTR_CMN_GRPID
) {
953 matched
= CompareRange(c_attr
.ca_gid
,
954 searchInfo1
->gid
, searchInfo2
->gid
);
955 if (matched
== false) goto TestDone
;
960 if ( searchAttributes
& ATTR_CMN_ACCESSMASK
) {
961 matched
= CompareRange((uint32_t)c_attr
.ca_mode
,
962 (uint32_t)searchInfo1
->mask
,
963 (uint32_t)searchInfo2
->mask
);
964 if (matched
== false) goto TestDone
;
969 /* If we got here w/o matching any, then set to false */
975 * Finally, determine whether we need to negate the sense of the match
976 * (i.e. find all objects that DON'T match).
978 if ( searchBits
& SRCHFS_NEGATEPARAMS
)
986 * Adds another record to the packed array for output
989 InsertMatch(struct hfsmount
*hfsmp
, uio_t a_uio
, CatalogRecord
*rec
,
990 CatalogKey
*key
, struct attrlist
*returnAttrList
,
991 void *attributesBuffer
, void *variableBuffer
, u_long
* nummatches
)
994 void *rovingAttributesBuffer
;
995 void *rovingVariableBuffer
;
996 u_long packedBufferSize
;
997 u_long privateDir
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
998 struct attrblock attrblk
;
999 struct cat_desc c_desc
;
1000 struct cat_attr c_attr
;
1001 struct cat_fork datafork
;
1002 struct cat_fork rsrcfork
;
1004 bzero(&c_desc
, sizeof(c_desc
));
1005 bzero(&c_attr
, sizeof(c_attr
));
1006 rovingAttributesBuffer
= (char*)attributesBuffer
+ sizeof(u_long
); /* Reserve space for length field */
1007 rovingVariableBuffer
= variableBuffer
;
1009 /* Convert catalog record into cat_attr format. */
1010 cat_convertattr(hfsmp
, rec
, &c_attr
, &datafork
, &rsrcfork
);
1012 /* hide our private meta data directory */
1013 if ((privateDir
!= 0) && (c_attr
.ca_fileid
== privateDir
)) {
1018 /* Hide the private journal files */
1020 ((c_attr
.ca_fileid
== hfsmp
->hfs_jnlfileid
) ||
1021 (c_attr
.ca_fileid
== hfsmp
->hfs_jnlinfoblkid
))) {
1026 if (returnAttrList
->commonattr
& ATTR_CMN_NAME
) {
1027 cat_convertkey(hfsmp
, key
, rec
, &c_desc
);
1029 c_desc
.cd_cnid
= c_attr
.ca_fileid
;
1030 if (hfsmp
->hfs_flags
& HFS_STANDARD
)
1031 c_desc
.cd_parentcnid
= key
->hfs
.parentID
;
1033 c_desc
.cd_parentcnid
= key
->hfsPlus
.parentID
;
1036 attrblk
.ab_attrlist
= returnAttrList
;
1037 attrblk
.ab_attrbufpp
= &rovingAttributesBuffer
;
1038 attrblk
.ab_varbufpp
= &rovingVariableBuffer
;
1039 attrblk
.ab_flags
= 0;
1040 attrblk
.ab_blocksize
= 0;
1042 hfs_packattrblk(&attrblk
, hfsmp
, NULL
, &c_desc
, &c_attr
, &datafork
, &rsrcfork
, current_proc());
1044 packedBufferSize
= (char*)rovingVariableBuffer
- (char*)attributesBuffer
;
1046 if ( packedBufferSize
> uio_resid(a_uio
) )
1047 return( errSearchBufferFull
);
1051 *((u_long
*)attributesBuffer
) = packedBufferSize
; /* Store length of fixed + var block */
1053 err
= uiomove( (caddr_t
)attributesBuffer
, packedBufferSize
, a_uio
); /* XXX should be packedBufferSize */
1055 cat_releasedesc(&c_desc
);
1062 UnpackSearchAttributeBlock( struct hfsmount
*hfsmp
, struct attrlist
*alist
, searchinfospec_t
*searchInfo
, void *attributeBuffer
)
1066 boolean_t is_64_bit
;
1068 DBG_ASSERT(searchInfo
!= NULL
);
1070 is_64_bit
= proc_is64bit(current_proc());
1072 bufferSize
= *((uint32_t *)attributeBuffer
);
1073 if (bufferSize
== 0)
1074 return (EINVAL
); /* XXX -DJB is a buffer size of zero ever valid for searchfs? */
1076 ++((uint32_t *)attributeBuffer
); /* advance past the size */
1079 * UnPack common attributes
1081 a
= alist
->commonattr
;
1083 if ( a
& ATTR_CMN_NAME
) {
1087 s
= (char*) attributeBuffer
+ ((attrreference_t
*) attributeBuffer
)->attr_dataoffset
;
1088 len
= ((attrreference_t
*) attributeBuffer
)->attr_length
;
1090 if (len
> sizeof(searchInfo
->name
))
1093 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1094 /* Convert name to pascal string to match HFS B-Tree names */
1097 if (utf8_to_hfs(HFSTOVCB(hfsmp
), len
-1, s
, (u_char
*)searchInfo
->name
) != 0)
1100 searchInfo
->nameLength
= searchInfo
->name
[0];
1102 searchInfo
->name
[0] = searchInfo
->nameLength
= 0;
1104 ++((attrreference_t
*)attributeBuffer
);
1107 /* Convert name to Unicode to match HFS Plus B-Tree names */
1110 if (utf8_decodestr(s
, len
-1, (UniChar
*)searchInfo
->name
, &ucslen
,
1111 sizeof(searchInfo
->name
), ':', UTF_DECOMPOSED
))
1114 searchInfo
->nameLength
= ucslen
/ sizeof(UniChar
);
1116 searchInfo
->nameLength
= 0;
1118 ++((attrreference_t
*)attributeBuffer
);
1121 if ( a
& ATTR_CMN_OBJID
) {
1122 searchInfo
->nodeID
= ((fsobj_id_t
*) attributeBuffer
)->fid_objno
; /* ignore fid_generation */
1123 ++((fsobj_id_t
*)attributeBuffer
);
1125 if ( a
& ATTR_CMN_PAROBJID
) {
1126 searchInfo
->parentDirID
= ((fsobj_id_t
*) attributeBuffer
)->fid_objno
; /* ignore fid_generation */
1127 ++((fsobj_id_t
*)attributeBuffer
);
1129 if ( a
& ATTR_CMN_CRTIME
) {
1131 struct user_timespec tmp
;
1132 tmp
= *((struct user_timespec
*)attributeBuffer
);
1133 searchInfo
->creationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1134 searchInfo
->creationDate
.tv_nsec
= tmp
.tv_nsec
;
1135 ++((struct user_timespec
*)attributeBuffer
);
1138 searchInfo
->creationDate
= *((struct timespec
*)attributeBuffer
);
1139 ++((struct timespec
*)attributeBuffer
);
1142 if ( a
& ATTR_CMN_MODTIME
) {
1144 struct user_timespec tmp
;
1145 tmp
= *((struct user_timespec
*)attributeBuffer
);
1146 searchInfo
->modificationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1147 searchInfo
->modificationDate
.tv_nsec
= tmp
.tv_nsec
;
1148 ++((struct user_timespec
*)attributeBuffer
);
1151 searchInfo
->modificationDate
= *((struct timespec
*)attributeBuffer
);
1152 ++((struct timespec
*)attributeBuffer
);
1155 if ( a
& ATTR_CMN_CHGTIME
) {
1157 struct user_timespec tmp
;
1158 tmp
= *((struct user_timespec
*)attributeBuffer
);
1159 searchInfo
->changeDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1160 searchInfo
->changeDate
.tv_nsec
= tmp
.tv_nsec
;
1161 ++((struct user_timespec
*)attributeBuffer
);
1164 searchInfo
->changeDate
= *((struct timespec
*)attributeBuffer
);
1165 ++((struct timespec
*)attributeBuffer
);
1168 if ( a
& ATTR_CMN_ACCTIME
) {
1170 struct user_timespec tmp
;
1171 tmp
= *((struct user_timespec
*)attributeBuffer
);
1172 searchInfo
->accessDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1173 searchInfo
->accessDate
.tv_nsec
= tmp
.tv_nsec
;
1174 ++((struct user_timespec
*)attributeBuffer
);
1177 searchInfo
->accessDate
= *((struct timespec
*)attributeBuffer
);
1178 ++((struct timespec
*)attributeBuffer
);
1181 if ( a
& ATTR_CMN_BKUPTIME
) {
1183 struct user_timespec tmp
;
1184 tmp
= *((struct user_timespec
*)attributeBuffer
);
1185 searchInfo
->lastBackupDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1186 searchInfo
->lastBackupDate
.tv_nsec
= tmp
.tv_nsec
;
1187 ++((struct user_timespec
*)attributeBuffer
);
1190 searchInfo
->lastBackupDate
= *((struct timespec
*)attributeBuffer
);
1191 ++((struct timespec
*)attributeBuffer
);
1194 if ( a
& ATTR_CMN_FNDRINFO
) {
1195 bcopy( attributeBuffer
, searchInfo
->finderInfo
, sizeof(searchInfo
->finderInfo
) );
1196 (uint8_t *)attributeBuffer
+= 32;
1198 if ( a
& ATTR_CMN_OWNERID
) {
1199 searchInfo
->uid
= *((uid_t
*)attributeBuffer
);
1200 ++((uid_t
*)attributeBuffer
);
1202 if ( a
& ATTR_CMN_GRPID
) {
1203 searchInfo
->gid
= *((gid_t
*)attributeBuffer
);
1204 ++((gid_t
*)attributeBuffer
);
1206 if ( a
& ATTR_CMN_ACCESSMASK
) {
1207 searchInfo
->mask
= *((mode_t
*)attributeBuffer
);
1208 ++((mode_t
*)attributeBuffer
);
1214 if ( a
& ATTR_DIR_ENTRYCOUNT
) {
1215 searchInfo
->d
.numFiles
= *((u_int32_t
*)attributeBuffer
);
1216 ++((u_int32_t
*)attributeBuffer
);
1220 a
= alist
->fileattr
;
1222 if ( a
& ATTR_FILE_DATALENGTH
) {
1223 searchInfo
->f
.dataLogicalLength
= *((off_t
*)attributeBuffer
);
1224 ++((off_t
*)attributeBuffer
);
1226 if ( a
& ATTR_FILE_DATAALLOCSIZE
) {
1227 searchInfo
->f
.dataPhysicalLength
= *((off_t
*)attributeBuffer
);
1228 ++((off_t
*)attributeBuffer
);
1230 if ( a
& ATTR_FILE_RSRCLENGTH
) {
1231 searchInfo
->f
.resourceLogicalLength
= *((off_t
*)attributeBuffer
);
1232 ++((off_t
*)attributeBuffer
);
1234 if ( a
& ATTR_FILE_RSRCALLOCSIZE
) {
1235 searchInfo
->f
.resourcePhysicalLength
= *((off_t
*)attributeBuffer
);
1236 ++((off_t
*)attributeBuffer
);
1244 #if 1 // Installer workaround (2940423)
1245 /* this routine was added as part of the work around where some installers would fail */
1246 /* because they incorrectly assumed search results were in some kind of order. */
1247 /* This routine is used to indentify the problematic target. At this point we */
1248 /* only know of one. This routine could be modified for more (I hope not). */
1249 static Boolean
IsTargetName( searchinfospec_t
* searchInfoPtr
, Boolean isHFSPlus
)
1251 if ( searchInfoPtr
->name
== NULL
)
1255 HFSUniStr255 myName
= {
1256 7, /* number of unicode characters */
1258 'L','i','b','r','a','r','y'
1261 if ( FastUnicodeCompare( myName
.unicode
, myName
.length
,
1262 (UniChar
*)searchInfoPtr
->name
,
1263 searchInfoPtr
->nameLength
) == 0 ) {
1268 u_char myName
[32] = {
1269 0x07,'L','i','b','r','a','r','y'
1271 if ( FastRelString(myName
, (u_char
*)searchInfoPtr
->name
) == 0 ) {
1277 } /* IsTargetName */
1278 #endif // Installer workaround