2 * Copyright (c) 1997-2007 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>
53 #include <security/mac_framework.h>
58 #include "hfs_catalog.h"
59 #include "hfs_attrlist.h"
60 #include "hfs_endian.h"
62 #include "hfscommon/headers/FileMgrInternal.h"
63 #include "hfscommon/headers/HFSUnicodeWrappers.h"
64 #include "hfscommon/headers/BTreesPrivate.h"
65 #include "hfscommon/headers/BTreeScanner.h"
66 #include "hfscommon/headers/CatalogPrivate.h"
68 /* Search criterea. */
69 struct directoryInfoSpec
76 off_t dataLogicalLength
;
77 off_t dataPhysicalLength
;
78 off_t resourceLogicalLength
;
79 off_t resourcePhysicalLength
;
84 u_char name
[kHFSPlusMaxFileNameBytes
];
86 char attributes
; // see IM:Files 2-100
88 u_int32_t parentDirID
;
89 struct timespec creationDate
;
90 struct timespec modificationDate
;
91 struct timespec changeDate
;
92 struct timespec accessDate
;
93 struct timespec lastBackupDate
;
94 u_int8_t finderInfo
[32];
98 struct fileInfoSpec f
;
99 struct directoryInfoSpec d
;
101 typedef struct searchinfospec searchinfospec_t
;
103 static void ResolveHardlink(struct hfsmount
*hfsmp
, HFSPlusCatalogFile
*recp
);
106 static int UnpackSearchAttributeBlock(struct hfsmount
*hfsmp
, struct attrlist
*alist
,
107 searchinfospec_t
*searchInfo
, void *attributeBuffer
, int firstblock
);
109 static int CheckCriteria( ExtendedVCB
*vcb
,
111 struct attrlist
*attrList
,
114 searchinfospec_t
*searchInfo1
,
115 searchinfospec_t
*searchInfo2
);
117 static int CheckAccess(ExtendedVCB
*vcb
, u_long searchBits
, CatalogKey
*key
, struct vfs_context
*ctx
);
119 static int InsertMatch(struct hfsmount
*hfsmp
, uio_t a_uio
, CatalogRecord
*rec
,
120 CatalogKey
*key
, struct attrlist
*returnAttrList
,
121 void *attributesBuffer
, void *variableBuffer
,
122 uint32_t * nummatches
);
124 static Boolean
CompareRange(u_long val
, u_long low
, u_long high
);
125 static Boolean
CompareWideRange(u_int64_t val
, u_int64_t low
, u_int64_t high
);
127 static Boolean
CompareRange( u_long val
, u_long low
, u_long high
)
129 return( (val
>= low
) && (val
<= high
) );
132 static Boolean
CompareWideRange( u_int64_t val
, u_int64_t low
, u_int64_t high
)
134 return( (val
>= low
) && (val
<= high
) );
136 //#define CompareRange(val, low, high) ((val >= low) && (val <= high))
139 /************************************************************************/
140 /* Entry for searchfs() */
141 /************************************************************************/
143 #define errSearchBufferFull 101 /* Internal search errors */
152 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 int timerExpired
= 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
)
207 * Reject requests for unsupported attributes.
209 if ((ap
->a_returnattrs
->commonattr
& ~HFS_ATTR_CMN_VALID
) ||
210 (ap
->a_returnattrs
->volattr
!= 0) ||
211 (ap
->a_returnattrs
->dirattr
& ~HFS_ATTR_DIR_VALID
) ||
212 (ap
->a_returnattrs
->fileattr
& ~HFS_ATTR_FILE_VALID
) ||
213 (ap
->a_returnattrs
->forkattr
!= 0)) {
217 /* SRCHFS_SKIPLINKS requires root access.
218 * This option cannot be used with either
219 * the ATTR_CMN_NAME or ATTR_CMN_PAROBJID
222 if (ap
->a_options
& SRCHFS_SKIPLINKS
) {
225 attrs
= ap
->a_searchattrs
->commonattr
| ap
->a_returnattrs
->commonattr
;
226 if (attrs
& (ATTR_CMN_NAME
| ATTR_CMN_PAROBJID
))
228 if ((err
= vfs_context_suser(ap
->a_context
)))
232 // If both 32-bit and 64-bit parent ids or file ids are given
233 // then return an error.
235 attrgroup_t test_attrs
=ap
->a_searchattrs
->commonattr
;
237 if (((test_attrs
& ATTR_CMN_OBJID
) && (test_attrs
& ATTR_CMN_FILEID
)) ||
238 ((test_attrs
& ATTR_CMN_PARENTID
) && (test_attrs
& ATTR_CMN_PAROBJID
)))
242 if (uio_resid(ap
->a_uio
) <= 0)
245 isHFSPlus
= (vcb
->vcbSigWord
== kHFSPlusSigWord
);
246 hfsmp
= VTOHFS(ap
->a_vp
);
248 searchTime
= kMaxMicroSecsInKernel
;
249 if (ap
->a_timelimit
->tv_sec
== 0 &&
250 ap
->a_timelimit
->tv_usec
> 0 &&
251 ap
->a_timelimit
->tv_usec
< kMaxMicroSecsInKernel
) {
252 searchTime
= ap
->a_timelimit
->tv_usec
;
255 /* UnPack the search boundries, searchInfo1, searchInfo2 */
256 err
= UnpackSearchAttributeBlock(hfsmp
, ap
->a_searchattrs
,
257 &searchInfo1
, ap
->a_searchparams1
, 1);
259 err
= UnpackSearchAttributeBlock(hfsmp
, ap
->a_searchattrs
,
260 &searchInfo2
, ap
->a_searchparams2
, 0);
263 //shadow search bits if 64-bit file/parent ids are used
264 if (ap
->a_searchattrs
->commonattr
& ATTR_CMN_FILEID
)
265 ap
->a_searchattrs
->commonattr
|= ATTR_CMN_OBJID
;
266 if (ap
->a_searchattrs
->commonattr
& ATTR_CMN_PARENTID
)
267 ap
->a_searchattrs
->commonattr
|= ATTR_CMN_PAROBJID
;
269 fixedBlockSize
= sizeof(u_int32_t
) + hfs_attrblksize(ap
->a_returnattrs
); /* u_int32_t for length word */
271 eachReturnBufferSize
= fixedBlockSize
;
273 if ( ap
->a_returnattrs
->commonattr
& ATTR_CMN_NAME
) /* XXX should be more robust! */
274 eachReturnBufferSize
+= kHFSPlusMaxFileNameBytes
+ 1;
276 MALLOC( attributesBuffer
, void *, eachReturnBufferSize
, M_TEMP
, M_WAITOK
);
277 if (attributesBuffer
== NULL
) {
279 goto ExitThisRoutine
;
281 variableBuffer
= (void*)((char*) attributesBuffer
+ fixedBlockSize
);
283 // XXXdbg - have to lock the user's buffer so we don't fault
284 // while holding the shared catalog file lock. see the comment
285 // in hfs_readdir() for more details.
287 if (hfsmp
->jnl
&& uio_isuserspace(ap
->a_uio
)) {
288 user_start
= uio_curriovbase(ap
->a_uio
);
289 user_len
= uio_curriovlen(ap
->a_uio
);
291 if ((err
= vslock(user_start
, user_len
)) != 0) {
293 goto ExitThisRoutine
;
297 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
299 catalogFCB
= GetFileControlBlock(vcb
->catalogRefNum
);
300 myCurrentKeyPtr
= NULL
;
301 myCurrentDataPtr
= NULL
;
302 myCatPositionPtr
= (CatPosition
*)ap
->a_searchstate
;
304 if (ap
->a_options
& SRCHFS_START
) {
305 /* Starting a new search. */
306 /* Make sure the on-disk Catalog file is current */
307 (void) hfs_fsync(vcb
->catalogRefNum
, MNT_WAIT
, 0, p
);
309 hfs_systemfile_unlock(hfsmp
, lockflags
);
310 hfs_journal_flush(hfsmp
);
311 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
314 ap
->a_options
&= ~SRCHFS_START
;
315 bzero((caddr_t
)myCatPositionPtr
, sizeof(*myCatPositionPtr
));
316 err
= BTScanInitialize(catalogFCB
, 0, 0, 0, kCatSearchBufferSize
, &myBTScanState
);
318 goto ExitThisRoutine
;
321 /* Resuming a search. */
322 err
= BTScanInitialize(catalogFCB
, myCatPositionPtr
->nextNode
,
323 myCatPositionPtr
->nextRecord
,
324 myCatPositionPtr
->recordsFound
,
325 kCatSearchBufferSize
,
327 /* Make sure Catalog hasn't changed. */
329 && myCatPositionPtr
->writeCount
!= myBTScanState
.btcb
->writeCount
) {
330 myCatPositionPtr
->writeCount
= myBTScanState
.btcb
->writeCount
;
331 err
= EBUSY
; /* catChangedErr */
334 hfs_systemfile_unlock(hfsmp
, lockflags
);
337 goto ExitThisRoutine
;
340 * Check all the catalog btree records...
341 * return the attributes for matching items
344 struct timeval myCurrentTime
;
345 struct timeval myElapsedTime
;
347 err
= BTScanNextRecord(&myBTScanState
, timerExpired
,
348 (void **)&myCurrentKeyPtr
, (void **)&myCurrentDataPtr
,
353 /* Resolve any hardlinks */
354 if (isHFSPlus
&& (ap
->a_options
& SRCHFS_SKIPLINKS
) == 0) {
355 ResolveHardlink(vcb
, (HFSPlusCatalogFile
*)myCurrentDataPtr
);
357 if (CheckCriteria( vcb
, ap
->a_options
, ap
->a_searchattrs
, myCurrentDataPtr
,
358 myCurrentKeyPtr
, &searchInfo1
, &searchInfo2
)
359 && CheckAccess(vcb
, ap
->a_options
, myCurrentKeyPtr
, ap
->a_context
)) {
360 err
= InsertMatch(hfsmp
, ap
->a_uio
, myCurrentDataPtr
,
361 myCurrentKeyPtr
, ap
->a_returnattrs
,
362 attributesBuffer
, variableBuffer
, ap
->a_nummatches
);
365 * The last match didn't fit so come back
366 * to this record on the next trip.
368 --myBTScanState
.recordsFound
;
369 --myBTScanState
.recordNum
;
373 if (*(ap
->a_nummatches
) >= ap
->a_maxmatches
)
378 * Check our elapsed time and bail if we've hit the max.
379 * The idea here is to throttle the amount of time we
380 * spend in the kernel.
382 microuptime(&myCurrentTime
);
383 timersub(&myCurrentTime
, &myBTScanState
.startTime
, &myElapsedTime
);
384 /* Note: assumes kMaxMicroSecsInKernel is less than 1,000,000 */
385 if (myElapsedTime
.tv_sec
> 0
386 || myElapsedTime
.tv_usec
>= searchTime
) {
391 /* Update catalog position */
392 myCatPositionPtr
->writeCount
= myBTScanState
.btcb
->writeCount
;
394 BTScanTerminate(&myBTScanState
, &myCatPositionPtr
->nextNode
,
395 &myCatPositionPtr
->nextRecord
,
396 &myCatPositionPtr
->recordsFound
);
398 if ( err
== E_NONE
) {
399 err
= EAGAIN
; /* signal to the user to call searchfs again */
400 } else if ( err
== errSearchBufferFull
) {
401 if ( *(ap
->a_nummatches
) > 0 )
405 } else if ( err
== btNotFound
) {
406 err
= E_NONE
; /* the entire disk has been searched */
407 } else if ( err
== fsBTTimeOutErr
) {
412 if (attributesBuffer
)
413 FREE(attributesBuffer
, M_TEMP
);
415 if (hfsmp
->jnl
&& user_start
) {
416 vsunlock(user_start
, user_len
, TRUE
);
419 return (MacToVFSError(err
));
424 ResolveHardlink(struct hfsmount
*hfsmp
, HFSPlusCatalogFile
*recp
)
426 u_int32_t type
, creator
;
429 time_t filecreatedate
;
431 if (recp
->recordType
!= kHFSPlusFileRecord
) {
434 type
= SWAP_BE32(recp
->userInfo
.fdType
);
435 creator
= SWAP_BE32(recp
->userInfo
.fdCreator
);
436 filecreatedate
= to_bsd_time(recp
->createDate
);
438 if ((type
== kHardLinkFileType
&& creator
== kHFSPlusCreator
) &&
439 (filecreatedate
== (time_t)hfsmp
->vcbCrDate
||
440 filecreatedate
== (time_t)hfsmp
->hfs_metadata_createdate
)) {
442 } else if ((type
== kHFSAliasType
&& creator
== kHFSAliasCreator
) &&
443 (recp
->flags
& kHFSHasLinkChainMask
) &&
444 (filecreatedate
== (time_t)hfsmp
->vcbCrDate
||
445 filecreatedate
== (time_t)hfsmp
->hfs_metadata_createdate
)) {
449 if (isfilelink
|| isdirlink
) {
453 /* Export link's cnid (a unique value) instead of inode's cnid */
454 saved_cnid
= recp
->fileID
;
455 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
457 (void) cat_resolvelink(hfsmp
, recp
->hl_linkReference
, isdirlink
, recp
);
459 recp
->fileID
= saved_cnid
;
460 hfs_systemfile_unlock(hfsmp
, lockflags
);
466 CompareMasked(const u_int32_t
*thisValue
, const u_int32_t
*compareData
,
467 const u_int32_t
*compareMask
, u_int32_t count
)
472 matched
= true; /* Assume it will all match */
474 for (i
=0; i
<count
; i
++) {
475 if (((*thisValue
++ ^ *compareData
++) & *compareMask
++) != 0) {
486 ComparePartialUnicodeName (register ConstUniCharArrayPtr str
, register ItemCount s_len
,
487 register ConstUniCharArrayPtr find
, register ItemCount f_len
)
489 if (f_len
== 0 || s_len
== 0)
495 } while (FastUnicodeCompare(str
++, f_len
, find
, f_len
) != 0);
502 ComparePartialPascalName ( register ConstStr31Param str
, register ConstStr31Param find
)
504 register u_char s_len
= str
[0];
505 register u_char f_len
= find
[0];
506 register u_char
*tsp
;
509 if (f_len
== 0 || s_len
== 0)
512 bcopy(str
, tmpstr
, s_len
+ 1);
515 while (s_len
-- >= f_len
) {
518 if (FastRelString(tsp
++, find
) == 0)
527 * Check to see if caller has access rights to this item
531 CheckAccess(ExtendedVCB
*theVCBPtr
, u_long searchBits
, CatalogKey
*theKeyPtr
, struct vfs_context
*ctx
)
536 HFSCatalogNodeID myNodeID
;
538 struct FndrDirInfo
*finfop
;
539 struct vnode
* vp
= NULL
;
541 myResult
= 0; /* default to "no access" */
543 if (!vfs_context_suser(ctx
)) {
544 myResult
= 1; /* allow access */
545 goto ExitThisRoutine
; /* root always has access */
548 hfsmp
= VCBTOHFS( theVCBPtr
);
549 isHFSPlus
= ( theVCBPtr
->vcbSigWord
== kHFSPlusSigWord
);
551 myNodeID
= theKeyPtr
->hfsPlus
.parentID
;
553 myNodeID
= theKeyPtr
->hfs
.parentID
;
555 while ( myNodeID
>= kRootDirID
) {
558 /* now go get catalog data for this directory */
559 myErr
= hfs_vget(hfsmp
, myNodeID
, &vp
, 0);
561 goto ExitThisRoutine
; /* no access */
565 finfop
= (struct FndrDirInfo
*)&cp
->c_attr
.ca_finderinfo
[0];
567 if ( searchBits
& SRCHFS_SKIPPACKAGES
) {
568 if ( (SWAP_BE16(finfop
->frFlags
) & kHasBundle
)
569 || (cp
->c_desc
.cd_nameptr
!= NULL
570 && is_package_name((const char *)cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
)) ) {
572 goto ExitThisRoutine
;
576 if ( searchBits
& SRCHFS_SKIPINAPPROPRIATE
) {
577 if ( cp
->c_parentcnid
== kRootDirID
&& cp
->c_desc
.cd_nameptr
!= NULL
&&
578 vn_searchfs_inappropriate_name((const char *)cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
) ) {
580 goto ExitThisRoutine
;
584 if ( (searchBits
& SRCHFS_SKIPINVISIBLE
) &&
585 (SWAP_BE16(finfop
->frFlags
) & kIsInvisible
) ) {
587 goto ExitThisRoutine
;
590 myNodeID
= cp
->c_parentcnid
; /* move up the hierarchy */
591 hfs_unlock(VTOC(vp
));
594 if (vp
->v_type
== VDIR
) {
595 myErr
= mac_vnode_check_readdir(ctx
, vp
);
597 myErr
= mac_vnode_check_stat(ctx
, NOCRED
, vp
);
602 goto ExitThisRoutine
;
606 if (vp
->v_type
== VDIR
) {
607 myErr
= vnode_authorize(vp
, NULL
, (KAUTH_VNODE_SEARCH
| KAUTH_VNODE_LIST_DIRECTORY
), ctx
);
609 myErr
= vnode_authorize(vp
, NULL
, (KAUTH_VNODE_SEARCH
), ctx
);
614 goto ExitThisRoutine
; /* no access */
617 myResult
= 1; /* allow access */
621 hfs_unlock(VTOC(vp
));
629 CheckCriteria( ExtendedVCB
*vcb
,
631 struct attrlist
*attrList
,
634 searchinfospec_t
*searchInfo1
,
635 searchinfospec_t
*searchInfo2
)
637 Boolean matched
, atleastone
;
639 attrgroup_t searchAttributes
;
640 struct cat_attr c_attr
;
641 struct cat_fork datafork
;
642 struct cat_fork rsrcfork
;
644 bzero(&c_attr
, sizeof(c_attr
));
645 isHFSPlus
= (vcb
->vcbSigWord
== kHFSPlusSigWord
);
647 switch (rec
->recordType
) {
648 case kHFSFolderRecord
:
649 case kHFSPlusFolderRecord
:
650 if ( (searchBits
& SRCHFS_MATCHDIRS
) == 0 ) { /* If we are NOT searching folders */
657 if ( (searchBits
& SRCHFS_MATCHFILES
) == 0 ) { /* If we are NOT searching files */
663 case kHFSPlusFileRecord
:
664 /* Check if hardlink links should be skipped. */
665 if (searchBits
& SRCHFS_SKIPLINKS
) {
666 cnid_t parid
= key
->hfsPlus
.parentID
;
667 HFSPlusCatalogFile
*filep
= (HFSPlusCatalogFile
*)rec
;
669 if ((SWAP_BE32(filep
->userInfo
.fdType
) == kHardLinkFileType
) &&
670 (SWAP_BE32(filep
->userInfo
.fdCreator
) == kHFSPlusCreator
)) {
671 return (false); /* skip over file link records */
672 } else if ((parid
== vcb
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) &&
673 (filep
->bsdInfo
.special
.linkCount
== 0)) {
674 return (false); /* skip over unlinked files */
675 } else if ((SWAP_BE32(filep
->userInfo
.fdType
) == kHFSAliasType
) &&
676 (SWAP_BE32(filep
->userInfo
.fdCreator
) == kHFSAliasCreator
) &&
677 (filep
->flags
& kHFSHasLinkChainMask
)) {
678 return (false); /* skip over dir link records */
680 } else if (key
->hfsPlus
.parentID
== vcb
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) {
681 return (false); /* skip over private files */
682 } else if (key
->hfsPlus
.parentID
== vcb
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) {
683 return (false); /* skip over private files */
686 if ( (searchBits
& SRCHFS_MATCHFILES
) == 0 ) { /* If we are NOT searching files */
692 default: /* Never match a thread record or any other type. */
693 return( false ); /* Not a file or folder record, so can't search it */
696 matched
= true; /* Assume we got a match */
697 atleastone
= false; /* Dont insert unless we match at least one criteria */
699 /* First, attempt to match the name -- either partial or complete */
700 if ( attrList
->commonattr
& ATTR_CMN_NAME
) {
702 /* Check for partial/full HFS Plus name match */
704 if ( searchBits
& SRCHFS_MATCHPARTIALNAMES
) {
705 matched
= ComparePartialUnicodeName(key
->hfsPlus
.nodeName
.unicode
,
706 key
->hfsPlus
.nodeName
.length
,
707 (UniChar
*)searchInfo1
->name
,
708 searchInfo1
->nameLength
);
709 } else /* full HFS Plus name match */ {
710 matched
= (FastUnicodeCompare(key
->hfsPlus
.nodeName
.unicode
,
711 key
->hfsPlus
.nodeName
.length
,
712 (UniChar
*)searchInfo1
->name
,
713 searchInfo1
->nameLength
) == 0);
716 /* Check for partial/full HFS name match */
718 if ( searchBits
& SRCHFS_MATCHPARTIALNAMES
)
719 matched
= ComparePartialPascalName(key
->hfs
.nodeName
, (u_char
*)searchInfo1
->name
);
720 else /* full HFS name match */
721 matched
= (FastRelString(key
->hfs
.nodeName
, (u_char
*)searchInfo1
->name
) == 0);
724 if ( matched
== false || (searchBits
& ~SRCHFS_MATCHPARTIALNAMES
) == 0 )
725 goto TestDone
; /* no match, or nothing more to compare */
730 /* Convert catalog record into cat_attr format. */
731 cat_convertattr(VCBTOHFS(vcb
), rec
, &c_attr
, &datafork
, &rsrcfork
);
733 if (searchBits
& SRCHFS_SKIPINVISIBLE
) {
736 switch (rec
->recordType
) {
737 case kHFSFolderRecord
:
738 case kHFSPlusFolderRecord
: {
739 struct FndrDirInfo
*finder_info
;
741 finder_info
= (struct FndrDirInfo
*)&c_attr
.ca_finderinfo
[0];
742 flags
= SWAP_BE16(finder_info
->frFlags
);
747 case kHFSPlusFileRecord
: {
748 struct FndrFileInfo
*finder_info
;
750 finder_info
= (struct FndrFileInfo
*)&c_attr
.ca_finderinfo
[0];
751 flags
= SWAP_BE16(finder_info
->fdFlags
);
756 flags
= kIsInvisible
;
761 if (flags
& kIsInvisible
) {
769 /* Now that we have a record worth searching, see if it matches the search attributes */
770 if (rec
->recordType
== kHFSFileRecord
||
771 rec
->recordType
== kHFSPlusFileRecord
) {
772 if ((attrList
->fileattr
& ~ATTR_FILE_VALIDMASK
) != 0) { /* attr we do know about */
776 else if ((attrList
->fileattr
& ATTR_FILE_VALIDMASK
) != 0) {
777 searchAttributes
= attrList
->fileattr
;
780 if ( c_attr
.ca_flags
& UF_COMPRESSED
) {
781 /* for compressed files, set the data length to the uncompressed data size */
782 if (( searchAttributes
& ATTR_FILE_DATALENGTH
) ||
783 ( searchAttributes
& ATTR_FILE_DATAALLOCSIZE
) ) {
784 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 */
785 datafork
.cf_blocks
= rsrcfork
.cf_blocks
;
788 /* treat compressed files as if their resource fork is empty */
789 if (( searchAttributes
& ATTR_FILE_RSRCLENGTH
) ||
790 ( searchAttributes
& ATTR_FILE_RSRCALLOCSIZE
) ) {
791 rsrcfork
.cf_size
= 0;
792 rsrcfork
.cf_blocks
= 0;
795 #endif /* HFS_COMPRESSION */
797 /* File logical length (data fork) */
798 if ( searchAttributes
& ATTR_FILE_DATALENGTH
) {
799 matched
= CompareWideRange(
801 searchInfo1
->f
.dataLogicalLength
,
802 searchInfo2
->f
.dataLogicalLength
);
803 if (matched
== false) goto TestDone
;
807 /* File physical length (data fork) */
808 if ( searchAttributes
& ATTR_FILE_DATAALLOCSIZE
) {
809 matched
= CompareWideRange(
810 (u_int64_t
)datafork
.cf_blocks
* (u_int64_t
)vcb
->blockSize
,
811 searchInfo1
->f
.dataPhysicalLength
,
812 searchInfo2
->f
.dataPhysicalLength
);
813 if (matched
== false) goto TestDone
;
817 /* File logical length (resource fork) */
818 if ( searchAttributes
& ATTR_FILE_RSRCLENGTH
) {
819 matched
= CompareWideRange(
821 searchInfo1
->f
.resourceLogicalLength
,
822 searchInfo2
->f
.resourceLogicalLength
);
823 if (matched
== false) goto TestDone
;
827 /* File physical length (resource fork) */
828 if ( searchAttributes
& ATTR_FILE_RSRCALLOCSIZE
) {
829 matched
= CompareWideRange(
830 (u_int64_t
)rsrcfork
.cf_blocks
* (u_int64_t
)vcb
->blockSize
,
831 searchInfo1
->f
.resourcePhysicalLength
,
832 searchInfo2
->f
.resourcePhysicalLength
);
833 if (matched
== false) goto TestDone
;
838 atleastone
= true; /* to match SRCHFS_MATCHFILES */
842 * Check the directory attributes
844 else if (rec
->recordType
== kHFSFolderRecord
||
845 rec
->recordType
== kHFSPlusFolderRecord
) {
846 if ((attrList
->dirattr
& ~ATTR_DIR_VALIDMASK
) != 0) { /* attr we do know about */
850 else if ((attrList
->dirattr
& ATTR_DIR_VALIDMASK
) != 0) {
851 searchAttributes
= attrList
->dirattr
;
853 /* Directory valence */
854 if ( searchAttributes
& ATTR_DIR_ENTRYCOUNT
) {
855 matched
= CompareRange(c_attr
.ca_entries
,
856 searchInfo1
->d
.numFiles
,
857 searchInfo2
->d
.numFiles
);
858 if (matched
== false) goto TestDone
;
863 atleastone
= true; /* to match SRCHFS_MATCHDIRS */
868 * Check the common attributes
870 searchAttributes
= attrList
->commonattr
;
871 if ( (searchAttributes
& ATTR_CMN_VALIDMASK
) != 0 ) {
873 if ( searchAttributes
& ATTR_CMN_OBJID
) {
874 matched
= CompareRange(c_attr
.ca_fileid
,
876 searchInfo2
->nodeID
);
877 if (matched
== false) goto TestDone
;
882 if ( searchAttributes
& ATTR_CMN_PAROBJID
) {
883 HFSCatalogNodeID parentID
;
886 parentID
= key
->hfsPlus
.parentID
;
888 parentID
= key
->hfs
.parentID
;
890 matched
= CompareRange(parentID
, searchInfo1
->parentDirID
,
891 searchInfo2
->parentDirID
);
892 if (matched
== false) goto TestDone
;
896 /* Finder Info & Extended Finder Info where extFinderInfo is last 32 bytes */
897 if ( searchAttributes
& ATTR_CMN_FNDRINFO
) {
898 u_int32_t
*thisValue
;
899 thisValue
= (u_int32_t
*) &c_attr
.ca_finderinfo
;
902 * Note: ioFlFndrInfo and ioDrUsrWds have the same offset in search info, so
903 * no need to test the object type here.
905 matched
= CompareMasked(thisValue
,
906 (u_int32_t
*)&searchInfo1
->finderInfo
,
907 (u_int32_t
*) &searchInfo2
->finderInfo
, 8);
908 if (matched
== false) goto TestDone
;
913 if ( searchAttributes
& ATTR_CMN_CRTIME
) {
914 matched
= CompareRange(c_attr
.ca_itime
,
915 searchInfo1
->creationDate
.tv_sec
,
916 searchInfo2
->creationDate
.tv_sec
);
917 if (matched
== false) goto TestDone
;
922 if ( searchAttributes
& ATTR_CMN_MODTIME
) {
923 matched
= CompareRange(c_attr
.ca_mtime
,
924 searchInfo1
->modificationDate
.tv_sec
,
925 searchInfo2
->modificationDate
.tv_sec
);
926 if (matched
== false) goto TestDone
;
931 if ( searchAttributes
& ATTR_CMN_CHGTIME
) {
932 matched
= CompareRange(c_attr
.ca_ctime
,
933 searchInfo1
->changeDate
.tv_sec
,
934 searchInfo2
->changeDate
.tv_sec
);
935 if (matched
== false) goto TestDone
;
940 if ( searchAttributes
& ATTR_CMN_ACCTIME
) {
941 matched
= CompareRange(c_attr
.ca_atime
,
942 searchInfo1
->accessDate
.tv_sec
,
943 searchInfo2
->accessDate
.tv_sec
);
944 if (matched
== false) goto TestDone
;
949 if ( searchAttributes
& ATTR_CMN_BKUPTIME
) {
950 matched
= CompareRange(c_attr
.ca_btime
,
951 searchInfo1
->lastBackupDate
.tv_sec
,
952 searchInfo2
->lastBackupDate
.tv_sec
);
953 if (matched
== false) goto TestDone
;
958 if ( searchAttributes
& ATTR_CMN_OWNERID
) {
959 matched
= CompareRange(c_attr
.ca_uid
,
960 searchInfo1
->uid
, searchInfo2
->uid
);
961 if (matched
== false) goto TestDone
;
966 if ( searchAttributes
& ATTR_CMN_GRPID
) {
967 matched
= CompareRange(c_attr
.ca_gid
,
968 searchInfo1
->gid
, searchInfo2
->gid
);
969 if (matched
== false) goto TestDone
;
974 if ( searchAttributes
& ATTR_CMN_ACCESSMASK
) {
975 matched
= CompareRange((u_int32_t
)c_attr
.ca_mode
,
976 (u_int32_t
)searchInfo1
->mask
,
977 (u_int32_t
)searchInfo2
->mask
);
978 if (matched
== false) goto TestDone
;
983 /* If we got here w/o matching any, then set to false */
989 * Finally, determine whether we need to negate the sense of the match
990 * (i.e. find all objects that DON'T match).
992 if ( searchBits
& SRCHFS_NEGATEPARAMS
)
1000 * Adds another record to the packed array for output
1003 InsertMatch(struct hfsmount
*hfsmp
, uio_t a_uio
, CatalogRecord
*rec
,
1004 CatalogKey
*key
, struct attrlist
*returnAttrList
,
1005 void *attributesBuffer
, void *variableBuffer
, uint32_t * nummatches
)
1008 void *rovingAttributesBuffer
;
1009 void *rovingVariableBuffer
;
1010 long packedBufferSize
;
1011 struct attrblock attrblk
;
1012 struct cat_desc c_desc
;
1013 struct cat_attr c_attr
;
1014 struct cat_fork datafork
;
1015 struct cat_fork rsrcfork
;
1017 bzero(&c_desc
, sizeof(c_desc
));
1018 bzero(&c_attr
, sizeof(c_attr
));
1019 rovingAttributesBuffer
= (char*)attributesBuffer
+ sizeof(u_int32_t
); /* Reserve space for length field */
1020 rovingVariableBuffer
= variableBuffer
;
1022 /* Convert catalog record into cat_attr format. */
1023 cat_convertattr(hfsmp
, rec
, &c_attr
, &datafork
, &rsrcfork
);
1025 /* Hide our private meta data directories */
1026 if (c_attr
.ca_fileid
== hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
||
1027 c_attr
.ca_fileid
== hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) {
1032 /* Hide the private journal files */
1034 ((c_attr
.ca_fileid
== hfsmp
->hfs_jnlfileid
) ||
1035 (c_attr
.ca_fileid
== hfsmp
->hfs_jnlinfoblkid
))) {
1040 if (returnAttrList
->commonattr
& ATTR_CMN_NAME
) {
1041 cat_convertkey(hfsmp
, key
, rec
, &c_desc
);
1043 c_desc
.cd_cnid
= c_attr
.ca_fileid
;
1044 if (hfsmp
->hfs_flags
& HFS_STANDARD
)
1045 c_desc
.cd_parentcnid
= key
->hfs
.parentID
;
1047 c_desc
.cd_parentcnid
= key
->hfsPlus
.parentID
;
1050 attrblk
.ab_attrlist
= returnAttrList
;
1051 attrblk
.ab_attrbufpp
= &rovingAttributesBuffer
;
1052 attrblk
.ab_varbufpp
= &rovingVariableBuffer
;
1053 attrblk
.ab_flags
= 0;
1054 attrblk
.ab_blocksize
= 0;
1055 attrblk
.ab_context
= vfs_context_current();
1057 hfs_packattrblk(&attrblk
, hfsmp
, NULL
, &c_desc
, &c_attr
, &datafork
, &rsrcfork
, vfs_context_current());
1059 packedBufferSize
= (char*)rovingVariableBuffer
- (char*)attributesBuffer
;
1061 if ( packedBufferSize
> uio_resid(a_uio
) )
1062 return( errSearchBufferFull
);
1066 *((u_int32_t
*)attributesBuffer
) = packedBufferSize
; /* Store length of fixed + var block */
1068 err
= uiomove( (caddr_t
)attributesBuffer
, packedBufferSize
, a_uio
); /* XXX should be packedBufferSize */
1070 cat_releasedesc(&c_desc
);
1077 UnpackSearchAttributeBlock( struct hfsmount
*hfsmp
, struct attrlist
*alist
,
1078 searchinfospec_t
*searchInfo
, void *attributeBuffer
, int firstblock
)
1081 u_int32_t bufferSize
;
1082 boolean_t is_64_bit
;
1084 DBG_ASSERT(searchInfo
!= NULL
);
1086 is_64_bit
= proc_is64bit(current_proc());
1088 bufferSize
= *((u_int32_t
*)attributeBuffer
);
1089 if (bufferSize
== 0)
1090 return (EINVAL
); /* XXX -DJB is a buffer size of zero ever valid for searchfs? */
1092 attributeBuffer
= (u_int32_t
*)attributeBuffer
+ 1; /* advance past the size */
1095 * UnPack common attributes
1097 a
= alist
->commonattr
;
1099 if ( a
& ATTR_CMN_NAME
) {
1101 /* Only use the attrreference_t for the first searchparams */
1105 s
= (char*) attributeBuffer
+ ((attrreference_t
*) attributeBuffer
)->attr_dataoffset
;
1106 len
= ((attrreference_t
*) attributeBuffer
)->attr_length
;
1108 if (len
> sizeof(searchInfo
->name
))
1111 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1112 /* Convert name to pascal string to match HFS B-Tree names */
1115 if (utf8_to_hfs(HFSTOVCB(hfsmp
), len
-1, (u_char
*)s
, (u_char
*)searchInfo
->name
) != 0)
1118 searchInfo
->nameLength
= searchInfo
->name
[0];
1120 searchInfo
->name
[0] = searchInfo
->nameLength
= 0;
1124 /* Convert name to Unicode to match HFS Plus B-Tree names */
1127 if (utf8_decodestr((u_int8_t
*)s
, len
-1, (UniChar
*)searchInfo
->name
, &ucslen
,
1128 sizeof(searchInfo
->name
), ':', UTF_DECOMPOSED
| UTF_ESCAPE_ILLEGAL
))
1131 searchInfo
->nameLength
= ucslen
/ sizeof(UniChar
);
1133 searchInfo
->nameLength
= 0;
1137 attributeBuffer
= (attrreference_t
*) attributeBuffer
+1;
1139 if ( a
& ATTR_CMN_OBJID
) {
1140 searchInfo
->nodeID
= ((fsobj_id_t
*) attributeBuffer
)->fid_objno
; /* ignore fid_generation */
1141 attributeBuffer
= (fsobj_id_t
*)attributeBuffer
+ 1;
1143 if ( a
& ATTR_CMN_PAROBJID
) {
1144 searchInfo
->parentDirID
= ((fsobj_id_t
*) attributeBuffer
)->fid_objno
; /* ignore fid_generation */
1145 attributeBuffer
= (fsobj_id_t
*)attributeBuffer
+ 1;
1148 if ( a
& ATTR_CMN_CRTIME
) {
1150 struct user64_timespec tmp
;
1151 tmp
= *((struct user64_timespec
*)attributeBuffer
);
1152 searchInfo
->creationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1153 searchInfo
->creationDate
.tv_nsec
= tmp
.tv_nsec
;
1154 attributeBuffer
= (struct user64_timespec
*)attributeBuffer
+ 1;
1157 struct user32_timespec tmp
;
1158 tmp
= *((struct user32_timespec
*)attributeBuffer
);
1159 searchInfo
->creationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1160 searchInfo
->creationDate
.tv_nsec
= tmp
.tv_nsec
;
1161 attributeBuffer
= (struct user32_timespec
*)attributeBuffer
+ 1;
1164 if ( a
& ATTR_CMN_MODTIME
) {
1166 struct user64_timespec tmp
;
1167 tmp
= *((struct user64_timespec
*)attributeBuffer
);
1168 searchInfo
->modificationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1169 searchInfo
->modificationDate
.tv_nsec
= tmp
.tv_nsec
;
1170 attributeBuffer
= (struct user64_timespec
*)attributeBuffer
+ 1;
1173 struct user32_timespec tmp
;
1174 tmp
= *((struct user32_timespec
*)attributeBuffer
);
1175 searchInfo
->modificationDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1176 searchInfo
->modificationDate
.tv_nsec
= tmp
.tv_nsec
;
1177 attributeBuffer
= (struct user32_timespec
*)attributeBuffer
+ 1;
1180 if ( a
& ATTR_CMN_CHGTIME
) {
1182 struct user64_timespec tmp
;
1183 tmp
= *((struct user64_timespec
*)attributeBuffer
);
1184 searchInfo
->changeDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1185 searchInfo
->changeDate
.tv_nsec
= tmp
.tv_nsec
;
1186 attributeBuffer
= (struct user64_timespec
*)attributeBuffer
+ 1;
1189 struct user32_timespec tmp
;
1190 tmp
= *((struct user32_timespec
*)attributeBuffer
);
1191 searchInfo
->changeDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1192 searchInfo
->changeDate
.tv_nsec
= tmp
.tv_nsec
;
1193 attributeBuffer
= (struct user32_timespec
*)attributeBuffer
+ 1;
1196 if ( a
& ATTR_CMN_ACCTIME
) {
1198 struct user64_timespec tmp
;
1199 tmp
= *((struct user64_timespec
*)attributeBuffer
);
1200 searchInfo
->accessDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1201 searchInfo
->accessDate
.tv_nsec
= tmp
.tv_nsec
;
1202 attributeBuffer
= (struct user64_timespec
*)attributeBuffer
+ 1;
1205 struct user32_timespec tmp
;
1206 tmp
= *((struct user32_timespec
*)attributeBuffer
);
1207 searchInfo
->accessDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1208 searchInfo
->accessDate
.tv_nsec
= tmp
.tv_nsec
;
1209 attributeBuffer
= (struct user32_timespec
*)attributeBuffer
+ 1;
1212 if ( a
& ATTR_CMN_BKUPTIME
) {
1214 struct user64_timespec tmp
;
1215 tmp
= *((struct user64_timespec
*)attributeBuffer
);
1216 searchInfo
->lastBackupDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1217 searchInfo
->lastBackupDate
.tv_nsec
= tmp
.tv_nsec
;
1218 attributeBuffer
= (struct user64_timespec
*)attributeBuffer
+ 1;
1221 struct user32_timespec tmp
;
1222 tmp
= *((struct user32_timespec
*)attributeBuffer
);
1223 searchInfo
->lastBackupDate
.tv_sec
= (time_t)tmp
.tv_sec
;
1224 searchInfo
->lastBackupDate
.tv_nsec
= tmp
.tv_nsec
;
1225 attributeBuffer
= (struct user32_timespec
*)attributeBuffer
+ 1;
1228 if ( a
& ATTR_CMN_FNDRINFO
) {
1229 bcopy( attributeBuffer
, searchInfo
->finderInfo
, sizeof(searchInfo
->finderInfo
) );
1230 attributeBuffer
= (u_int8_t
*)attributeBuffer
+ 32;
1232 if ( a
& ATTR_CMN_OWNERID
) {
1233 searchInfo
->uid
= *((uid_t
*)attributeBuffer
);
1234 attributeBuffer
= (uid_t
*)attributeBuffer
+ 1;
1236 if ( a
& ATTR_CMN_GRPID
) {
1237 searchInfo
->gid
= *((gid_t
*)attributeBuffer
);
1238 attributeBuffer
= (gid_t
*)attributeBuffer
+ 1;
1240 if ( a
& ATTR_CMN_ACCESSMASK
) {
1241 searchInfo
->mask
= *((mode_t
*)attributeBuffer
);
1242 attributeBuffer
= (mode_t
*)attributeBuffer
+ 1;
1244 if ( a
& ATTR_CMN_FILEID
) {
1245 searchInfo
->nodeID
= (u_int32_t
)*((u_int64_t
*) attributeBuffer
);
1246 attributeBuffer
= (u_int64_t
*)attributeBuffer
+ 1;
1248 if ( a
& ATTR_CMN_PARENTID
) {
1249 searchInfo
->parentDirID
= (u_int32_t
)*((u_int64_t
*) attributeBuffer
);
1250 attributeBuffer
= (u_int64_t
*)attributeBuffer
+ 1;
1256 if ( a
& ATTR_DIR_ENTRYCOUNT
) {
1257 searchInfo
->d
.numFiles
= *((u_int32_t
*)attributeBuffer
);
1258 attributeBuffer
= (u_int32_t
*)attributeBuffer
+ 1;
1262 a
= alist
->fileattr
;
1264 if ( a
& ATTR_FILE_DATALENGTH
) {
1265 searchInfo
->f
.dataLogicalLength
= *((off_t
*)attributeBuffer
);
1266 attributeBuffer
= (off_t
*)attributeBuffer
+ 1;
1268 if ( a
& ATTR_FILE_DATAALLOCSIZE
) {
1269 searchInfo
->f
.dataPhysicalLength
= *((off_t
*)attributeBuffer
);
1270 attributeBuffer
= (off_t
*)attributeBuffer
+ 1;
1272 if ( a
& ATTR_FILE_RSRCLENGTH
) {
1273 searchInfo
->f
.resourceLogicalLength
= *((off_t
*)attributeBuffer
);
1274 attributeBuffer
= (off_t
*)attributeBuffer
+ 1;
1276 if ( a
& ATTR_FILE_RSRCALLOCSIZE
) {
1277 searchInfo
->f
.resourcePhysicalLength
= *((off_t
*)attributeBuffer
);
1278 attributeBuffer
= (off_t
*)attributeBuffer
+ 1;