2 * Copyright (c) 1997-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
32 * support for mandatory and extensible security protections. This notice
33 * is included in support of clause 2.2 (b) of the Apple Public License,
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
43 #include <mach/machine/vm_types.h>
44 #include <sys/vnode.h>
45 #include <sys/malloc.h>
46 #include <sys/signalvar.h>
48 #include <sys/utfconv.h>
49 #include <sys/kauth.h>
50 #include <sys/vnode_internal.h>
51 #include <sys/mount_internal.h>
54 #include <security/mac_framework.h>
59 #include "hfs_catalog.h"
60 #include "hfs_attrlist.h"
61 #include "hfs_endian.h"
63 #include "hfscommon/headers/FileMgrInternal.h"
64 #include "hfscommon/headers/HFSUnicodeWrappers.h"
65 #include "hfscommon/headers/BTreesPrivate.h"
66 #include "hfscommon/headers/BTreeScanner.h"
67 #include "hfscommon/headers/CatalogPrivate.h"
69 /* Search criterea. */
70 struct directoryInfoSpec
77 off_t dataLogicalLength
;
78 off_t dataPhysicalLength
;
79 off_t resourceLogicalLength
;
80 off_t resourcePhysicalLength
;
85 u_char name
[kHFSPlusMaxFileNameBytes
];
87 char attributes
; // see IM:Files 2-100
89 u_int32_t parentDirID
;
90 struct timespec creationDate
;
91 struct timespec modificationDate
;
92 struct timespec changeDate
;
93 struct timespec accessDate
;
94 struct timespec lastBackupDate
;
95 u_int8_t finderInfo
[32];
99 struct fileInfoSpec f
;
100 struct directoryInfoSpec d
;
102 typedef struct searchinfospec searchinfospec_t
;
104 static void ResolveHardlink(struct hfsmount
*hfsmp
, HFSPlusCatalogFile
*recp
);
107 static int UnpackSearchAttributeBlock(struct hfsmount
*hfsmp
, struct attrlist
*alist
,
108 searchinfospec_t
*searchInfo
, void *attributeBuffer
, int firstblock
);
110 static int CheckCriteria( ExtendedVCB
*vcb
,
112 struct attrlist
*attrList
,
115 searchinfospec_t
*searchInfo1
,
116 searchinfospec_t
*searchInfo2
);
118 static int CheckAccess(ExtendedVCB
*vcb
, u_long searchBits
, CatalogKey
*key
, struct vfs_context
*ctx
);
120 static int InsertMatch(struct hfsmount
*hfsmp
, uio_t a_uio
, CatalogRecord
*rec
,
121 CatalogKey
*key
, struct attrlist
*returnAttrList
,
122 void *attributesBuffer
, void *variableBuffer
,
123 uint32_t * nummatches
);
125 static Boolean
CompareRange(u_long val
, u_long low
, u_long high
);
126 static Boolean
CompareWideRange(u_int64_t val
, u_int64_t low
, u_int64_t high
);
128 static Boolean
CompareRange( u_long val
, u_long low
, u_long high
)
130 return( (val
>= low
) && (val
<= high
) );
133 static Boolean
CompareWideRange( u_int64_t val
, u_int64_t low
, u_int64_t high
)
135 return( (val
>= low
) && (val
<= high
) );
137 //#define CompareRange(val, low, high) ((val >= low) && (val <= high))
140 /************************************************************************/
141 /* Entry for searchfs() */
142 /************************************************************************/
144 #define errSearchBufferFull 101 /* Internal search errors */
153 IN kauth_cred_t cred;
160 struct vnop_searchfs_args
*ap
; /*
161 struct vnodeop_desc *a_desc;
163 void *a_searchparams1;
164 void *a_searchparams2;
165 struct attrlist *a_searchattrs;
167 struct timeval *a_timelimit;
168 struct attrlist *a_returnattrs;
169 u_long *a_nummatches;
173 struct searchstate *a_searchstate;
174 vfs_context_t a_context;
177 ExtendedVCB
*vcb
= VTOVCB(ap
->a_vp
);
178 struct hfsmount
*hfsmp
;
180 searchinfospec_t searchInfo1
;
181 searchinfospec_t searchInfo2
;
182 void *attributesBuffer
= NULL
;
183 void *variableBuffer
;
184 u_int32_t fixedBlockSize
;
185 u_int32_t eachReturnBufferSize
;
186 struct proc
*p
= current_proc();
189 CatalogKey
* myCurrentKeyPtr
;
190 CatalogRecord
* myCurrentDataPtr
;
191 CatPosition
* myCatPositionPtr
;
192 BTScanState myBTScanState
;
193 user_addr_t user_start
= 0;
194 user_size_t user_len
= 0;
198 boolean_t timerExpired
= FALSE
;
199 boolean_t needThrottle
= FALSE
;
201 /* XXX Parameter check a_searchattrs? */
203 *(ap
->a_nummatches
) = 0;
205 if (ap
->a_options
& ~SRCHFS_VALIDOPTIONSMASK
)
209 * Reject requests for unsupported attributes.
211 if ((ap
->a_returnattrs
->commonattr
& ~HFS_ATTR_CMN_VALID
) ||
212 (ap
->a_returnattrs
->volattr
!= 0) ||
213 (ap
->a_returnattrs
->dirattr
& ~HFS_ATTR_DIR_VALID
) ||
214 (ap
->a_returnattrs
->fileattr
& ~HFS_ATTR_FILE_VALID
) ||
215 (ap
->a_returnattrs
->forkattr
!= 0)) {
219 /* SRCHFS_SKIPLINKS requires root access.
220 * This option cannot be used with either
221 * the ATTR_CMN_NAME or ATTR_CMN_PAROBJID
224 if (ap
->a_options
& SRCHFS_SKIPLINKS
) {
227 attrs
= ap
->a_searchattrs
->commonattr
| ap
->a_returnattrs
->commonattr
;
228 if (attrs
& (ATTR_CMN_NAME
| ATTR_CMN_PAROBJID
))
230 if ((err
= vfs_context_suser(ap
->a_context
)))
234 // If both 32-bit and 64-bit parent ids or file ids are given
235 // then return an error.
237 attrgroup_t test_attrs
=ap
->a_searchattrs
->commonattr
;
239 if (((test_attrs
& ATTR_CMN_OBJID
) && (test_attrs
& ATTR_CMN_FILEID
)) ||
240 ((test_attrs
& ATTR_CMN_PARENTID
) && (test_attrs
& ATTR_CMN_PAROBJID
)))
244 if (uio_resid(ap
->a_uio
) <= 0)
247 isHFSPlus
= (vcb
->vcbSigWord
== kHFSPlusSigWord
);
248 hfsmp
= VTOHFS(ap
->a_vp
);
250 searchTime
= kMaxMicroSecsInKernel
;
251 if (ap
->a_timelimit
->tv_sec
== 0 &&
252 ap
->a_timelimit
->tv_usec
> 0 &&
253 ap
->a_timelimit
->tv_usec
< kMaxMicroSecsInKernel
) {
254 searchTime
= ap
->a_timelimit
->tv_usec
;
257 /* UnPack the search boundries, searchInfo1, searchInfo2 */
258 err
= UnpackSearchAttributeBlock(hfsmp
, ap
->a_searchattrs
,
259 &searchInfo1
, ap
->a_searchparams1
, 1);
261 err
= UnpackSearchAttributeBlock(hfsmp
, ap
->a_searchattrs
,
262 &searchInfo2
, ap
->a_searchparams2
, 0);
265 //shadow search bits if 64-bit file/parent ids are used
266 if (ap
->a_searchattrs
->commonattr
& ATTR_CMN_FILEID
)
267 ap
->a_searchattrs
->commonattr
|= ATTR_CMN_OBJID
;
268 if (ap
->a_searchattrs
->commonattr
& ATTR_CMN_PARENTID
)
269 ap
->a_searchattrs
->commonattr
|= ATTR_CMN_PAROBJID
;
271 fixedBlockSize
= sizeof(u_int32_t
) + hfs_attrblksize(ap
->a_returnattrs
); /* u_int32_t for length word */
273 eachReturnBufferSize
= fixedBlockSize
;
275 if ( ap
->a_returnattrs
->commonattr
& ATTR_CMN_NAME
) /* XXX should be more robust! */
276 eachReturnBufferSize
+= kHFSPlusMaxFileNameBytes
+ 1;
278 MALLOC( attributesBuffer
, void *, eachReturnBufferSize
, M_TEMP
, M_WAITOK
);
279 if (attributesBuffer
== NULL
) {
281 goto ExitThisRoutine
;
283 variableBuffer
= (void*)((char*) attributesBuffer
+ fixedBlockSize
);
285 // XXXdbg - have to lock the user's buffer so we don't fault
286 // while holding the shared catalog file lock. see the comment
287 // in hfs_readdir() for more details.
289 if (hfsmp
->jnl
&& uio_isuserspace(ap
->a_uio
)) {
290 user_start
= uio_curriovbase(ap
->a_uio
);
291 user_len
= uio_curriovlen(ap
->a_uio
);
293 if ((err
= vslock(user_start
, user_len
)) != 0) {
295 goto ExitThisRoutine
;
299 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
301 catalogFCB
= GetFileControlBlock(vcb
->catalogRefNum
);
302 myCurrentKeyPtr
= NULL
;
303 myCurrentDataPtr
= NULL
;
304 myCatPositionPtr
= (CatPosition
*)ap
->a_searchstate
;
306 if (ap
->a_options
& SRCHFS_START
) {
307 /* Starting a new search. */
308 /* Make sure the on-disk Catalog file is current */
309 (void) hfs_fsync(vcb
->catalogRefNum
, MNT_WAIT
, 0, p
);
311 hfs_systemfile_unlock(hfsmp
, lockflags
);
312 hfs_journal_flush(hfsmp
, FALSE
);
313 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
316 ap
->a_options
&= ~SRCHFS_START
;
317 bzero((caddr_t
)myCatPositionPtr
, sizeof(*myCatPositionPtr
));
318 err
= BTScanInitialize(catalogFCB
, 0, 0, 0, kCatSearchBufferSize
, &myBTScanState
);
320 goto ExitThisRoutine
;
323 /* Resuming a search. */
324 err
= BTScanInitialize(catalogFCB
, myCatPositionPtr
->nextNode
,
325 myCatPositionPtr
->nextRecord
,
326 myCatPositionPtr
->recordsFound
,
327 kCatSearchBufferSize
,
329 /* Make sure Catalog hasn't changed. */
331 && myCatPositionPtr
->writeCount
!= myBTScanState
.btcb
->writeCount
) {
332 myCatPositionPtr
->writeCount
= myBTScanState
.btcb
->writeCount
;
333 err
= EBUSY
; /* catChangedErr */
336 hfs_systemfile_unlock(hfsmp
, lockflags
);
339 goto ExitThisRoutine
;
341 if (throttle_get_io_policy(&ut
) == IOPOL_THROTTLE
)
344 * Check all the catalog btree records...
345 * return the attributes for matching items
348 struct timeval myCurrentTime
;
349 struct timeval myElapsedTime
;
351 err
= BTScanNextRecord(&myBTScanState
, timerExpired
,
352 (void **)&myCurrentKeyPtr
, (void **)&myCurrentDataPtr
,
357 /* Resolve any hardlinks */
358 if (isHFSPlus
&& (ap
->a_options
& SRCHFS_SKIPLINKS
) == 0) {
359 ResolveHardlink(vcb
, (HFSPlusCatalogFile
*)myCurrentDataPtr
);
361 if (CheckCriteria( vcb
, ap
->a_options
, ap
->a_searchattrs
, myCurrentDataPtr
,
362 myCurrentKeyPtr
, &searchInfo1
, &searchInfo2
)
363 && CheckAccess(vcb
, ap
->a_options
, myCurrentKeyPtr
, ap
->a_context
)) {
364 err
= InsertMatch(hfsmp
, ap
->a_uio
, myCurrentDataPtr
,
365 myCurrentKeyPtr
, ap
->a_returnattrs
,
366 attributesBuffer
, variableBuffer
, ap
->a_nummatches
);
369 * The last match didn't fit so come back
370 * to this record on the next trip.
372 --myBTScanState
.recordsFound
;
373 --myBTScanState
.recordNum
;
377 if (*(ap
->a_nummatches
) >= ap
->a_maxmatches
)
380 if (timerExpired
== FALSE
) {
382 * Check our elapsed time and bail if we've hit the max.
383 * The idea here is to throttle the amount of time we
384 * spend in the kernel.
386 microuptime(&myCurrentTime
);
387 timersub(&myCurrentTime
, &myBTScanState
.startTime
, &myElapsedTime
);
389 * Note: assumes kMaxMicroSecsInKernel is less than 1,000,000
391 if (myElapsedTime
.tv_sec
> 0
392 || myElapsedTime
.tv_usec
>= searchTime
) {
394 } else if (needThrottle
== TRUE
) {
395 if (throttle_io_will_be_throttled(ut
->uu_lowpri_window
, HFSTOVFS(hfsmp
)))
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 if (attributesBuffer
)
423 FREE(attributesBuffer
, M_TEMP
);
425 if (hfsmp
->jnl
&& user_start
) {
426 vsunlock(user_start
, user_len
, TRUE
);
429 return (MacToVFSError(err
));
434 ResolveHardlink(struct hfsmount
*hfsmp
, HFSPlusCatalogFile
*recp
)
436 u_int32_t type
, creator
;
439 time_t filecreatedate
;
441 if (recp
->recordType
!= kHFSPlusFileRecord
) {
444 type
= SWAP_BE32(recp
->userInfo
.fdType
);
445 creator
= SWAP_BE32(recp
->userInfo
.fdCreator
);
446 filecreatedate
= to_bsd_time(recp
->createDate
);
448 if ((type
== kHardLinkFileType
&& creator
== kHFSPlusCreator
) &&
449 (filecreatedate
== (time_t)hfsmp
->hfs_itime
||
450 filecreatedate
== (time_t)hfsmp
->hfs_metadata_createdate
)) {
452 } else if ((type
== kHFSAliasType
&& creator
== kHFSAliasCreator
) &&
453 (recp
->flags
& kHFSHasLinkChainMask
) &&
454 (filecreatedate
== (time_t)hfsmp
->hfs_itime
||
455 filecreatedate
== (time_t)hfsmp
->hfs_metadata_createdate
)) {
459 if (isfilelink
|| isdirlink
) {
463 /* Export link's cnid (a unique value) instead of inode's cnid */
464 saved_cnid
= recp
->fileID
;
465 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
467 (void) cat_resolvelink(hfsmp
, recp
->hl_linkReference
, isdirlink
, recp
);
469 recp
->fileID
= saved_cnid
;
470 hfs_systemfile_unlock(hfsmp
, lockflags
);
476 CompareMasked(const u_int32_t
*thisValue
, const u_int32_t
*compareData
,
477 const u_int32_t
*compareMask
, u_int32_t count
)
482 matched
= true; /* Assume it will all match */
484 for (i
=0; i
<count
; i
++) {
485 if (((*thisValue
++ ^ *compareData
++) & *compareMask
++) != 0) {
496 ComparePartialUnicodeName (register ConstUniCharArrayPtr str
, register ItemCount s_len
,
497 register ConstUniCharArrayPtr find
, register ItemCount f_len
)
499 if (f_len
== 0 || s_len
== 0)
505 } while (FastUnicodeCompare(str
++, f_len
, find
, f_len
) != 0);
512 ComparePartialPascalName ( register ConstStr31Param str
, register ConstStr31Param find
)
514 register u_char s_len
= str
[0];
515 register u_char f_len
= find
[0];
516 register u_char
*tsp
;
519 if (f_len
== 0 || s_len
== 0)
522 bcopy(str
, tmpstr
, s_len
+ 1);
525 while (s_len
-- >= f_len
) {
528 if (FastRelString(tsp
++, find
) == 0)
537 * Check to see if caller has access rights to this item
541 CheckAccess(ExtendedVCB
*theVCBPtr
, u_long searchBits
, CatalogKey
*theKeyPtr
, struct vfs_context
*ctx
)
546 HFSCatalogNodeID myNodeID
;
548 struct FndrDirInfo
*finfop
;
549 struct vnode
* vp
= NULL
;
551 myResult
= 0; /* default to "no access" */
553 if (!vfs_context_suser(ctx
)) {
554 myResult
= 1; /* allow access */
555 goto ExitThisRoutine
; /* root always has access */
558 hfsmp
= VCBTOHFS( theVCBPtr
);
559 isHFSPlus
= ( theVCBPtr
->vcbSigWord
== kHFSPlusSigWord
);
561 myNodeID
= theKeyPtr
->hfsPlus
.parentID
;
563 myNodeID
= theKeyPtr
->hfs
.parentID
;
565 while ( myNodeID
>= kRootDirID
) {
568 /* now go get catalog data for this directory */
569 myErr
= hfs_vget(hfsmp
, myNodeID
, &vp
, 0, 0);
571 goto ExitThisRoutine
; /* no access */
575 finfop
= (struct FndrDirInfo
*)&cp
->c_attr
.ca_finderinfo
[0];
577 if ( searchBits
& SRCHFS_SKIPPACKAGES
) {
578 if ( (SWAP_BE16(finfop
->frFlags
) & kHasBundle
)
579 || (cp
->c_desc
.cd_nameptr
!= NULL
580 && is_package_name((const char *)cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
)) ) {
582 goto ExitThisRoutine
;
586 if ( searchBits
& SRCHFS_SKIPINAPPROPRIATE
) {
587 if ( cp
->c_parentcnid
== kRootDirID
&& cp
->c_desc
.cd_nameptr
!= NULL
&&
588 vn_searchfs_inappropriate_name((const char *)cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
) ) {
590 goto ExitThisRoutine
;
594 if ( (searchBits
& SRCHFS_SKIPINVISIBLE
) &&
595 (SWAP_BE16(finfop
->frFlags
) & kIsInvisible
) ) {
597 goto ExitThisRoutine
;
600 myNodeID
= cp
->c_parentcnid
; /* move up the hierarchy */
601 hfs_unlock(VTOC(vp
));
604 if (vp
->v_type
== VDIR
) {
605 myErr
= mac_vnode_check_readdir(ctx
, vp
);
607 myErr
= mac_vnode_check_stat(ctx
, NOCRED
, vp
);
612 goto ExitThisRoutine
;
616 if (vp
->v_type
== VDIR
) {
617 myErr
= vnode_authorize(vp
, NULL
, (KAUTH_VNODE_SEARCH
| KAUTH_VNODE_LIST_DIRECTORY
), ctx
);
619 myErr
= vnode_authorize(vp
, NULL
, (KAUTH_VNODE_SEARCH
), ctx
);
624 goto ExitThisRoutine
; /* no access */
627 myResult
= 1; /* allow access */
631 hfs_unlock(VTOC(vp
));
639 CheckCriteria( ExtendedVCB
*vcb
,
641 struct attrlist
*attrList
,
644 searchinfospec_t
*searchInfo1
,
645 searchinfospec_t
*searchInfo2
)
647 Boolean matched
, atleastone
;
649 attrgroup_t searchAttributes
;
650 struct cat_attr c_attr
;
651 struct cat_fork datafork
;
652 struct cat_fork rsrcfork
;
654 bzero(&c_attr
, sizeof(c_attr
));
655 isHFSPlus
= (vcb
->vcbSigWord
== kHFSPlusSigWord
);
657 switch (rec
->recordType
) {
658 case kHFSFolderRecord
:
659 case kHFSPlusFolderRecord
:
660 if ( (searchBits
& SRCHFS_MATCHDIRS
) == 0 ) { /* If we are NOT searching folders */
667 if ( (searchBits
& SRCHFS_MATCHFILES
) == 0 ) { /* If we are NOT searching files */
673 case kHFSPlusFileRecord
:
674 /* Check if hardlink links should be skipped. */
675 if (searchBits
& SRCHFS_SKIPLINKS
) {
676 cnid_t parid
= key
->hfsPlus
.parentID
;
677 HFSPlusCatalogFile
*filep
= (HFSPlusCatalogFile
*)rec
;
679 if ((SWAP_BE32(filep
->userInfo
.fdType
) == kHardLinkFileType
) &&
680 (SWAP_BE32(filep
->userInfo
.fdCreator
) == kHFSPlusCreator
)) {
681 return (false); /* skip over file link records */
682 } else if ((parid
== vcb
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) &&
683 (filep
->bsdInfo
.special
.linkCount
== 0)) {
684 return (false); /* skip over unlinked files */
685 } else if ((SWAP_BE32(filep
->userInfo
.fdType
) == kHFSAliasType
) &&
686 (SWAP_BE32(filep
->userInfo
.fdCreator
) == kHFSAliasCreator
) &&
687 (filep
->flags
& kHFSHasLinkChainMask
)) {
688 return (false); /* skip over dir link records */
690 } else if (key
->hfsPlus
.parentID
== vcb
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) {
691 return (false); /* skip over private files */
692 } else if (key
->hfsPlus
.parentID
== vcb
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) {
693 return (false); /* skip over private files */
696 if ( (searchBits
& SRCHFS_MATCHFILES
) == 0 ) { /* If we are NOT searching files */
702 default: /* Never match a thread record or any other type. */
703 return( false ); /* Not a file or folder record, so can't search it */
706 matched
= true; /* Assume we got a match */
707 atleastone
= false; /* Dont insert unless we match at least one criteria */
709 /* First, attempt to match the name -- either partial or complete */
710 if ( attrList
->commonattr
& ATTR_CMN_NAME
) {
712 /* Check for partial/full HFS Plus name match */
714 if ( searchBits
& SRCHFS_MATCHPARTIALNAMES
) {
715 matched
= ComparePartialUnicodeName(key
->hfsPlus
.nodeName
.unicode
,
716 key
->hfsPlus
.nodeName
.length
,
717 (UniChar
*)searchInfo1
->name
,
718 searchInfo1
->nameLength
);
719 } else /* full HFS Plus name match */ {
720 matched
= (FastUnicodeCompare(key
->hfsPlus
.nodeName
.unicode
,
721 key
->hfsPlus
.nodeName
.length
,
722 (UniChar
*)searchInfo1
->name
,
723 searchInfo1
->nameLength
) == 0);
726 /* Check for partial/full HFS name match */
728 if ( searchBits
& SRCHFS_MATCHPARTIALNAMES
)
729 matched
= ComparePartialPascalName(key
->hfs
.nodeName
, (u_char
*)searchInfo1
->name
);
730 else /* full HFS name match */
731 matched
= (FastRelString(key
->hfs
.nodeName
, (u_char
*)searchInfo1
->name
) == 0);
734 if ( matched
== false || (searchBits
& ~SRCHFS_MATCHPARTIALNAMES
) == 0 )
735 goto TestDone
; /* no match, or nothing more to compare */
740 /* Convert catalog record into cat_attr format. */
741 cat_convertattr(VCBTOHFS(vcb
), rec
, &c_attr
, &datafork
, &rsrcfork
);
743 if (searchBits
& SRCHFS_SKIPINVISIBLE
) {
746 switch (rec
->recordType
) {
747 case kHFSFolderRecord
:
748 case kHFSPlusFolderRecord
: {
749 struct FndrDirInfo
*finder_info
;
751 finder_info
= (struct FndrDirInfo
*)&c_attr
.ca_finderinfo
[0];
752 flags
= SWAP_BE16(finder_info
->frFlags
);
757 case kHFSPlusFileRecord
: {
758 struct FndrFileInfo
*finder_info
;
760 finder_info
= (struct FndrFileInfo
*)&c_attr
.ca_finderinfo
[0];
761 flags
= SWAP_BE16(finder_info
->fdFlags
);
766 flags
= kIsInvisible
;
771 if (flags
& kIsInvisible
) {
779 /* Now that we have a record worth searching, see if it matches the search attributes */
780 if (rec
->recordType
== kHFSFileRecord
||
781 rec
->recordType
== kHFSPlusFileRecord
) {
782 if ((attrList
->fileattr
& ~ATTR_FILE_VALIDMASK
) != 0) { /* attr we do know about */
786 else if ((attrList
->fileattr
& ATTR_FILE_VALIDMASK
) != 0) {
787 searchAttributes
= attrList
->fileattr
;
790 if ( c_attr
.ca_flags
& UF_COMPRESSED
) {
791 /* for compressed files, set the data length to the uncompressed data size */
792 if (( searchAttributes
& ATTR_FILE_DATALENGTH
) ||
793 ( searchAttributes
& ATTR_FILE_DATAALLOCSIZE
) ) {
794 if ( 0 == hfs_uncompressed_size_of_compressed_file(vcb
, NULL
, c_attr
.ca_fileid
, &datafork
.cf_size
, 1) ) { /* 1 == don't take the cnode lock */
795 datafork
.cf_blocks
= rsrcfork
.cf_blocks
;
798 /* treat compressed files as if their resource fork is empty */
799 if (( searchAttributes
& ATTR_FILE_RSRCLENGTH
) ||
800 ( searchAttributes
& ATTR_FILE_RSRCALLOCSIZE
) ) {
801 rsrcfork
.cf_size
= 0;
802 rsrcfork
.cf_blocks
= 0;
805 #endif /* HFS_COMPRESSION */
807 /* File logical length (data fork) */
808 if ( searchAttributes
& ATTR_FILE_DATALENGTH
) {
809 matched
= CompareWideRange(
811 searchInfo1
->f
.dataLogicalLength
,
812 searchInfo2
->f
.dataLogicalLength
);
813 if (matched
== false) goto TestDone
;
817 /* File physical length (data fork) */
818 if ( searchAttributes
& ATTR_FILE_DATAALLOCSIZE
) {
819 matched
= CompareWideRange(
820 (u_int64_t
)datafork
.cf_blocks
* (u_int64_t
)vcb
->blockSize
,
821 searchInfo1
->f
.dataPhysicalLength
,
822 searchInfo2
->f
.dataPhysicalLength
);
823 if (matched
== false) goto TestDone
;
827 /* File logical length (resource fork) */
828 if ( searchAttributes
& ATTR_FILE_RSRCLENGTH
) {
829 matched
= CompareWideRange(
831 searchInfo1
->f
.resourceLogicalLength
,
832 searchInfo2
->f
.resourceLogicalLength
);
833 if (matched
== false) goto TestDone
;
837 /* File physical length (resource fork) */
838 if ( searchAttributes
& ATTR_FILE_RSRCALLOCSIZE
) {
839 matched
= CompareWideRange(
840 (u_int64_t
)rsrcfork
.cf_blocks
* (u_int64_t
)vcb
->blockSize
,
841 searchInfo1
->f
.resourcePhysicalLength
,
842 searchInfo2
->f
.resourcePhysicalLength
);
843 if (matched
== false) goto TestDone
;
848 atleastone
= true; /* to match SRCHFS_MATCHFILES */
852 * Check the directory attributes
854 else if (rec
->recordType
== kHFSFolderRecord
||
855 rec
->recordType
== kHFSPlusFolderRecord
) {
856 if ((attrList
->dirattr
& ~ATTR_DIR_VALIDMASK
) != 0) { /* attr we do know about */
860 else if ((attrList
->dirattr
& ATTR_DIR_VALIDMASK
) != 0) {
861 searchAttributes
= attrList
->dirattr
;
863 /* Directory valence */
864 if ( searchAttributes
& ATTR_DIR_ENTRYCOUNT
) {
865 matched
= CompareRange(c_attr
.ca_entries
,
866 searchInfo1
->d
.numFiles
,
867 searchInfo2
->d
.numFiles
);
868 if (matched
== false) goto TestDone
;
873 atleastone
= true; /* to match SRCHFS_MATCHDIRS */
878 * Check the common attributes
880 searchAttributes
= attrList
->commonattr
;
881 if ( (searchAttributes
& ATTR_CMN_VALIDMASK
) != 0 ) {
883 if ( searchAttributes
& ATTR_CMN_OBJID
) {
884 matched
= CompareRange(c_attr
.ca_fileid
,
886 searchInfo2
->nodeID
);
887 if (matched
== false) goto TestDone
;
892 if ( searchAttributes
& ATTR_CMN_PAROBJID
) {
893 HFSCatalogNodeID parentID
;
896 parentID
= key
->hfsPlus
.parentID
;
898 parentID
= key
->hfs
.parentID
;
900 matched
= CompareRange(parentID
, searchInfo1
->parentDirID
,
901 searchInfo2
->parentDirID
);
902 if (matched
== false) goto TestDone
;
906 /* Finder Info & Extended Finder Info where extFinderInfo is last 32 bytes */
907 if ( searchAttributes
& ATTR_CMN_FNDRINFO
) {
908 u_int32_t
*thisValue
;
909 thisValue
= (u_int32_t
*) &c_attr
.ca_finderinfo
;
912 * Note: ioFlFndrInfo and ioDrUsrWds have the same offset in search info, so
913 * no need to test the object type here.
915 matched
= CompareMasked(thisValue
,
916 (u_int32_t
*)&searchInfo1
->finderInfo
,
917 (u_int32_t
*) &searchInfo2
->finderInfo
, 8);
918 if (matched
== false) goto TestDone
;
923 if ( searchAttributes
& ATTR_CMN_CRTIME
) {
924 matched
= CompareRange(c_attr
.ca_itime
,
925 searchInfo1
->creationDate
.tv_sec
,
926 searchInfo2
->creationDate
.tv_sec
);
927 if (matched
== false) goto TestDone
;
932 if ( searchAttributes
& ATTR_CMN_MODTIME
) {
933 matched
= CompareRange(c_attr
.ca_mtime
,
934 searchInfo1
->modificationDate
.tv_sec
,
935 searchInfo2
->modificationDate
.tv_sec
);
936 if (matched
== false) goto TestDone
;
941 if ( searchAttributes
& ATTR_CMN_CHGTIME
) {
942 matched
= CompareRange(c_attr
.ca_ctime
,
943 searchInfo1
->changeDate
.tv_sec
,
944 searchInfo2
->changeDate
.tv_sec
);
945 if (matched
== false) goto TestDone
;
950 if ( searchAttributes
& ATTR_CMN_ACCTIME
) {
951 matched
= CompareRange(c_attr
.ca_atime
,
952 searchInfo1
->accessDate
.tv_sec
,
953 searchInfo2
->accessDate
.tv_sec
);
954 if (matched
== false) goto TestDone
;
959 if ( searchAttributes
& ATTR_CMN_BKUPTIME
) {
960 matched
= CompareRange(c_attr
.ca_btime
,
961 searchInfo1
->lastBackupDate
.tv_sec
,
962 searchInfo2
->lastBackupDate
.tv_sec
);
963 if (matched
== false) goto TestDone
;
968 if ( searchAttributes
& ATTR_CMN_OWNERID
) {
969 matched
= CompareRange(c_attr
.ca_uid
,
970 searchInfo1
->uid
, searchInfo2
->uid
);
971 if (matched
== false) goto TestDone
;
976 if ( searchAttributes
& ATTR_CMN_GRPID
) {
977 matched
= CompareRange(c_attr
.ca_gid
,
978 searchInfo1
->gid
, searchInfo2
->gid
);
979 if (matched
== false) goto TestDone
;
984 if ( searchAttributes
& ATTR_CMN_ACCESSMASK
) {
985 matched
= CompareRange((u_int32_t
)c_attr
.ca_mode
,
986 (u_int32_t
)searchInfo1
->mask
,
987 (u_int32_t
)searchInfo2
->mask
);
988 if (matched
== false) goto TestDone
;
993 /* If we got here w/o matching any, then set to false */
999 * Finally, determine whether we need to negate the sense of the match
1000 * (i.e. find all objects that DON'T match).
1002 if ( searchBits
& SRCHFS_NEGATEPARAMS
)
1010 * Adds another record to the packed array for output
1013 InsertMatch(struct hfsmount
*hfsmp
, uio_t a_uio
, CatalogRecord
*rec
,
1014 CatalogKey
*key
, struct attrlist
*returnAttrList
,
1015 void *attributesBuffer
, void *variableBuffer
, uint32_t * nummatches
)
1018 void *rovingAttributesBuffer
;
1019 void *rovingVariableBuffer
;
1020 long packedBufferSize
;
1021 struct attrblock attrblk
;
1022 struct cat_desc c_desc
;
1023 struct cat_attr c_attr
;
1024 struct cat_fork datafork
;
1025 struct cat_fork rsrcfork
;
1027 bzero(&c_desc
, sizeof(c_desc
));
1028 bzero(&c_attr
, sizeof(c_attr
));
1029 rovingAttributesBuffer
= (char*)attributesBuffer
+ sizeof(u_int32_t
); /* Reserve space for length field */
1030 rovingVariableBuffer
= variableBuffer
;
1032 /* Convert catalog record into cat_attr format. */
1033 cat_convertattr(hfsmp
, rec
, &c_attr
, &datafork
, &rsrcfork
);
1035 /* Hide our private meta data directories */
1036 if (c_attr
.ca_fileid
== hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
||
1037 c_attr
.ca_fileid
== hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) {
1042 /* Hide the private journal files */
1044 ((c_attr
.ca_fileid
== hfsmp
->hfs_jnlfileid
) ||
1045 (c_attr
.ca_fileid
== hfsmp
->hfs_jnlinfoblkid
))) {
1050 if (returnAttrList
->commonattr
& ATTR_CMN_NAME
) {
1051 cat_convertkey(hfsmp
, key
, rec
, &c_desc
);
1053 c_desc
.cd_cnid
= c_attr
.ca_fileid
;
1054 if (hfsmp
->hfs_flags
& HFS_STANDARD
)
1055 c_desc
.cd_parentcnid
= key
->hfs
.parentID
;
1057 c_desc
.cd_parentcnid
= key
->hfsPlus
.parentID
;
1060 attrblk
.ab_attrlist
= returnAttrList
;
1061 attrblk
.ab_attrbufpp
= &rovingAttributesBuffer
;
1062 attrblk
.ab_varbufpp
= &rovingVariableBuffer
;
1063 attrblk
.ab_flags
= 0;
1064 attrblk
.ab_blocksize
= 0;
1065 attrblk
.ab_context
= vfs_context_current();
1067 hfs_packattrblk(&attrblk
, hfsmp
, NULL
, &c_desc
, &c_attr
, &datafork
, &rsrcfork
, vfs_context_current());
1069 packedBufferSize
= (char*)rovingVariableBuffer
- (char*)attributesBuffer
;
1071 if ( packedBufferSize
> uio_resid(a_uio
) )
1072 return( errSearchBufferFull
);
1076 *((u_int32_t
*)attributesBuffer
) = packedBufferSize
; /* Store length of fixed + var block */
1078 err
= uiomove( (caddr_t
)attributesBuffer
, packedBufferSize
, a_uio
); /* XXX should be packedBufferSize */
1080 cat_releasedesc(&c_desc
);
1087 UnpackSearchAttributeBlock( struct hfsmount
*hfsmp
, struct attrlist
*alist
,
1088 searchinfospec_t
*searchInfo
, void *attributeBuffer
, int firstblock
)
1091 u_int32_t bufferSize
;
1092 boolean_t is_64_bit
;
1094 DBG_ASSERT(searchInfo
!= NULL
);
1096 is_64_bit
= proc_is64bit(current_proc());
1098 bufferSize
= *((u_int32_t
*)attributeBuffer
);
1099 if (bufferSize
== 0)
1100 return (EINVAL
); /* XXX -DJB is a buffer size of zero ever valid for searchfs? */
1102 attributeBuffer
= (u_int32_t
*)attributeBuffer
+ 1; /* advance past the size */
1105 * UnPack common attributes
1107 a
= alist
->commonattr
;
1109 if ( a
& ATTR_CMN_NAME
) {
1111 /* Only use the attrreference_t for the first searchparams */
1115 s
= (char*) attributeBuffer
+ ((attrreference_t
*) attributeBuffer
)->attr_dataoffset
;
1116 len
= ((attrreference_t
*) attributeBuffer
)->attr_length
;
1118 if (len
> sizeof(searchInfo
->name
))
1121 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1122 /* Convert name to pascal string to match HFS B-Tree names */
1125 if (utf8_to_hfs(HFSTOVCB(hfsmp
), len
-1, (u_char
*)s
, (u_char
*)searchInfo
->name
) != 0)
1128 searchInfo
->nameLength
= searchInfo
->name
[0];
1130 searchInfo
->name
[0] = searchInfo
->nameLength
= 0;
1134 /* Convert name to Unicode to match HFS Plus B-Tree names */
1137 if (utf8_decodestr((u_int8_t
*)s
, len
-1, (UniChar
*)searchInfo
->name
, &ucslen
,
1138 sizeof(searchInfo
->name
), ':', UTF_DECOMPOSED
| UTF_ESCAPE_ILLEGAL
))
1141 searchInfo
->nameLength
= ucslen
/ sizeof(UniChar
);
1143 searchInfo
->nameLength
= 0;
1147 attributeBuffer
= (attrreference_t
*) attributeBuffer
+1;
1149 if ( a
& ATTR_CMN_OBJID
) {
1150 searchInfo
->nodeID
= ((fsobj_id_t
*) attributeBuffer
)->fid_objno
; /* ignore fid_generation */
1151 attributeBuffer
= (fsobj_id_t
*)attributeBuffer
+ 1;
1153 if ( a
& ATTR_CMN_PAROBJID
) {
1154 searchInfo
->parentDirID
= ((fsobj_id_t
*) attributeBuffer
)->fid_objno
; /* ignore fid_generation */
1155 attributeBuffer
= (fsobj_id_t
*)attributeBuffer
+ 1;
1158 if ( a
& ATTR_CMN_CRTIME
) {
1160 struct user64_timespec tmp
;
1161 tmp
= *((struct user64_timespec
*)attributeBuffer
);
1162 searchInfo
->creationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1163 searchInfo
->creationDate
.tv_nsec
= tmp
.tv_nsec
;
1164 attributeBuffer
= (struct user64_timespec
*)attributeBuffer
+ 1;
1167 struct user32_timespec tmp
;
1168 tmp
= *((struct user32_timespec
*)attributeBuffer
);
1169 searchInfo
->creationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1170 searchInfo
->creationDate
.tv_nsec
= tmp
.tv_nsec
;
1171 attributeBuffer
= (struct user32_timespec
*)attributeBuffer
+ 1;
1174 if ( a
& ATTR_CMN_MODTIME
) {
1176 struct user64_timespec tmp
;
1177 tmp
= *((struct user64_timespec
*)attributeBuffer
);
1178 searchInfo
->modificationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1179 searchInfo
->modificationDate
.tv_nsec
= tmp
.tv_nsec
;
1180 attributeBuffer
= (struct user64_timespec
*)attributeBuffer
+ 1;
1183 struct user32_timespec tmp
;
1184 tmp
= *((struct user32_timespec
*)attributeBuffer
);
1185 searchInfo
->modificationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1186 searchInfo
->modificationDate
.tv_nsec
= tmp
.tv_nsec
;
1187 attributeBuffer
= (struct user32_timespec
*)attributeBuffer
+ 1;
1190 if ( a
& ATTR_CMN_CHGTIME
) {
1192 struct user64_timespec tmp
;
1193 tmp
= *((struct user64_timespec
*)attributeBuffer
);
1194 searchInfo
->changeDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1195 searchInfo
->changeDate
.tv_nsec
= tmp
.tv_nsec
;
1196 attributeBuffer
= (struct user64_timespec
*)attributeBuffer
+ 1;
1199 struct user32_timespec tmp
;
1200 tmp
= *((struct user32_timespec
*)attributeBuffer
);
1201 searchInfo
->changeDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1202 searchInfo
->changeDate
.tv_nsec
= tmp
.tv_nsec
;
1203 attributeBuffer
= (struct user32_timespec
*)attributeBuffer
+ 1;
1206 if ( a
& ATTR_CMN_ACCTIME
) {
1208 struct user64_timespec tmp
;
1209 tmp
= *((struct user64_timespec
*)attributeBuffer
);
1210 searchInfo
->accessDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1211 searchInfo
->accessDate
.tv_nsec
= tmp
.tv_nsec
;
1212 attributeBuffer
= (struct user64_timespec
*)attributeBuffer
+ 1;
1215 struct user32_timespec tmp
;
1216 tmp
= *((struct user32_timespec
*)attributeBuffer
);
1217 searchInfo
->accessDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1218 searchInfo
->accessDate
.tv_nsec
= tmp
.tv_nsec
;
1219 attributeBuffer
= (struct user32_timespec
*)attributeBuffer
+ 1;
1222 if ( a
& ATTR_CMN_BKUPTIME
) {
1224 struct user64_timespec tmp
;
1225 tmp
= *((struct user64_timespec
*)attributeBuffer
);
1226 searchInfo
->lastBackupDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1227 searchInfo
->lastBackupDate
.tv_nsec
= tmp
.tv_nsec
;
1228 attributeBuffer
= (struct user64_timespec
*)attributeBuffer
+ 1;
1231 struct user32_timespec tmp
;
1232 tmp
= *((struct user32_timespec
*)attributeBuffer
);
1233 searchInfo
->lastBackupDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1234 searchInfo
->lastBackupDate
.tv_nsec
= tmp
.tv_nsec
;
1235 attributeBuffer
= (struct user32_timespec
*)attributeBuffer
+ 1;
1238 if ( a
& ATTR_CMN_FNDRINFO
) {
1239 bcopy( attributeBuffer
, searchInfo
->finderInfo
, sizeof(searchInfo
->finderInfo
) );
1240 attributeBuffer
= (u_int8_t
*)attributeBuffer
+ 32;
1242 if ( a
& ATTR_CMN_OWNERID
) {
1243 searchInfo
->uid
= *((uid_t
*)attributeBuffer
);
1244 attributeBuffer
= (uid_t
*)attributeBuffer
+ 1;
1246 if ( a
& ATTR_CMN_GRPID
) {
1247 searchInfo
->gid
= *((gid_t
*)attributeBuffer
);
1248 attributeBuffer
= (gid_t
*)attributeBuffer
+ 1;
1250 if ( a
& ATTR_CMN_ACCESSMASK
) {
1251 searchInfo
->mask
= *((mode_t
*)attributeBuffer
);
1252 attributeBuffer
= (mode_t
*)attributeBuffer
+ 1;
1254 if ( a
& ATTR_CMN_FILEID
) {
1255 searchInfo
->nodeID
= (u_int32_t
)*((u_int64_t
*) attributeBuffer
);
1256 attributeBuffer
= (u_int64_t
*)attributeBuffer
+ 1;
1258 if ( a
& ATTR_CMN_PARENTID
) {
1259 searchInfo
->parentDirID
= (u_int32_t
)*((u_int64_t
*) attributeBuffer
);
1260 attributeBuffer
= (u_int64_t
*)attributeBuffer
+ 1;
1266 if ( a
& ATTR_DIR_ENTRYCOUNT
) {
1267 searchInfo
->d
.numFiles
= *((u_int32_t
*)attributeBuffer
);
1268 attributeBuffer
= (u_int32_t
*)attributeBuffer
+ 1;
1272 a
= alist
->fileattr
;
1274 if ( a
& ATTR_FILE_DATALENGTH
) {
1275 searchInfo
->f
.dataLogicalLength
= *((off_t
*)attributeBuffer
);
1276 attributeBuffer
= (off_t
*)attributeBuffer
+ 1;
1278 if ( a
& ATTR_FILE_DATAALLOCSIZE
) {
1279 searchInfo
->f
.dataPhysicalLength
= *((off_t
*)attributeBuffer
);
1280 attributeBuffer
= (off_t
*)attributeBuffer
+ 1;
1282 if ( a
& ATTR_FILE_RSRCLENGTH
) {
1283 searchInfo
->f
.resourceLogicalLength
= *((off_t
*)attributeBuffer
);
1284 attributeBuffer
= (off_t
*)attributeBuffer
+ 1;
1286 if ( a
& ATTR_FILE_RSRCALLOCSIZE
) {
1287 searchInfo
->f
.resourcePhysicalLength
= *((off_t
*)attributeBuffer
);
1288 attributeBuffer
= (off_t
*)attributeBuffer
+ 1;