2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
35 #include <sys/mount.h>
36 #include <sys/vnode.h>
37 #include <sys/dirent.h>
38 #include <vfs/vfs_support.h>
39 #include <libkern/libkern.h>
41 #include <sys/utfconv.h>
44 #include "hfs_catalog.h"
45 #include "hfs_format.h"
46 #include "hfs_endian.h"
48 #include "hfscommon/headers/BTreesInternal.h"
49 #include "hfscommon/headers/HFSUnicodeWrappers.h"
53 * Initialization of an FSBufferDescriptor structure.
55 #define BDINIT(bd, addr) { \
56 (bd).bufferAddress = (addr); \
57 (bd).itemSize = sizeof(*(addr)); \
63 BTreeIterator iterator
;
64 HFSPlusCatalogKey key
;
69 struct cat_desc
* s_desc
;
70 struct cat_attr
* s_attr
;
71 struct cat_fork
* s_datafork
;
72 struct cat_fork
* s_rsrcfork
;
73 struct hfsmount
* s_hfsmp
;
76 struct position_state
{
81 struct hfsmount
*hfsmp
;
84 /* Map file mode type to directory entry types */
85 u_char modetodirtype
[16] = {
86 DT_REG
, DT_FIFO
, DT_CHR
, DT_UNKNOWN
,
87 DT_DIR
, DT_UNKNOWN
, DT_BLK
, DT_UNKNOWN
,
88 DT_REG
, DT_UNKNOWN
, DT_LNK
, DT_UNKNOWN
,
89 DT_SOCK
, DT_UNKNOWN
, DT_WHT
, DT_UNKNOWN
91 #define MODE_TO_DT(mode) (modetodirtype[((mode) & S_IFMT) >> 12])
94 static int cat_lookupbykey(struct hfsmount
*hfsmp
, CatalogKey
*keyp
, u_long hint
, int wantrsrc
,
95 struct cat_desc
*descp
, struct cat_attr
*attrp
, struct cat_fork
*forkp
, cnid_t
*desc_cnid
);
97 static int cat_lookupmangled(struct hfsmount
*hfsmp
, struct cat_desc
*descp
, int wantrsrc
,
98 struct cat_desc
*outdescp
, struct cat_attr
*attrp
, struct cat_fork
*forkp
);
100 extern int mac_roman_to_unicode(const Str31 hfs_str
, UniChar
*uni_str
,
101 UInt32 maxCharLen
, UInt32
*unicodeChars
);
103 extern int unicode_to_hfs(ExtendedVCB
*vcb
, ByteCount srcLen
,
104 const u_int16_t
* srcStr
, Str31 dstStr
, int retry
);
107 /* Internal catalog support routines */
109 static int cat_findposition(const CatalogKey
*ckp
, const CatalogRecord
*crp
,
110 struct position_state
*state
);
112 static int resolvelinkid(struct hfsmount
*hfsmp
, u_long linkref
, ino_t
*ino
);
114 static int getkey(struct hfsmount
*hfsmp
, cnid_t cnid
, CatalogKey
* key
);
116 static int buildkey(struct hfsmount
*hfsmp
, struct cat_desc
*descp
,
117 HFSPlusCatalogKey
*key
, int retry
);
119 static void buildthreadkey(HFSCatalogNodeID parentID
, int std_hfs
, CatalogKey
*key
);
121 static void buildrecord(struct cat_attr
*attrp
, cnid_t cnid
, int std_hfs
, u_int32_t encoding
, CatalogRecord
*crp
, int *recordSize
);
123 static int catrec_update(const CatalogKey
*ckp
, CatalogRecord
*crp
, struct update_state
*state
);
125 static int builddesc(const HFSPlusCatalogKey
*key
, cnid_t cnid
, u_long hint
, u_long encoding
,
126 int isdir
, struct cat_desc
*descp
);
128 static void getbsdattr(struct hfsmount
*hfsmp
, const struct HFSPlusCatalogFile
*crp
, struct cat_attr
* attrp
);
130 static void promotekey(struct hfsmount
*hfsmp
, const HFSCatalogKey
*hfskey
, HFSPlusCatalogKey
*keyp
, u_long
*encoding
);
131 static void promotefork(struct hfsmount
*hfsmp
, const struct HFSCatalogFile
*file
, int resource
, struct cat_fork
* forkp
);
132 static void promoteattr(struct hfsmount
*hfsmp
, const CatalogRecord
*dataPtr
, struct HFSPlusCatalogFile
*crp
);
134 static cnid_t
getcnid(const CatalogRecord
*crp
);
135 static u_long
getencoding(const CatalogRecord
*crp
);
136 static cnid_t
getparentcnid(const CatalogRecord
*recp
);
138 static int isadir(const CatalogRecord
*crp
);
140 static int buildthread(void *keyp
, void *recp
, int std_hfs
, int directory
);
145 cat_preflight(struct hfsmount
*hfsmp
, catops_t ops
, cat_cookie_t
*cookie
, struct proc
*p
)
151 fcb
= GetFileControlBlock(hfsmp
->hfs_catalog_vp
);
153 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
155 result
= BTReserveSpace(fcb
, ops
, (void*)cookie
);
157 hfs_systemfile_unlock(hfsmp
, lockflags
);
159 return MacToVFSError(result
);
164 cat_postflight(struct hfsmount
*hfsmp
, cat_cookie_t
*cookie
, struct proc
*p
)
169 fcb
= GetFileControlBlock(hfsmp
->hfs_catalog_vp
);
171 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
173 (void) BTReleaseReserve(fcb
, (void*)cookie
);
175 hfs_systemfile_unlock(hfsmp
, lockflags
);
182 struct hfsmount
*hfsmp
,
183 CatalogRecord
* recp
,
184 struct cat_attr
*attrp
,
185 struct cat_fork
*datafp
,
186 struct cat_fork
*rsrcfp
)
188 int std_hfs
= HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
;
191 struct HFSPlusCatalogFile cnoderec
;
193 promoteattr(hfsmp
, recp
, &cnoderec
);
194 getbsdattr(hfsmp
, &cnoderec
, attrp
);
196 getbsdattr(hfsmp
, (struct HFSPlusCatalogFile
*)recp
, attrp
);
200 bzero(datafp
, sizeof(*datafp
));
202 promotefork(hfsmp
, (HFSCatalogFile
*)&recp
->hfsFile
, 0, datafp
);
203 promotefork(hfsmp
, (HFSCatalogFile
*)&recp
->hfsFile
, 1, rsrcfp
);
205 /* Convert the data fork. */
206 datafp
->cf_size
= recp
->hfsPlusFile
.dataFork
.logicalSize
;
207 datafp
->cf_blocks
= recp
->hfsPlusFile
.dataFork
.totalBlocks
;
208 if ((hfsmp
->hfc_stage
== HFC_RECORDING
) &&
209 (attrp
->ca_atime
>= hfsmp
->hfc_timebase
)) {
210 datafp
->cf_bytesread
=
211 recp
->hfsPlusFile
.dataFork
.clumpSize
*
212 HFSTOVCB(hfsmp
)->blockSize
;
214 datafp
->cf_bytesread
= 0;
216 datafp
->cf_vblocks
= 0;
217 bcopy(&recp
->hfsPlusFile
.dataFork
.extents
[0],
218 &datafp
->cf_extents
[0], sizeof(HFSPlusExtentRecord
));
220 /* Convert the resource fork. */
221 rsrcfp
->cf_size
= recp
->hfsPlusFile
.resourceFork
.logicalSize
;
222 rsrcfp
->cf_blocks
= recp
->hfsPlusFile
.resourceFork
.totalBlocks
;
223 if ((hfsmp
->hfc_stage
== HFC_RECORDING
) &&
224 (attrp
->ca_atime
>= hfsmp
->hfc_timebase
)) {
225 datafp
->cf_bytesread
=
226 recp
->hfsPlusFile
.resourceFork
.clumpSize
*
227 HFSTOVCB(hfsmp
)->blockSize
;
229 datafp
->cf_bytesread
= 0;
231 rsrcfp
->cf_vblocks
= 0;
232 bcopy(&recp
->hfsPlusFile
.resourceFork
.extents
[0],
233 &rsrcfp
->cf_extents
[0], sizeof(HFSPlusExtentRecord
));
240 struct hfsmount
*hfsmp
,
242 CatalogRecord
* recp
,
243 struct cat_desc
*descp
)
245 int std_hfs
= HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
;
246 HFSPlusCatalogKey
* pluskey
= NULL
;
250 MALLOC(pluskey
, HFSPlusCatalogKey
*, sizeof(HFSPlusCatalogKey
), M_TEMP
, M_WAITOK
);
251 promotekey(hfsmp
, (HFSCatalogKey
*)key
, pluskey
, &encoding
);
254 pluskey
= (HFSPlusCatalogKey
*)key
;
255 encoding
= getencoding(recp
);
258 builddesc(pluskey
, getcnid(recp
), 0, encoding
, isadir(recp
), descp
);
260 FREE(pluskey
, M_TEMP
);
271 cat_releasedesc(struct cat_desc
*descp
)
278 if ((descp
->cd_flags
& CD_HASBUF
) &&
279 (descp
->cd_nameptr
!= NULL
)) {
280 name
= descp
->cd_nameptr
;
281 descp
->cd_nameptr
= NULL
;
282 descp
->cd_namelen
= 0;
283 descp
->cd_flags
&= ~CD_HASBUF
;
284 vfs_removename(name
);
286 descp
->cd_nameptr
= NULL
;
287 descp
->cd_namelen
= 0;
291 * These Catalog functions allow access to the HFS Catalog (database).
292 * The catalog b-tree lock must be aquired before calling any of these routines.
296 * cat_lookup - lookup a catalog node using a cnode decriptor
300 cat_lookup(struct hfsmount
*hfsmp
, struct cat_desc
*descp
, int wantrsrc
,
301 struct cat_desc
*outdescp
, struct cat_attr
*attrp
,
302 struct cat_fork
*forkp
, cnid_t
*desc_cnid
)
308 std_hfs
= (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
);
310 MALLOC(keyp
, CatalogKey
*, sizeof(CatalogKey
), M_TEMP
, M_WAITOK
);
312 result
= buildkey(hfsmp
, descp
, (HFSPlusCatalogKey
*)keyp
, 1);
316 result
= cat_lookupbykey(hfsmp
, keyp
, descp
->cd_hint
, wantrsrc
, outdescp
, attrp
, forkp
, desc_cnid
);
318 if (result
== ENOENT
) {
320 struct cat_desc temp_desc
;
321 if (outdescp
== NULL
) {
322 bzero(&temp_desc
, sizeof(temp_desc
));
323 outdescp
= &temp_desc
;
325 result
= cat_lookupmangled(hfsmp
, descp
, wantrsrc
, outdescp
, attrp
, forkp
);
327 *desc_cnid
= outdescp
->cd_cnid
;
329 if (outdescp
== &temp_desc
) {
330 /* Release the local copy of desc */
331 cat_releasedesc(outdescp
);
333 } else if (hfsmp
->hfs_encoding
!= kTextEncodingMacRoman
) {
334 // make MacRoman key from utf-8
335 // result = cat_lookupbykey(hfsmp, keyp, descp->cd_hint, attrp, forkp);
336 // update desc text encoding so that other catalog ops succeed
347 cat_insertfilethread(struct hfsmount
*hfsmp
, struct cat_desc
*descp
)
349 struct BTreeIterator
*iterator
;
350 struct FSBufferDescriptor file_data
;
351 struct HFSCatalogFile file_rec
;
356 if (HFSTOVCB(hfsmp
)->vcbSigWord
!= kHFSSigWord
)
359 fcb
= GetFileControlBlock(HFSTOVCB(hfsmp
)->catalogRefNum
);
361 MALLOC(iterator
, BTreeIterator
*, 2 * sizeof(*iterator
), M_TEMP
, M_WAITOK
);
362 bzero(&iterator
[0], 2* sizeof(*iterator
));
363 result
= buildkey(hfsmp
, descp
, (HFSPlusCatalogKey
*)&iterator
[0].key
, 0);
367 BDINIT(file_data
, &file_rec
);
368 result
= BTSearchRecord(fcb
, &iterator
[0], &file_data
, &datasize
, &iterator
[0]);
372 if (file_rec
.recordType
!= kHFSFileRecord
) {
377 if ((file_rec
.flags
& kHFSThreadExistsMask
) == 0) {
378 struct FSBufferDescriptor thread_data
;
379 struct HFSCatalogThread thread_rec
;
381 file_rec
.flags
|= kHFSThreadExistsMask
;
382 BDINIT(thread_data
, &thread_rec
);
383 thread_data
.itemSize
= buildthread(&iterator
[0].key
, &thread_rec
, 1, 0);
384 buildthreadkey(file_rec
.fileID
, 1, (CatalogKey
*)&iterator
[1].key
);
386 result
= BTInsertRecord(fcb
, &iterator
[1], &thread_data
, thread_data
.itemSize
);
390 (void) BTReplaceRecord(fcb
, &iterator
[0], &file_data
, datasize
);
391 (void) BTFlushPath(fcb
);
394 (void) BTFlushPath(fcb
);
395 FREE(iterator
, M_TEMP
);
397 return MacToVFSError(result
);
402 * cat_findname - obtain a descriptor from cnid
404 * Only a thread lookup is performed.
408 cat_findname(struct hfsmount
*hfsmp
, cnid_t cnid
, struct cat_desc
*outdescp
)
410 struct BTreeIterator
* iterator
;
411 FSBufferDescriptor btdata
;
413 CatalogRecord
* recp
;
419 std_hfs
= (hfsmp
->hfs_flags
& HFS_STANDARD
);
421 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
422 buildthreadkey(cnid
, std_hfs
, (CatalogKey
*)&iterator
->key
);
423 iterator
->hint
.nodeNum
= 0;
425 MALLOC(recp
, CatalogRecord
*, sizeof(CatalogRecord
), M_TEMP
, M_WAITOK
);
426 BDINIT(btdata
, recp
);
428 result
= BTSearchRecord(VTOF(hfsmp
->hfs_catalog_vp
), iterator
, &btdata
, NULL
, NULL
);
432 /* Turn thread record into a cnode key (in place). */
433 switch (recp
->recordType
) {
434 case kHFSFolderThreadRecord
:
437 case kHFSFileThreadRecord
:
438 keyp
= (CatalogKey
*)((char *)&recp
->hfsThread
.reserved
+ 6);
439 keyp
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
+ keyp
->hfs
.nodeName
[0];
442 case kHFSPlusFolderThreadRecord
:
445 case kHFSPlusFileThreadRecord
:
446 keyp
= (CatalogKey
*)&recp
->hfsPlusThread
.reserved
;
447 keyp
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
+
448 (keyp
->hfsPlus
.nodeName
.length
* 2);
455 HFSPlusCatalogKey
* pluskey
= NULL
;
458 MALLOC(pluskey
, HFSPlusCatalogKey
*, sizeof(HFSPlusCatalogKey
), M_TEMP
, M_WAITOK
);
459 promotekey(hfsmp
, &keyp
->hfs
, pluskey
, &encoding
);
460 builddesc(pluskey
, cnid
, 0, encoding
, isdir
, outdescp
);
461 FREE(pluskey
, M_TEMP
);
464 builddesc((HFSPlusCatalogKey
*)keyp
, cnid
, 0, 0, isdir
, outdescp
);
468 FREE(iterator
, M_TEMP
);
470 return MacToVFSError(result
);
474 * cat_idlookup - lookup a catalog node using a cnode id
478 cat_idlookup(struct hfsmount
*hfsmp
, cnid_t cnid
, struct cat_desc
*outdescp
,
479 struct cat_attr
*attrp
, struct cat_fork
*forkp
)
481 struct BTreeIterator
* iterator
;
482 FSBufferDescriptor btdata
;
485 CatalogRecord
* recp
;
489 std_hfs
= (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
);
491 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
492 bzero(iterator
, sizeof(*iterator
));
493 buildthreadkey(cnid
, std_hfs
, (CatalogKey
*)&iterator
->key
);
495 MALLOC(recp
, CatalogRecord
*, sizeof(CatalogRecord
), M_TEMP
, M_WAITOK
);
496 BDINIT(btdata
, recp
);
498 result
= BTSearchRecord(VTOF(HFSTOVCB(hfsmp
)->catalogRefNum
), iterator
,
499 &btdata
, &datasize
, iterator
);
503 /* Turn thread record into a cnode key (in place) */
504 switch (recp
->recordType
) {
505 case kHFSFileThreadRecord
:
506 case kHFSFolderThreadRecord
:
507 keyp
= (CatalogKey
*)((char *)&recp
->hfsThread
.reserved
+ 6);
508 keyp
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
+ keyp
->hfs
.nodeName
[0];
511 case kHFSPlusFileThreadRecord
:
512 case kHFSPlusFolderThreadRecord
:
513 keyp
= (CatalogKey
*)&recp
->hfsPlusThread
.reserved
;
514 keyp
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
+
515 (keyp
->hfsPlus
.nodeName
.length
* 2);
523 result
= cat_lookupbykey(hfsmp
, keyp
, 0, 0, outdescp
, attrp
, forkp
, NULL
);
526 FREE(iterator
, M_TEMP
);
528 return MacToVFSError(result
);
533 * cat_lookupmangled - lookup a catalog node using a mangled name
536 cat_lookupmangled(struct hfsmount
*hfsmp
, struct cat_desc
*descp
, int wantrsrc
,
537 struct cat_desc
*outdescp
, struct cat_attr
*attrp
, struct cat_fork
*forkp
)
546 fileID
= GetEmbeddedFileID(descp
->cd_nameptr
, descp
->cd_namelen
, &prefixlen
);
547 if (fileID
< kHFSFirstUserCatalogNodeID
)
550 result
= cat_idlookup(hfsmp
, fileID
, outdescp
, attrp
, forkp
);
554 /* It must be in the correct directory */
555 if (descp
->cd_parentcnid
!= outdescp
->cd_parentcnid
)
558 if ((outdescp
->cd_namelen
< prefixlen
) ||
559 bcmp(outdescp
->cd_nameptr
, descp
->cd_nameptr
, prefixlen
-6) != 0)
565 cat_releasedesc(outdescp
);
571 * cat_lookupbykey - lookup a catalog node using a cnode key
574 cat_lookupbykey(struct hfsmount
*hfsmp
, CatalogKey
*keyp
, u_long hint
, int wantrsrc
,
575 struct cat_desc
*descp
, struct cat_attr
*attrp
, struct cat_fork
*forkp
, cnid_t
*desc_cnid
)
577 struct BTreeIterator
* iterator
;
578 FSBufferDescriptor btdata
;
579 CatalogRecord
* recp
;
587 std_hfs
= (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
);
589 MALLOC(recp
, CatalogRecord
*, sizeof(CatalogRecord
), M_TEMP
, M_WAITOK
);
590 BDINIT(btdata
, recp
);
591 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
592 bzero(iterator
, sizeof(*iterator
));
593 iterator
->hint
.nodeNum
= hint
;
594 bcopy(keyp
, &iterator
->key
, sizeof(CatalogKey
));
596 result
= BTSearchRecord(VTOF(HFSTOVCB(hfsmp
)->catalogRefNum
), iterator
,
597 &btdata
, &datasize
, iterator
);
601 /* Save the cnid now in case there's a hard link */
602 cnid
= getcnid(recp
);
603 encoding
= getencoding(recp
);
604 hint
= iterator
->hint
.nodeNum
;
606 /* Hide the journal files (if any) */
607 if ((hfsmp
->jnl
|| ((HFSTOVCB(hfsmp
)->vcbAtrb
& kHFSVolumeJournaledMask
) && (hfsmp
->hfs_flags
& HFS_READ_ONLY
))) &&
608 ((cnid
== hfsmp
->hfs_jnlfileid
) ||
609 (cnid
== hfsmp
->hfs_jnlinfoblkid
))) {
616 * When a hardlink link is encountered, auto resolve it
620 && (recp
->recordType
== kHFSPlusFileRecord
)
621 && (SWAP_BE32(recp
->hfsPlusFile
.userInfo
.fdType
) == kHardLinkFileType
)
622 && (SWAP_BE32(recp
->hfsPlusFile
.userInfo
.fdCreator
) == kHFSPlusCreator
)
623 && ((to_bsd_time(recp
->hfsPlusFile
.createDate
) == (time_t)HFSTOVCB(hfsmp
)->vcbCrDate
) ||
624 (to_bsd_time(recp
->hfsPlusFile
.createDate
) == (time_t)hfsmp
->hfs_metadata_createdate
))) {
626 ilink
= recp
->hfsPlusFile
.bsdInfo
.special
.iNodeNum
;
628 (void) resolvelink(hfsmp
, ilink
, (struct HFSPlusCatalogFile
*)recp
);
633 struct HFSPlusCatalogFile cnoderec
;
635 promoteattr(hfsmp
, recp
, &cnoderec
);
636 getbsdattr(hfsmp
, &cnoderec
, attrp
);
638 getbsdattr(hfsmp
, (struct HFSPlusCatalogFile
*)recp
, attrp
);
640 attrp
->ca_rdev
= ilink
;
645 bzero(forkp
, sizeof(*forkp
));
646 } else if (std_hfs
) {
647 promotefork(hfsmp
, (HFSCatalogFile
*)&recp
->hfsFile
, wantrsrc
, forkp
);
648 } else if (wantrsrc
) {
649 /* Convert the resource fork. */
650 forkp
->cf_size
= recp
->hfsPlusFile
.resourceFork
.logicalSize
;
651 forkp
->cf_blocks
= recp
->hfsPlusFile
.resourceFork
.totalBlocks
;
652 if ((hfsmp
->hfc_stage
== HFC_RECORDING
) &&
653 (to_bsd_time(recp
->hfsPlusFile
.accessDate
) >= hfsmp
->hfc_timebase
)) {
654 forkp
->cf_bytesread
=
655 recp
->hfsPlusFile
.resourceFork
.clumpSize
*
656 HFSTOVCB(hfsmp
)->blockSize
;
658 forkp
->cf_bytesread
= 0;
660 forkp
->cf_vblocks
= 0;
661 bcopy(&recp
->hfsPlusFile
.resourceFork
.extents
[0],
662 &forkp
->cf_extents
[0], sizeof(HFSPlusExtentRecord
));
667 /* Convert the data fork. */
668 forkp
->cf_size
= recp
->hfsPlusFile
.dataFork
.logicalSize
;
669 forkp
->cf_blocks
= recp
->hfsPlusFile
.dataFork
.totalBlocks
;
670 if ((hfsmp
->hfc_stage
== HFC_RECORDING
) &&
671 (to_bsd_time(recp
->hfsPlusFile
.accessDate
) >= hfsmp
->hfc_timebase
)) {
672 forkp
->cf_bytesread
=
673 recp
->hfsPlusFile
.dataFork
.clumpSize
*
674 HFSTOVCB(hfsmp
)->blockSize
;
676 forkp
->cf_bytesread
= 0;
678 forkp
->cf_vblocks
= 0;
679 bcopy(&recp
->hfsPlusFile
.dataFork
.extents
[0],
680 &forkp
->cf_extents
[0], sizeof(HFSPlusExtentRecord
));
682 /* Validate the fork's resident extents. */
684 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
685 if (forkp
->cf_extents
[i
].startBlock
+ forkp
->cf_extents
[i
].blockCount
>= hfsmp
->totalBlocks
) {
686 /* Suppress any bad extents so a remove can succeed. */
687 forkp
->cf_extents
[i
].startBlock
= 0;
688 forkp
->cf_extents
[i
].blockCount
= 0;
691 attrp
->ca_mode
&= S_IFMT
| S_IRUSR
| S_IRGRP
| S_IROTH
;
694 validblks
+= forkp
->cf_extents
[i
].blockCount
;
697 /* Adjust for any missing blocks. */
698 if ((validblks
< forkp
->cf_blocks
) && (forkp
->cf_extents
[7].blockCount
== 0)) {
701 forkp
->cf_blocks
= validblks
;
703 attrp
->ca_blocks
= validblks
+ recp
->hfsPlusFile
.resourceFork
.totalBlocks
;
705 psize
= (u_int64_t
)validblks
* (u_int64_t
)hfsmp
->blockSize
;
706 if (psize
< forkp
->cf_size
) {
707 forkp
->cf_size
= psize
;
714 HFSPlusCatalogKey
* pluskey
= NULL
;
717 MALLOC(pluskey
, HFSPlusCatalogKey
*, sizeof(HFSPlusCatalogKey
), M_TEMP
, M_WAITOK
);
718 promotekey(hfsmp
, (HFSCatalogKey
*)&iterator
->key
, pluskey
, &encoding
);
721 pluskey
= (HFSPlusCatalogKey
*)&iterator
->key
;
723 builddesc(pluskey
, cnid
, hint
, encoding
, isadir(recp
), descp
);
725 FREE(pluskey
, M_TEMP
);
729 if (desc_cnid
!= NULL
) {
733 FREE(iterator
, M_TEMP
);
736 return MacToVFSError(result
);
741 * cat_create - create a node in the catalog
745 cat_create(struct hfsmount
*hfsmp
, struct cat_desc
*descp
, struct cat_attr
*attrp
,
746 struct cat_desc
*out_descp
)
751 FSBufferDescriptor btdata
;
760 modeformat
= attrp
->ca_mode
& S_IFMT
;
762 vcb
= HFSTOVCB(hfsmp
);
763 fcb
= GetFileControlBlock(vcb
->catalogRefNum
);
764 std_hfs
= (vcb
->vcbSigWord
== kHFSSigWord
);
767 * Atomically get the next CNID. If we have wrapped the CNIDs
768 * then keep the hfsmp lock held until we have found a CNID.
770 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
772 nextCNID
= hfsmp
->vcbNxtCNID
;
773 if (nextCNID
== 0xFFFFFFFF) {
777 hfsmp
->vcbNxtCNID
= kHFSFirstUserCatalogNodeID
;
778 hfsmp
->vcbAtrb
|= kHFSCatalogNodeIDsReusedMask
;
783 hfsmp
->vcbFlags
|= 0xFF00;
784 /* OK to drop lock if CNIDs are not wrapping */
785 if ((hfsmp
->vcbAtrb
& kHFSCatalogNodeIDsReusedMask
) == 0) {
786 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
789 return (result
); /* HFS only exit */
792 /* Get space for iterator, key and data */
793 MALLOC(bto
, struct btobj
*, sizeof(struct btobj
), M_TEMP
, M_WAITOK
);
794 bto
->iterator
.hint
.nodeNum
= 0;
796 result
= buildkey(hfsmp
, descp
, &bto
->key
, 0);
801 encoding
= hfs_pickencoding(bto
->key
.nodeName
.unicode
,
802 bto
->key
.nodeName
.length
);
803 hfs_setencodingbits(hfsmp
, encoding
);
807 * Insert the thread record first
809 if (!std_hfs
|| (modeformat
== S_IFDIR
)) {
810 datalen
= buildthread((void*)&bto
->key
, &bto
->data
, std_hfs
,
811 S_ISDIR(attrp
->ca_mode
));
812 btdata
.bufferAddress
= &bto
->data
;
813 btdata
.itemSize
= datalen
;
814 btdata
.itemCount
= 1;
817 buildthreadkey(nextCNID
, std_hfs
, (CatalogKey
*) &bto
->iterator
.key
);
819 result
= BTInsertRecord(fcb
, &bto
->iterator
, &btdata
, datalen
);
820 if ((result
== btExists
) && !std_hfs
&& mntlock
) {
822 * Allow CNIDs on HFS Plus volumes to wrap around
824 if (++nextCNID
< kHFSFirstUserCatalogNodeID
) {
825 nextCNID
= kHFSFirstUserCatalogNodeID
;
831 if (result
) goto exit
;
835 * CNID is now established. If we have wrapped then
836 * update the vcbNxtCNID and drop the vcb lock.
839 hfsmp
->vcbNxtCNID
= nextCNID
+ 1;
840 if (hfsmp
->vcbNxtCNID
< kHFSFirstUserCatalogNodeID
) {
841 hfsmp
->vcbNxtCNID
= kHFSFirstUserCatalogNodeID
;
843 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
848 * Now insert the file/directory record
850 buildrecord(attrp
, nextCNID
, std_hfs
, encoding
, &bto
->data
, &datalen
);
851 btdata
.bufferAddress
= &bto
->data
;
852 btdata
.itemSize
= datalen
;
853 btdata
.itemCount
= 1;
855 bcopy(&bto
->key
, &bto
->iterator
.key
, sizeof(bto
->key
));
857 result
= BTInsertRecord(fcb
, &bto
->iterator
, &btdata
, datalen
);
859 if (result
== btExists
)
862 /* Back out the thread record */
863 if (!std_hfs
|| S_ISDIR(attrp
->ca_mode
)) {
864 buildthreadkey(nextCNID
, std_hfs
, (CatalogKey
*)&bto
->iterator
.key
);
865 (void) BTDeleteRecord(fcb
, &bto
->iterator
);
871 * Insert was Successfull, update name, parent and volume
875 if (out_descp
!= NULL
) {
876 HFSPlusCatalogKey
* pluskey
= NULL
;
879 MALLOC(pluskey
, HFSPlusCatalogKey
*, sizeof(HFSPlusCatalogKey
), M_TEMP
, M_WAITOK
);
880 promotekey(hfsmp
, (HFSCatalogKey
*)&bto
->iterator
.key
, pluskey
, &encoding
);
883 pluskey
= (HFSPlusCatalogKey
*)&bto
->iterator
.key
;
885 builddesc(pluskey
, nextCNID
, bto
->iterator
.hint
.nodeNum
,
886 encoding
, S_ISDIR(attrp
->ca_mode
), out_descp
);
888 FREE(pluskey
, M_TEMP
);
891 attrp
->ca_fileid
= nextCNID
;
895 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
897 (void) BTFlushPath(fcb
);
900 return MacToVFSError(result
);
905 * cnode_rename - rename a catalog node
907 * Assumes that the target's directory exists.
909 * Order of B-tree operations:
910 * 1. BTSearchRecord(from_cnode, &data);
911 * 2. BTInsertRecord(to_cnode, &data);
912 * 3. BTDeleteRecord(from_cnode);
913 * 4. BTDeleteRecord(from_thread);
914 * 5. BTInsertRecord(to_thread);
919 struct hfsmount
* hfsmp
,
920 struct cat_desc
* from_cdp
,
921 struct cat_desc
* todir_cdp
,
922 struct cat_desc
* to_cdp
,
923 struct cat_desc
* out_cdp
)
925 struct BTreeIterator
* to_iterator
= NULL
;
926 struct BTreeIterator
* from_iterator
= NULL
;
927 FSBufferDescriptor btdata
;
928 CatalogRecord
* recp
= NULL
;
929 HFSPlusCatalogKey
* to_key
;
936 int directory
= from_cdp
->cd_flags
& CD_ISDIR
;
940 vcb
= HFSTOVCB(hfsmp
);
941 fcb
= GetFileControlBlock(vcb
->catalogRefNum
);
942 std_hfs
= (vcb
->vcbSigWord
== kHFSSigWord
);
944 if (from_cdp
->cd_namelen
== 0 || to_cdp
->cd_namelen
== 0)
947 MALLOC(from_iterator
, BTreeIterator
*, sizeof(*from_iterator
), M_TEMP
, M_WAITOK
);
948 bzero(from_iterator
, sizeof(*from_iterator
));
949 if ((result
= buildkey(hfsmp
, from_cdp
, (HFSPlusCatalogKey
*)&from_iterator
->key
, 0)))
952 MALLOC(to_iterator
, BTreeIterator
*, sizeof(*to_iterator
), M_TEMP
, M_WAITOK
);
953 bzero(to_iterator
, sizeof(*to_iterator
));
954 if ((result
= buildkey(hfsmp
, to_cdp
, (HFSPlusCatalogKey
*)&to_iterator
->key
, 0)))
957 to_key
= (HFSPlusCatalogKey
*)&to_iterator
->key
;
958 MALLOC(recp
, CatalogRecord
*, sizeof(CatalogRecord
), M_TEMP
, M_WAITOK
);
959 BDINIT(btdata
, recp
);
962 * When moving a directory, make sure its a valid move.
964 if (directory
&& (from_cdp
->cd_parentcnid
!= to_cdp
->cd_parentcnid
)) {
965 struct BTreeIterator iterator
;
966 cnid_t cnid
= from_cdp
->cd_cnid
;
967 cnid_t pathcnid
= todir_cdp
->cd_parentcnid
;
969 /* First check the obvious ones */
970 if (cnid
== fsRtDirID
||
971 cnid
== to_cdp
->cd_parentcnid
||
976 bzero(&iterator
, sizeof(iterator
));
978 * Traverese destination path all the way back to the root
979 * making sure that source directory is not encountered.
982 while (pathcnid
> fsRtDirID
) {
983 buildthreadkey(pathcnid
, std_hfs
,
984 (CatalogKey
*)&iterator
.key
);
985 result
= BTSearchRecord(fcb
, &iterator
, &btdata
,
987 if (result
) goto exit
;
989 pathcnid
= getparentcnid(recp
);
990 if (pathcnid
== cnid
) {
998 * Step 1: Find cnode data at old location
1000 result
= BTSearchRecord(fcb
, from_iterator
, &btdata
,
1001 &datasize
, from_iterator
);
1003 if (std_hfs
|| (result
!= btNotFound
))
1006 struct cat_desc temp_desc
;
1008 /* Probably the node has mangled name */
1009 result
= cat_lookupmangled(hfsmp
, from_cdp
, 0, &temp_desc
, NULL
, NULL
);
1013 /* The file has mangled name. Search the cnode data using full name */
1014 bzero(from_iterator
, sizeof(*from_iterator
));
1015 result
= buildkey(hfsmp
, &temp_desc
, (HFSPlusCatalogKey
*)&from_iterator
->key
, 0);
1017 cat_releasedesc(&temp_desc
);
1021 result
= BTSearchRecord(fcb
, from_iterator
, &btdata
, &datasize
, from_iterator
);
1023 cat_releasedesc(&temp_desc
);
1027 cat_releasedesc(&temp_desc
);
1030 /* Update the text encoding (on disk and in descriptor) */
1032 encoding
= hfs_pickencoding(to_key
->nodeName
.unicode
,
1033 to_key
->nodeName
.length
);
1034 hfs_setencodingbits(hfsmp
, encoding
);
1035 recp
->hfsPlusFile
.textEncoding
= encoding
;
1037 out_cdp
->cd_encoding
= encoding
;
1040 if (std_hfs
&& !directory
&&
1041 !(recp
->hfsFile
.flags
& kHFSThreadExistsMask
))
1045 * If the keys are identical then there's nothing left to do!
1047 * update the hint and exit
1050 if (std_hfs
&& hfskeycompare(to_key
, iter
->key
) == 0)
1052 if (!std_hfs
&& hfspluskeycompare(to_key
, iter
->key
) == 0)
1056 /* Step 2: Insert cnode at new location */
1057 result
= BTInsertRecord(fcb
, to_iterator
, &btdata
, datasize
);
1058 if (result
== btExists
) {
1059 int fromtype
= recp
->recordType
;
1061 if (from_cdp
->cd_parentcnid
!= to_cdp
->cd_parentcnid
)
1062 goto exit
; /* EEXIST */
1064 /* Find cnode data at new location */
1065 result
= BTSearchRecord(fcb
, to_iterator
, &btdata
, &datasize
, NULL
);
1069 if ((fromtype
!= recp
->recordType
) ||
1070 (from_cdp
->cd_cnid
!= getcnid(recp
))) {
1072 goto exit
; /* EEXIST */
1074 /* The old name is a case variant and must be removed */
1075 result
= BTDeleteRecord(fcb
, from_iterator
);
1079 /* Insert cnode (now that case duplicate is gone) */
1080 result
= BTInsertRecord(fcb
, to_iterator
, &btdata
, datasize
);
1082 /* Try and restore original before leaving */
1087 err
= BTInsertRecord(fcb
, from_iterator
, &btdata
, datasize
);
1089 panic("cat_create: could not undo (BTInsert = %d)", err
);
1092 (void) BTInsertRecord(fcb
, from_iterator
, &btdata
, datasize
);
1101 /* Step 3: Remove cnode from old location */
1103 result
= BTDeleteRecord(fcb
, from_iterator
);
1105 /* Try and delete new record before leaving */
1110 err
= BTDeleteRecord(fcb
, to_iterator
);
1112 panic("cat_create: could not undo (BTDelete = %d)", err
);
1115 (void) BTDeleteRecord(fcb
, to_iterator
);
1121 /* #### POINT OF NO RETURN #### */
1124 * Step 4: Remove cnode's old thread record
1126 buildthreadkey(from_cdp
->cd_cnid
, std_hfs
, (CatalogKey
*)&from_iterator
->key
);
1127 (void) BTDeleteRecord(fcb
, from_iterator
);
1130 * Step 5: Insert cnode's new thread record
1131 * (optional for HFS files)
1134 datasize
= buildthread(&to_iterator
->key
, recp
, std_hfs
, directory
);
1135 btdata
.itemSize
= datasize
;
1136 buildthreadkey(from_cdp
->cd_cnid
, std_hfs
, (CatalogKey
*)&from_iterator
->key
);
1137 result
= BTInsertRecord(fcb
, from_iterator
, &btdata
, datasize
);
1141 HFSPlusCatalogKey
* pluskey
= NULL
;
1144 MALLOC(pluskey
, HFSPlusCatalogKey
*, sizeof(HFSPlusCatalogKey
), M_TEMP
, M_WAITOK
);
1145 promotekey(hfsmp
, (HFSCatalogKey
*)&to_iterator
->key
, pluskey
, &encoding
);
1147 /* Save the real encoding hint in the Finder Info (field 4). */
1148 if (directory
&& from_cdp
->cd_cnid
== kHFSRootFolderID
) {
1151 realhint
= hfs_pickencoding(pluskey
->nodeName
.unicode
, pluskey
->nodeName
.length
);
1152 vcb
->vcbFndrInfo
[4] = SET_HFS_TEXT_ENCODING(realhint
);
1156 pluskey
= (HFSPlusCatalogKey
*)&to_iterator
->key
;
1158 builddesc(pluskey
, from_cdp
->cd_cnid
, to_iterator
->hint
.nodeNum
,
1159 encoding
, directory
, out_cdp
);
1161 FREE(pluskey
, M_TEMP
);
1165 (void) BTFlushPath(fcb
);
1167 FREE(from_iterator
, M_TEMP
);
1169 FREE(to_iterator
, M_TEMP
);
1172 return MacToVFSError(result
);
1177 * cat_delete - delete a node from the catalog
1179 * Order of B-tree operations:
1180 * 1. BTDeleteRecord(cnode);
1181 * 2. BTDeleteRecord(thread);
1182 * 3. BTUpdateRecord(parent);
1186 cat_delete(struct hfsmount
*hfsmp
, struct cat_desc
*descp
, struct cat_attr
*attrp
)
1190 BTreeIterator
*iterator
;
1195 vcb
= HFSTOVCB(hfsmp
);
1196 fcb
= GetFileControlBlock(vcb
->catalogRefNum
);
1197 std_hfs
= (vcb
->vcbSigWord
== kHFSSigWord
);
1201 * The root directory cannot be deleted
1202 * A directory must be empty
1203 * A file must be zero length (no blocks)
1205 if (descp
->cd_cnid
< kHFSFirstUserCatalogNodeID
||
1206 descp
->cd_parentcnid
== kHFSRootParentID
)
1209 /* XXX Preflight Missing */
1211 /* Get space for iterator */
1212 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1213 iterator
->hint
.nodeNum
= 0;
1216 * Derive a key from either the file ID (for a virtual inode)
1217 * or the descriptor.
1219 if (descp
->cd_namelen
== 0) {
1220 result
= getkey(hfsmp
, attrp
->ca_fileid
, (CatalogKey
*)&iterator
->key
);
1221 cnid
= attrp
->ca_fileid
;
1223 result
= buildkey(hfsmp
, descp
, (HFSPlusCatalogKey
*)&iterator
->key
, 0);
1224 cnid
= descp
->cd_cnid
;
1230 result
= BTDeleteRecord(fcb
, iterator
);
1232 if (std_hfs
|| (result
!= btNotFound
))
1235 struct cat_desc temp_desc
;
1237 /* Probably the node has mangled name */
1238 result
= cat_lookupmangled(hfsmp
, descp
, 0, &temp_desc
, attrp
, NULL
);
1242 /* The file has mangled name. Delete the file using full name */
1243 bzero(iterator
, sizeof(*iterator
));
1244 result
= buildkey(hfsmp
, &temp_desc
, (HFSPlusCatalogKey
*)&iterator
->key
, 0);
1245 cnid
= temp_desc
.cd_cnid
;
1247 cat_releasedesc(&temp_desc
);
1251 result
= BTDeleteRecord(fcb
, iterator
);
1253 cat_releasedesc(&temp_desc
);
1257 cat_releasedesc(&temp_desc
);
1260 /* Delete thread record, ignore errors */
1261 buildthreadkey(cnid
, std_hfs
, (CatalogKey
*)&iterator
->key
);
1262 (void) BTDeleteRecord(fcb
, iterator
);
1265 (void) BTFlushPath(fcb
);
1266 FREE(iterator
, M_TEMP
);
1268 return MacToVFSError(result
);
1273 * cnode_update - update the catalog node described by descp
1274 * using the data from attrp and forkp.
1278 cat_update(struct hfsmount
*hfsmp
, struct cat_desc
*descp
, struct cat_attr
*attrp
,
1279 struct cat_fork
*dataforkp
, struct cat_fork
*rsrcforkp
)
1283 BTreeIterator
* iterator
;
1284 struct update_state state
;
1288 vcb
= HFSTOVCB(hfsmp
);
1289 fcb
= GetFileControlBlock(vcb
->catalogRefNum
);
1290 std_hfs
= (vcb
->vcbSigWord
== kHFSSigWord
);
1292 state
.s_desc
= descp
;
1293 state
.s_attr
= attrp
;
1294 state
.s_datafork
= dataforkp
;
1295 state
.s_rsrcfork
= rsrcforkp
;
1296 state
.s_hfsmp
= hfsmp
;
1298 /* Get space for iterator */
1299 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1302 * For open-deleted files we need to do a lookup by cnid
1303 * (using thread rec).
1305 * For hard links, the target of the update is the inode
1306 * itself (not the link record) so a lookup by fileid
1307 * (i.e. thread rec) is needed.
1309 if ((descp
->cd_cnid
!= attrp
->ca_fileid
) || (descp
->cd_namelen
== 0))
1310 result
= getkey(hfsmp
, attrp
->ca_fileid
, (CatalogKey
*)&iterator
->key
);
1312 result
= buildkey(hfsmp
, descp
, (HFSPlusCatalogKey
*)&iterator
->key
, 0);
1316 /* Pass a node hint */
1317 iterator
->hint
.nodeNum
= descp
->cd_hint
;
1319 result
= BTUpdateRecord(fcb
, iterator
,
1320 (IterateCallBackProcPtr
)catrec_update
, &state
);
1324 /* Update the node hint. */
1325 descp
->cd_hint
= iterator
->hint
.nodeNum
;
1328 (void) BTFlushPath(fcb
);
1329 FREE(iterator
, M_TEMP
);
1331 return MacToVFSError(result
);
1335 * catrec_update - Update the fields of a catalog record
1336 * This is called from within BTUpdateRecord.
1339 catrec_update(const CatalogKey
*ckp
, CatalogRecord
*crp
, struct update_state
*state
)
1341 struct cat_desc
*descp
;
1342 struct cat_attr
*attrp
;
1343 struct cat_fork
*forkp
;
1344 struct hfsmount
*hfsmp
;
1348 descp
= state
->s_desc
;
1349 attrp
= state
->s_attr
;
1350 hfsmp
= state
->s_hfsmp
;
1351 blksize
= HFSTOVCB(hfsmp
)->blockSize
;
1353 switch (crp
->recordType
) {
1354 case kHFSFolderRecord
: {
1355 HFSCatalogFolder
*dir
;
1357 dir
= (struct HFSCatalogFolder
*)crp
;
1358 /* Do a quick sanity check */
1359 if ((ckp
->hfs
.parentID
!= descp
->cd_parentcnid
) ||
1360 (dir
->folderID
!= descp
->cd_cnid
))
1361 return (btNotFound
);
1362 dir
->valence
= attrp
->ca_entries
;
1363 dir
->createDate
= UTCToLocal(to_hfs_time(attrp
->ca_itime
));
1364 dir
->modifyDate
= UTCToLocal(to_hfs_time(attrp
->ca_mtime
));
1365 dir
->backupDate
= UTCToLocal(to_hfs_time(attrp
->ca_btime
));
1366 bcopy(&attrp
->ca_finderinfo
[0], &dir
->userInfo
, 16);
1367 bcopy(&attrp
->ca_finderinfo
[16], &dir
->finderInfo
, 16);
1370 case kHFSFileRecord
: {
1371 HFSCatalogFile
*file
;
1373 file
= (struct HFSCatalogFile
*)crp
;
1374 /* Do a quick sanity check */
1375 if ((ckp
->hfs
.parentID
!= descp
->cd_parentcnid
) ||
1376 (file
->fileID
!= attrp
->ca_fileid
))
1377 return (btNotFound
);
1378 file
->createDate
= UTCToLocal(to_hfs_time(attrp
->ca_itime
));
1379 file
->modifyDate
= UTCToLocal(to_hfs_time(attrp
->ca_mtime
));
1380 file
->backupDate
= UTCToLocal(to_hfs_time(attrp
->ca_btime
));
1381 bcopy(&attrp
->ca_finderinfo
[0], &file
->userInfo
, 16);
1382 bcopy(&attrp
->ca_finderinfo
[16], &file
->finderInfo
, 16);
1383 if (state
->s_rsrcfork
) {
1384 forkp
= state
->s_rsrcfork
;
1385 file
->rsrcLogicalSize
= forkp
->cf_size
;
1386 file
->rsrcPhysicalSize
= forkp
->cf_blocks
* blksize
;
1387 for (i
= 0; i
< kHFSExtentDensity
; ++i
) {
1388 file
->rsrcExtents
[i
].startBlock
=
1389 (u_int16_t
)forkp
->cf_extents
[i
].startBlock
;
1390 file
->rsrcExtents
[i
].blockCount
=
1391 (u_int16_t
)forkp
->cf_extents
[i
].blockCount
;
1394 if (state
->s_datafork
) {
1395 forkp
= state
->s_datafork
;
1396 file
->dataLogicalSize
= forkp
->cf_size
;
1397 file
->dataPhysicalSize
= forkp
->cf_blocks
* blksize
;
1398 for (i
= 0; i
< kHFSExtentDensity
; ++i
) {
1399 file
->dataExtents
[i
].startBlock
=
1400 (u_int16_t
)forkp
->cf_extents
[i
].startBlock
;
1401 file
->dataExtents
[i
].blockCount
=
1402 (u_int16_t
)forkp
->cf_extents
[i
].blockCount
;
1407 case kHFSPlusFolderRecord
: {
1408 HFSPlusCatalogFolder
*dir
;
1410 dir
= (struct HFSPlusCatalogFolder
*)crp
;
1411 /* Do a quick sanity check */
1412 if ((ckp
->hfsPlus
.parentID
!= descp
->cd_parentcnid
) ||
1413 (dir
->folderID
!= descp
->cd_cnid
))
1414 return (btNotFound
);
1415 dir
->flags
= attrp
->ca_recflags
;
1416 dir
->valence
= attrp
->ca_entries
;
1417 dir
->createDate
= to_hfs_time(attrp
->ca_itime
);
1418 dir
->contentModDate
= to_hfs_time(attrp
->ca_mtime
);
1419 dir
->backupDate
= to_hfs_time(attrp
->ca_btime
);
1420 dir
->accessDate
= to_hfs_time(attrp
->ca_atime
);
1421 attrp
->ca_atimeondisk
= attrp
->ca_atime
;
1422 dir
->attributeModDate
= to_hfs_time(attrp
->ca_ctime
);
1423 dir
->textEncoding
= descp
->cd_encoding
;
1424 dir
->attrBlocks
= attrp
->ca_attrblks
;
1425 bcopy(&attrp
->ca_finderinfo
[0], &dir
->userInfo
, 32);
1427 * Update the BSD Info if it was already initialized on
1428 * disk or if the runtime values have been modified.
1430 * If the BSD info was already initialized, but
1431 * MNT_UNKNOWNPERMISSIONS is set, then the runtime IDs are
1432 * probably different than what was on disk. We don't want
1433 * to overwrite the on-disk values (so if we turn off
1434 * MNT_UNKNOWNPERMISSIONS, the old IDs get used again).
1435 * This way, we can still change fields like the mode or
1436 * dates even when MNT_UNKNOWNPERMISSIONS is set.
1438 * Note that if MNT_UNKNOWNPERMISSIONS is set, hfs_chown
1439 * won't change the uid or gid from their defaults. So, if
1440 * the BSD info wasn't set, and the runtime values are not
1441 * default, then what changed was the mode or flags. We
1442 * have to set the uid and gid to something, so use the
1443 * supplied values (which will be default), which has the
1444 * same effect as creating a new file while
1445 * MNT_UNKNOWNPERMISSIONS is set.
1447 if ((dir
->bsdInfo
.fileMode
!= 0) ||
1448 (attrp
->ca_flags
!= 0) ||
1449 (attrp
->ca_uid
!= hfsmp
->hfs_uid
) ||
1450 (attrp
->ca_gid
!= hfsmp
->hfs_gid
) ||
1451 ((attrp
->ca_mode
& ALLPERMS
) !=
1452 (hfsmp
->hfs_dir_mask
& ACCESSPERMS
))) {
1453 if ((dir
->bsdInfo
.fileMode
== 0) ||
1454 (((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
) == 0) {
1455 dir
->bsdInfo
.ownerID
= attrp
->ca_uid
;
1456 dir
->bsdInfo
.groupID
= attrp
->ca_gid
;
1458 dir
->bsdInfo
.ownerFlags
= attrp
->ca_flags
& 0x000000FF;
1459 dir
->bsdInfo
.adminFlags
= attrp
->ca_flags
>> 16;
1460 dir
->bsdInfo
.fileMode
= attrp
->ca_mode
;
1464 case kHFSPlusFileRecord
: {
1465 HFSPlusCatalogFile
*file
;
1467 file
= (struct HFSPlusCatalogFile
*)crp
;
1468 /* Do a quick sanity check */
1469 if (file
->fileID
!= attrp
->ca_fileid
)
1470 return (btNotFound
);
1471 file
->flags
= attrp
->ca_recflags
;
1472 file
->createDate
= to_hfs_time(attrp
->ca_itime
);
1473 file
->contentModDate
= to_hfs_time(attrp
->ca_mtime
);
1474 file
->backupDate
= to_hfs_time(attrp
->ca_btime
);
1475 file
->accessDate
= to_hfs_time(attrp
->ca_atime
);
1476 attrp
->ca_atimeondisk
= attrp
->ca_atime
;
1477 file
->attributeModDate
= to_hfs_time(attrp
->ca_ctime
);
1478 file
->textEncoding
= descp
->cd_encoding
;
1479 file
->attrBlocks
= attrp
->ca_attrblks
;
1480 bcopy(&attrp
->ca_finderinfo
[0], &file
->userInfo
, 32);
1482 * Update the BSD Info if it was already initialized on
1483 * disk or if the runtime values have been modified.
1485 * If the BSD info was already initialized, but
1486 * MNT_UNKNOWNPERMISSIONS is set, then the runtime IDs are
1487 * probably different than what was on disk. We don't want
1488 * to overwrite the on-disk values (so if we turn off
1489 * MNT_UNKNOWNPERMISSIONS, the old IDs get used again).
1490 * This way, we can still change fields like the mode or
1491 * dates even when MNT_UNKNOWNPERMISSIONS is set.
1493 * Note that if MNT_UNKNOWNPERMISSIONS is set, hfs_chown
1494 * won't change the uid or gid from their defaults. So, if
1495 * the BSD info wasn't set, and the runtime values are not
1496 * default, then what changed was the mode or flags. We
1497 * have to set the uid and gid to something, so use the
1498 * supplied values (which will be default), which has the
1499 * same effect as creating a new file while
1500 * MNT_UNKNOWNPERMISSIONS is set.
1502 if ((file
->bsdInfo
.fileMode
!= 0) ||
1503 (attrp
->ca_flags
!= 0) ||
1504 (attrp
->ca_uid
!= hfsmp
->hfs_uid
) ||
1505 (attrp
->ca_gid
!= hfsmp
->hfs_gid
) ||
1506 ((attrp
->ca_mode
& ALLPERMS
) !=
1507 (hfsmp
->hfs_file_mask
& ACCESSPERMS
))) {
1508 if ((file
->bsdInfo
.fileMode
== 0) ||
1509 (((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
) == 0) {
1510 file
->bsdInfo
.ownerID
= attrp
->ca_uid
;
1511 file
->bsdInfo
.groupID
= attrp
->ca_gid
;
1513 file
->bsdInfo
.ownerFlags
= attrp
->ca_flags
& 0x000000FF;
1514 file
->bsdInfo
.adminFlags
= attrp
->ca_flags
>> 16;
1515 file
->bsdInfo
.fileMode
= attrp
->ca_mode
;
1517 if (state
->s_rsrcfork
) {
1518 forkp
= state
->s_rsrcfork
;
1519 file
->resourceFork
.logicalSize
= forkp
->cf_size
;
1520 file
->resourceFork
.totalBlocks
= forkp
->cf_blocks
;
1521 bcopy(&forkp
->cf_extents
[0], &file
->resourceFork
.extents
,
1522 sizeof(HFSPlusExtentRecord
));
1523 /* Push blocks read to disk */
1524 file
->resourceFork
.clumpSize
=
1525 howmany(forkp
->cf_bytesread
, blksize
);
1527 if (state
->s_datafork
) {
1528 forkp
= state
->s_datafork
;
1529 file
->dataFork
.logicalSize
= forkp
->cf_size
;
1530 file
->dataFork
.totalBlocks
= forkp
->cf_blocks
;
1531 bcopy(&forkp
->cf_extents
[0], &file
->dataFork
.extents
,
1532 sizeof(HFSPlusExtentRecord
));
1533 /* Push blocks read to disk */
1534 file
->dataFork
.clumpSize
=
1535 howmany(forkp
->cf_bytesread
, blksize
);
1538 if ((file
->resourceFork
.extents
[0].startBlock
!= 0) &&
1539 (file
->resourceFork
.extents
[0].startBlock
==
1540 file
->dataFork
.extents
[0].startBlock
))
1541 panic("catrec_update: rsrc fork == data fork");
1543 /* Synchronize the lock state */
1544 if (attrp
->ca_flags
& (SF_IMMUTABLE
| UF_IMMUTABLE
))
1545 file
->flags
|= kHFSFileLockedMask
;
1547 file
->flags
&= ~kHFSFileLockedMask
;
1549 /* Push out special field if necessary */
1550 if (S_ISBLK(attrp
->ca_mode
) || S_ISCHR(attrp
->ca_mode
))
1551 file
->bsdInfo
.special
.rawDevice
= attrp
->ca_rdev
;
1552 else if (descp
->cd_cnid
!= attrp
->ca_fileid
1553 || attrp
->ca_nlink
== 2)
1554 file
->bsdInfo
.special
.linkCount
= attrp
->ca_nlink
;
1558 return (btNotFound
);
1564 * Callback to collect directory entries.
1565 * Called with readattr_state for each item in a directory.
1567 struct readattr_state
{
1568 struct hfsmount
*hfsmp
;
1569 struct cat_entrylist
*list
;
1576 cat_readattr(const CatalogKey
*key
, const CatalogRecord
*rec
,
1577 struct readattr_state
*state
)
1579 struct cat_entrylist
*list
= state
->list
;
1580 struct hfsmount
*hfsmp
= state
->hfsmp
;
1581 struct cat_entry
*cep
;
1584 if (list
->realentries
>= list
->maxentries
)
1585 return (0); /* stop */
1587 parentcnid
= state
->stdhfs
? key
->hfs
.parentID
: key
->hfsPlus
.parentID
;
1589 switch(rec
->recordType
) {
1590 case kHFSPlusFolderRecord
:
1591 case kHFSPlusFileRecord
:
1592 case kHFSFolderRecord
:
1593 case kHFSFileRecord
:
1594 if (parentcnid
!= state
->dir_cnid
) {
1595 state
->error
= ENOENT
;
1596 return (0); /* stop */
1600 state
->error
= ENOENT
;
1601 return (0); /* stop */
1604 /* Hide the private meta data directory and journal files */
1605 if (parentcnid
== kHFSRootFolderID
) {
1606 if ((rec
->recordType
== kHFSPlusFolderRecord
) &&
1607 (rec
->hfsPlusFolder
.folderID
== hfsmp
->hfs_privdir_desc
.cd_cnid
)) {
1608 return (1); /* continue */
1610 if ((hfsmp
->jnl
|| ((HFSTOVCB(hfsmp
)->vcbAtrb
& kHFSVolumeJournaledMask
) && (hfsmp
->hfs_flags
& HFS_READ_ONLY
))) &&
1611 (rec
->recordType
== kHFSPlusFileRecord
) &&
1612 ((rec
->hfsPlusFile
.fileID
== hfsmp
->hfs_jnlfileid
) ||
1613 (rec
->hfsPlusFile
.fileID
== hfsmp
->hfs_jnlinfoblkid
))) {
1615 return (1); /* continue */
1619 cep
= &list
->entry
[list
->realentries
++];
1621 if (state
->stdhfs
) {
1622 struct HFSPlusCatalogFile cnoderec
;
1623 HFSPlusCatalogKey
* pluskey
;
1626 promoteattr(hfsmp
, rec
, &cnoderec
);
1627 getbsdattr(hfsmp
, &cnoderec
, &cep
->ce_attr
);
1629 MALLOC(pluskey
, HFSPlusCatalogKey
*, sizeof(HFSPlusCatalogKey
), M_TEMP
, M_WAITOK
);
1630 promotekey(hfsmp
, (HFSCatalogKey
*)key
, pluskey
, &encoding
);
1631 builddesc(pluskey
, getcnid(rec
), 0, encoding
, isadir(rec
), &cep
->ce_desc
);
1632 FREE(pluskey
, M_TEMP
);
1634 if (rec
->recordType
== kHFSFileRecord
) {
1635 int blksize
= HFSTOVCB(hfsmp
)->blockSize
;
1637 cep
->ce_datasize
= rec
->hfsFile
.dataLogicalSize
;
1638 cep
->ce_datablks
= rec
->hfsFile
.dataPhysicalSize
/ blksize
;
1639 cep
->ce_rsrcsize
= rec
->hfsFile
.rsrcLogicalSize
;
1640 cep
->ce_rsrcblks
= rec
->hfsFile
.rsrcPhysicalSize
/ blksize
;
1643 getbsdattr(hfsmp
, (struct HFSPlusCatalogFile
*)rec
, &cep
->ce_attr
);
1644 builddesc((HFSPlusCatalogKey
*)key
, getcnid(rec
), 0, getencoding(rec
),
1645 isadir(rec
), &cep
->ce_desc
);
1647 if (rec
->recordType
== kHFSPlusFileRecord
) {
1648 cep
->ce_datasize
= rec
->hfsPlusFile
.dataFork
.logicalSize
;
1649 cep
->ce_datablks
= rec
->hfsPlusFile
.dataFork
.totalBlocks
;
1650 cep
->ce_rsrcsize
= rec
->hfsPlusFile
.resourceFork
.logicalSize
;
1651 cep
->ce_rsrcblks
= rec
->hfsPlusFile
.resourceFork
.totalBlocks
;
1653 /* Save link reference for later processing. */
1654 if ((SWAP_BE32(rec
->hfsPlusFile
.userInfo
.fdType
) == kHardLinkFileType
)
1655 && (SWAP_BE32(rec
->hfsPlusFile
.userInfo
.fdCreator
) == kHFSPlusCreator
))
1656 cep
->ce_attr
.ca_rdev
= rec
->hfsPlusFile
.bsdInfo
.special
.iNodeNum
;
1660 return (list
->realentries
< list
->maxentries
);
1664 * Pack a cat_entrylist buffer with attributes from the catalog
1666 * Note: index is zero relative
1670 cat_getentriesattr(struct hfsmount
*hfsmp
, directoryhint_t
*dirhint
, struct cat_entrylist
*ce_list
)
1674 BTreeIterator
* iterator
;
1675 struct readattr_state state
;
1683 ce_list
->realentries
= 0;
1685 fcb
= GetFileControlBlock(HFSTOVCB(hfsmp
)->catalogRefNum
);
1686 std_hfs
= (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
);
1687 parentcnid
= dirhint
->dh_desc
.cd_parentcnid
;
1689 state
.hfsmp
= hfsmp
;
1690 state
.list
= ce_list
;
1691 state
.dir_cnid
= parentcnid
;
1692 state
.stdhfs
= std_hfs
;
1695 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1696 bzero(iterator
, sizeof(*iterator
));
1697 key
= (CatalogKey
*)&iterator
->key
;
1699 iterator
->hint
.nodeNum
= dirhint
->dh_desc
.cd_hint
;
1700 index
= dirhint
->dh_index
+ 1;
1703 * Attempt to build a key from cached filename
1705 if (dirhint
->dh_desc
.cd_namelen
!= 0) {
1706 if (buildkey(hfsmp
, &dirhint
->dh_desc
, (HFSPlusCatalogKey
*)key
, 0) == 0) {
1712 * If the last entry wasn't cached then position the btree iterator
1714 if ((index
== 0) || !have_key
) {
1716 * Position the iterator at the directory's thread record.
1717 * (i.e. just before the first entry)
1719 buildthreadkey(dirhint
->dh_desc
.cd_parentcnid
, (hfsmp
->hfs_flags
& HFS_STANDARD
), key
);
1720 result
= BTSearchRecord(fcb
, iterator
, NULL
, NULL
, iterator
);
1722 result
= MacToVFSError(result
);
1727 * Iterate until we reach the entry just
1728 * before the one we want to start with.
1731 struct position_state ps
;
1736 ps
.parentID
= dirhint
->dh_desc
.cd_parentcnid
;
1739 result
= BTIterateRecords(fcb
, kBTreeNextRecord
, iterator
,
1740 (IterateCallBackProcPtr
)cat_findposition
, &ps
);
1744 result
= MacToVFSError(result
);
1746 result
= MacToVFSError(result
);
1752 /* Fill list with entries starting at iterator->key. */
1753 result
= BTIterateRecords(fcb
, kBTreeNextRecord
, iterator
,
1754 (IterateCallBackProcPtr
)cat_readattr
, &state
);
1757 result
= state
.error
;
1758 else if (ce_list
->realentries
== 0)
1761 result
= MacToVFSError(result
);
1767 * Resolve any hard links.
1769 for (i
= 0; i
< (int)ce_list
->realentries
; ++i
) {
1770 struct FndrFileInfo
*fip
;
1771 struct cat_entry
*cep
;
1772 struct HFSPlusCatalogFile filerec
;
1774 cep
= &ce_list
->entry
[i
];
1775 if (!S_ISREG(cep
->ce_attr
.ca_mode
))
1778 /* Note: Finder info is still in Big Endian */
1779 fip
= (struct FndrFileInfo
*)&cep
->ce_attr
.ca_finderinfo
;
1781 /* Check for hard link signature. */
1782 if ((cep
->ce_attr
.ca_rdev
!= 0)
1783 && (SWAP_BE32(fip
->fdType
) == kHardLinkFileType
)
1784 && (SWAP_BE32(fip
->fdCreator
) == kHFSPlusCreator
)
1785 && ((cep
->ce_attr
.ca_itime
== (time_t)HFSTOVCB(hfsmp
)->vcbCrDate
) ||
1786 (cep
->ce_attr
.ca_itime
== (time_t)hfsmp
->hfs_metadata_createdate
))) {
1788 if (resolvelink(hfsmp
, cep
->ce_attr
.ca_rdev
, &filerec
) != 0)
1790 /* Repack entry from inode record. */
1791 getbsdattr(hfsmp
, &filerec
, &cep
->ce_attr
);
1792 cep
->ce_datasize
= filerec
.dataFork
.logicalSize
;
1793 cep
->ce_datablks
= filerec
.dataFork
.totalBlocks
;
1794 cep
->ce_rsrcsize
= filerec
.resourceFork
.logicalSize
;
1795 cep
->ce_rsrcblks
= filerec
.resourceFork
.totalBlocks
;
1799 FREE(iterator
, M_TEMP
);
1801 return MacToVFSError(result
);
1804 #define SMALL_DIRENTRY_SIZE (int)(sizeof(struct dirent) - (MAXNAMLEN + 1) + 8)
1807 * Callback to pack directory entries.
1808 * Called with packdirentry_state for each item in a directory.
1811 /* Hard link information collected during cat_getdirentries. */
1814 user_addr_t dirent_addr
;
1816 typedef struct linkinfo linkinfo_t
;
1818 /* State information for the cat_packdirentry callback function. */
1819 struct packdirentry_state
{
1821 u_int32_t cbs_parentID
;
1822 u_int32_t cbs_index
;
1824 ExtendedVCB
* cbs_hfsmp
;
1827 int32_t cbs_maxlinks
;
1828 linkinfo_t
* cbs_linkinfo
;
1829 struct cat_desc
* cbs_desc
;
1830 // struct dirent * cbs_stdentry;
1831 // followign fields are only used for NFS readdir, which uses the next file id as the seek offset of each entry
1832 struct direntry
* cbs_direntry
;
1833 struct direntry
* cbs_prevdirentry
;
1834 u_int32_t cbs_previlinkref
;
1835 Boolean cbs_hasprevdirentry
;
1840 cat_packdirentry(const CatalogKey
*ckp
, const CatalogRecord
*crp
,
1841 struct packdirentry_state
*state
)
1843 struct hfsmount
*hfsmp
;
1847 struct dirent catent
;
1848 struct direntry
* entry
= NULL
;
1850 u_int32_t ilinkref
= 0;
1851 u_int32_t curlinkref
= 0;
1855 u_int8_t is_mangled
= 0;
1857 user_addr_t uiobase
;
1862 Boolean stop_after_pack
= false;
1864 hfsmp
= state
->cbs_hfsmp
;
1866 if (hfsmp
->hfs_flags
& HFS_STANDARD
)
1867 curID
= ckp
->hfs
.parentID
;
1869 curID
= ckp
->hfsPlus
.parentID
;
1871 /* We're done when parent directory changes */
1872 if (state
->cbs_parentID
!= curID
) {
1873 if (state
->cbs_extended
) {
1874 if (state
->cbs_hasprevdirentry
) { /* the last record haven't been returned yet, so we want to stop after
1875 * packing the last item */
1876 stop_after_pack
= true;
1878 state
->cbs_result
= ENOENT
;
1879 return (0); /* stop */
1882 state
->cbs_result
= ENOENT
;
1883 return (0); /* stop */
1887 if (state
->cbs_extended
) {
1888 entry
= state
->cbs_direntry
;
1889 nameptr
= &entry
->d_name
[0];
1890 maxnamelen
= NAME_MAX
;
1892 nameptr
= &catent
.d_name
[0];
1893 maxnamelen
= NAME_MAX
;
1896 if (state
->cbs_extended
&& stop_after_pack
) {
1897 cnid
= INT_MAX
; /* the last item returns a non-zero invalid cookie */
1899 if (!(hfsmp
->hfs_flags
& HFS_STANDARD
)) {
1900 switch(crp
->recordType
) {
1901 case kHFSPlusFolderRecord
:
1903 cnid
= crp
->hfsPlusFolder
.folderID
;
1904 /* Hide our private meta data directory */
1905 if ((curID
== kHFSRootFolderID
) &&
1906 (cnid
== hfsmp
->hfs_privdir_desc
.cd_cnid
)) {
1911 case kHFSPlusFileRecord
:
1912 itime
= to_bsd_time(crp
->hfsPlusFile
.createDate
);
1914 * When a hardlink link is encountered save its link ref.
1916 if ((SWAP_BE32(crp
->hfsPlusFile
.userInfo
.fdType
) == kHardLinkFileType
) &&
1917 (SWAP_BE32(crp
->hfsPlusFile
.userInfo
.fdCreator
) == kHFSPlusCreator
) &&
1918 ((itime
== (time_t)hfsmp
->hfs_itime
) ||
1919 (itime
== (time_t)hfsmp
->hfs_metadata_createdate
))) {
1920 ilinkref
= crp
->hfsPlusFile
.bsdInfo
.special
.iNodeNum
;
1922 type
= MODE_TO_DT(crp
->hfsPlusFile
.bsdInfo
.fileMode
);
1923 cnid
= crp
->hfsPlusFile
.fileID
;
1924 /* Hide the journal files */
1925 if ((curID
== kHFSRootFolderID
) &&
1926 ((hfsmp
->jnl
|| ((HFSTOVCB(hfsmp
)->vcbAtrb
& kHFSVolumeJournaledMask
) && (hfsmp
->hfs_flags
& HFS_READ_ONLY
)))) &&
1927 ((cnid
== hfsmp
->hfs_jnlfileid
) ||
1928 (cnid
== hfsmp
->hfs_jnlinfoblkid
))) {
1933 return (0); /* stop */
1936 cnp
= (CatalogName
*) &ckp
->hfsPlus
.nodeName
;
1937 result
= utf8_encodestr(cnp
->ustr
.unicode
, cnp
->ustr
.length
* sizeof(UniChar
),
1938 nameptr
, &namelen
, maxnamelen
+ 1, ':', 0);
1939 if (result
== ENAMETOOLONG
) {
1940 result
= ConvertUnicodeToUTF8Mangled(cnp
->ustr
.length
* sizeof(UniChar
),
1941 cnp
->ustr
.unicode
, maxnamelen
+ 1,
1942 (ByteCount
*)&namelen
, nameptr
,
1947 switch(crp
->recordType
) {
1948 case kHFSFolderRecord
:
1950 cnid
= crp
->hfsFolder
.folderID
;
1952 case kHFSFileRecord
:
1954 cnid
= crp
->hfsFile
.fileID
;
1957 return (0); /* stop */
1960 cnp
= (CatalogName
*) ckp
->hfs
.nodeName
;
1961 result
= hfs_to_utf8(hfsmp
, cnp
->pstr
, maxnamelen
+ 1,
1962 (ByteCount
*)&namelen
, nameptr
);
1964 * When an HFS name cannot be encoded with the current
1965 * volume encoding we use MacRoman as a fallback.
1968 result
= mac_roman_to_utf8(cnp
->pstr
, maxnamelen
+ 1,
1969 (ByteCount
*)&namelen
, nameptr
);
1973 if (state
->cbs_extended
) {
1975 * The index is 1 relative and includes "." and ".."
1977 * Also stuff the cnid in the upper 32 bits of the cookie. The cookie is stored to the previous entry, which
1978 * will be packed and copied this time
1980 state
->cbs_prevdirentry
->d_seekoff
= (state
->cbs_index
+ 3) | ((u_int64_t
)cnid
<< 32);
1981 uiosize
= state
->cbs_prevdirentry
->d_reclen
;
1982 uioaddr
= (caddr_t
) state
->cbs_prevdirentry
;
1984 catent
.d_type
= type
;
1985 catent
.d_namlen
= namelen
;
1986 catent
.d_reclen
= uiosize
= STD_DIRENT_LEN(namelen
);
1988 catent
.d_fileno
= 0; /* file number = 0 means skip entry */
1990 catent
.d_fileno
= cnid
;
1991 uioaddr
= (caddr_t
) &catent
;
1994 /* Save current base address for post processing of hard-links. */
1995 uiobase
= uio_curriovbase(state
->cbs_uio
);
1997 /* If this entry won't fit then we're done */
1998 if ((uiosize
> uio_resid(state
->cbs_uio
)) ||
1999 (ilinkref
!= 0 && state
->cbs_nlinks
== state
->cbs_maxlinks
)) {
2000 return (0); /* stop */
2003 if (!state
->cbs_extended
|| state
->cbs_hasprevdirentry
) {
2004 state
->cbs_result
= uiomove(uioaddr
, uiosize
, state
->cbs_uio
);
2005 if (state
->cbs_result
== 0) {
2008 /* Remember previous entry */
2009 state
->cbs_desc
->cd_cnid
= cnid
;
2010 if (type
== DT_DIR
) {
2011 state
->cbs_desc
->cd_flags
|= CD_ISDIR
;
2013 state
->cbs_desc
->cd_flags
&= ~CD_ISDIR
;
2015 if (state
->cbs_desc
->cd_nameptr
!= NULL
) {
2016 vfs_removename(state
->cbs_desc
->cd_nameptr
);
2019 state
->cbs_desc
->cd_encoding
= xxxx
;
2022 state
->cbs_desc
->cd_namelen
= namelen
;
2023 state
->cbs_desc
->cd_nameptr
= vfs_addname(nameptr
, namelen
, 0, 0);
2025 /* Store unmangled name for the directory hint else it will
2026 * restart readdir at the last location again
2030 size_t tmp_namelen
= 0;
2032 cnp
= (CatalogName
*)&ckp
->hfsPlus
.nodeName
;
2033 bufsize
= 1 + utf8_encodelen(cnp
->ustr
.unicode
,
2034 cnp
->ustr
.length
* sizeof(UniChar
),
2036 MALLOC(new_nameptr
, char *, bufsize
, M_TEMP
, M_WAITOK
);
2037 result
= utf8_encodestr(cnp
->ustr
.unicode
,
2038 cnp
->ustr
.length
* sizeof(UniChar
),
2039 new_nameptr
, &tmp_namelen
,
2042 state
->cbs_desc
->cd_namelen
= tmp_namelen
;
2043 state
->cbs_desc
->cd_nameptr
= vfs_addname(new_nameptr
, tmp_namelen
, 0, 0);
2045 FREE(new_nameptr
, M_TEMP
);
2048 if (state
->cbs_hasprevdirentry
) {
2049 curlinkref
= ilinkref
; /* save current */
2050 ilinkref
= state
->cbs_previlinkref
; /* use previous */
2053 * Record any hard links for post processing.
2055 if ((ilinkref
!= 0) &&
2056 (state
->cbs_result
== 0) &&
2057 (state
->cbs_nlinks
< state
->cbs_maxlinks
)) {
2058 state
->cbs_linkinfo
[state
->cbs_nlinks
].dirent_addr
= uiobase
;
2059 state
->cbs_linkinfo
[state
->cbs_nlinks
].link_ref
= ilinkref
;
2060 state
->cbs_nlinks
++;
2062 if (state
->cbs_hasprevdirentry
) {
2063 ilinkref
= curlinkref
; /* restore current */
2067 if (state
->cbs_extended
) { /* fill the direntry to be used the next time */
2068 if (stop_after_pack
) {
2069 state
->cbs_eof
= true;
2070 return (0); /* stop */
2072 entry
->d_type
= type
;
2073 entry
->d_namlen
= namelen
;
2074 entry
->d_reclen
= EXT_DIRENT_LEN(namelen
);
2076 entry
->d_fileno
= 0; /* file number = 0 means skip entry */
2078 entry
->d_fileno
= cnid
;
2079 /* swap the current and previous entry */
2080 struct direntry
* tmp
;
2081 tmp
= state
->cbs_direntry
;
2082 state
->cbs_direntry
= state
->cbs_prevdirentry
;
2083 state
->cbs_prevdirentry
= tmp
;
2084 state
->cbs_hasprevdirentry
= true;
2085 state
->cbs_previlinkref
= ilinkref
;
2088 /* Continue iteration if there's room */
2089 return (state
->cbs_result
== 0 &&
2090 uio_resid(state
->cbs_uio
) >= SMALL_DIRENTRY_SIZE
);
2095 * Pack a uio buffer with directory entries from the catalog
2099 cat_getdirentries(struct hfsmount
*hfsmp
, int entrycnt
, directoryhint_t
*dirhint
,
2100 uio_t uio
, int extended
, int * items
, int * eofflag
)
2103 BTreeIterator
* iterator
;
2105 struct packdirentry_state state
;
2113 fcb
= GetFileControlBlock(hfsmp
->hfs_catalog_vp
);
2116 * Get a buffer for link info array, btree iterator and a direntry:
2118 maxlinks
= MIN(entrycnt
, uio_resid(uio
) / SMALL_DIRENTRY_SIZE
);
2119 bufsize
= (maxlinks
* sizeof(linkinfo_t
)) + sizeof(*iterator
);
2121 bufsize
+= 2*sizeof(struct direntry
);
2123 MALLOC(buffer
, void *, bufsize
, M_TEMP
, M_WAITOK
);
2124 bzero(buffer
, bufsize
);
2126 state
.cbs_extended
= extended
;
2127 state
.cbs_hasprevdirentry
= false;
2128 state
.cbs_previlinkref
= 0;
2129 state
.cbs_nlinks
= 0;
2130 state
.cbs_maxlinks
= maxlinks
;
2131 state
.cbs_linkinfo
= (linkinfo_t
*) buffer
;
2133 iterator
= (BTreeIterator
*) ((char *)buffer
+ (maxlinks
* sizeof(linkinfo_t
)));
2134 key
= (CatalogKey
*)&iterator
->key
;
2136 index
= dirhint
->dh_index
+ 1;
2138 state
.cbs_direntry
= (struct direntry
*)((char *)iterator
+ sizeof(BTreeIterator
));
2139 state
.cbs_prevdirentry
= state
.cbs_direntry
+ 1;
2140 state
.cbs_eof
= false;
2143 * Attempt to build a key from cached filename
2145 if (dirhint
->dh_desc
.cd_namelen
!= 0) {
2146 if (buildkey(hfsmp
, &dirhint
->dh_desc
, (HFSPlusCatalogKey
*)key
, 0) == 0) {
2152 * If the last entry wasn't cached then position the btree iterator
2154 if ((index
== 0) || !have_key
) {
2156 * Position the iterator at the directory's thread record.
2157 * (i.e. just before the first entry)
2159 buildthreadkey(dirhint
->dh_desc
.cd_parentcnid
, (hfsmp
->hfs_flags
& HFS_STANDARD
), key
);
2160 result
= BTSearchRecord(fcb
, iterator
, NULL
, NULL
, iterator
);
2162 result
= MacToVFSError(result
);
2167 * Iterate until we reach the entry just
2168 * before the one we want to start with.
2171 struct position_state ps
;
2176 ps
.parentID
= dirhint
->dh_desc
.cd_parentcnid
;
2179 result
= BTIterateRecords(fcb
, kBTreeNextRecord
, iterator
,
2180 (IterateCallBackProcPtr
)cat_findposition
, &ps
);
2184 result
= MacToVFSError(result
);
2186 result
= MacToVFSError(result
);
2192 state
.cbs_index
= index
;
2193 state
.cbs_hfsmp
= hfsmp
;
2194 state
.cbs_uio
= uio
;
2195 state
.cbs_desc
= &dirhint
->dh_desc
;
2196 state
.cbs_result
= 0;
2197 state
.cbs_parentID
= dirhint
->dh_desc
.cd_parentcnid
;
2199 enum BTreeIterationOperations op
;
2200 if (extended
&& index
!= 0 && have_key
)
2201 op
= kBTreeCurrentRecord
;
2203 op
= kBTreeNextRecord
;
2206 * Process as many entries as possible starting at iterator->key.
2208 result
= BTIterateRecords(fcb
, op
, iterator
,
2209 (IterateCallBackProcPtr
)cat_packdirentry
, &state
);
2211 /* Note that state.cbs_index is still valid on errors */
2212 *items
= state
.cbs_index
- index
;
2213 index
= state
.cbs_index
;
2215 if (state
.cbs_eof
) {
2219 /* Finish updating the catalog iterator. */
2220 dirhint
->dh_desc
.cd_hint
= iterator
->hint
.nodeNum
;
2221 dirhint
->dh_desc
.cd_flags
|= CD_DECOMPOSED
;
2222 dirhint
->dh_index
= index
- 1;
2225 * Post process any hard links to get the real file id.
2227 if (state
.cbs_nlinks
> 0) {
2228 u_int32_t fileid
= 0;
2229 user_addr_t address
;
2232 for (i
= 0; i
< state
.cbs_nlinks
; ++i
) {
2233 if (resolvelinkid(hfsmp
, state
.cbs_linkinfo
[i
].link_ref
, &fileid
) != 0)
2235 /* This assumes that d_ino is always first field. */
2236 address
= state
.cbs_linkinfo
[i
].dirent_addr
;
2237 if (address
== (user_addr_t
)0)
2239 if (uio_isuserspace(uio
)) {
2240 (void) copyout(&fileid
, address
,
2241 extended
? sizeof(ino64_t
) : sizeof(ino_t
));
2242 } else /* system space */ {
2243 ino64_t
*inoptr
= (ino64_t
*)CAST_DOWN(caddr_t
, address
);
2249 if (state
.cbs_result
)
2250 result
= state
.cbs_result
;
2252 result
= MacToVFSError(result
);
2254 if (result
== ENOENT
) {
2259 FREE(buffer
, M_TEMP
);
2266 * Callback to establish directory position.
2267 * Called with position_state for each item in a directory.
2270 cat_findposition(const CatalogKey
*ckp
, const CatalogRecord
*crp
,
2271 struct position_state
*state
)
2275 if (state
->hfsmp
->hfs_flags
& HFS_STANDARD
)
2276 curID
= ckp
->hfs
.parentID
;
2278 curID
= ckp
->hfsPlus
.parentID
;
2280 /* Make sure parent directory didn't change */
2281 if (state
->parentID
!= curID
) {
2282 state
->error
= EINVAL
;
2283 return (0); /* stop */
2286 /* Count this entry */
2287 switch(crp
->recordType
) {
2288 case kHFSPlusFolderRecord
:
2289 case kHFSPlusFileRecord
:
2290 case kHFSFolderRecord
:
2291 case kHFSFileRecord
:
2295 printf("cat_findposition: invalid record type %d in dir %d\n",
2296 crp
->recordType
, curID
);
2297 state
->error
= EINVAL
;
2298 return (0); /* stop */
2301 return (state
->count
< state
->index
);
2306 * cat_binarykeycompare - compare two HFS Plus catalog keys.
2308 * The name portion of the key is compared using a 16-bit binary comparison.
2309 * This is called from the b-tree code.
2313 cat_binarykeycompare(HFSPlusCatalogKey
*searchKey
, HFSPlusCatalogKey
*trialKey
)
2315 u_int32_t searchParentID
, trialParentID
;
2318 searchParentID
= searchKey
->parentID
;
2319 trialParentID
= trialKey
->parentID
;
2322 if (searchParentID
> trialParentID
) {
2324 } else if (searchParentID
< trialParentID
) {
2327 u_int16_t
* str1
= &searchKey
->nodeName
.unicode
[0];
2328 u_int16_t
* str2
= &trialKey
->nodeName
.unicode
[0];
2329 int length1
= searchKey
->nodeName
.length
;
2330 int length2
= trialKey
->nodeName
.length
;
2334 if (length1
< length2
) {
2337 } else if (length1
> length2
) {
2364 * Compare two standard HFS catalog keys
2366 * Result: +n search key > trial key
2367 * 0 search key = trial key
2368 * -n search key < trial key
2371 CompareCatalogKeys(HFSCatalogKey
*searchKey
, HFSCatalogKey
*trialKey
)
2373 cnid_t searchParentID
, trialParentID
;
2376 searchParentID
= searchKey
->parentID
;
2377 trialParentID
= trialKey
->parentID
;
2379 if (searchParentID
> trialParentID
)
2381 else if (searchParentID
< trialParentID
)
2383 else /* parent dirID's are equal, compare names */
2384 result
= FastRelString(searchKey
->nodeName
, trialKey
->nodeName
);
2391 * Compare two HFS+ catalog keys
2393 * Result: +n search key > trial key
2394 * 0 search key = trial key
2395 * -n search key < trial key
2398 CompareExtendedCatalogKeys(HFSPlusCatalogKey
*searchKey
, HFSPlusCatalogKey
*trialKey
)
2400 cnid_t searchParentID
, trialParentID
;
2403 searchParentID
= searchKey
->parentID
;
2404 trialParentID
= trialKey
->parentID
;
2406 if (searchParentID
> trialParentID
) {
2409 else if (searchParentID
< trialParentID
) {
2412 /* parent node ID's are equal, compare names */
2413 if ( searchKey
->nodeName
.length
== 0 || trialKey
->nodeName
.length
== 0 )
2414 result
= searchKey
->nodeName
.length
- trialKey
->nodeName
.length
;
2416 result
= FastUnicodeCompare(&searchKey
->nodeName
.unicode
[0],
2417 searchKey
->nodeName
.length
,
2418 &trialKey
->nodeName
.unicode
[0],
2419 trialKey
->nodeName
.length
);
2427 * buildkey - build a Catalog b-tree key from a cnode descriptor
2430 buildkey(struct hfsmount
*hfsmp
, struct cat_desc
*descp
,
2431 HFSPlusCatalogKey
*key
, int retry
)
2435 size_t unicodeBytes
= 0;
2437 if (descp
->cd_namelen
== 0 || descp
->cd_nameptr
[0] == '\0')
2438 return (EINVAL
); /* invalid name */
2440 key
->parentID
= descp
->cd_parentcnid
;
2441 key
->nodeName
.length
= 0;
2443 * Convert filename from UTF-8 into Unicode
2446 if ((descp
->cd_flags
& CD_DECOMPOSED
) == 0)
2447 utf8_flags
|= UTF_DECOMPOSED
;
2448 result
= utf8_decodestr(descp
->cd_nameptr
, descp
->cd_namelen
,
2449 key
->nodeName
.unicode
, &unicodeBytes
,
2450 sizeof(key
->nodeName
.unicode
), ':', utf8_flags
);
2451 key
->nodeName
.length
= unicodeBytes
/ sizeof(UniChar
);
2452 key
->keyLength
= kHFSPlusCatalogKeyMinimumLength
+ unicodeBytes
;
2454 if (result
!= ENAMETOOLONG
)
2455 result
= EINVAL
; /* name has invalid characters */
2460 * For HFS volumes convert to an HFS compatible key
2462 * XXX need to save the encoding that succeeded
2464 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) {
2465 HFSCatalogKey hfskey
;
2467 bzero(&hfskey
, sizeof(hfskey
));
2468 hfskey
.keyLength
= kHFSCatalogKeyMinimumLength
;
2469 hfskey
.parentID
= key
->parentID
;
2470 hfskey
.nodeName
[0] = 0;
2471 if (key
->nodeName
.length
> 0) {
2472 if (unicode_to_hfs(HFSTOVCB(hfsmp
),
2473 key
->nodeName
.length
* 2,
2474 key
->nodeName
.unicode
,
2475 &hfskey
.nodeName
[0], retry
) != 0) {
2478 hfskey
.keyLength
+= hfskey
.nodeName
[0];
2480 bcopy(&hfskey
, key
, sizeof(hfskey
));
2487 * Resolve hard link reference to obtain the inode record.
2491 resolvelink(struct hfsmount
*hfsmp
, u_long linkref
, struct HFSPlusCatalogFile
*recp
)
2493 FSBufferDescriptor btdata
;
2494 struct BTreeIterator
*iterator
;
2495 struct cat_desc idesc
;
2499 BDINIT(btdata
, recp
);
2500 MAKE_INODE_NAME(inodename
, linkref
);
2502 /* Get space for iterator */
2503 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
2504 bzero(iterator
, sizeof(*iterator
));
2506 /* Build a descriptor for private dir. */
2507 idesc
.cd_parentcnid
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
2508 idesc
.cd_nameptr
= inodename
;
2509 idesc
.cd_namelen
= strlen(inodename
);
2512 idesc
.cd_encoding
= 0;
2513 (void) buildkey(hfsmp
, &idesc
, (HFSPlusCatalogKey
*)&iterator
->key
, 0);
2515 result
= BTSearchRecord(VTOF(HFSTOVCB(hfsmp
)->catalogRefNum
), iterator
,
2516 &btdata
, NULL
, NULL
);
2519 /* Make sure there's a reference */
2520 if (recp
->bsdInfo
.special
.linkCount
== 0)
2521 recp
->bsdInfo
.special
.linkCount
= 2;
2523 printf("HFS resolvelink: can't find %s\n", inodename
);
2526 FREE(iterator
, M_TEMP
);
2528 return (result
? ENOENT
: 0);
2532 * Resolve hard link reference to obtain the inode number.
2535 resolvelinkid(struct hfsmount
*hfsmp
, u_long linkref
, ino_t
*ino
)
2537 struct HFSPlusCatalogFile record
;
2540 error
= resolvelink(hfsmp
, linkref
, &record
);
2542 if (record
.fileID
== 0)
2545 *ino
= record
.fileID
;
2551 * getkey - get a key from id by doing a thread lookup
2554 getkey(struct hfsmount
*hfsmp
, cnid_t cnid
, CatalogKey
* key
)
2556 struct BTreeIterator
* iterator
;
2557 FSBufferDescriptor btdata
;
2560 CatalogRecord
* recp
;
2564 std_hfs
= (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
);
2566 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
2567 bzero(iterator
, sizeof(*iterator
));
2568 buildthreadkey(cnid
, std_hfs
, (CatalogKey
*)&iterator
->key
);
2570 MALLOC(recp
, CatalogRecord
*, sizeof(CatalogRecord
), M_TEMP
, M_WAITOK
);
2571 BDINIT(btdata
, recp
);
2573 result
= BTSearchRecord(VTOF(HFSTOVCB(hfsmp
)->catalogRefNum
), iterator
,
2574 &btdata
, &datasize
, iterator
);
2578 /* Turn thread record into a cnode key (in place) */
2579 switch (recp
->recordType
) {
2580 case kHFSFileThreadRecord
:
2581 case kHFSFolderThreadRecord
:
2582 keyp
= (CatalogKey
*)((char *)&recp
->hfsThread
.reserved
+ 6);
2583 keyp
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
+ keyp
->hfs
.nodeName
[0];
2584 bcopy(keyp
, key
, keyp
->hfs
.keyLength
+ 1);
2587 case kHFSPlusFileThreadRecord
:
2588 case kHFSPlusFolderThreadRecord
:
2589 keyp
= (CatalogKey
*)&recp
->hfsPlusThread
.reserved
;
2590 keyp
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
+
2591 (keyp
->hfsPlus
.nodeName
.length
* 2);
2592 bcopy(keyp
, key
, keyp
->hfsPlus
.keyLength
+ 2);
2601 FREE(iterator
, M_TEMP
);
2604 return MacToVFSError(result
);
2608 * getkeyplusattr - From id, fetch the key and the bsd attrs for a file/dir (could pass
2609 * null arguments to cat_idlookup instead, but we save around 10% by not building the
2610 * cat_desc here). Both key and attrp must point to real structures.
2614 cat_getkeyplusattr(struct hfsmount
*hfsmp
, cnid_t cnid
, CatalogKey
* key
, struct cat_attr
*attrp
)
2618 result
= getkey(hfsmp
, cnid
, key
);
2621 result
= cat_lookupbykey(hfsmp
, key
, 0, 0, NULL
, attrp
, NULL
, NULL
);
2624 return MacToVFSError(result
);
2629 * buildrecord - build a default catalog directory or file record
2632 buildrecord(struct cat_attr
*attrp
, cnid_t cnid
, int std_hfs
, u_int32_t encoding
,
2633 CatalogRecord
*crp
, int *recordSize
)
2635 int type
= attrp
->ca_mode
& S_IFMT
;
2636 u_int32_t createtime
= to_hfs_time(attrp
->ca_itime
);
2639 createtime
= UTCToLocal(createtime
);
2640 if (type
== S_IFDIR
) {
2641 bzero(crp
, sizeof(HFSCatalogFolder
));
2642 crp
->recordType
= kHFSFolderRecord
;
2643 crp
->hfsFolder
.folderID
= cnid
;
2644 crp
->hfsFolder
.createDate
= createtime
;
2645 crp
->hfsFolder
.modifyDate
= createtime
;
2646 bcopy(attrp
->ca_finderinfo
, &crp
->hfsFolder
.userInfo
, 32);
2647 *recordSize
= sizeof(HFSCatalogFolder
);
2649 bzero(crp
, sizeof(HFSCatalogFile
));
2650 crp
->recordType
= kHFSFileRecord
;
2651 crp
->hfsFile
.fileID
= cnid
;
2652 crp
->hfsFile
.createDate
= createtime
;
2653 crp
->hfsFile
.modifyDate
= createtime
;
2654 bcopy(attrp
->ca_finderinfo
, &crp
->hfsFile
.userInfo
, 16);
2655 bcopy(&attrp
->ca_finderinfo
[16], &crp
->hfsFile
.finderInfo
, 16);
2656 *recordSize
= sizeof(HFSCatalogFile
);
2659 struct HFSPlusBSDInfo
* bsdp
= NULL
;
2660 struct FndrFileInfo
* fip
= NULL
;
2662 if (type
== S_IFDIR
) {
2663 crp
->recordType
= kHFSPlusFolderRecord
;
2664 crp
->hfsPlusFolder
.flags
= 0;
2665 crp
->hfsPlusFolder
.valence
= 0;
2666 crp
->hfsPlusFolder
.folderID
= cnid
;
2667 crp
->hfsPlusFolder
.createDate
= createtime
;
2668 crp
->hfsPlusFolder
.contentModDate
= createtime
;
2669 crp
->hfsPlusFolder
.attributeModDate
= createtime
;
2670 crp
->hfsPlusFolder
.accessDate
= createtime
;
2671 crp
->hfsPlusFolder
.backupDate
= 0;
2672 crp
->hfsPlusFolder
.textEncoding
= encoding
;
2673 crp
->hfsPlusFolder
.attrBlocks
= 0;
2674 bcopy(attrp
->ca_finderinfo
, &crp
->hfsPlusFolder
.userInfo
, 32);
2675 bsdp
= &crp
->hfsPlusFolder
.bsdInfo
;
2676 bsdp
->special
.rawDevice
= 0;
2677 *recordSize
= sizeof(HFSPlusCatalogFolder
);
2679 crp
->recordType
= kHFSPlusFileRecord
;
2680 crp
->hfsPlusFile
.flags
= kHFSThreadExistsMask
;
2681 crp
->hfsPlusFile
.reserved1
= 0;
2682 crp
->hfsPlusFile
.fileID
= cnid
;
2683 crp
->hfsPlusFile
.createDate
= createtime
;
2684 crp
->hfsPlusFile
.contentModDate
= createtime
;
2685 crp
->hfsPlusFile
.accessDate
= createtime
;
2686 crp
->hfsPlusFile
.attributeModDate
= createtime
;
2687 crp
->hfsPlusFile
.backupDate
= 0;
2688 crp
->hfsPlusFile
.textEncoding
= encoding
;
2689 crp
->hfsPlusFile
.attrBlocks
= 0;
2690 bsdp
= &crp
->hfsPlusFile
.bsdInfo
;
2691 bsdp
->special
.rawDevice
= 0;
2695 /* BLK/CHR need to save the device info */
2696 bsdp
->special
.rawDevice
= attrp
->ca_rdev
;
2697 bzero(&crp
->hfsPlusFile
.userInfo
, 32);
2700 /* Hardlink links need to save the linkref */
2701 fip
= (FndrFileInfo
*)&attrp
->ca_finderinfo
;
2702 if ((SWAP_BE32(fip
->fdType
) == kHardLinkFileType
) &&
2703 (SWAP_BE32(fip
->fdCreator
) == kHFSPlusCreator
)) {
2704 bsdp
->special
.iNodeNum
= attrp
->ca_rdev
;
2706 bcopy(attrp
->ca_finderinfo
, &crp
->hfsPlusFile
.userInfo
, 32);
2709 /* Symlinks also have a type and creator */
2710 bcopy(attrp
->ca_finderinfo
, &crp
->hfsPlusFile
.userInfo
, 32);
2713 bzero(&crp
->hfsPlusFile
.dataFork
, 2*sizeof(HFSPlusForkData
));
2714 *recordSize
= sizeof(HFSPlusCatalogFile
);
2716 bsdp
->ownerID
= attrp
->ca_uid
;
2717 bsdp
->groupID
= attrp
->ca_gid
;
2718 bsdp
->fileMode
= attrp
->ca_mode
;
2719 bsdp
->adminFlags
= attrp
->ca_flags
>> 16;
2720 bsdp
->ownerFlags
= attrp
->ca_flags
& 0x000000FF;
2726 * builddesc - build a cnode descriptor from an HFS+ key
2729 builddesc(const HFSPlusCatalogKey
*key
, cnid_t cnid
, u_long hint
, u_long encoding
,
2730 int isdir
, struct cat_desc
*descp
)
2738 /* guess a size... */
2739 bufsize
= (3 * key
->nodeName
.length
) + 1;
2740 if (bufsize
>= sizeof(tmpbuff
) - 1) {
2741 MALLOC(nameptr
, char *, bufsize
, M_TEMP
, M_WAITOK
);
2743 nameptr
= &tmpbuff
[0];
2746 result
= utf8_encodestr(key
->nodeName
.unicode
,
2747 key
->nodeName
.length
* sizeof(UniChar
),
2748 nameptr
, (size_t *)&utf8len
,
2751 if (result
== ENAMETOOLONG
) {
2752 bufsize
= 1 + utf8_encodelen(key
->nodeName
.unicode
,
2753 key
->nodeName
.length
* sizeof(UniChar
),
2755 FREE(nameptr
, M_TEMP
);
2756 MALLOC(nameptr
, char *, bufsize
, M_TEMP
, M_WAITOK
);
2758 result
= utf8_encodestr(key
->nodeName
.unicode
,
2759 key
->nodeName
.length
* sizeof(UniChar
),
2760 nameptr
, (size_t *)&utf8len
,
2763 descp
->cd_parentcnid
= key
->parentID
;
2764 descp
->cd_nameptr
= vfs_addname(nameptr
, utf8len
, 0, 0);
2765 descp
->cd_namelen
= utf8len
;
2766 descp
->cd_cnid
= cnid
;
2767 descp
->cd_hint
= hint
;
2768 descp
->cd_flags
= CD_DECOMPOSED
| CD_HASBUF
;
2770 descp
->cd_flags
|= CD_ISDIR
;
2771 descp
->cd_encoding
= encoding
;
2772 if (nameptr
!= &tmpbuff
[0]) {
2773 FREE(nameptr
, M_TEMP
);
2780 * getbsdattr - get attributes in bsd format
2784 getbsdattr(struct hfsmount
*hfsmp
, const struct HFSPlusCatalogFile
*crp
, struct cat_attr
* attrp
)
2786 int isDirectory
= (crp
->recordType
== kHFSPlusFolderRecord
);
2787 const struct HFSPlusBSDInfo
*bsd
= &crp
->bsdInfo
;
2789 attrp
->ca_recflags
= crp
->flags
;
2790 attrp
->ca_nlink
= 1;
2791 attrp
->ca_atime
= to_bsd_time(crp
->accessDate
);
2792 attrp
->ca_atimeondisk
= attrp
->ca_atime
;
2793 attrp
->ca_mtime
= to_bsd_time(crp
->contentModDate
);
2794 attrp
->ca_ctime
= to_bsd_time(crp
->attributeModDate
);
2795 attrp
->ca_itime
= to_bsd_time(crp
->createDate
);
2796 attrp
->ca_btime
= to_bsd_time(crp
->backupDate
);
2798 if ((bsd
->fileMode
& S_IFMT
) == 0) {
2799 attrp
->ca_flags
= 0;
2800 attrp
->ca_uid
= hfsmp
->hfs_uid
;
2801 attrp
->ca_gid
= hfsmp
->hfs_gid
;
2803 attrp
->ca_mode
= S_IFDIR
| (hfsmp
->hfs_dir_mask
& ACCESSPERMS
);
2805 attrp
->ca_mode
= S_IFREG
| (hfsmp
->hfs_file_mask
& ACCESSPERMS
);
2809 attrp
->ca_uid
= bsd
->ownerID
;
2810 attrp
->ca_gid
= bsd
->groupID
;
2811 attrp
->ca_flags
= bsd
->ownerFlags
| (bsd
->adminFlags
<< 16);
2812 attrp
->ca_mode
= (mode_t
)bsd
->fileMode
;
2813 switch (attrp
->ca_mode
& S_IFMT
) {
2814 case S_IFCHR
: /* fall through */
2816 attrp
->ca_rdev
= bsd
->special
.rawDevice
;
2819 /* Pick up the hard link count */
2820 if (bsd
->special
.linkCount
> 0)
2821 attrp
->ca_nlink
= bsd
->special
.linkCount
;
2825 if (((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
) {
2827 * Override the permissions as determined by the mount auguments
2828 * in ALMOST the same way unset permissions are treated but keep
2829 * track of whether or not the file or folder is hfs locked
2830 * by leaving the h_pflags field unchanged from what was unpacked
2831 * out of the catalog.
2833 attrp
->ca_uid
= hfsmp
->hfs_uid
;
2834 attrp
->ca_gid
= hfsmp
->hfs_gid
;
2839 if (!S_ISDIR(attrp
->ca_mode
)) {
2840 attrp
->ca_mode
&= ~S_IFMT
;
2841 attrp
->ca_mode
|= S_IFDIR
;
2843 attrp
->ca_nlink
= 2 + ((HFSPlusCatalogFolder
*)crp
)->valence
;
2844 attrp
->ca_entries
= ((HFSPlusCatalogFolder
*)crp
)->valence
;
2845 attrp
->ca_attrblks
= ((HFSPlusCatalogFolder
*)crp
)->attrBlocks
;
2847 /* Keep IMMUTABLE bits in sync with HFS locked flag */
2848 if (crp
->flags
& kHFSFileLockedMask
) {
2849 /* The file's supposed to be locked:
2850 Make sure at least one of the IMMUTABLE bits is set: */
2851 if ((attrp
->ca_flags
& (SF_IMMUTABLE
| UF_IMMUTABLE
)) == 0)
2852 attrp
->ca_flags
|= UF_IMMUTABLE
;
2854 /* The file's supposed to be unlocked: */
2855 attrp
->ca_flags
&= ~(SF_IMMUTABLE
| UF_IMMUTABLE
);
2857 /* get total blocks (both forks) */
2858 attrp
->ca_blocks
= crp
->dataFork
.totalBlocks
+ crp
->resourceFork
.totalBlocks
;
2859 attrp
->ca_attrblks
= crp
->attrBlocks
;
2860 /* On HFS+ the ThreadExists flag must always be set. */
2861 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) == 0)
2862 attrp
->ca_recflags
|= kHFSThreadExistsMask
;
2865 attrp
->ca_fileid
= crp
->fileID
;
2867 bcopy(&crp
->userInfo
, attrp
->ca_finderinfo
, 32);
2871 * promotekey - promote hfs key to hfs plus key
2875 promotekey(struct hfsmount
*hfsmp
, const HFSCatalogKey
*hfskey
,
2876 HFSPlusCatalogKey
*keyp
, u_long
*encoding
)
2878 hfs_to_unicode_func_t hfs_get_unicode
= hfsmp
->hfs_get_unicode
;
2882 *encoding
= hfsmp
->hfs_encoding
;
2884 error
= hfs_get_unicode(hfskey
->nodeName
, keyp
->nodeName
.unicode
,
2885 kHFSPlusMaxFileNameChars
, &uniCount
);
2887 * When an HFS name cannot be encoded with the current
2888 * encoding use MacRoman as a fallback.
2890 if (error
&& hfsmp
->hfs_encoding
!= kTextEncodingMacRoman
) {
2892 (void) mac_roman_to_unicode(hfskey
->nodeName
,
2893 keyp
->nodeName
.unicode
,
2894 kHFSPlusMaxFileNameChars
,
2898 keyp
->nodeName
.length
= uniCount
;
2899 keyp
->parentID
= hfskey
->parentID
;
2903 * promotefork - promote hfs fork info to hfs plus
2907 promotefork(struct hfsmount
*hfsmp
, const struct HFSCatalogFile
*filep
,
2908 int resource
, struct cat_fork
* forkp
)
2910 struct HFSPlusExtentDescriptor
*xp
;
2911 u_long blocksize
= HFSTOVCB(hfsmp
)->blockSize
;
2913 bzero(forkp
, sizeof(*forkp
));
2914 xp
= &forkp
->cf_extents
[0];
2916 forkp
->cf_size
= filep
->rsrcLogicalSize
;
2917 forkp
->cf_blocks
= filep
->rsrcPhysicalSize
/ blocksize
;
2918 forkp
->cf_bytesread
= 0;
2919 forkp
->cf_vblocks
= 0;
2920 xp
[0].startBlock
= (u_int32_t
)filep
->rsrcExtents
[0].startBlock
;
2921 xp
[0].blockCount
= (u_int32_t
)filep
->rsrcExtents
[0].blockCount
;
2922 xp
[1].startBlock
= (u_int32_t
)filep
->rsrcExtents
[1].startBlock
;
2923 xp
[1].blockCount
= (u_int32_t
)filep
->rsrcExtents
[1].blockCount
;
2924 xp
[2].startBlock
= (u_int32_t
)filep
->rsrcExtents
[2].startBlock
;
2925 xp
[2].blockCount
= (u_int32_t
)filep
->rsrcExtents
[2].blockCount
;
2927 forkp
->cf_size
= filep
->dataLogicalSize
;
2928 forkp
->cf_blocks
= filep
->dataPhysicalSize
/ blocksize
;
2929 forkp
->cf_bytesread
= 0;
2930 forkp
->cf_vblocks
= 0;
2931 xp
[0].startBlock
= (u_int32_t
)filep
->dataExtents
[0].startBlock
;
2932 xp
[0].blockCount
= (u_int32_t
)filep
->dataExtents
[0].blockCount
;
2933 xp
[1].startBlock
= (u_int32_t
)filep
->dataExtents
[1].startBlock
;
2934 xp
[1].blockCount
= (u_int32_t
)filep
->dataExtents
[1].blockCount
;
2935 xp
[2].startBlock
= (u_int32_t
)filep
->dataExtents
[2].startBlock
;
2936 xp
[2].blockCount
= (u_int32_t
)filep
->dataExtents
[2].blockCount
;
2941 * promoteattr - promote hfs catalog attributes to hfs plus
2945 promoteattr(struct hfsmount
*hfsmp
, const CatalogRecord
*dataPtr
, struct HFSPlusCatalogFile
*crp
)
2947 u_long blocksize
= HFSTOVCB(hfsmp
)->blockSize
;
2949 if (dataPtr
->recordType
== kHFSFolderRecord
) {
2950 struct HFSCatalogFolder
* folder
;
2952 folder
= (struct HFSCatalogFolder
*) dataPtr
;
2953 crp
->recordType
= kHFSPlusFolderRecord
;
2954 crp
->flags
= folder
->flags
;
2955 crp
->fileID
= folder
->folderID
;
2956 crp
->createDate
= LocalToUTC(folder
->createDate
);
2957 crp
->contentModDate
= LocalToUTC(folder
->modifyDate
);
2958 crp
->backupDate
= LocalToUTC(folder
->backupDate
);
2959 crp
->reserved1
= folder
->valence
;
2960 bcopy(&folder
->userInfo
, &crp
->userInfo
, 32);
2962 struct HFSCatalogFile
* file
;
2964 file
= (struct HFSCatalogFile
*) dataPtr
;
2965 crp
->recordType
= kHFSPlusFileRecord
;
2966 crp
->flags
= file
->flags
;
2967 crp
->fileID
= file
->fileID
;
2968 crp
->createDate
= LocalToUTC(file
->createDate
);
2969 crp
->contentModDate
= LocalToUTC(file
->modifyDate
);
2970 crp
->backupDate
= LocalToUTC(file
->backupDate
);
2972 bcopy(&file
->userInfo
, &crp
->userInfo
, 16);
2973 bcopy(&file
->finderInfo
, &crp
->finderInfo
, 16);
2974 crp
->dataFork
.totalBlocks
= file
->dataPhysicalSize
/ blocksize
;
2975 crp
->resourceFork
.totalBlocks
= file
->rsrcPhysicalSize
/ blocksize
;
2977 crp
->textEncoding
= 0;
2978 crp
->attributeModDate
= crp
->contentModDate
;
2979 crp
->accessDate
= crp
->contentModDate
;
2980 bzero(&crp
->bsdInfo
, sizeof(HFSPlusBSDInfo
));
2981 crp
->attrBlocks
= 0;
2985 * Build a catalog node thread record from a catalog key
2986 * and return the size of the record.
2989 buildthread(void *keyp
, void *recp
, int std_hfs
, int directory
)
2994 HFSCatalogKey
*key
= (HFSCatalogKey
*)keyp
;
2995 HFSCatalogThread
*rec
= (HFSCatalogThread
*)recp
;
2997 size
= sizeof(HFSCatalogThread
);
3000 rec
->recordType
= kHFSFolderThreadRecord
;
3002 rec
->recordType
= kHFSFileThreadRecord
;
3003 rec
->parentID
= key
->parentID
;
3004 bcopy(key
->nodeName
, rec
->nodeName
, key
->nodeName
[0]+1);
3007 HFSPlusCatalogKey
*key
= (HFSPlusCatalogKey
*)keyp
;
3008 HFSPlusCatalogThread
*rec
= (HFSPlusCatalogThread
*)recp
;
3010 size
= sizeof(HFSPlusCatalogThread
);
3012 rec
->recordType
= kHFSPlusFolderThreadRecord
;
3014 rec
->recordType
= kHFSPlusFileThreadRecord
;
3016 rec
->parentID
= key
->parentID
;
3017 bcopy(&key
->nodeName
, &rec
->nodeName
,
3018 sizeof(UniChar
) * (key
->nodeName
.length
+ 1));
3020 /* HFS Plus has varaible sized thread records */
3021 size
-= (sizeof(rec
->nodeName
.unicode
) -
3022 (rec
->nodeName
.length
* sizeof(UniChar
)));
3029 * Build a catalog node thread key.
3032 buildthreadkey(HFSCatalogNodeID parentID
, int std_hfs
, CatalogKey
*key
)
3035 key
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
;
3036 key
->hfs
.reserved
= 0;
3037 key
->hfs
.parentID
= parentID
;
3038 key
->hfs
.nodeName
[0] = 0;
3040 key
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
;
3041 key
->hfsPlus
.parentID
= parentID
;
3042 key
->hfsPlus
.nodeName
.length
= 0;
3047 * Extract the text encoding from a catalog node record.
3050 getencoding(const CatalogRecord
*crp
)
3054 if (crp
->recordType
== kHFSPlusFolderRecord
)
3055 encoding
= crp
->hfsPlusFolder
.textEncoding
;
3056 else if (crp
->recordType
== kHFSPlusFileRecord
)
3057 encoding
= crp
->hfsPlusFile
.textEncoding
;
3065 * Extract the CNID from a catalog node record.
3068 getcnid(const CatalogRecord
*crp
)
3072 switch (crp
->recordType
) {
3073 case kHFSFolderRecord
:
3074 cnid
= crp
->hfsFolder
.folderID
;
3076 case kHFSFileRecord
:
3077 cnid
= crp
->hfsFile
.fileID
;
3079 case kHFSPlusFolderRecord
:
3080 cnid
= crp
->hfsPlusFolder
.folderID
;
3082 case kHFSPlusFileRecord
:
3083 cnid
= crp
->hfsPlusFile
.fileID
;
3086 printf("hfs: getcnid: unknown recordType (crp @ 0x%x)\n", crp
);
3094 * Extract the parent ID from a catalog node record.
3097 getparentcnid(const CatalogRecord
*recp
)
3101 switch (recp
->recordType
) {
3102 case kHFSFileThreadRecord
:
3103 case kHFSFolderThreadRecord
:
3104 cnid
= recp
->hfsThread
.parentID
;
3107 case kHFSPlusFileThreadRecord
:
3108 case kHFSPlusFolderThreadRecord
:
3109 cnid
= recp
->hfsPlusThread
.parentID
;
3112 panic("hfs: getparentcnid: unknown recordType (crp @ 0x%x)\n", recp
);
3120 * Determine if a catalog node record is a directory.
3123 isadir(const CatalogRecord
*crp
)
3125 return (crp
->recordType
== kHFSFolderRecord
||
3126 crp
->recordType
== kHFSPlusFolderRecord
);