]> git.saurik.com Git - apple/xnu.git/blame - bsd/hfs/hfs_search.c
xnu-3248.60.10.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_search.c
CommitLineData
1c79356b 1/*
39236c6e 2 * Copyright (c) 1997-2013 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 *
9bccf70c 28 * @(#)hfs_search.c
1c79356b 29 */
2d21ac55
A
30/*
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,
34 * Version 2.0.
35 */
1c79356b
A
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/file.h>
1c79356b
A
41#include <sys/proc.h>
42#include <sys/conf.h>
43#include <mach/machine/vm_types.h>
44#include <sys/vnode.h>
45#include <sys/malloc.h>
46#include <sys/signalvar.h>
47#include <sys/attr.h>
48#include <sys/utfconv.h>
91447636
A
49#include <sys/kauth.h>
50#include <sys/vnode_internal.h>
6d2010ae 51#include <sys/mount_internal.h>
1c79356b 52
2d21ac55
A
53#if CONFIG_MACF
54#include <security/mac_framework.h>
55#endif
56
1c79356b
A
57#include "hfs.h"
58#include "hfs_dbg.h"
9bccf70c
A
59#include "hfs_catalog.h"
60#include "hfs_attrlist.h"
61#include "hfs_endian.h"
62
1c79356b 63#include "hfscommon/headers/FileMgrInternal.h"
1c79356b 64#include "hfscommon/headers/HFSUnicodeWrappers.h"
14353aa8
A
65#include "hfscommon/headers/BTreesPrivate.h"
66#include "hfscommon/headers/BTreeScanner.h"
91447636 67#include "hfscommon/headers/CatalogPrivate.h"
1c79356b 68
316670eb
A
69#if CONFIG_SEARCHFS
70
9bccf70c
A
71/* Search criterea. */
72struct directoryInfoSpec
73{
91447636 74 u_int32_t numFiles;
9bccf70c
A
75};
76
77struct fileInfoSpec
78{
79 off_t dataLogicalLength;
80 off_t dataPhysicalLength;
81 off_t resourceLogicalLength;
82 off_t resourcePhysicalLength;
83};
84
85struct searchinfospec
86{
87 u_char name[kHFSPlusMaxFileNameBytes];
b0d623f7 88 u_int32_t nameLength;
9bccf70c 89 char attributes; // see IM:Files 2-100
b0d623f7
A
90 u_int32_t nodeID;
91 u_int32_t parentDirID;
9bccf70c
A
92 struct timespec creationDate;
93 struct timespec modificationDate;
94 struct timespec changeDate;
95 struct timespec accessDate;
96 struct timespec lastBackupDate;
2d21ac55 97 u_int8_t finderInfo[32];
9bccf70c
A
98 uid_t uid;
99 gid_t gid;
100 mode_t mask;
101 struct fileInfoSpec f;
102 struct directoryInfoSpec d;
103};
104typedef struct searchinfospec searchinfospec_t;
1c79356b 105
2d21ac55 106static void ResolveHardlink(struct hfsmount *hfsmp, HFSPlusCatalogFile *recp);
1c79356b 107
1c79356b 108
91447636 109static int UnpackSearchAttributeBlock(struct hfsmount *hfsmp, struct attrlist *alist,
b0d623f7 110 searchinfospec_t *searchInfo, void *attributeBuffer, int firstblock);
9bccf70c
A
111
112static int CheckCriteria( ExtendedVCB *vcb,
113 u_long searchBits,
114 struct attrlist *attrList,
115 CatalogRecord *rec,
116 CatalogKey *key,
117 searchinfospec_t *searchInfo1,
39236c6e
A
118 searchinfospec_t *searchInfo2,
119 struct vfs_context *ctx);
9bccf70c 120
13fec989 121static int CheckAccess(ExtendedVCB *vcb, u_long searchBits, CatalogKey *key, struct vfs_context *ctx);
9bccf70c 122
91447636 123static int InsertMatch(struct hfsmount *hfsmp, uio_t a_uio, CatalogRecord *rec,
1c79356b
A
124 CatalogKey *key, struct attrlist *returnAttrList,
125 void *attributesBuffer, void *variableBuffer,
b0d623f7 126 uint32_t * nummatches );
1c79356b
A
127
128static Boolean CompareRange(u_long val, u_long low, u_long high);
129static Boolean CompareWideRange(u_int64_t val, u_int64_t low, u_int64_t high);
130
131static Boolean CompareRange( u_long val, u_long low, u_long high )
132{
133 return( (val >= low) && (val <= high) );
134}
135
136static Boolean CompareWideRange( u_int64_t val, u_int64_t low, u_int64_t high )
137{
138 return( (val >= low) && (val <= high) );
139}
140//#define CompareRange(val, low, high) ((val >= low) && (val <= high))
9bccf70c 141
1c79356b
A
142
143/************************************************************************/
9bccf70c 144/* Entry for searchfs() */
1c79356b
A
145/************************************************************************/
146
147#define errSearchBufferFull 101 /* Internal search errors */
148/*
149#
150#% searchfs vp L L L
151#
91447636 152vnop_searchfs {
1c79356b
A
153 IN struct vnode *vp;
154 IN off_t length;
155 IN int flags;
91447636 156 IN kauth_cred_t cred;
1c79356b
A
157 IN struct proc *p;
158};
159*/
160
161int
91447636
A
162hfs_vnop_search(ap)
163 struct vnop_searchfs_args *ap; /*
9bccf70c
A
164 struct vnodeop_desc *a_desc;
165 struct vnode *a_vp;
166 void *a_searchparams1;
167 void *a_searchparams2;
168 struct attrlist *a_searchattrs;
169 u_long a_maxmatches;
170 struct timeval *a_timelimit;
171 struct attrlist *a_returnattrs;
172 u_long *a_nummatches;
173 u_long a_scriptcode;
174 u_long a_options;
175 struct uio *a_uio;
176 struct searchstate *a_searchstate;
91447636 177 vfs_context_t a_context;
9bccf70c 178 */
1c79356b 179{
9bccf70c 180 ExtendedVCB *vcb = VTOVCB(ap->a_vp);
91447636 181 struct hfsmount *hfsmp;
9bccf70c
A
182 FCB * catalogFCB;
183 searchinfospec_t searchInfo1;
184 searchinfospec_t searchInfo2;
b0d623f7 185 void *attributesBuffer = NULL;
9bccf70c 186 void *variableBuffer;
b0d623f7
A
187 u_int32_t fixedBlockSize;
188 u_int32_t eachReturnBufferSize;
2d21ac55 189 struct proc *p = current_proc();
9bccf70c
A
190 int err = E_NONE;
191 int isHFSPlus;
9bccf70c
A
192 CatalogKey * myCurrentKeyPtr;
193 CatalogRecord * myCurrentDataPtr;
194 CatPosition * myCatPositionPtr;
195 BTScanState myBTScanState;
91447636
A
196 user_addr_t user_start = 0;
197 user_size_t user_len = 0;
55e303ae 198 int32_t searchTime;
91447636 199 int lockflags;
6d2010ae
A
200 struct uthread *ut;
201 boolean_t timerExpired = FALSE;
202 boolean_t needThrottle = FALSE;
1c79356b
A
203
204 /* XXX Parameter check a_searchattrs? */
205
206 *(ap->a_nummatches) = 0;
207
39236c6e 208 if (ap->a_options & ~SRCHFS_VALIDOPTIONSMASK) {
9bccf70c 209 return (EINVAL);
39236c6e 210 }
1c79356b 211
2d21ac55 212 /*
316670eb
A
213 * Fail requests for attributes that HFS does not support for the
214 * items that match the search criteria. Note that these checks
215 * are for the OUTBOUND attributes to be returned (not search criteria).
2d21ac55
A
216 */
217 if ((ap->a_returnattrs->commonattr & ~HFS_ATTR_CMN_VALID) ||
218 (ap->a_returnattrs->volattr != 0) ||
219 (ap->a_returnattrs->dirattr & ~HFS_ATTR_DIR_VALID) ||
220 (ap->a_returnattrs->fileattr & ~HFS_ATTR_FILE_VALID) ||
221 (ap->a_returnattrs->forkattr != 0)) {
39236c6e 222
2d21ac55
A
223 return (EINVAL);
224 }
225
55e303ae
A
226 /* SRCHFS_SKIPLINKS requires root access.
227 * This option cannot be used with either
228 * the ATTR_CMN_NAME or ATTR_CMN_PAROBJID
229 * attributes.
230 */
231 if (ap->a_options & SRCHFS_SKIPLINKS) {
232 attrgroup_t attrs;
233
234 attrs = ap->a_searchattrs->commonattr | ap->a_returnattrs->commonattr;
39236c6e 235 if (attrs & (ATTR_CMN_NAME | ATTR_CMN_PAROBJID)) {
55e303ae 236 return (EINVAL);
39236c6e
A
237 }
238
239 if ((err = vfs_context_suser(ap->a_context))) {
55e303ae 240 return (err);
39236c6e 241 }
55e303ae
A
242 }
243
2d21ac55
A
244 // If both 32-bit and 64-bit parent ids or file ids are given
245 // then return an error.
246
247 attrgroup_t test_attrs=ap->a_searchattrs->commonattr;
248
249 if (((test_attrs & ATTR_CMN_OBJID) && (test_attrs & ATTR_CMN_FILEID)) ||
39236c6e 250 ((test_attrs & ATTR_CMN_PARENTID) && (test_attrs & ATTR_CMN_PAROBJID))) {
2d21ac55 251 return (EINVAL);
39236c6e 252 }
2d21ac55 253
39236c6e 254 if (uio_resid(ap->a_uio) <= 0) {
1c79356b 255 return (EINVAL);
39236c6e 256 }
1c79356b
A
257
258 isHFSPlus = (vcb->vcbSigWord == kHFSPlusSigWord);
91447636 259 hfsmp = VTOHFS(ap->a_vp);
55e303ae
A
260
261 searchTime = kMaxMicroSecsInKernel;
262 if (ap->a_timelimit->tv_sec == 0 &&
263 ap->a_timelimit->tv_usec > 0 &&
264 ap->a_timelimit->tv_usec < kMaxMicroSecsInKernel) {
265 searchTime = ap->a_timelimit->tv_usec;
266 }
1c79356b
A
267
268 /* UnPack the search boundries, searchInfo1, searchInfo2 */
91447636 269 err = UnpackSearchAttributeBlock(hfsmp, ap->a_searchattrs,
b0d623f7 270 &searchInfo1, ap->a_searchparams1, 1);
39236c6e
A
271 if (err) {
272 return err;
273 }
91447636 274 err = UnpackSearchAttributeBlock(hfsmp, ap->a_searchattrs,
b0d623f7 275 &searchInfo2, ap->a_searchparams2, 0);
39236c6e
A
276 if (err) {
277 return err;
278 }
2d21ac55
A
279 //shadow search bits if 64-bit file/parent ids are used
280 if (ap->a_searchattrs->commonattr & ATTR_CMN_FILEID)
281 ap->a_searchattrs->commonattr |= ATTR_CMN_OBJID;
282 if (ap->a_searchattrs->commonattr & ATTR_CMN_PARENTID)
283 ap->a_searchattrs->commonattr |= ATTR_CMN_PAROBJID;
284
285 fixedBlockSize = sizeof(u_int32_t) + hfs_attrblksize(ap->a_returnattrs); /* u_int32_t for length word */
91447636 286
1c79356b 287 eachReturnBufferSize = fixedBlockSize;
9bccf70c 288
1c79356b
A
289 if ( ap->a_returnattrs->commonattr & ATTR_CMN_NAME ) /* XXX should be more robust! */
290 eachReturnBufferSize += kHFSPlusMaxFileNameBytes + 1;
291
292 MALLOC( attributesBuffer, void *, eachReturnBufferSize, M_TEMP, M_WAITOK );
b0d623f7
A
293 if (attributesBuffer == NULL) {
294 err = ENOMEM;
295 goto ExitThisRoutine;
296 }
316670eb 297 bzero(attributesBuffer, eachReturnBufferSize);
1c79356b
A
298 variableBuffer = (void*)((char*) attributesBuffer + fixedBlockSize);
299
b4c24cb9
A
300 // XXXdbg - have to lock the user's buffer so we don't fault
301 // while holding the shared catalog file lock. see the comment
302 // in hfs_readdir() for more details.
303 //
91447636
A
304 if (hfsmp->jnl && uio_isuserspace(ap->a_uio)) {
305 user_start = uio_curriovbase(ap->a_uio);
306 user_len = uio_curriovlen(ap->a_uio);
b4c24cb9
A
307
308 if ((err = vslock(user_start, user_len)) != 0) {
91447636 309 user_start = 0;
b4c24cb9
A
310 goto ExitThisRoutine;
311 }
312 }
313
91447636 314 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
1c79356b 315
9bccf70c 316 catalogFCB = GetFileControlBlock(vcb->catalogRefNum);
14353aa8
A
317 myCurrentKeyPtr = NULL;
318 myCurrentDataPtr = NULL;
319 myCatPositionPtr = (CatPosition *)ap->a_searchstate;
1c79356b 320
14353aa8
A
321 if (ap->a_options & SRCHFS_START) {
322 /* Starting a new search. */
9bccf70c 323 /* Make sure the on-disk Catalog file is current */
91447636
A
324 (void) hfs_fsync(vcb->catalogRefNum, MNT_WAIT, 0, p);
325 if (hfsmp->jnl) {
326 hfs_systemfile_unlock(hfsmp, lockflags);
3e170ce0 327 hfs_flush(hfsmp, HFS_FLUSH_JOURNAL);
91447636 328 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
55e303ae
A
329 }
330
14353aa8 331 ap->a_options &= ~SRCHFS_START;
2d21ac55 332 bzero((caddr_t)myCatPositionPtr, sizeof(*myCatPositionPtr));
14353aa8 333 err = BTScanInitialize(catalogFCB, 0, 0, 0, kCatSearchBufferSize, &myBTScanState);
2d21ac55 334 if (err) {
3e170ce0 335 hfs_systemfile_unlock(hfsmp, lockflags);
2d21ac55 336 goto ExitThisRoutine;
d52fe63f 337 }
14353aa8
A
338 } else {
339 /* Resuming a search. */
340 err = BTScanInitialize(catalogFCB, myCatPositionPtr->nextNode,
341 myCatPositionPtr->nextRecord,
342 myCatPositionPtr->recordsFound,
343 kCatSearchBufferSize,
344 &myBTScanState);
14353aa8
A
345 /* Make sure Catalog hasn't changed. */
346 if (err == 0
347 && myCatPositionPtr->writeCount != myBTScanState.btcb->writeCount) {
348 myCatPositionPtr->writeCount = myBTScanState.btcb->writeCount;
349 err = EBUSY; /* catChangedErr */
1c79356b 350 }
14353aa8 351 }
91447636 352 hfs_systemfile_unlock(hfsmp, lockflags);
1c79356b 353
14353aa8
A
354 if (err)
355 goto ExitThisRoutine;
9bccf70c 356
6d2010ae
A
357 if (throttle_get_io_policy(&ut) == IOPOL_THROTTLE)
358 needThrottle = TRUE;
14353aa8
A
359 /*
360 * Check all the catalog btree records...
361 * return the attributes for matching items
362 */
363 for (;;) {
364 struct timeval myCurrentTime;
365 struct timeval myElapsedTime;
1c79356b 366
14353aa8
A
367 err = BTScanNextRecord(&myBTScanState, timerExpired,
368 (void **)&myCurrentKeyPtr, (void **)&myCurrentDataPtr,
369 NULL);
370 if (err)
371 break;
372
9bccf70c 373 /* Resolve any hardlinks */
91447636 374 if (isHFSPlus && (ap->a_options & SRCHFS_SKIPLINKS) == 0) {
2d21ac55 375 ResolveHardlink(vcb, (HFSPlusCatalogFile *)myCurrentDataPtr);
91447636 376 }
9bccf70c 377 if (CheckCriteria( vcb, ap->a_options, ap->a_searchattrs, myCurrentDataPtr,
39236c6e 378 myCurrentKeyPtr, &searchInfo1, &searchInfo2, ap->a_context )
13fec989 379 && CheckAccess(vcb, ap->a_options, myCurrentKeyPtr, ap->a_context)) {
91447636 380 err = InsertMatch(hfsmp, ap->a_uio, myCurrentDataPtr,
9bccf70c 381 myCurrentKeyPtr, ap->a_returnattrs,
91447636 382 attributesBuffer, variableBuffer, ap->a_nummatches);
14353aa8
A
383 if (err) {
384 /*
385 * The last match didn't fit so come back
386 * to this record on the next trip.
387 */
388 --myBTScanState.recordsFound;
389 --myBTScanState.recordNum;
1c79356b 390 break;
14353aa8 391 }
9bccf70c 392
14353aa8
A
393 if (*(ap->a_nummatches) >= ap->a_maxmatches)
394 break;
395 }
6d2010ae
A
396 if (timerExpired == FALSE) {
397 /*
398 * Check our elapsed time and bail if we've hit the max.
399 * The idea here is to throttle the amount of time we
400 * spend in the kernel.
401 */
402 microuptime(&myCurrentTime);
403 timersub(&myCurrentTime, &myBTScanState.startTime, &myElapsedTime);
404 /*
405 * Note: assumes kMaxMicroSecsInKernel is less than 1,000,000
406 */
407 if (myElapsedTime.tv_sec > 0
408 || myElapsedTime.tv_usec >= searchTime) {
409 timerExpired = TRUE;
410 } else if (needThrottle == TRUE) {
411 if (throttle_io_will_be_throttled(ut->uu_lowpri_window, HFSTOVFS(hfsmp)))
412 timerExpired = TRUE;
413 }
1c79356b
A
414 }
415 }
9bccf70c 416
14353aa8
A
417 /* Update catalog position */
418 myCatPositionPtr->writeCount = myBTScanState.btcb->writeCount;
1c79356b 419
9bccf70c
A
420 BTScanTerminate(&myBTScanState, &myCatPositionPtr->nextNode,
421 &myCatPositionPtr->nextRecord,
422 &myCatPositionPtr->recordsFound);
1c79356b
A
423
424 if ( err == E_NONE ) {
425 err = EAGAIN; /* signal to the user to call searchfs again */
426 } else if ( err == errSearchBufferFull ) {
427 if ( *(ap->a_nummatches) > 0 )
428 err = EAGAIN;
429 else
430 err = ENOBUFS;
431 } else if ( err == btNotFound ) {
432 err = E_NONE; /* the entire disk has been searched */
14353aa8
A
433 } else if ( err == fsBTTimeOutErr ) {
434 err = EAGAIN;
1c79356b
A
435 }
436
437ExitThisRoutine:
b0d623f7
A
438 if (attributesBuffer)
439 FREE(attributesBuffer, M_TEMP);
1c79356b 440
316670eb 441 if (user_start) {
b4c24cb9
A
442 vsunlock(user_start, user_len, TRUE);
443 }
444
14353aa8 445 return (MacToVFSError(err));
1c79356b
A
446}
447
448
9bccf70c 449static void
2d21ac55 450ResolveHardlink(struct hfsmount *hfsmp, HFSPlusCatalogFile *recp)
9bccf70c 451{
2d21ac55
A
452 u_int32_t type, creator;
453 int isdirlink = 0;
454 int isfilelink = 0;
455 time_t filecreatedate;
456
457 if (recp->recordType != kHFSPlusFileRecord) {
458 return;
459 }
460 type = SWAP_BE32(recp->userInfo.fdType);
461 creator = SWAP_BE32(recp->userInfo.fdCreator);
462 filecreatedate = to_bsd_time(recp->createDate);
463
464 if ((type == kHardLinkFileType && creator == kHFSPlusCreator) &&
6d2010ae 465 (filecreatedate == (time_t)hfsmp->hfs_itime ||
2d21ac55
A
466 filecreatedate == (time_t)hfsmp->hfs_metadata_createdate)) {
467 isfilelink = 1;
468 } else if ((type == kHFSAliasType && creator == kHFSAliasCreator) &&
469 (recp->flags & kHFSHasLinkChainMask) &&
6d2010ae 470 (filecreatedate == (time_t)hfsmp->hfs_itime ||
2d21ac55
A
471 filecreatedate == (time_t)hfsmp->hfs_metadata_createdate)) {
472 isdirlink = 1;
473 }
474
475 if (isfilelink || isdirlink) {
55e303ae 476 cnid_t saved_cnid;
2d21ac55 477 int lockflags;
55e303ae
A
478
479 /* Export link's cnid (a unique value) instead of inode's cnid */
480 saved_cnid = recp->fileID;
2d21ac55
A
481 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
482
483 (void) cat_resolvelink(hfsmp, recp->hl_linkReference, isdirlink, recp);
484
55e303ae 485 recp->fileID = saved_cnid;
2d21ac55 486 hfs_systemfile_unlock(hfsmp, lockflags);
9bccf70c
A
487 }
488}
489
490
1c79356b 491static Boolean
2d21ac55
A
492CompareMasked(const u_int32_t *thisValue, const u_int32_t *compareData,
493 const u_int32_t *compareMask, u_int32_t count)
1c79356b
A
494{
495 Boolean matched;
2d21ac55 496 u_int32_t i;
1c79356b
A
497
498 matched = true; /* Assume it will all match */
499
500 for (i=0; i<count; i++) {
501 if (((*thisValue++ ^ *compareData++) & *compareMask++) != 0) {
502 matched = false;
503 break;
504 }
505 }
506
507 return matched;
508}
509
510
511static Boolean
512ComparePartialUnicodeName (register ConstUniCharArrayPtr str, register ItemCount s_len,
39236c6e 513 register ConstUniCharArrayPtr find, register ItemCount f_len, int caseSensitive )
1c79356b 514{
39236c6e 515 if (f_len == 0 || s_len == 0) {
1c79356b 516 return FALSE;
39236c6e 517 }
1c79356b 518
39236c6e
A
519 if (caseSensitive) {
520 do {
521 if (s_len-- < f_len)
522 return FALSE;
523 } while (UnicodeBinaryCompare(str++, f_len, find, f_len) != 0);
524 }
525 else {
526 do {
527 if (s_len-- < f_len)
528 return FALSE;
529 } while (FastUnicodeCompare(str++, f_len, find, f_len) != 0);
530 }
1c79356b
A
531
532 return TRUE;
533}
534
39236c6e 535#if CONFIG_HFS_STD
1c79356b
A
536static Boolean
537ComparePartialPascalName ( register ConstStr31Param str, register ConstStr31Param find )
538{
539 register u_char s_len = str[0];
540 register u_char f_len = find[0];
541 register u_char *tsp;
542 Str31 tmpstr;
543
544 if (f_len == 0 || s_len == 0)
545 return FALSE;
546
547 bcopy(str, tmpstr, s_len + 1);
548 tsp = &tmpstr[0];
549
550 while (s_len-- >= f_len) {
551 *tsp = f_len;
552
553 if (FastRelString(tsp++, find) == 0)
554 return TRUE;
555 }
556
557 return FALSE;
558}
39236c6e 559#endif
1c79356b
A
560
561
562/*
563 * Check to see if caller has access rights to this item
564 */
9bccf70c 565
1c79356b 566static int
13fec989 567CheckAccess(ExtendedVCB *theVCBPtr, u_long searchBits, CatalogKey *theKeyPtr, struct vfs_context *ctx)
1c79356b 568{
91447636
A
569 Boolean isHFSPlus;
570 int myErr;
571 int myResult;
9bccf70c 572 HFSCatalogNodeID myNodeID;
91447636
A
573 hfsmount_t * hfsmp;
574 struct FndrDirInfo *finfop;
575 struct vnode * vp = NULL;
55e303ae 576
9bccf70c 577 myResult = 0; /* default to "no access" */
9bccf70c 578
13fec989 579 if (!vfs_context_suser(ctx)) {
9bccf70c
A
580 myResult = 1; /* allow access */
581 goto ExitThisRoutine; /* root always has access */
582 }
583
91447636 584 hfsmp = VCBTOHFS( theVCBPtr );
9bccf70c
A
585 isHFSPlus = ( theVCBPtr->vcbSigWord == kHFSPlusSigWord );
586 if ( isHFSPlus )
587 myNodeID = theKeyPtr->hfsPlus.parentID;
39236c6e 588#if CONFIG_HFS_STD
9bccf70c
A
589 else
590 myNodeID = theKeyPtr->hfs.parentID;
39236c6e 591#endif
9bccf70c
A
592
593 while ( myNodeID >= kRootDirID ) {
91447636
A
594 cnode_t * cp;
595
9bccf70c 596 /* now go get catalog data for this directory */
6d2010ae 597 myErr = hfs_vget(hfsmp, myNodeID, &vp, 0, 0);
91447636 598 if ( myErr ) {
9bccf70c 599 goto ExitThisRoutine; /* no access */
91447636 600 }
9bccf70c 601
91447636
A
602 cp = VTOC(vp);
603 finfop = (struct FndrDirInfo *)&cp->c_attr.ca_finderinfo[0];
604
605 if ( searchBits & SRCHFS_SKIPPACKAGES ) {
606 if ( (SWAP_BE16(finfop->frFlags) & kHasBundle)
607 || (cp->c_desc.cd_nameptr != NULL
2d21ac55 608 && is_package_name((const char *)cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen)) ) {
91447636
A
609 myResult = 0;
610 goto ExitThisRoutine;
55e303ae
A
611 }
612 }
613
91447636
A
614 if ( searchBits & SRCHFS_SKIPINAPPROPRIATE ) {
615 if ( cp->c_parentcnid == kRootDirID && cp->c_desc.cd_nameptr != NULL &&
b0d623f7 616 vn_searchfs_inappropriate_name((const char *)cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen) ) {
91447636
A
617 myResult = 0;
618 goto ExitThisRoutine;
55e303ae
A
619 }
620 }
621
91447636
A
622 if ( (searchBits & SRCHFS_SKIPINVISIBLE) &&
623 (SWAP_BE16(finfop->frFlags) & kIsInvisible) ) {
55e303ae
A
624 myResult = 0;
625 goto ExitThisRoutine;
626 }
627
91447636
A
628 myNodeID = cp->c_parentcnid; /* move up the hierarchy */
629 hfs_unlock(VTOC(vp));
2d21ac55
A
630
631#if CONFIG_MACF
632 if (vp->v_type == VDIR) {
633 myErr = mac_vnode_check_readdir(ctx, vp);
634 } else {
635 myErr = mac_vnode_check_stat(ctx, NOCRED, vp);
636 }
637 if (myErr) {
638 vnode_put(vp);
639 vp = NULL;
640 goto ExitThisRoutine;
641 }
642#endif /* MAC */
643
b36670ce 644 if (vp->v_type == VDIR) {
13fec989 645 myErr = vnode_authorize(vp, NULL, (KAUTH_VNODE_SEARCH | KAUTH_VNODE_LIST_DIRECTORY), ctx);
b36670ce 646 } else {
13fec989 647 myErr = vnode_authorize(vp, NULL, (KAUTH_VNODE_SEARCH), ctx);
b36670ce 648 }
91447636
A
649 vnode_put(vp);
650 vp = NULL;
651 if ( myErr ) {
9bccf70c 652 goto ExitThisRoutine; /* no access */
91447636 653 }
9bccf70c 654 }
9bccf70c
A
655 myResult = 1; /* allow access */
656
657ExitThisRoutine:
91447636
A
658 if ( vp != NULL ) {
659 hfs_unlock(VTOC(vp));
660 vnode_put(vp);
661 }
9bccf70c
A
662 return ( myResult );
663
1c79356b
A
664}
665
9bccf70c
A
666static int
667CheckCriteria( ExtendedVCB *vcb,
668 u_long searchBits,
669 struct attrlist *attrList,
670 CatalogRecord *rec,
d52fe63f 671 CatalogKey *key,
9bccf70c 672 searchinfospec_t *searchInfo1,
39236c6e
A
673 searchinfospec_t *searchInfo2,
674 struct vfs_context *ctx)
1c79356b
A
675{
676 Boolean matched, atleastone;
677 Boolean isHFSPlus;
678 attrgroup_t searchAttributes;
91447636 679 struct cat_attr c_attr;
9bccf70c
A
680 struct cat_fork datafork;
681 struct cat_fork rsrcfork;
39236c6e 682 int force_case_sensitivity = proc_is_forcing_hfs_case_sensitivity(vfs_context_proc(ctx));
1c79356b 683
91447636 684 bzero(&c_attr, sizeof(c_attr));
1c79356b
A
685 isHFSPlus = (vcb->vcbSigWord == kHFSPlusSigWord);
686
9bccf70c 687 switch (rec->recordType) {
39236c6e
A
688
689#if CONFIG_HFS_STD
690 case kHFSFolderRecord:
691 if ( (searchBits & SRCHFS_MATCHDIRS) == 0 ) { /* If we are NOT searching folders */
692 matched = false;
693 goto TestDone;
55e303ae 694 }
39236c6e 695 break;
55e303ae 696
39236c6e
A
697 case kHFSFileRecord:
698 if ( (searchBits & SRCHFS_MATCHFILES) == 0 ) { /* If we are NOT searching files */
699 matched = false;
700 goto TestDone;
701 }
702 break;
703#endif
1c79356b 704
39236c6e
A
705 case kHFSPlusFolderRecord:
706 if ( (searchBits & SRCHFS_MATCHDIRS) == 0 ) { /* If we are NOT searching folders */
707 matched = false;
708 goto TestDone;
709 }
710 break;
711
712 case kHFSPlusFileRecord:
713 /* Check if hardlink links should be skipped. */
714 if (searchBits & SRCHFS_SKIPLINKS) {
715 cnid_t parid = key->hfsPlus.parentID;
716 HFSPlusCatalogFile *filep = (HFSPlusCatalogFile *)rec;
717
718 if ((SWAP_BE32(filep->userInfo.fdType) == kHardLinkFileType) &&
719 (SWAP_BE32(filep->userInfo.fdCreator) == kHFSPlusCreator)) {
720 return (false); /* skip over file link records */
721 } else if ((parid == vcb->hfs_private_desc[FILE_HARDLINKS].cd_cnid) &&
722 (filep->bsdInfo.special.linkCount == 0)) {
723 return (false); /* skip over unlinked files */
724 } else if ((SWAP_BE32(filep->userInfo.fdType) == kHFSAliasType) &&
725 (SWAP_BE32(filep->userInfo.fdCreator) == kHFSAliasCreator) &&
726 (filep->flags & kHFSHasLinkChainMask)) {
727 return (false); /* skip over dir link records */
728 }
729 } else if (key->hfsPlus.parentID == vcb->hfs_private_desc[FILE_HARDLINKS].cd_cnid) {
730 return (false); /* skip over private files */
731 } else if (key->hfsPlus.parentID == vcb->hfs_private_desc[DIR_HARDLINKS].cd_cnid) {
732 return (false); /* skip over private files */
733 }
734
735 if ( (searchBits & SRCHFS_MATCHFILES) == 0 ) { /* If we are NOT searching files */
736 matched = false;
737 goto TestDone;
738 }
739 break;
740
741 default: /* Never match a thread record or any other type. */
742 return( false ); /* Not a file or folder record, so can't search it */
1c79356b
A
743 }
744
745 matched = true; /* Assume we got a match */
746 atleastone = false; /* Dont insert unless we match at least one criteria */
747
748 /* First, attempt to match the name -- either partial or complete */
749 if ( attrList->commonattr & ATTR_CMN_NAME ) {
750 if (isHFSPlus) {
39236c6e
A
751 int case_sensitive = 0;
752
22ba694c
A
753 /*
754 * Longstanding default behavior here is to use a non-case-sensitive
755 * search, even on case-sensitive filesystems.
756 *
757 * We only force case sensitivity if the controlling process has explicitly
758 * asked for it in the proc flags, and only if they are not doing
759 * a partial name match. Consider that if you are doing a partial
760 * name match ("all files that begin with 'image'"), the likelihood is
761 * high that you would want to see all matches, even those that do not
762 * explicitly match the case.
763 */
764 if (force_case_sensitivity) {
39236c6e
A
765 case_sensitive = 1;
766 }
767
1c79356b
A
768 /* Check for partial/full HFS Plus name match */
769
770 if ( searchBits & SRCHFS_MATCHPARTIALNAMES ) {
22ba694c 771 /* always use a case-INSENSITIVE search here */
1c79356b
A
772 matched = ComparePartialUnicodeName(key->hfsPlus.nodeName.unicode,
773 key->hfsPlus.nodeName.length,
774 (UniChar*)searchInfo1->name,
22ba694c 775 searchInfo1->nameLength, 0);
39236c6e
A
776 }
777 else {
778 /* Full name match. Are we HFSX (case sensitive) or HFS+ ? */
779 if (case_sensitive) {
780 matched = (UnicodeBinaryCompare(key->hfsPlus.nodeName.unicode,
781 key->hfsPlus.nodeName.length,
782 (UniChar*)searchInfo1->name,
783 searchInfo1->nameLength ) == 0);
784 }
785 else {
786 matched = (FastUnicodeCompare(key->hfsPlus.nodeName.unicode,
787 key->hfsPlus.nodeName.length,
788 (UniChar*)searchInfo1->name,
789 searchInfo1->nameLength ) == 0);
790 }
1c79356b 791 }
39236c6e
A
792 }
793#if CONFIG_HFS_STD
794 else {
1c79356b
A
795 /* Check for partial/full HFS name match */
796
797 if ( searchBits & SRCHFS_MATCHPARTIALNAMES )
798 matched = ComparePartialPascalName(key->hfs.nodeName, (u_char*)searchInfo1->name);
799 else /* full HFS name match */
800 matched = (FastRelString(key->hfs.nodeName, (u_char*)searchInfo1->name) == 0);
801 }
39236c6e 802#endif
d52fe63f 803
1c79356b
A
804 if ( matched == false || (searchBits & ~SRCHFS_MATCHPARTIALNAMES) == 0 )
805 goto TestDone; /* no match, or nothing more to compare */
806
807 atleastone = true;
808 }
9bccf70c
A
809
810 /* Convert catalog record into cat_attr format. */
811 cat_convertattr(VCBTOHFS(vcb), rec, &c_attr, &datafork, &rsrcfork);
1c79356b 812
55e303ae
A
813 if (searchBits & SRCHFS_SKIPINVISIBLE) {
814 int flags;
815
816 switch (rec->recordType) {
39236c6e
A
817#if CONFIG_HFS_STD
818 case kHFSFolderRecord:
819 {
820 struct FndrDirInfo *finder_info;
821
822 finder_info = (struct FndrDirInfo *)&c_attr.ca_finderinfo[0];
823 flags = SWAP_BE16(finder_info->frFlags);
824 break;
825 }
826
827 case kHFSFileRecord:
828 {
829 struct FndrFileInfo *finder_info;
830
831 finder_info = (struct FndrFileInfo *)&c_attr.ca_finderinfo[0];
832 flags = SWAP_BE16(finder_info->fdFlags);
833 break;
834 }
835#endif
836
837 case kHFSPlusFolderRecord:
838 {
839 struct FndrDirInfo *finder_info;
840
841 finder_info = (struct FndrDirInfo *)&c_attr.ca_finderinfo[0];
842 flags = SWAP_BE16(finder_info->frFlags);
843 break;
844 }
845
846 case kHFSPlusFileRecord:
847 {
848 struct FndrFileInfo *finder_info;
849
850 finder_info = (struct FndrFileInfo *)&c_attr.ca_finderinfo[0];
851 flags = SWAP_BE16(finder_info->fdFlags);
852 break;
853 }
854
855 default:
856 {
857 flags = kIsInvisible;
858 break;
859 }
55e303ae
A
860 }
861
39236c6e
A
862 if (flags & kIsInvisible) {
863 matched = false;
864 goto TestDone;
55e303ae 865 }
55e303ae
A
866 }
867
868
869
1c79356b 870 /* Now that we have a record worth searching, see if it matches the search attributes */
39236c6e 871#if CONFIG_HFS_STD
9bccf70c
A
872 if (rec->recordType == kHFSFileRecord ||
873 rec->recordType == kHFSPlusFileRecord) {
39236c6e
A
874#else
875 if (rec->recordType == kHFSPlusFileRecord) {
876#endif
877
14353aa8 878 if ((attrList->fileattr & ~ATTR_FILE_VALIDMASK) != 0) { /* attr we do know about */
1c79356b
A
879 matched = false;
880 goto TestDone;
881 }
14353aa8 882 else if ((attrList->fileattr & ATTR_FILE_VALIDMASK) != 0) {
1c79356b 883 searchAttributes = attrList->fileattr;
b0d623f7
A
884
885#if HFS_COMPRESSION
886 if ( c_attr.ca_flags & UF_COMPRESSED ) {
887 /* for compressed files, set the data length to the uncompressed data size */
888 if (( searchAttributes & ATTR_FILE_DATALENGTH ) ||
889 ( searchAttributes & ATTR_FILE_DATAALLOCSIZE ) ) {
890 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 */
891 datafork.cf_blocks = rsrcfork.cf_blocks;
892 }
893 }
894 /* treat compressed files as if their resource fork is empty */
895 if (( searchAttributes & ATTR_FILE_RSRCLENGTH ) ||
896 ( searchAttributes & ATTR_FILE_RSRCALLOCSIZE ) ) {
897 rsrcfork.cf_size = 0;
898 rsrcfork.cf_blocks = 0;
899 }
900 }
901#endif /* HFS_COMPRESSION */
902
1c79356b
A
903 /* File logical length (data fork) */
904 if ( searchAttributes & ATTR_FILE_DATALENGTH ) {
905 matched = CompareWideRange(
9bccf70c 906 datafork.cf_size,
1c79356b
A
907 searchInfo1->f.dataLogicalLength,
908 searchInfo2->f.dataLogicalLength);
909 if (matched == false) goto TestDone;
910 atleastone = true;
911 }
912
913 /* File physical length (data fork) */
914 if ( searchAttributes & ATTR_FILE_DATAALLOCSIZE ) {
915 matched = CompareWideRange(
9bccf70c 916 (u_int64_t)datafork.cf_blocks * (u_int64_t)vcb->blockSize,
1c79356b
A
917 searchInfo1->f.dataPhysicalLength,
918 searchInfo2->f.dataPhysicalLength);
919 if (matched == false) goto TestDone;
920 atleastone = true;
921 }
922
923 /* File logical length (resource fork) */
924 if ( searchAttributes & ATTR_FILE_RSRCLENGTH ) {
925 matched = CompareWideRange(
9bccf70c 926 rsrcfork.cf_size,
1c79356b
A
927 searchInfo1->f.resourceLogicalLength,
928 searchInfo2->f.resourceLogicalLength);
929 if (matched == false) goto TestDone;
930 atleastone = true;
931 }
932
933 /* File physical length (resource fork) */
934 if ( searchAttributes & ATTR_FILE_RSRCALLOCSIZE ) {
935 matched = CompareWideRange(
9bccf70c 936 (u_int64_t)rsrcfork.cf_blocks * (u_int64_t)vcb->blockSize,
1c79356b
A
937 searchInfo1->f.resourcePhysicalLength,
938 searchInfo2->f.resourcePhysicalLength);
939 if (matched == false) goto TestDone;
940 atleastone = true;
941 }
942 }
943 else {
9bccf70c 944 atleastone = true; /* to match SRCHFS_MATCHFILES */
1c79356b
A
945 }
946 }
947 /*
948 * Check the directory attributes
949 */
39236c6e 950#if CONFIG_HFS_STD
9bccf70c
A
951 else if (rec->recordType == kHFSFolderRecord ||
952 rec->recordType == kHFSPlusFolderRecord) {
39236c6e
A
953#else
954 else if (rec->recordType == kHFSPlusFolderRecord) {
955#endif
1c79356b
A
956 if ((attrList->dirattr & ~ATTR_DIR_VALIDMASK) != 0) { /* attr we do know about */
957 matched = false;
958 goto TestDone;
959 }
960 else if ((attrList->dirattr & ATTR_DIR_VALIDMASK) != 0) {
961 searchAttributes = attrList->dirattr;
962
963 /* Directory valence */
964 if ( searchAttributes & ATTR_DIR_ENTRYCOUNT ) {
9bccf70c
A
965 matched = CompareRange(c_attr.ca_entries,
966 searchInfo1->d.numFiles,
967 searchInfo2->d.numFiles );
1c79356b
A
968 if (matched == false) goto TestDone;
969 atleastone = true;
970 }
971 }
972 else {
973 atleastone = true; /* to match SRCHFS_MATCHDIRS */
974 }
975 }
976
977 /*
978 * Check the common attributes
979 */
980 searchAttributes = attrList->commonattr;
981 if ( (searchAttributes & ATTR_CMN_VALIDMASK) != 0 ) {
1c79356b
A
982 /* node ID */
983 if ( searchAttributes & ATTR_CMN_OBJID ) {
9bccf70c
A
984 matched = CompareRange(c_attr.ca_fileid,
985 searchInfo1->nodeID,
986 searchInfo2->nodeID );
1c79356b
A
987 if (matched == false) goto TestDone;
988 atleastone = true;
989 }
990
991 /* Parent ID */
992 if ( searchAttributes & ATTR_CMN_PAROBJID ) {
993 HFSCatalogNodeID parentID;
994
995 if (isHFSPlus)
996 parentID = key->hfsPlus.parentID;
39236c6e 997#if CONFIG_HFS_STD
1c79356b
A
998 else
999 parentID = key->hfs.parentID;
39236c6e 1000#endif
1c79356b 1001
9bccf70c
A
1002 matched = CompareRange(parentID, searchInfo1->parentDirID,
1003 searchInfo2->parentDirID );
1c79356b
A
1004 if (matched == false) goto TestDone;
1005 atleastone = true;
1006 }
1007
1008 /* Finder Info & Extended Finder Info where extFinderInfo is last 32 bytes */
1009 if ( searchAttributes & ATTR_CMN_FNDRINFO ) {
2d21ac55
A
1010 u_int32_t *thisValue;
1011 thisValue = (u_int32_t *) &c_attr.ca_finderinfo;
1c79356b
A
1012
1013 /*
1014 * Note: ioFlFndrInfo and ioDrUsrWds have the same offset in search info, so
1015 * no need to test the object type here.
1016 */
9bccf70c 1017 matched = CompareMasked(thisValue,
2d21ac55
A
1018 (u_int32_t *)&searchInfo1->finderInfo,
1019 (u_int32_t *) &searchInfo2->finderInfo, 8);
1c79356b
A
1020 if (matched == false) goto TestDone;
1021 atleastone = true;
1022 }
1023
1024 /* Create date */
1025 if ( searchAttributes & ATTR_CMN_CRTIME ) {
9bccf70c
A
1026 matched = CompareRange(c_attr.ca_itime,
1027 searchInfo1->creationDate.tv_sec,
1028 searchInfo2->creationDate.tv_sec);
1c79356b
A
1029 if (matched == false) goto TestDone;
1030 atleastone = true;
1031 }
1032
1033 /* Mod date */
1034 if ( searchAttributes & ATTR_CMN_MODTIME ) {
9bccf70c
A
1035 matched = CompareRange(c_attr.ca_mtime,
1036 searchInfo1->modificationDate.tv_sec,
1037 searchInfo2->modificationDate.tv_sec);
1c79356b
A
1038 if (matched == false) goto TestDone;
1039 atleastone = true;
1040 }
1041
1042 /* Change Time */
1043 if ( searchAttributes & ATTR_CMN_CHGTIME ) {
9bccf70c
A
1044 matched = CompareRange(c_attr.ca_ctime,
1045 searchInfo1->changeDate.tv_sec,
1046 searchInfo2->changeDate.tv_sec);
1c79356b
A
1047 if (matched == false) goto TestDone;
1048 atleastone = true;
1049 }
1050
9bccf70c
A
1051 /* Access date */
1052 if ( searchAttributes & ATTR_CMN_ACCTIME ) {
1053 matched = CompareRange(c_attr.ca_atime,
1054 searchInfo1->accessDate.tv_sec,
1055 searchInfo2->accessDate.tv_sec);
1056 if (matched == false) goto TestDone;
1057 atleastone = true;
1058 }
1059
1c79356b
A
1060 /* Backup date */
1061 if ( searchAttributes & ATTR_CMN_BKUPTIME ) {
9bccf70c
A
1062 matched = CompareRange(c_attr.ca_btime,
1063 searchInfo1->lastBackupDate.tv_sec,
1064 searchInfo2->lastBackupDate.tv_sec);
1c79356b
A
1065 if (matched == false) goto TestDone;
1066 atleastone = true;
1067 }
1068
1069 /* User ID */
1070 if ( searchAttributes & ATTR_CMN_OWNERID ) {
9bccf70c
A
1071 matched = CompareRange(c_attr.ca_uid,
1072 searchInfo1->uid, searchInfo2->uid);
1c79356b
A
1073 if (matched == false) goto TestDone;
1074 atleastone = true;
1075 }
1076
1077 /* Group ID */
1078 if ( searchAttributes & ATTR_CMN_GRPID ) {
9bccf70c
A
1079 matched = CompareRange(c_attr.ca_gid,
1080 searchInfo1->gid, searchInfo2->gid);
1c79356b
A
1081 if (matched == false) goto TestDone;
1082 atleastone = true;
1083 }
1084
1085 /* mode */
1086 if ( searchAttributes & ATTR_CMN_ACCESSMASK ) {
2d21ac55
A
1087 matched = CompareRange((u_int32_t)c_attr.ca_mode,
1088 (u_int32_t)searchInfo1->mask,
1089 (u_int32_t)searchInfo2->mask);
1c79356b
A
1090 if (matched == false) goto TestDone;
1091 atleastone = true;
1092 }
1c79356b
A
1093 }
1094
1095 /* If we got here w/o matching any, then set to false */
1096 if (! atleastone)
1097 matched = false;
1098
1099TestDone:
1100 /*
1101 * Finally, determine whether we need to negate the sense of the match
1102 * (i.e. find all objects that DON'T match).
1103 */
1104 if ( searchBits & SRCHFS_NEGATEPARAMS )
1105 matched = !matched;
1106
1107 return( matched );
1108}
1109
1110
1111/*
1112 * Adds another record to the packed array for output
1113 */
1114static int
91447636
A
1115InsertMatch(struct hfsmount *hfsmp, uio_t a_uio, CatalogRecord *rec,
1116 CatalogKey *key, struct attrlist *returnAttrList,
b0d623f7 1117 void *attributesBuffer, void *variableBuffer, uint32_t * nummatches)
1c79356b
A
1118{
1119 int err;
1120 void *rovingAttributesBuffer;
1121 void *rovingVariableBuffer;
b0d623f7 1122 long packedBufferSize;
9bccf70c 1123 struct attrblock attrblk;
91447636
A
1124 struct cat_desc c_desc;
1125 struct cat_attr c_attr;
9bccf70c
A
1126 struct cat_fork datafork;
1127 struct cat_fork rsrcfork;
1c79356b 1128
91447636
A
1129 bzero(&c_desc, sizeof(c_desc));
1130 bzero(&c_attr, sizeof(c_attr));
b0d623f7 1131 rovingAttributesBuffer = (char*)attributesBuffer + sizeof(u_int32_t); /* Reserve space for length field */
1c79356b 1132 rovingVariableBuffer = variableBuffer;
1c79356b 1133
9bccf70c 1134 /* Convert catalog record into cat_attr format. */
91447636 1135 cat_convertattr(hfsmp, rec, &c_attr, &datafork, &rsrcfork);
1c79356b 1136
2d21ac55
A
1137 /* Hide our private meta data directories */
1138 if (c_attr.ca_fileid == hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid ||
1139 c_attr.ca_fileid == hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid) {
9bccf70c
A
1140 err = 0;
1141 goto exit;
1142 }
1c79356b 1143
b4c24cb9 1144 /* Hide the private journal files */
91447636
A
1145 if (hfsmp->jnl &&
1146 ((c_attr.ca_fileid == hfsmp->hfs_jnlfileid) ||
1147 (c_attr.ca_fileid == hfsmp->hfs_jnlinfoblkid))) {
b4c24cb9
A
1148 err = 0;
1149 goto exit;
1150 }
1151
9bccf70c 1152 if (returnAttrList->commonattr & ATTR_CMN_NAME) {
39236c6e
A
1153 err = cat_convertkey(hfsmp, key, rec, &c_desc);
1154 if (err) {
1155 /* This means that we probably had a CNID error */
1156 goto exit;
1157 }
9bccf70c
A
1158 } else {
1159 c_desc.cd_cnid = c_attr.ca_fileid;
39236c6e 1160 if ((hfsmp->hfs_flags & HFS_STANDARD) == 0)
91447636 1161 c_desc.cd_parentcnid = key->hfsPlus.parentID;
39236c6e
A
1162#if CONFIG_HFS_STD
1163 else
1164 c_desc.cd_parentcnid = key->hfs.parentID;
1165#endif
1166
9bccf70c 1167 }
1c79356b 1168
9bccf70c
A
1169 attrblk.ab_attrlist = returnAttrList;
1170 attrblk.ab_attrbufpp = &rovingAttributesBuffer;
1171 attrblk.ab_varbufpp = &rovingVariableBuffer;
1172 attrblk.ab_flags = 0;
1173 attrblk.ab_blocksize = 0;
2d21ac55 1174 attrblk.ab_context = vfs_context_current();
1c79356b 1175
b0d623f7 1176 hfs_packattrblk(&attrblk, hfsmp, NULL, &c_desc, &c_attr, &datafork, &rsrcfork, vfs_context_current());
1c79356b
A
1177
1178 packedBufferSize = (char*)rovingVariableBuffer - (char*)attributesBuffer;
1179
91447636 1180 if ( packedBufferSize > uio_resid(a_uio) )
1c79356b
A
1181 return( errSearchBufferFull );
1182
1183 (* nummatches)++;
1184
b0d623f7 1185 *((u_int32_t *)attributesBuffer) = packedBufferSize; /* Store length of fixed + var block */
1c79356b 1186
316670eb 1187 err = uiomove( (caddr_t)attributesBuffer, packedBufferSize, a_uio );
9bccf70c
A
1188exit:
1189 cat_releasedesc(&c_desc);
1c79356b
A
1190
1191 return( err );
1192}
1193
1194
1195static int
b0d623f7
A
1196UnpackSearchAttributeBlock( struct hfsmount *hfsmp, struct attrlist *alist,
1197 searchinfospec_t *searchInfo, void *attributeBuffer, int firstblock)
1c79356b
A
1198{
1199 attrgroup_t a;
b0d623f7 1200 u_int32_t bufferSize;
91447636 1201 boolean_t is_64_bit;
1c79356b
A
1202
1203 DBG_ASSERT(searchInfo != NULL);
91447636
A
1204
1205 is_64_bit = proc_is64bit(current_proc());
1c79356b 1206
2d21ac55 1207 bufferSize = *((u_int32_t *)attributeBuffer);
1c79356b
A
1208 if (bufferSize == 0)
1209 return (EINVAL); /* XXX -DJB is a buffer size of zero ever valid for searchfs? */
1210
2d21ac55 1211 attributeBuffer = (u_int32_t *)attributeBuffer + 1; /* advance past the size */
1c79356b
A
1212
1213 /*
1214 * UnPack common attributes
1215 */
1216 a = alist->commonattr;
1217 if ( a != 0 ) {
1218 if ( a & ATTR_CMN_NAME ) {
b0d623f7
A
1219 if (firstblock) {
1220 /* Only use the attrreference_t for the first searchparams */
1221 char *s;
1222 u_int32_t len;
1c79356b 1223
b0d623f7
A
1224 s = (char*) attributeBuffer + ((attrreference_t *) attributeBuffer)->attr_dataoffset;
1225 len = ((attrreference_t *) attributeBuffer)->attr_length;
1c79356b 1226
b0d623f7
A
1227 if (len > sizeof(searchInfo->name))
1228 return (EINVAL);
1c79356b 1229
1c79356b 1230
39236c6e 1231 if ((hfsmp->hfs_flags & HFS_STANDARD) == 0) {
b0d623f7
A
1232 size_t ucslen;
1233 /* Convert name to Unicode to match HFS Plus B-Tree names */
1234
1235 if (len > 0) {
1236 if (utf8_decodestr((u_int8_t *)s, len-1, (UniChar*)searchInfo->name, &ucslen,
1237 sizeof(searchInfo->name), ':', UTF_DECOMPOSED | UTF_ESCAPE_ILLEGAL))
1238 return (EINVAL);
1239
1240 searchInfo->nameLength = ucslen / sizeof(UniChar);
1241 } else {
1242 searchInfo->nameLength = 0;
1243 }
1c79356b 1244 }
39236c6e
A
1245#if CONFIG_HFS_STD
1246 else {
1247 /* Convert name to pascal string to match HFS (Standard) B-Tree names */
1248
1249 if (len > 0) {
1250 if (utf8_to_hfs(HFSTOVCB(hfsmp), len-1, (u_char *)s, (u_char*)searchInfo->name) != 0)
1251 return (EINVAL);
1252
1253 searchInfo->nameLength = searchInfo->name[0];
1254 } else {
1255 searchInfo->name[0] = searchInfo->nameLength = 0;
1256 }
1257 }
1258#endif
1c79356b 1259 }
b0d623f7 1260 attributeBuffer = (attrreference_t*) attributeBuffer +1;
1c79356b
A
1261 }
1262 if ( a & ATTR_CMN_OBJID ) {
1263 searchInfo->nodeID = ((fsobj_id_t *) attributeBuffer)->fid_objno; /* ignore fid_generation */
2d21ac55 1264 attributeBuffer = (fsobj_id_t *)attributeBuffer + 1;
1c79356b
A
1265 }
1266 if ( a & ATTR_CMN_PAROBJID ) {
2d21ac55
A
1267 searchInfo->parentDirID = ((fsobj_id_t *) attributeBuffer)->fid_objno; /* ignore fid_generation */
1268 attributeBuffer = (fsobj_id_t *)attributeBuffer + 1;
1c79356b 1269 }
2d21ac55 1270
1c79356b 1271 if ( a & ATTR_CMN_CRTIME ) {
91447636 1272 if (is_64_bit) {
b0d623f7
A
1273 struct user64_timespec tmp;
1274 tmp = *((struct user64_timespec *)attributeBuffer);
91447636
A
1275 searchInfo->creationDate.tv_sec = (time_t)tmp.tv_sec;
1276 searchInfo->creationDate.tv_nsec = tmp.tv_nsec;
b0d623f7 1277 attributeBuffer = (struct user64_timespec *)attributeBuffer + 1;
91447636
A
1278 }
1279 else {
b0d623f7
A
1280 struct user32_timespec tmp;
1281 tmp = *((struct user32_timespec *)attributeBuffer);
1282 searchInfo->creationDate.tv_sec = (time_t)tmp.tv_sec;
1283 searchInfo->creationDate.tv_nsec = tmp.tv_nsec;
1284 attributeBuffer = (struct user32_timespec *)attributeBuffer + 1;
91447636 1285 }
1c79356b
A
1286 }
1287 if ( a & ATTR_CMN_MODTIME ) {
91447636 1288 if (is_64_bit) {
b0d623f7
A
1289 struct user64_timespec tmp;
1290 tmp = *((struct user64_timespec *)attributeBuffer);
91447636
A
1291 searchInfo->modificationDate.tv_sec = (time_t)tmp.tv_sec;
1292 searchInfo->modificationDate.tv_nsec = tmp.tv_nsec;
b0d623f7 1293 attributeBuffer = (struct user64_timespec *)attributeBuffer + 1;
91447636
A
1294 }
1295 else {
b0d623f7
A
1296 struct user32_timespec tmp;
1297 tmp = *((struct user32_timespec *)attributeBuffer);
1298 searchInfo->modificationDate.tv_sec = (time_t)tmp.tv_sec;
1299 searchInfo->modificationDate.tv_nsec = tmp.tv_nsec;
1300 attributeBuffer = (struct user32_timespec *)attributeBuffer + 1;
91447636 1301 }
1c79356b
A
1302 }
1303 if ( a & ATTR_CMN_CHGTIME ) {
91447636 1304 if (is_64_bit) {
b0d623f7
A
1305 struct user64_timespec tmp;
1306 tmp = *((struct user64_timespec *)attributeBuffer);
91447636
A
1307 searchInfo->changeDate.tv_sec = (time_t)tmp.tv_sec;
1308 searchInfo->changeDate.tv_nsec = tmp.tv_nsec;
b0d623f7 1309 attributeBuffer = (struct user64_timespec *)attributeBuffer + 1;
91447636
A
1310 }
1311 else {
b0d623f7
A
1312 struct user32_timespec tmp;
1313 tmp = *((struct user32_timespec *)attributeBuffer);
1314 searchInfo->changeDate.tv_sec = (time_t)tmp.tv_sec;
1315 searchInfo->changeDate.tv_nsec = tmp.tv_nsec;
1316 attributeBuffer = (struct user32_timespec *)attributeBuffer + 1;
91447636 1317 }
1c79356b 1318 }
9bccf70c 1319 if ( a & ATTR_CMN_ACCTIME ) {
91447636 1320 if (is_64_bit) {
b0d623f7
A
1321 struct user64_timespec tmp;
1322 tmp = *((struct user64_timespec *)attributeBuffer);
91447636
A
1323 searchInfo->accessDate.tv_sec = (time_t)tmp.tv_sec;
1324 searchInfo->accessDate.tv_nsec = tmp.tv_nsec;
b0d623f7 1325 attributeBuffer = (struct user64_timespec *)attributeBuffer + 1;
91447636
A
1326 }
1327 else {
b0d623f7
A
1328 struct user32_timespec tmp;
1329 tmp = *((struct user32_timespec *)attributeBuffer);
1330 searchInfo->accessDate.tv_sec = (time_t)tmp.tv_sec;
1331 searchInfo->accessDate.tv_nsec = tmp.tv_nsec;
1332 attributeBuffer = (struct user32_timespec *)attributeBuffer + 1;
91447636 1333 }
9bccf70c 1334 }
1c79356b 1335 if ( a & ATTR_CMN_BKUPTIME ) {
91447636 1336 if (is_64_bit) {
b0d623f7
A
1337 struct user64_timespec tmp;
1338 tmp = *((struct user64_timespec *)attributeBuffer);
91447636
A
1339 searchInfo->lastBackupDate.tv_sec = (time_t)tmp.tv_sec;
1340 searchInfo->lastBackupDate.tv_nsec = tmp.tv_nsec;
b0d623f7 1341 attributeBuffer = (struct user64_timespec *)attributeBuffer + 1;
91447636
A
1342 }
1343 else {
b0d623f7
A
1344 struct user32_timespec tmp;
1345 tmp = *((struct user32_timespec *)attributeBuffer);
1346 searchInfo->lastBackupDate.tv_sec = (time_t)tmp.tv_sec;
1347 searchInfo->lastBackupDate.tv_nsec = tmp.tv_nsec;
1348 attributeBuffer = (struct user32_timespec *)attributeBuffer + 1;
91447636 1349 }
1c79356b
A
1350 }
1351 if ( a & ATTR_CMN_FNDRINFO ) {
91447636 1352 bcopy( attributeBuffer, searchInfo->finderInfo, sizeof(searchInfo->finderInfo) );
2d21ac55 1353 attributeBuffer = (u_int8_t *)attributeBuffer + 32;
1c79356b
A
1354 }
1355 if ( a & ATTR_CMN_OWNERID ) {
1356 searchInfo->uid = *((uid_t *)attributeBuffer);
2d21ac55 1357 attributeBuffer = (uid_t *)attributeBuffer + 1;
1c79356b
A
1358 }
1359 if ( a & ATTR_CMN_GRPID ) {
1360 searchInfo->gid = *((gid_t *)attributeBuffer);
2d21ac55 1361 attributeBuffer = (gid_t *)attributeBuffer + 1;
1c79356b
A
1362 }
1363 if ( a & ATTR_CMN_ACCESSMASK ) {
1364 searchInfo->mask = *((mode_t *)attributeBuffer);
2d21ac55
A
1365 attributeBuffer = (mode_t *)attributeBuffer + 1;
1366 }
1367 if ( a & ATTR_CMN_FILEID ) {
1368 searchInfo->nodeID = (u_int32_t)*((u_int64_t *) attributeBuffer);
1369 attributeBuffer = (u_int64_t *)attributeBuffer + 1;
1370 }
1371 if ( a & ATTR_CMN_PARENTID ) {
1372 searchInfo->parentDirID = (u_int32_t)*((u_int64_t *) attributeBuffer);
1373 attributeBuffer = (u_int64_t *)attributeBuffer + 1;
1c79356b
A
1374 }
1375 }
1376
1377 a = alist->dirattr;
1378 if ( a != 0 ) {
1379 if ( a & ATTR_DIR_ENTRYCOUNT ) {
91447636 1380 searchInfo->d.numFiles = *((u_int32_t *)attributeBuffer);
2d21ac55 1381 attributeBuffer = (u_int32_t *)attributeBuffer + 1;
1c79356b
A
1382 }
1383 }
1384
1385 a = alist->fileattr;
1386 if ( a != 0 ) {
1387 if ( a & ATTR_FILE_DATALENGTH ) {
1388 searchInfo->f.dataLogicalLength = *((off_t *)attributeBuffer);
2d21ac55 1389 attributeBuffer = (off_t *)attributeBuffer + 1;
1c79356b
A
1390 }
1391 if ( a & ATTR_FILE_DATAALLOCSIZE ) {
1392 searchInfo->f.dataPhysicalLength = *((off_t *)attributeBuffer);
2d21ac55 1393 attributeBuffer = (off_t *)attributeBuffer + 1;
1c79356b
A
1394 }
1395 if ( a & ATTR_FILE_RSRCLENGTH ) {
1396 searchInfo->f.resourceLogicalLength = *((off_t *)attributeBuffer);
2d21ac55 1397 attributeBuffer = (off_t *)attributeBuffer + 1;
1c79356b
A
1398 }
1399 if ( a & ATTR_FILE_RSRCALLOCSIZE ) {
1400 searchInfo->f.resourcePhysicalLength = *((off_t *)attributeBuffer);
2d21ac55 1401 attributeBuffer = (off_t *)attributeBuffer + 1;
1c79356b
A
1402 }
1403 }
1404
1405 return (0);
1406}
316670eb 1407#endif /* CONFIG_SEARCHFS */