2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/systm.h>
24 #include <sys/kernel.h>
25 #include <sys/malloc.h>
27 #include <sys/mount.h>
28 #include <sys/vnode.h>
29 #include <sys/dirent.h>
30 #include <vfs/vfs_support.h>
31 #include <libkern/libkern.h>
33 #include <sys/utfconv.h>
36 #include "hfs_catalog.h"
37 #include "hfs_format.h"
38 #include "hfs_endian.h"
40 #include "hfscommon/headers/BTreesInternal.h"
41 #include "hfscommon/headers/HFSUnicodeWrappers.h"
45 * Initialization of an FSBufferDescriptor structure.
47 #define BDINIT(bd, addr) { \
48 (bd).bufferAddress = (addr); \
49 (bd).itemSize = sizeof(*(addr)); \
55 BTreeIterator iterator
;
56 HFSPlusCatalogKey key
;
61 struct cat_desc
* s_desc
;
62 struct cat_attr
* s_attr
;
63 struct cat_fork
* s_datafork
;
64 struct cat_fork
* s_rsrcfork
;
65 struct hfsmount
* s_hfsmp
;
68 struct position_state
{
73 struct hfsmount
*hfsmp
;
76 /* Map file mode type to directory entry types */
77 u_char modetodirtype
[16] = {
78 DT_REG
, DT_FIFO
, DT_CHR
, DT_UNKNOWN
,
79 DT_DIR
, DT_UNKNOWN
, DT_BLK
, DT_UNKNOWN
,
80 DT_REG
, DT_UNKNOWN
, DT_LNK
, DT_UNKNOWN
,
81 DT_SOCK
, DT_UNKNOWN
, DT_WHT
, DT_UNKNOWN
83 #define MODE_TO_DT(mode) (modetodirtype[((mode) & S_IFMT) >> 12])
86 static int cat_lookupbykey(struct hfsmount
*hfsmp
, CatalogKey
*keyp
, u_long hint
, int wantrsrc
,
87 struct cat_desc
*descp
, struct cat_attr
*attrp
, struct cat_fork
*forkp
, cnid_t
*desc_cnid
);
89 static int cat_lookupmangled(struct hfsmount
*hfsmp
, struct cat_desc
*descp
, int wantrsrc
,
90 struct cat_desc
*outdescp
, struct cat_attr
*attrp
, struct cat_fork
*forkp
);
92 extern int mac_roman_to_unicode(const Str31 hfs_str
, UniChar
*uni_str
,
93 UInt32 maxCharLen
, UInt32
*unicodeChars
);
95 extern int unicode_to_hfs(ExtendedVCB
*vcb
, ByteCount srcLen
,
96 const u_int16_t
* srcStr
, Str31 dstStr
, int retry
);
99 /* Internal catalog support routines */
101 static int cat_findposition(const CatalogKey
*ckp
, const CatalogRecord
*crp
,
102 struct position_state
*state
);
104 static int resolvelinkid(struct hfsmount
*hfsmp
, u_long linkref
, ino_t
*ino
);
106 static int getkey(struct hfsmount
*hfsmp
, cnid_t cnid
, CatalogKey
* key
);
108 static int buildkey(struct hfsmount
*hfsmp
, struct cat_desc
*descp
,
109 HFSPlusCatalogKey
*key
, int retry
);
111 static void buildthreadkey(HFSCatalogNodeID parentID
, int std_hfs
, CatalogKey
*key
);
113 static void buildrecord(struct cat_attr
*attrp
, cnid_t cnid
, int std_hfs
, u_int32_t encoding
, CatalogRecord
*crp
, int *recordSize
);
115 static int catrec_update(const CatalogKey
*ckp
, CatalogRecord
*crp
, struct update_state
*state
);
117 static int builddesc(const HFSPlusCatalogKey
*key
, cnid_t cnid
, u_long hint
, u_long encoding
,
118 int isdir
, struct cat_desc
*descp
);
120 static void getbsdattr(struct hfsmount
*hfsmp
, const struct HFSPlusCatalogFile
*crp
, struct cat_attr
* attrp
);
122 static void promotekey(struct hfsmount
*hfsmp
, const HFSCatalogKey
*hfskey
, HFSPlusCatalogKey
*keyp
, u_long
*encoding
);
123 static void promotefork(struct hfsmount
*hfsmp
, const struct HFSCatalogFile
*file
, int resource
, struct cat_fork
* forkp
);
124 static void promoteattr(struct hfsmount
*hfsmp
, const CatalogRecord
*dataPtr
, struct HFSPlusCatalogFile
*crp
);
126 static cnid_t
getcnid(const CatalogRecord
*crp
);
127 static u_long
getencoding(const CatalogRecord
*crp
);
128 static cnid_t
getparentcnid(const CatalogRecord
*recp
);
130 static int isadir(const CatalogRecord
*crp
);
132 static int buildthread(void *keyp
, void *recp
, int std_hfs
, int directory
);
137 cat_preflight(struct hfsmount
*hfsmp
, catops_t ops
, cat_cookie_t
*cookie
, struct proc
*p
)
143 fcb
= GetFileControlBlock(hfsmp
->hfs_catalog_vp
);
145 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
147 result
= BTReserveSpace(fcb
, ops
, (void*)cookie
);
149 hfs_systemfile_unlock(hfsmp
, lockflags
);
151 return MacToVFSError(result
);
156 cat_postflight(struct hfsmount
*hfsmp
, cat_cookie_t
*cookie
, struct proc
*p
)
161 fcb
= GetFileControlBlock(hfsmp
->hfs_catalog_vp
);
163 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
165 (void) BTReleaseReserve(fcb
, (void*)cookie
);
167 hfs_systemfile_unlock(hfsmp
, lockflags
);
174 struct hfsmount
*hfsmp
,
175 CatalogRecord
* recp
,
176 struct cat_attr
*attrp
,
177 struct cat_fork
*datafp
,
178 struct cat_fork
*rsrcfp
)
180 int std_hfs
= HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
;
183 struct HFSPlusCatalogFile cnoderec
;
185 promoteattr(hfsmp
, recp
, &cnoderec
);
186 getbsdattr(hfsmp
, &cnoderec
, attrp
);
188 getbsdattr(hfsmp
, (struct HFSPlusCatalogFile
*)recp
, attrp
);
192 bzero(datafp
, sizeof(*datafp
));
194 promotefork(hfsmp
, (HFSCatalogFile
*)&recp
->hfsFile
, 0, datafp
);
195 promotefork(hfsmp
, (HFSCatalogFile
*)&recp
->hfsFile
, 1, rsrcfp
);
197 /* Convert the data fork. */
198 datafp
->cf_size
= recp
->hfsPlusFile
.dataFork
.logicalSize
;
199 datafp
->cf_blocks
= recp
->hfsPlusFile
.dataFork
.totalBlocks
;
200 if ((hfsmp
->hfc_stage
== HFC_RECORDING
) &&
201 (attrp
->ca_atime
>= hfsmp
->hfc_timebase
)) {
202 datafp
->cf_bytesread
=
203 recp
->hfsPlusFile
.dataFork
.clumpSize
*
204 HFSTOVCB(hfsmp
)->blockSize
;
206 datafp
->cf_bytesread
= 0;
208 datafp
->cf_vblocks
= 0;
209 bcopy(&recp
->hfsPlusFile
.dataFork
.extents
[0],
210 &datafp
->cf_extents
[0], sizeof(HFSPlusExtentRecord
));
212 /* Convert the resource fork. */
213 rsrcfp
->cf_size
= recp
->hfsPlusFile
.resourceFork
.logicalSize
;
214 rsrcfp
->cf_blocks
= recp
->hfsPlusFile
.resourceFork
.totalBlocks
;
215 if ((hfsmp
->hfc_stage
== HFC_RECORDING
) &&
216 (attrp
->ca_atime
>= hfsmp
->hfc_timebase
)) {
217 datafp
->cf_bytesread
=
218 recp
->hfsPlusFile
.resourceFork
.clumpSize
*
219 HFSTOVCB(hfsmp
)->blockSize
;
221 datafp
->cf_bytesread
= 0;
223 rsrcfp
->cf_vblocks
= 0;
224 bcopy(&recp
->hfsPlusFile
.resourceFork
.extents
[0],
225 &rsrcfp
->cf_extents
[0], sizeof(HFSPlusExtentRecord
));
232 struct hfsmount
*hfsmp
,
234 CatalogRecord
* recp
,
235 struct cat_desc
*descp
)
237 int std_hfs
= HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
;
238 HFSPlusCatalogKey
* pluskey
= NULL
;
242 MALLOC(pluskey
, HFSPlusCatalogKey
*, sizeof(HFSPlusCatalogKey
), M_TEMP
, M_WAITOK
);
243 promotekey(hfsmp
, (HFSCatalogKey
*)key
, pluskey
, &encoding
);
246 pluskey
= (HFSPlusCatalogKey
*)key
;
247 encoding
= getencoding(recp
);
250 builddesc(pluskey
, getcnid(recp
), 0, encoding
, isadir(recp
), descp
);
252 FREE(pluskey
, M_TEMP
);
263 cat_releasedesc(struct cat_desc
*descp
)
270 if ((descp
->cd_flags
& CD_HASBUF
) &&
271 (descp
->cd_nameptr
!= NULL
)) {
272 name
= descp
->cd_nameptr
;
273 descp
->cd_nameptr
= NULL
;
274 descp
->cd_namelen
= 0;
275 descp
->cd_flags
&= ~CD_HASBUF
;
276 vfs_removename(name
);
278 descp
->cd_nameptr
= NULL
;
279 descp
->cd_namelen
= 0;
283 * These Catalog functions allow access to the HFS Catalog (database).
284 * The catalog b-tree lock must be aquired before calling any of these routines.
288 * cat_lookup - lookup a catalog node using a cnode decriptor
292 cat_lookup(struct hfsmount
*hfsmp
, struct cat_desc
*descp
, int wantrsrc
,
293 struct cat_desc
*outdescp
, struct cat_attr
*attrp
,
294 struct cat_fork
*forkp
, cnid_t
*desc_cnid
)
300 std_hfs
= (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
);
302 MALLOC(keyp
, CatalogKey
*, sizeof(CatalogKey
), M_TEMP
, M_WAITOK
);
304 result
= buildkey(hfsmp
, descp
, (HFSPlusCatalogKey
*)keyp
, 1);
308 result
= cat_lookupbykey(hfsmp
, keyp
, descp
->cd_hint
, wantrsrc
, outdescp
, attrp
, forkp
, desc_cnid
);
310 if (result
== ENOENT
) {
312 struct cat_desc temp_desc
;
313 if (outdescp
== NULL
) {
314 bzero(&temp_desc
, sizeof(temp_desc
));
315 outdescp
= &temp_desc
;
317 result
= cat_lookupmangled(hfsmp
, descp
, wantrsrc
, outdescp
, attrp
, forkp
);
319 *desc_cnid
= outdescp
->cd_cnid
;
321 if (outdescp
== &temp_desc
) {
322 /* Release the local copy of desc */
323 cat_releasedesc(outdescp
);
325 } else if (hfsmp
->hfs_encoding
!= kTextEncodingMacRoman
) {
326 // make MacRoman key from utf-8
327 // result = cat_lookupbykey(hfsmp, keyp, descp->cd_hint, attrp, forkp);
328 // update desc text encoding so that other catalog ops succeed
339 cat_insertfilethread(struct hfsmount
*hfsmp
, struct cat_desc
*descp
)
341 struct BTreeIterator
*iterator
;
342 struct FSBufferDescriptor file_data
;
343 struct HFSCatalogFile file_rec
;
348 if (HFSTOVCB(hfsmp
)->vcbSigWord
!= kHFSSigWord
)
351 fcb
= GetFileControlBlock(HFSTOVCB(hfsmp
)->catalogRefNum
);
353 MALLOC(iterator
, BTreeIterator
*, 2 * sizeof(*iterator
), M_TEMP
, M_WAITOK
);
354 bzero(&iterator
[0], 2* sizeof(*iterator
));
355 result
= buildkey(hfsmp
, descp
, (HFSPlusCatalogKey
*)&iterator
[0].key
, 0);
359 BDINIT(file_data
, &file_rec
);
360 result
= BTSearchRecord(fcb
, &iterator
[0], &file_data
, &datasize
, &iterator
[0]);
364 if (file_rec
.recordType
!= kHFSFileRecord
) {
369 if ((file_rec
.flags
& kHFSThreadExistsMask
) == 0) {
370 struct FSBufferDescriptor thread_data
;
371 struct HFSCatalogThread thread_rec
;
373 file_rec
.flags
|= kHFSThreadExistsMask
;
374 BDINIT(thread_data
, &thread_rec
);
375 thread_data
.itemSize
= buildthread(&iterator
[0].key
, &thread_rec
, 1, 0);
376 buildthreadkey(file_rec
.fileID
, 1, (CatalogKey
*)&iterator
[1].key
);
378 result
= BTInsertRecord(fcb
, &iterator
[1], &thread_data
, thread_data
.itemSize
);
382 (void) BTReplaceRecord(fcb
, &iterator
[0], &file_data
, datasize
);
383 (void) BTFlushPath(fcb
);
386 (void) BTFlushPath(fcb
);
387 FREE(iterator
, M_TEMP
);
389 return MacToVFSError(result
);
394 * cat_findname - obtain a descriptor from cnid
396 * Only a thread lookup is performed.
400 cat_findname(struct hfsmount
*hfsmp
, cnid_t cnid
, struct cat_desc
*outdescp
)
402 struct BTreeIterator
* iterator
;
403 FSBufferDescriptor btdata
;
405 CatalogRecord
* recp
;
411 std_hfs
= (hfsmp
->hfs_flags
& HFS_STANDARD
);
413 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
414 buildthreadkey(cnid
, std_hfs
, (CatalogKey
*)&iterator
->key
);
415 iterator
->hint
.nodeNum
= 0;
417 MALLOC(recp
, CatalogRecord
*, sizeof(CatalogRecord
), M_TEMP
, M_WAITOK
);
418 BDINIT(btdata
, recp
);
420 result
= BTSearchRecord(VTOF(hfsmp
->hfs_catalog_vp
), iterator
, &btdata
, NULL
, NULL
);
424 /* Turn thread record into a cnode key (in place). */
425 switch (recp
->recordType
) {
426 case kHFSFolderThreadRecord
:
429 case kHFSFileThreadRecord
:
430 keyp
= (CatalogKey
*)((char *)&recp
->hfsThread
.reserved
+ 6);
431 keyp
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
+ keyp
->hfs
.nodeName
[0];
434 case kHFSPlusFolderThreadRecord
:
437 case kHFSPlusFileThreadRecord
:
438 keyp
= (CatalogKey
*)&recp
->hfsPlusThread
.reserved
;
439 keyp
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
+
440 (keyp
->hfsPlus
.nodeName
.length
* 2);
447 HFSPlusCatalogKey
* pluskey
= NULL
;
450 MALLOC(pluskey
, HFSPlusCatalogKey
*, sizeof(HFSPlusCatalogKey
), M_TEMP
, M_WAITOK
);
451 promotekey(hfsmp
, &keyp
->hfs
, pluskey
, &encoding
);
452 builddesc(pluskey
, cnid
, 0, encoding
, isdir
, outdescp
);
453 FREE(pluskey
, M_TEMP
);
456 builddesc((HFSPlusCatalogKey
*)keyp
, cnid
, 0, 0, isdir
, outdescp
);
460 FREE(iterator
, M_TEMP
);
462 return MacToVFSError(result
);
466 * cat_idlookup - lookup a catalog node using a cnode id
470 cat_idlookup(struct hfsmount
*hfsmp
, cnid_t cnid
, struct cat_desc
*outdescp
,
471 struct cat_attr
*attrp
, struct cat_fork
*forkp
)
473 struct BTreeIterator
* iterator
;
474 FSBufferDescriptor btdata
;
477 CatalogRecord
* recp
;
481 std_hfs
= (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
);
483 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
484 bzero(iterator
, sizeof(*iterator
));
485 buildthreadkey(cnid
, std_hfs
, (CatalogKey
*)&iterator
->key
);
487 MALLOC(recp
, CatalogRecord
*, sizeof(CatalogRecord
), M_TEMP
, M_WAITOK
);
488 BDINIT(btdata
, recp
);
490 result
= BTSearchRecord(VTOF(HFSTOVCB(hfsmp
)->catalogRefNum
), iterator
,
491 &btdata
, &datasize
, iterator
);
495 /* Turn thread record into a cnode key (in place) */
496 switch (recp
->recordType
) {
497 case kHFSFileThreadRecord
:
498 case kHFSFolderThreadRecord
:
499 keyp
= (CatalogKey
*)((char *)&recp
->hfsThread
.reserved
+ 6);
500 keyp
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
+ keyp
->hfs
.nodeName
[0];
503 case kHFSPlusFileThreadRecord
:
504 case kHFSPlusFolderThreadRecord
:
505 keyp
= (CatalogKey
*)&recp
->hfsPlusThread
.reserved
;
506 keyp
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
+
507 (keyp
->hfsPlus
.nodeName
.length
* 2);
515 result
= cat_lookupbykey(hfsmp
, keyp
, 0, 0, outdescp
, attrp
, forkp
, NULL
);
518 FREE(iterator
, M_TEMP
);
520 return MacToVFSError(result
);
525 * cat_lookupmangled - lookup a catalog node using a mangled name
528 cat_lookupmangled(struct hfsmount
*hfsmp
, struct cat_desc
*descp
, int wantrsrc
,
529 struct cat_desc
*outdescp
, struct cat_attr
*attrp
, struct cat_fork
*forkp
)
538 fileID
= GetEmbeddedFileID(descp
->cd_nameptr
, descp
->cd_namelen
, &prefixlen
);
539 if (fileID
< kHFSFirstUserCatalogNodeID
)
542 result
= cat_idlookup(hfsmp
, fileID
, outdescp
, attrp
, forkp
);
546 /* It must be in the correct directory */
547 if (descp
->cd_parentcnid
!= outdescp
->cd_parentcnid
)
550 if ((outdescp
->cd_namelen
< prefixlen
) ||
551 bcmp(outdescp
->cd_nameptr
, descp
->cd_nameptr
, prefixlen
-6) != 0)
557 cat_releasedesc(outdescp
);
563 * cat_lookupbykey - lookup a catalog node using a cnode key
566 cat_lookupbykey(struct hfsmount
*hfsmp
, CatalogKey
*keyp
, u_long hint
, int wantrsrc
,
567 struct cat_desc
*descp
, struct cat_attr
*attrp
, struct cat_fork
*forkp
, cnid_t
*desc_cnid
)
569 struct BTreeIterator
* iterator
;
570 FSBufferDescriptor btdata
;
571 CatalogRecord
* recp
;
579 std_hfs
= (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
);
581 MALLOC(recp
, CatalogRecord
*, sizeof(CatalogRecord
), M_TEMP
, M_WAITOK
);
582 BDINIT(btdata
, recp
);
583 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
584 bzero(iterator
, sizeof(*iterator
));
585 iterator
->hint
.nodeNum
= hint
;
586 bcopy(keyp
, &iterator
->key
, sizeof(CatalogKey
));
588 result
= BTSearchRecord(VTOF(HFSTOVCB(hfsmp
)->catalogRefNum
), iterator
,
589 &btdata
, &datasize
, iterator
);
593 /* Save the cnid now in case there's a hard link */
594 cnid
= getcnid(recp
);
595 encoding
= getencoding(recp
);
596 hint
= iterator
->hint
.nodeNum
;
598 /* Hide the journal files (if any) */
600 ((cnid
== hfsmp
->hfs_jnlfileid
) ||
601 (cnid
== hfsmp
->hfs_jnlinfoblkid
))) {
608 * When a hardlink link is encountered, auto resolve it
612 && (recp
->recordType
== kHFSPlusFileRecord
)
613 && (SWAP_BE32(recp
->hfsPlusFile
.userInfo
.fdType
) == kHardLinkFileType
)
614 && (SWAP_BE32(recp
->hfsPlusFile
.userInfo
.fdCreator
) == kHFSPlusCreator
)
615 && ((to_bsd_time(recp
->hfsPlusFile
.createDate
) == (time_t)HFSTOVCB(hfsmp
)->vcbCrDate
) ||
616 (to_bsd_time(recp
->hfsPlusFile
.createDate
) == (time_t)hfsmp
->hfs_metadata_createdate
))) {
618 ilink
= recp
->hfsPlusFile
.bsdInfo
.special
.iNodeNum
;
620 (void) resolvelink(hfsmp
, ilink
, (struct HFSPlusCatalogFile
*)recp
);
625 struct HFSPlusCatalogFile cnoderec
;
627 promoteattr(hfsmp
, recp
, &cnoderec
);
628 getbsdattr(hfsmp
, &cnoderec
, attrp
);
630 getbsdattr(hfsmp
, (struct HFSPlusCatalogFile
*)recp
, attrp
);
632 attrp
->ca_rdev
= ilink
;
637 bzero(forkp
, sizeof(*forkp
));
638 } else if (std_hfs
) {
639 promotefork(hfsmp
, (HFSCatalogFile
*)&recp
->hfsFile
, wantrsrc
, forkp
);
640 } else if (wantrsrc
) {
641 /* Convert the resource fork. */
642 forkp
->cf_size
= recp
->hfsPlusFile
.resourceFork
.logicalSize
;
643 forkp
->cf_blocks
= recp
->hfsPlusFile
.resourceFork
.totalBlocks
;
644 if ((hfsmp
->hfc_stage
== HFC_RECORDING
) &&
645 (to_bsd_time(recp
->hfsPlusFile
.accessDate
) >= hfsmp
->hfc_timebase
)) {
646 forkp
->cf_bytesread
=
647 recp
->hfsPlusFile
.resourceFork
.clumpSize
*
648 HFSTOVCB(hfsmp
)->blockSize
;
650 forkp
->cf_bytesread
= 0;
652 forkp
->cf_vblocks
= 0;
653 bcopy(&recp
->hfsPlusFile
.resourceFork
.extents
[0],
654 &forkp
->cf_extents
[0], sizeof(HFSPlusExtentRecord
));
656 /* Convert the data fork. */
657 forkp
->cf_size
= recp
->hfsPlusFile
.dataFork
.logicalSize
;
658 forkp
->cf_blocks
= recp
->hfsPlusFile
.dataFork
.totalBlocks
;
659 if ((hfsmp
->hfc_stage
== HFC_RECORDING
) &&
660 (to_bsd_time(recp
->hfsPlusFile
.accessDate
) >= hfsmp
->hfc_timebase
)) {
661 forkp
->cf_bytesread
=
662 recp
->hfsPlusFile
.dataFork
.clumpSize
*
663 HFSTOVCB(hfsmp
)->blockSize
;
665 forkp
->cf_bytesread
= 0;
667 forkp
->cf_vblocks
= 0;
668 bcopy(&recp
->hfsPlusFile
.dataFork
.extents
[0],
669 &forkp
->cf_extents
[0], sizeof(HFSPlusExtentRecord
));
673 HFSPlusCatalogKey
* pluskey
= NULL
;
676 MALLOC(pluskey
, HFSPlusCatalogKey
*, sizeof(HFSPlusCatalogKey
), M_TEMP
, M_WAITOK
);
677 promotekey(hfsmp
, (HFSCatalogKey
*)&iterator
->key
, pluskey
, &encoding
);
680 pluskey
= (HFSPlusCatalogKey
*)&iterator
->key
;
682 builddesc(pluskey
, cnid
, hint
, encoding
, isadir(recp
), descp
);
684 FREE(pluskey
, M_TEMP
);
688 if (desc_cnid
!= NULL
) {
692 FREE(iterator
, M_TEMP
);
695 return MacToVFSError(result
);
700 * cat_create - create a node in the catalog
704 cat_create(struct hfsmount
*hfsmp
, struct cat_desc
*descp
, struct cat_attr
*attrp
,
705 struct cat_desc
*out_descp
)
710 FSBufferDescriptor btdata
;
719 modeformat
= attrp
->ca_mode
& S_IFMT
;
721 vcb
= HFSTOVCB(hfsmp
);
722 fcb
= GetFileControlBlock(vcb
->catalogRefNum
);
723 std_hfs
= (vcb
->vcbSigWord
== kHFSSigWord
);
726 * Atomically get the next CNID. If we have wrapped the CNIDs
727 * then keep the hfsmp lock held until we have found a CNID.
729 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
731 nextCNID
= hfsmp
->vcbNxtCNID
;
732 if (nextCNID
== 0xFFFFFFFF) {
736 hfsmp
->vcbNxtCNID
= kHFSFirstUserCatalogNodeID
;
737 hfsmp
->vcbAtrb
|= kHFSCatalogNodeIDsReusedMask
;
742 hfsmp
->vcbFlags
|= 0xFF00;
743 /* OK to drop lock if CNIDs are not wrapping */
744 if ((hfsmp
->vcbAtrb
& kHFSCatalogNodeIDsReusedMask
) == 0) {
745 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
748 return (result
); /* HFS only exit */
751 /* Get space for iterator, key and data */
752 MALLOC(bto
, struct btobj
*, sizeof(struct btobj
), M_TEMP
, M_WAITOK
);
753 bto
->iterator
.hint
.nodeNum
= 0;
755 result
= buildkey(hfsmp
, descp
, &bto
->key
, 0);
760 encoding
= hfs_pickencoding(bto
->key
.nodeName
.unicode
,
761 bto
->key
.nodeName
.length
);
762 hfs_setencodingbits(hfsmp
, encoding
);
766 * Insert the thread record first
768 if (!std_hfs
|| (modeformat
== S_IFDIR
)) {
769 datalen
= buildthread((void*)&bto
->key
, &bto
->data
, std_hfs
,
770 S_ISDIR(attrp
->ca_mode
));
771 btdata
.bufferAddress
= &bto
->data
;
772 btdata
.itemSize
= datalen
;
773 btdata
.itemCount
= 1;
776 buildthreadkey(nextCNID
, std_hfs
, (CatalogKey
*) &bto
->iterator
.key
);
778 result
= BTInsertRecord(fcb
, &bto
->iterator
, &btdata
, datalen
);
779 if ((result
== btExists
) && !std_hfs
&& mntlock
) {
781 * Allow CNIDs on HFS Plus volumes to wrap around
783 if (++nextCNID
< kHFSFirstUserCatalogNodeID
) {
784 nextCNID
= kHFSFirstUserCatalogNodeID
;
790 if (result
) goto exit
;
794 * CNID is now established. If we have wrapped then
795 * update the vcbNxtCNID and drop the vcb lock.
798 hfsmp
->vcbNxtCNID
= nextCNID
+ 1;
799 if (hfsmp
->vcbNxtCNID
< kHFSFirstUserCatalogNodeID
) {
800 hfsmp
->vcbNxtCNID
= kHFSFirstUserCatalogNodeID
;
802 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
807 * Now insert the file/directory record
809 buildrecord(attrp
, nextCNID
, std_hfs
, encoding
, &bto
->data
, &datalen
);
810 btdata
.bufferAddress
= &bto
->data
;
811 btdata
.itemSize
= datalen
;
812 btdata
.itemCount
= 1;
814 bcopy(&bto
->key
, &bto
->iterator
.key
, sizeof(bto
->key
));
816 result
= BTInsertRecord(fcb
, &bto
->iterator
, &btdata
, datalen
);
818 if (result
== btExists
)
821 /* Back out the thread record */
822 if (!std_hfs
|| S_ISDIR(attrp
->ca_mode
)) {
823 buildthreadkey(nextCNID
, std_hfs
, (CatalogKey
*)&bto
->iterator
.key
);
824 (void) BTDeleteRecord(fcb
, &bto
->iterator
);
830 * Insert was Successfull, update name, parent and volume
834 if (out_descp
!= NULL
) {
835 HFSPlusCatalogKey
* pluskey
= NULL
;
838 MALLOC(pluskey
, HFSPlusCatalogKey
*, sizeof(HFSPlusCatalogKey
), M_TEMP
, M_WAITOK
);
839 promotekey(hfsmp
, (HFSCatalogKey
*)&bto
->iterator
.key
, pluskey
, &encoding
);
842 pluskey
= (HFSPlusCatalogKey
*)&bto
->iterator
.key
;
844 builddesc(pluskey
, nextCNID
, bto
->iterator
.hint
.nodeNum
,
845 encoding
, S_ISDIR(attrp
->ca_mode
), out_descp
);
847 FREE(pluskey
, M_TEMP
);
850 attrp
->ca_fileid
= nextCNID
;
854 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
856 (void) BTFlushPath(fcb
);
859 return MacToVFSError(result
);
864 * cnode_rename - rename a catalog node
866 * Assumes that the target's directory exists.
868 * Order of B-tree operations:
869 * 1. BTSearchRecord(from_cnode, &data);
870 * 2. BTInsertRecord(to_cnode, &data);
871 * 3. BTDeleteRecord(from_cnode);
872 * 4. BTDeleteRecord(from_thread);
873 * 5. BTInsertRecord(to_thread);
878 struct hfsmount
* hfsmp
,
879 struct cat_desc
* from_cdp
,
880 struct cat_desc
* todir_cdp
,
881 struct cat_desc
* to_cdp
,
882 struct cat_desc
* out_cdp
)
884 struct BTreeIterator
* to_iterator
= NULL
;
885 struct BTreeIterator
* from_iterator
= NULL
;
886 FSBufferDescriptor btdata
;
887 CatalogRecord
* recp
= NULL
;
888 HFSPlusCatalogKey
* to_key
;
895 int directory
= from_cdp
->cd_flags
& CD_ISDIR
;
899 vcb
= HFSTOVCB(hfsmp
);
900 fcb
= GetFileControlBlock(vcb
->catalogRefNum
);
901 std_hfs
= (vcb
->vcbSigWord
== kHFSSigWord
);
903 if (from_cdp
->cd_namelen
== 0 || to_cdp
->cd_namelen
== 0)
906 MALLOC(from_iterator
, BTreeIterator
*, sizeof(*from_iterator
), M_TEMP
, M_WAITOK
);
907 bzero(from_iterator
, sizeof(*from_iterator
));
908 if ((result
= buildkey(hfsmp
, from_cdp
, (HFSPlusCatalogKey
*)&from_iterator
->key
, 0)))
911 MALLOC(to_iterator
, BTreeIterator
*, sizeof(*to_iterator
), M_TEMP
, M_WAITOK
);
912 bzero(to_iterator
, sizeof(*to_iterator
));
913 if ((result
= buildkey(hfsmp
, to_cdp
, (HFSPlusCatalogKey
*)&to_iterator
->key
, 0)))
916 to_key
= (HFSPlusCatalogKey
*)&to_iterator
->key
;
917 MALLOC(recp
, CatalogRecord
*, sizeof(CatalogRecord
), M_TEMP
, M_WAITOK
);
918 BDINIT(btdata
, recp
);
921 * When moving a directory, make sure its a valid move.
923 if (directory
&& (from_cdp
->cd_parentcnid
!= to_cdp
->cd_parentcnid
)) {
924 struct BTreeIterator iterator
;
925 cnid_t cnid
= from_cdp
->cd_cnid
;
926 cnid_t pathcnid
= todir_cdp
->cd_parentcnid
;
928 /* First check the obvious ones */
929 if (cnid
== fsRtDirID
||
930 cnid
== to_cdp
->cd_parentcnid
||
935 bzero(&iterator
, sizeof(iterator
));
937 * Traverese destination path all the way back to the root
938 * making sure that source directory is not encountered.
941 while (pathcnid
> fsRtDirID
) {
942 buildthreadkey(pathcnid
, std_hfs
,
943 (CatalogKey
*)&iterator
.key
);
944 result
= BTSearchRecord(fcb
, &iterator
, &btdata
,
946 if (result
) goto exit
;
948 pathcnid
= getparentcnid(recp
);
949 if (pathcnid
== cnid
) {
957 * Step 1: Find cnode data at old location
959 result
= BTSearchRecord(fcb
, from_iterator
, &btdata
,
960 &datasize
, from_iterator
);
962 if (std_hfs
|| (result
!= btNotFound
))
965 struct cat_desc temp_desc
;
967 /* Probably the node has mangled name */
968 result
= cat_lookupmangled(hfsmp
, from_cdp
, 0, &temp_desc
, NULL
, NULL
);
972 /* The file has mangled name. Search the cnode data using full name */
973 bzero(from_iterator
, sizeof(*from_iterator
));
974 result
= buildkey(hfsmp
, &temp_desc
, (HFSPlusCatalogKey
*)&from_iterator
->key
, 0);
976 cat_releasedesc(&temp_desc
);
980 result
= BTSearchRecord(fcb
, from_iterator
, &btdata
, &datasize
, from_iterator
);
982 cat_releasedesc(&temp_desc
);
986 cat_releasedesc(&temp_desc
);
989 /* Update the text encoding (on disk and in descriptor) */
991 encoding
= hfs_pickencoding(to_key
->nodeName
.unicode
,
992 to_key
->nodeName
.length
);
993 hfs_setencodingbits(hfsmp
, encoding
);
994 recp
->hfsPlusFile
.textEncoding
= encoding
;
996 out_cdp
->cd_encoding
= encoding
;
999 if (std_hfs
&& !directory
&&
1000 !(recp
->hfsFile
.flags
& kHFSThreadExistsMask
))
1004 * If the keys are identical then there's nothing left to do!
1006 * update the hint and exit
1009 if (std_hfs
&& hfskeycompare(to_key
, iter
->key
) == 0)
1011 if (!std_hfs
&& hfspluskeycompare(to_key
, iter
->key
) == 0)
1015 /* Step 2: Insert cnode at new location */
1016 result
= BTInsertRecord(fcb
, to_iterator
, &btdata
, datasize
);
1017 if (result
== btExists
) {
1018 int fromtype
= recp
->recordType
;
1020 if (from_cdp
->cd_parentcnid
!= to_cdp
->cd_parentcnid
)
1021 goto exit
; /* EEXIST */
1023 /* Find cnode data at new location */
1024 result
= BTSearchRecord(fcb
, to_iterator
, &btdata
, &datasize
, NULL
);
1026 if ((fromtype
!= recp
->recordType
) ||
1027 (from_cdp
->cd_cnid
!= getcnid(recp
)))
1028 goto exit
; /* EEXIST */
1030 /* The old name is a case variant and must be removed */
1031 result
= BTDeleteRecord(fcb
, from_iterator
);
1035 /* Insert cnode (now that case duplicate is gone) */
1036 result
= BTInsertRecord(fcb
, to_iterator
, &btdata
, datasize
);
1038 /* Try and restore original before leaving */
1043 err
= BTInsertRecord(fcb
, from_iterator
, &btdata
, datasize
);
1045 panic("cat_create: could not undo (BTInsert = %d)", err
);
1048 (void) BTInsertRecord(fcb
, from_iterator
, &btdata
, datasize
);
1057 /* Step 3: Remove cnode from old location */
1059 result
= BTDeleteRecord(fcb
, from_iterator
);
1061 /* Try and delete new record before leaving */
1066 err
= BTDeleteRecord(fcb
, to_iterator
);
1068 panic("cat_create: could not undo (BTDelete = %d)", err
);
1071 (void) BTDeleteRecord(fcb
, to_iterator
);
1077 /* #### POINT OF NO RETURN #### */
1080 * Step 4: Remove cnode's old thread record
1082 buildthreadkey(from_cdp
->cd_cnid
, std_hfs
, (CatalogKey
*)&from_iterator
->key
);
1083 (void) BTDeleteRecord(fcb
, from_iterator
);
1086 * Step 5: Insert cnode's new thread record
1087 * (optional for HFS files)
1090 datasize
= buildthread(&to_iterator
->key
, recp
, std_hfs
, directory
);
1091 btdata
.itemSize
= datasize
;
1092 buildthreadkey(from_cdp
->cd_cnid
, std_hfs
, (CatalogKey
*)&from_iterator
->key
);
1093 result
= BTInsertRecord(fcb
, from_iterator
, &btdata
, datasize
);
1097 HFSPlusCatalogKey
* pluskey
= NULL
;
1100 MALLOC(pluskey
, HFSPlusCatalogKey
*, sizeof(HFSPlusCatalogKey
), M_TEMP
, M_WAITOK
);
1101 promotekey(hfsmp
, (HFSCatalogKey
*)&to_iterator
->key
, pluskey
, &encoding
);
1103 /* Save the real encoding hint in the Finder Info (field 4). */
1104 if (directory
&& from_cdp
->cd_cnid
== kHFSRootFolderID
) {
1107 realhint
= hfs_pickencoding(pluskey
->nodeName
.unicode
, pluskey
->nodeName
.length
);
1108 vcb
->vcbFndrInfo
[4] = SET_HFS_TEXT_ENCODING(realhint
);
1112 pluskey
= (HFSPlusCatalogKey
*)&to_iterator
->key
;
1114 builddesc(pluskey
, from_cdp
->cd_cnid
, to_iterator
->hint
.nodeNum
,
1115 encoding
, directory
, out_cdp
);
1117 FREE(pluskey
, M_TEMP
);
1121 (void) BTFlushPath(fcb
);
1123 FREE(from_iterator
, M_TEMP
);
1125 FREE(to_iterator
, M_TEMP
);
1128 return MacToVFSError(result
);
1133 * cat_delete - delete a node from the catalog
1135 * Order of B-tree operations:
1136 * 1. BTDeleteRecord(cnode);
1137 * 2. BTDeleteRecord(thread);
1138 * 3. BTUpdateRecord(parent);
1142 cat_delete(struct hfsmount
*hfsmp
, struct cat_desc
*descp
, struct cat_attr
*attrp
)
1146 BTreeIterator
*iterator
;
1151 vcb
= HFSTOVCB(hfsmp
);
1152 fcb
= GetFileControlBlock(vcb
->catalogRefNum
);
1153 std_hfs
= (vcb
->vcbSigWord
== kHFSSigWord
);
1157 * The root directory cannot be deleted
1158 * A directory must be empty
1159 * A file must be zero length (no blocks)
1161 if (descp
->cd_cnid
< kHFSFirstUserCatalogNodeID
||
1162 descp
->cd_parentcnid
== kHFSRootParentID
)
1165 /* XXX Preflight Missing */
1167 /* Get space for iterator */
1168 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1169 iterator
->hint
.nodeNum
= 0;
1172 * Derive a key from either the file ID (for a virtual inode)
1173 * or the descriptor.
1175 if (descp
->cd_namelen
== 0) {
1176 result
= getkey(hfsmp
, attrp
->ca_fileid
, (CatalogKey
*)&iterator
->key
);
1177 cnid
= attrp
->ca_fileid
;
1179 result
= buildkey(hfsmp
, descp
, (HFSPlusCatalogKey
*)&iterator
->key
, 0);
1180 cnid
= descp
->cd_cnid
;
1186 result
= BTDeleteRecord(fcb
, iterator
);
1188 if (std_hfs
|| (result
!= btNotFound
))
1191 struct cat_desc temp_desc
;
1193 /* Probably the node has mangled name */
1194 result
= cat_lookupmangled(hfsmp
, descp
, 0, &temp_desc
, attrp
, NULL
);
1198 /* The file has mangled name. Delete the file using full name */
1199 bzero(iterator
, sizeof(*iterator
));
1200 result
= buildkey(hfsmp
, &temp_desc
, (HFSPlusCatalogKey
*)&iterator
->key
, 0);
1201 cnid
= temp_desc
.cd_cnid
;
1203 cat_releasedesc(&temp_desc
);
1207 result
= BTDeleteRecord(fcb
, iterator
);
1209 cat_releasedesc(&temp_desc
);
1213 cat_releasedesc(&temp_desc
);
1216 /* Delete thread record, ignore errors */
1217 buildthreadkey(cnid
, std_hfs
, (CatalogKey
*)&iterator
->key
);
1218 (void) BTDeleteRecord(fcb
, iterator
);
1221 (void) BTFlushPath(fcb
);
1222 FREE(iterator
, M_TEMP
);
1224 return MacToVFSError(result
);
1229 * cnode_update - update the catalog node described by descp
1230 * using the data from attrp and forkp.
1234 cat_update(struct hfsmount
*hfsmp
, struct cat_desc
*descp
, struct cat_attr
*attrp
,
1235 struct cat_fork
*dataforkp
, struct cat_fork
*rsrcforkp
)
1239 BTreeIterator
* iterator
;
1240 struct update_state state
;
1244 vcb
= HFSTOVCB(hfsmp
);
1245 fcb
= GetFileControlBlock(vcb
->catalogRefNum
);
1246 std_hfs
= (vcb
->vcbSigWord
== kHFSSigWord
);
1248 state
.s_desc
= descp
;
1249 state
.s_attr
= attrp
;
1250 state
.s_datafork
= dataforkp
;
1251 state
.s_rsrcfork
= rsrcforkp
;
1252 state
.s_hfsmp
= hfsmp
;
1254 /* Get space for iterator */
1255 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1258 * For open-deleted files we need to do a lookup by cnid
1259 * (using thread rec).
1261 * For hard links, the target of the update is the inode
1262 * itself (not the link record) so a lookup by fileid
1263 * (i.e. thread rec) is needed.
1265 if ((descp
->cd_cnid
!= attrp
->ca_fileid
) || (descp
->cd_namelen
== 0))
1266 result
= getkey(hfsmp
, attrp
->ca_fileid
, (CatalogKey
*)&iterator
->key
);
1268 result
= buildkey(hfsmp
, descp
, (HFSPlusCatalogKey
*)&iterator
->key
, 0);
1272 /* Pass a node hint */
1273 iterator
->hint
.nodeNum
= descp
->cd_hint
;
1275 result
= BTUpdateRecord(fcb
, iterator
,
1276 (IterateCallBackProcPtr
)catrec_update
, &state
);
1280 /* Update the node hint. */
1281 descp
->cd_hint
= iterator
->hint
.nodeNum
;
1284 (void) BTFlushPath(fcb
);
1285 FREE(iterator
, M_TEMP
);
1287 return MacToVFSError(result
);
1291 * catrec_update - Update the fields of a catalog record
1292 * This is called from within BTUpdateRecord.
1295 catrec_update(const CatalogKey
*ckp
, CatalogRecord
*crp
, struct update_state
*state
)
1297 struct cat_desc
*descp
;
1298 struct cat_attr
*attrp
;
1299 struct cat_fork
*forkp
;
1300 struct hfsmount
*hfsmp
;
1304 descp
= state
->s_desc
;
1305 attrp
= state
->s_attr
;
1306 hfsmp
= state
->s_hfsmp
;
1307 blksize
= HFSTOVCB(hfsmp
)->blockSize
;
1309 switch (crp
->recordType
) {
1310 case kHFSFolderRecord
: {
1311 HFSCatalogFolder
*dir
;
1313 dir
= (struct HFSCatalogFolder
*)crp
;
1314 /* Do a quick sanity check */
1315 if ((ckp
->hfs
.parentID
!= descp
->cd_parentcnid
) ||
1316 (dir
->folderID
!= descp
->cd_cnid
))
1317 return (btNotFound
);
1318 dir
->valence
= attrp
->ca_entries
;
1319 dir
->createDate
= UTCToLocal(to_hfs_time(attrp
->ca_itime
));
1320 dir
->modifyDate
= UTCToLocal(to_hfs_time(attrp
->ca_mtime
));
1321 dir
->backupDate
= UTCToLocal(to_hfs_time(attrp
->ca_btime
));
1322 bcopy(&attrp
->ca_finderinfo
[0], &dir
->userInfo
, 16);
1323 bcopy(&attrp
->ca_finderinfo
[16], &dir
->finderInfo
, 16);
1326 case kHFSFileRecord
: {
1327 HFSCatalogFile
*file
;
1329 file
= (struct HFSCatalogFile
*)crp
;
1330 /* Do a quick sanity check */
1331 if ((ckp
->hfs
.parentID
!= descp
->cd_parentcnid
) ||
1332 (file
->fileID
!= attrp
->ca_fileid
))
1333 return (btNotFound
);
1334 file
->createDate
= UTCToLocal(to_hfs_time(attrp
->ca_itime
));
1335 file
->modifyDate
= UTCToLocal(to_hfs_time(attrp
->ca_mtime
));
1336 file
->backupDate
= UTCToLocal(to_hfs_time(attrp
->ca_btime
));
1337 bcopy(&attrp
->ca_finderinfo
[0], &file
->userInfo
, 16);
1338 bcopy(&attrp
->ca_finderinfo
[16], &file
->finderInfo
, 16);
1339 if (state
->s_rsrcfork
) {
1340 forkp
= state
->s_rsrcfork
;
1341 file
->rsrcLogicalSize
= forkp
->cf_size
;
1342 file
->rsrcPhysicalSize
= forkp
->cf_blocks
* blksize
;
1343 for (i
= 0; i
< kHFSExtentDensity
; ++i
) {
1344 file
->rsrcExtents
[i
].startBlock
=
1345 (u_int16_t
)forkp
->cf_extents
[i
].startBlock
;
1346 file
->rsrcExtents
[i
].blockCount
=
1347 (u_int16_t
)forkp
->cf_extents
[i
].blockCount
;
1350 if (state
->s_datafork
) {
1351 forkp
= state
->s_datafork
;
1352 file
->dataLogicalSize
= forkp
->cf_size
;
1353 file
->dataPhysicalSize
= forkp
->cf_blocks
* blksize
;
1354 for (i
= 0; i
< kHFSExtentDensity
; ++i
) {
1355 file
->dataExtents
[i
].startBlock
=
1356 (u_int16_t
)forkp
->cf_extents
[i
].startBlock
;
1357 file
->dataExtents
[i
].blockCount
=
1358 (u_int16_t
)forkp
->cf_extents
[i
].blockCount
;
1363 case kHFSPlusFolderRecord
: {
1364 HFSPlusCatalogFolder
*dir
;
1366 dir
= (struct HFSPlusCatalogFolder
*)crp
;
1367 /* Do a quick sanity check */
1368 if ((ckp
->hfsPlus
.parentID
!= descp
->cd_parentcnid
) ||
1369 (dir
->folderID
!= descp
->cd_cnid
))
1370 return (btNotFound
);
1371 dir
->flags
= attrp
->ca_recflags
;
1372 dir
->valence
= attrp
->ca_entries
;
1373 dir
->createDate
= to_hfs_time(attrp
->ca_itime
);
1374 dir
->contentModDate
= to_hfs_time(attrp
->ca_mtime
);
1375 dir
->backupDate
= to_hfs_time(attrp
->ca_btime
);
1376 dir
->accessDate
= to_hfs_time(attrp
->ca_atime
);
1377 attrp
->ca_atimeondisk
= attrp
->ca_atime
;
1378 dir
->attributeModDate
= to_hfs_time(attrp
->ca_ctime
);
1379 dir
->textEncoding
= descp
->cd_encoding
;
1380 dir
->attrBlocks
= attrp
->ca_attrblks
;
1381 bcopy(&attrp
->ca_finderinfo
[0], &dir
->userInfo
, 32);
1383 * Update the BSD Info if it was already initialized on
1384 * disk or if the runtime values have been modified.
1386 * If the BSD info was already initialized, but
1387 * MNT_UNKNOWNPERMISSIONS is set, then the runtime IDs are
1388 * probably different than what was on disk. We don't want
1389 * to overwrite the on-disk values (so if we turn off
1390 * MNT_UNKNOWNPERMISSIONS, the old IDs get used again).
1391 * This way, we can still change fields like the mode or
1392 * dates even when MNT_UNKNOWNPERMISSIONS is set.
1394 * Note that if MNT_UNKNOWNPERMISSIONS is set, hfs_chown
1395 * won't change the uid or gid from their defaults. So, if
1396 * the BSD info wasn't set, and the runtime values are not
1397 * default, then what changed was the mode or flags. We
1398 * have to set the uid and gid to something, so use the
1399 * supplied values (which will be default), which has the
1400 * same effect as creating a new file while
1401 * MNT_UNKNOWNPERMISSIONS is set.
1403 if ((dir
->bsdInfo
.fileMode
!= 0) ||
1404 (attrp
->ca_flags
!= 0) ||
1405 (attrp
->ca_uid
!= hfsmp
->hfs_uid
) ||
1406 (attrp
->ca_gid
!= hfsmp
->hfs_gid
) ||
1407 ((attrp
->ca_mode
& ALLPERMS
) !=
1408 (hfsmp
->hfs_dir_mask
& ACCESSPERMS
))) {
1409 if ((dir
->bsdInfo
.fileMode
== 0) ||
1410 (((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
) == 0) {
1411 dir
->bsdInfo
.ownerID
= attrp
->ca_uid
;
1412 dir
->bsdInfo
.groupID
= attrp
->ca_gid
;
1414 dir
->bsdInfo
.ownerFlags
= attrp
->ca_flags
& 0x000000FF;
1415 dir
->bsdInfo
.adminFlags
= attrp
->ca_flags
>> 16;
1416 dir
->bsdInfo
.fileMode
= attrp
->ca_mode
;
1420 case kHFSPlusFileRecord
: {
1421 HFSPlusCatalogFile
*file
;
1423 file
= (struct HFSPlusCatalogFile
*)crp
;
1424 /* Do a quick sanity check */
1425 if (file
->fileID
!= attrp
->ca_fileid
)
1426 return (btNotFound
);
1427 file
->flags
= attrp
->ca_recflags
;
1428 file
->createDate
= to_hfs_time(attrp
->ca_itime
);
1429 file
->contentModDate
= to_hfs_time(attrp
->ca_mtime
);
1430 file
->backupDate
= to_hfs_time(attrp
->ca_btime
);
1431 file
->accessDate
= to_hfs_time(attrp
->ca_atime
);
1432 attrp
->ca_atimeondisk
= attrp
->ca_atime
;
1433 file
->attributeModDate
= to_hfs_time(attrp
->ca_ctime
);
1434 file
->textEncoding
= descp
->cd_encoding
;
1435 file
->attrBlocks
= attrp
->ca_attrblks
;
1436 bcopy(&attrp
->ca_finderinfo
[0], &file
->userInfo
, 32);
1438 * Update the BSD Info if it was already initialized on
1439 * disk or if the runtime values have been modified.
1441 * If the BSD info was already initialized, but
1442 * MNT_UNKNOWNPERMISSIONS is set, then the runtime IDs are
1443 * probably different than what was on disk. We don't want
1444 * to overwrite the on-disk values (so if we turn off
1445 * MNT_UNKNOWNPERMISSIONS, the old IDs get used again).
1446 * This way, we can still change fields like the mode or
1447 * dates even when MNT_UNKNOWNPERMISSIONS is set.
1449 * Note that if MNT_UNKNOWNPERMISSIONS is set, hfs_chown
1450 * won't change the uid or gid from their defaults. So, if
1451 * the BSD info wasn't set, and the runtime values are not
1452 * default, then what changed was the mode or flags. We
1453 * have to set the uid and gid to something, so use the
1454 * supplied values (which will be default), which has the
1455 * same effect as creating a new file while
1456 * MNT_UNKNOWNPERMISSIONS is set.
1458 if ((file
->bsdInfo
.fileMode
!= 0) ||
1459 (attrp
->ca_flags
!= 0) ||
1460 (attrp
->ca_uid
!= hfsmp
->hfs_uid
) ||
1461 (attrp
->ca_gid
!= hfsmp
->hfs_gid
) ||
1462 ((attrp
->ca_mode
& ALLPERMS
) !=
1463 (hfsmp
->hfs_file_mask
& ACCESSPERMS
))) {
1464 if ((file
->bsdInfo
.fileMode
== 0) ||
1465 (((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
) == 0) {
1466 file
->bsdInfo
.ownerID
= attrp
->ca_uid
;
1467 file
->bsdInfo
.groupID
= attrp
->ca_gid
;
1469 file
->bsdInfo
.ownerFlags
= attrp
->ca_flags
& 0x000000FF;
1470 file
->bsdInfo
.adminFlags
= attrp
->ca_flags
>> 16;
1471 file
->bsdInfo
.fileMode
= attrp
->ca_mode
;
1473 if (state
->s_rsrcfork
) {
1474 forkp
= state
->s_rsrcfork
;
1475 file
->resourceFork
.logicalSize
= forkp
->cf_size
;
1476 file
->resourceFork
.totalBlocks
= forkp
->cf_blocks
;
1477 bcopy(&forkp
->cf_extents
[0], &file
->resourceFork
.extents
,
1478 sizeof(HFSPlusExtentRecord
));
1479 /* Push blocks read to disk */
1480 file
->resourceFork
.clumpSize
=
1481 howmany(forkp
->cf_bytesread
, blksize
);
1483 if (state
->s_datafork
) {
1484 forkp
= state
->s_datafork
;
1485 file
->dataFork
.logicalSize
= forkp
->cf_size
;
1486 file
->dataFork
.totalBlocks
= forkp
->cf_blocks
;
1487 bcopy(&forkp
->cf_extents
[0], &file
->dataFork
.extents
,
1488 sizeof(HFSPlusExtentRecord
));
1489 /* Push blocks read to disk */
1490 file
->dataFork
.clumpSize
=
1491 howmany(forkp
->cf_bytesread
, blksize
);
1494 if ((file
->resourceFork
.extents
[0].startBlock
!= 0) &&
1495 (file
->resourceFork
.extents
[0].startBlock
==
1496 file
->dataFork
.extents
[0].startBlock
))
1497 panic("catrec_update: rsrc fork == data fork");
1499 /* Synchronize the lock state */
1500 if (attrp
->ca_flags
& (SF_IMMUTABLE
| UF_IMMUTABLE
))
1501 file
->flags
|= kHFSFileLockedMask
;
1503 file
->flags
&= ~kHFSFileLockedMask
;
1505 /* Push out special field if necessary */
1506 if (S_ISBLK(attrp
->ca_mode
) || S_ISCHR(attrp
->ca_mode
))
1507 file
->bsdInfo
.special
.rawDevice
= attrp
->ca_rdev
;
1508 else if (descp
->cd_cnid
!= attrp
->ca_fileid
1509 || attrp
->ca_nlink
== 2)
1510 file
->bsdInfo
.special
.linkCount
= attrp
->ca_nlink
;
1514 return (btNotFound
);
1520 * Callback to collect directory entries.
1521 * Called with readattr_state for each item in a directory.
1523 struct readattr_state
{
1524 struct hfsmount
*hfsmp
;
1525 struct cat_entrylist
*list
;
1532 cat_readattr(const CatalogKey
*key
, const CatalogRecord
*rec
,
1533 struct readattr_state
*state
)
1535 struct cat_entrylist
*list
= state
->list
;
1536 struct hfsmount
*hfsmp
= state
->hfsmp
;
1537 struct cat_entry
*cep
;
1540 if (list
->realentries
>= list
->maxentries
)
1541 return (0); /* stop */
1543 parentcnid
= state
->stdhfs
? key
->hfs
.parentID
: key
->hfsPlus
.parentID
;
1545 switch(rec
->recordType
) {
1546 case kHFSPlusFolderRecord
:
1547 case kHFSPlusFileRecord
:
1548 case kHFSFolderRecord
:
1549 case kHFSFileRecord
:
1550 if (parentcnid
!= state
->dir_cnid
) {
1551 state
->error
= ENOENT
;
1552 return (0); /* stop */
1556 state
->error
= ENOENT
;
1557 return (0); /* stop */
1560 /* Hide the private meta data directory and journal files */
1561 if (parentcnid
== kHFSRootFolderID
) {
1562 if ((rec
->recordType
== kHFSPlusFolderRecord
) &&
1563 (rec
->hfsPlusFolder
.folderID
== hfsmp
->hfs_privdir_desc
.cd_cnid
)) {
1564 return (1); /* continue */
1567 (rec
->recordType
== kHFSPlusFileRecord
) &&
1568 ((rec
->hfsPlusFile
.fileID
== hfsmp
->hfs_jnlfileid
) ||
1569 (rec
->hfsPlusFile
.fileID
== hfsmp
->hfs_jnlinfoblkid
))) {
1571 return (1); /* continue */
1575 cep
= &list
->entry
[list
->realentries
++];
1577 if (state
->stdhfs
) {
1578 struct HFSPlusCatalogFile cnoderec
;
1579 HFSPlusCatalogKey
* pluskey
;
1582 promoteattr(hfsmp
, rec
, &cnoderec
);
1583 getbsdattr(hfsmp
, &cnoderec
, &cep
->ce_attr
);
1585 MALLOC(pluskey
, HFSPlusCatalogKey
*, sizeof(HFSPlusCatalogKey
), M_TEMP
, M_WAITOK
);
1586 promotekey(hfsmp
, (HFSCatalogKey
*)key
, pluskey
, &encoding
);
1587 builddesc(pluskey
, getcnid(rec
), 0, encoding
, isadir(rec
), &cep
->ce_desc
);
1588 FREE(pluskey
, M_TEMP
);
1590 if (rec
->recordType
== kHFSFileRecord
) {
1591 int blksize
= HFSTOVCB(hfsmp
)->blockSize
;
1593 cep
->ce_datasize
= rec
->hfsFile
.dataLogicalSize
;
1594 cep
->ce_datablks
= rec
->hfsFile
.dataPhysicalSize
/ blksize
;
1595 cep
->ce_rsrcsize
= rec
->hfsFile
.rsrcLogicalSize
;
1596 cep
->ce_rsrcblks
= rec
->hfsFile
.rsrcPhysicalSize
/ blksize
;
1599 getbsdattr(hfsmp
, (struct HFSPlusCatalogFile
*)rec
, &cep
->ce_attr
);
1600 builddesc((HFSPlusCatalogKey
*)key
, getcnid(rec
), 0, getencoding(rec
),
1601 isadir(rec
), &cep
->ce_desc
);
1603 if (rec
->recordType
== kHFSPlusFileRecord
) {
1604 cep
->ce_datasize
= rec
->hfsPlusFile
.dataFork
.logicalSize
;
1605 cep
->ce_datablks
= rec
->hfsPlusFile
.dataFork
.totalBlocks
;
1606 cep
->ce_rsrcsize
= rec
->hfsPlusFile
.resourceFork
.logicalSize
;
1607 cep
->ce_rsrcblks
= rec
->hfsPlusFile
.resourceFork
.totalBlocks
;
1609 /* Save link reference for later processing. */
1610 if ((SWAP_BE32(rec
->hfsPlusFile
.userInfo
.fdType
) == kHardLinkFileType
)
1611 && (SWAP_BE32(rec
->hfsPlusFile
.userInfo
.fdCreator
) == kHFSPlusCreator
))
1612 cep
->ce_attr
.ca_rdev
= rec
->hfsPlusFile
.bsdInfo
.special
.iNodeNum
;
1616 return (list
->realentries
< list
->maxentries
);
1620 * Pack a cat_entrylist buffer with attributes from the catalog
1622 * Note: index is zero relative
1626 cat_getentriesattr(struct hfsmount
*hfsmp
, directoryhint_t
*dirhint
, struct cat_entrylist
*ce_list
)
1630 BTreeIterator
* iterator
;
1631 struct readattr_state state
;
1639 ce_list
->realentries
= 0;
1641 fcb
= GetFileControlBlock(HFSTOVCB(hfsmp
)->catalogRefNum
);
1642 std_hfs
= (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
);
1643 parentcnid
= dirhint
->dh_desc
.cd_parentcnid
;
1645 state
.hfsmp
= hfsmp
;
1646 state
.list
= ce_list
;
1647 state
.dir_cnid
= parentcnid
;
1648 state
.stdhfs
= std_hfs
;
1651 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1652 bzero(iterator
, sizeof(*iterator
));
1653 key
= (CatalogKey
*)&iterator
->key
;
1655 iterator
->hint
.nodeNum
= dirhint
->dh_desc
.cd_hint
;
1656 index
= dirhint
->dh_index
+ 1;
1659 * Attempt to build a key from cached filename
1661 if (dirhint
->dh_desc
.cd_namelen
!= 0) {
1662 if (buildkey(hfsmp
, &dirhint
->dh_desc
, (HFSPlusCatalogKey
*)key
, 0) == 0) {
1668 * If the last entry wasn't cached then position the btree iterator
1670 if ((index
== 0) || !have_key
) {
1672 * Position the iterator at the directory's thread record.
1673 * (i.e. just before the first entry)
1675 buildthreadkey(dirhint
->dh_desc
.cd_parentcnid
, (hfsmp
->hfs_flags
& HFS_STANDARD
), key
);
1676 result
= BTSearchRecord(fcb
, iterator
, NULL
, NULL
, iterator
);
1678 result
= MacToVFSError(result
);
1683 * Iterate until we reach the entry just
1684 * before the one we want to start with.
1687 struct position_state ps
;
1692 ps
.parentID
= dirhint
->dh_desc
.cd_parentcnid
;
1695 result
= BTIterateRecords(fcb
, kBTreeNextRecord
, iterator
,
1696 (IterateCallBackProcPtr
)cat_findposition
, &ps
);
1700 result
= MacToVFSError(result
);
1702 result
= MacToVFSError(result
);
1708 /* Fill list with entries starting at iterator->key. */
1709 result
= BTIterateRecords(fcb
, kBTreeNextRecord
, iterator
,
1710 (IterateCallBackProcPtr
)cat_readattr
, &state
);
1713 result
= state
.error
;
1714 else if (ce_list
->realentries
== 0)
1717 result
= MacToVFSError(result
);
1723 * Resolve any hard links.
1725 for (i
= 0; i
< (int)ce_list
->realentries
; ++i
) {
1726 struct FndrFileInfo
*fip
;
1727 struct cat_entry
*cep
;
1728 struct HFSPlusCatalogFile filerec
;
1730 cep
= &ce_list
->entry
[i
];
1731 if (!S_ISREG(cep
->ce_attr
.ca_mode
))
1734 /* Note: Finder info is still in Big Endian */
1735 fip
= (struct FndrFileInfo
*)&cep
->ce_attr
.ca_finderinfo
;
1737 /* Check for hard link signature. */
1738 if ((cep
->ce_attr
.ca_rdev
!= 0)
1739 && (SWAP_BE32(fip
->fdType
) == kHardLinkFileType
)
1740 && (SWAP_BE32(fip
->fdCreator
) == kHFSPlusCreator
)
1741 && ((cep
->ce_attr
.ca_itime
== (time_t)HFSTOVCB(hfsmp
)->vcbCrDate
) ||
1742 (cep
->ce_attr
.ca_itime
== (time_t)hfsmp
->hfs_metadata_createdate
))) {
1744 if (resolvelink(hfsmp
, cep
->ce_attr
.ca_rdev
, &filerec
) != 0)
1746 /* Repack entry from inode record. */
1747 getbsdattr(hfsmp
, &filerec
, &cep
->ce_attr
);
1748 cep
->ce_datasize
= filerec
.dataFork
.logicalSize
;
1749 cep
->ce_datablks
= filerec
.dataFork
.totalBlocks
;
1750 cep
->ce_rsrcsize
= filerec
.resourceFork
.logicalSize
;
1751 cep
->ce_rsrcblks
= filerec
.resourceFork
.totalBlocks
;
1755 FREE(iterator
, M_TEMP
);
1757 return MacToVFSError(result
);
1760 #define SMALL_DIRENTRY_SIZE (int)(sizeof(struct dirent) - (MAXNAMLEN + 1) + 8)
1763 * Callback to pack directory entries.
1764 * Called with packdirentry_state for each item in a directory.
1767 /* Hard link information collected during cat_getdirentries. */
1770 user_addr_t dirent_addr
;
1772 typedef struct linkinfo linkinfo_t
;
1774 /* State information for the cat_packdirentry callback function. */
1775 struct packdirentry_state
{
1777 u_int32_t cbs_parentID
;
1778 u_int32_t cbs_index
;
1780 ExtendedVCB
* cbs_hfsmp
;
1783 int32_t cbs_maxlinks
;
1784 linkinfo_t
* cbs_linkinfo
;
1785 struct cat_desc
* cbs_desc
;
1786 // struct dirent * cbs_stdentry;
1787 // followign fields are only used for NFS readdir, which uses the next file id as the seek offset of each entry
1788 struct direntry
* cbs_direntry
;
1789 struct direntry
* cbs_prevdirentry
;
1790 u_int32_t cbs_previlinkref
;
1791 Boolean cbs_hasprevdirentry
;
1796 cat_packdirentry(const CatalogKey
*ckp
, const CatalogRecord
*crp
,
1797 struct packdirentry_state
*state
)
1799 struct hfsmount
*hfsmp
;
1803 struct dirent catent
;
1804 struct direntry
* entry
= NULL
;
1806 u_int32_t ilinkref
= 0;
1807 u_int32_t curlinkref
= 0;
1811 u_int8_t is_mangled
= 0;
1813 user_addr_t uiobase
;
1818 Boolean stop_after_pack
= false;
1820 hfsmp
= state
->cbs_hfsmp
;
1822 if (hfsmp
->hfs_flags
& HFS_STANDARD
)
1823 curID
= ckp
->hfs
.parentID
;
1825 curID
= ckp
->hfsPlus
.parentID
;
1827 /* We're done when parent directory changes */
1828 if (state
->cbs_parentID
!= curID
) {
1829 if (state
->cbs_extended
) {
1830 if (state
->cbs_hasprevdirentry
) { /* the last record haven't been returned yet, so we want to stop after
1831 * packing the last item */
1832 stop_after_pack
= true;
1834 state
->cbs_result
= ENOENT
;
1835 return (0); /* stop */
1838 state
->cbs_result
= ENOENT
;
1839 return (0); /* stop */
1843 if (state
->cbs_extended
) {
1844 entry
= state
->cbs_direntry
;
1845 nameptr
= &entry
->d_name
[0];
1846 maxnamelen
= NAME_MAX
;
1848 nameptr
= &catent
.d_name
[0];
1849 maxnamelen
= NAME_MAX
;
1852 if (state
->cbs_extended
&& stop_after_pack
) {
1853 cnid
= INT_MAX
; /* the last item returns a non-zero invalid cookie */
1855 if (!(hfsmp
->hfs_flags
& HFS_STANDARD
)) {
1856 switch(crp
->recordType
) {
1857 case kHFSPlusFolderRecord
:
1859 cnid
= crp
->hfsPlusFolder
.folderID
;
1860 /* Hide our private meta data directory */
1861 if ((curID
== kHFSRootFolderID
) &&
1862 (cnid
== hfsmp
->hfs_privdir_desc
.cd_cnid
)) {
1867 case kHFSPlusFileRecord
:
1868 itime
= to_bsd_time(crp
->hfsPlusFile
.createDate
);
1870 * When a hardlink link is encountered save its link ref.
1872 if ((SWAP_BE32(crp
->hfsPlusFile
.userInfo
.fdType
) == kHardLinkFileType
) &&
1873 (SWAP_BE32(crp
->hfsPlusFile
.userInfo
.fdCreator
) == kHFSPlusCreator
) &&
1874 ((itime
== (time_t)hfsmp
->hfs_itime
) ||
1875 (itime
== (time_t)hfsmp
->hfs_metadata_createdate
))) {
1876 ilinkref
= crp
->hfsPlusFile
.bsdInfo
.special
.iNodeNum
;
1878 type
= MODE_TO_DT(crp
->hfsPlusFile
.bsdInfo
.fileMode
);
1879 cnid
= crp
->hfsPlusFile
.fileID
;
1880 /* Hide the journal files */
1881 if ((curID
== kHFSRootFolderID
) &&
1883 ((cnid
== hfsmp
->hfs_jnlfileid
) ||
1884 (cnid
== hfsmp
->hfs_jnlinfoblkid
))) {
1889 return (0); /* stop */
1892 cnp
= (CatalogName
*) &ckp
->hfsPlus
.nodeName
;
1893 result
= utf8_encodestr(cnp
->ustr
.unicode
, cnp
->ustr
.length
* sizeof(UniChar
),
1894 nameptr
, &namelen
, maxnamelen
+ 1, ':', 0);
1895 if (result
== ENAMETOOLONG
) {
1896 result
= ConvertUnicodeToUTF8Mangled(cnp
->ustr
.length
* sizeof(UniChar
),
1897 cnp
->ustr
.unicode
, maxnamelen
+ 1,
1898 (ByteCount
*)&namelen
, nameptr
,
1903 switch(crp
->recordType
) {
1904 case kHFSFolderRecord
:
1906 cnid
= crp
->hfsFolder
.folderID
;
1908 case kHFSFileRecord
:
1910 cnid
= crp
->hfsFile
.fileID
;
1913 return (0); /* stop */
1916 cnp
= (CatalogName
*) ckp
->hfs
.nodeName
;
1917 result
= hfs_to_utf8(hfsmp
, cnp
->pstr
, maxnamelen
+ 1,
1918 (ByteCount
*)&namelen
, nameptr
);
1920 * When an HFS name cannot be encoded with the current
1921 * volume encoding we use MacRoman as a fallback.
1924 result
= mac_roman_to_utf8(cnp
->pstr
, maxnamelen
+ 1,
1925 (ByteCount
*)&namelen
, nameptr
);
1929 if (state
->cbs_extended
) {
1931 * The index is 1 relative and includes "." and ".."
1933 * Also stuff the cnid in the upper 32 bits of the cookie. The cookie is stored to the previous entry, which
1934 * will be packed and copied this time
1936 state
->cbs_prevdirentry
->d_seekoff
= (state
->cbs_index
+ 3) | ((u_int64_t
)cnid
<< 32);
1937 uiosize
= state
->cbs_prevdirentry
->d_reclen
;
1938 uioaddr
= (caddr_t
) state
->cbs_prevdirentry
;
1940 catent
.d_type
= type
;
1941 catent
.d_namlen
= namelen
;
1942 catent
.d_reclen
= uiosize
= STD_DIRENT_LEN(namelen
);
1944 catent
.d_fileno
= 0; /* file number = 0 means skip entry */
1946 catent
.d_fileno
= cnid
;
1947 uioaddr
= (caddr_t
) &catent
;
1950 /* Save current base address for post processing of hard-links. */
1951 uiobase
= uio_curriovbase(state
->cbs_uio
);
1953 /* If this entry won't fit then we're done */
1954 if ((uiosize
> uio_resid(state
->cbs_uio
)) ||
1955 (ilinkref
!= 0 && state
->cbs_nlinks
== state
->cbs_maxlinks
)) {
1956 return (0); /* stop */
1959 if (!state
->cbs_extended
|| state
->cbs_hasprevdirentry
) {
1960 state
->cbs_result
= uiomove(uioaddr
, uiosize
, state
->cbs_uio
);
1961 if (state
->cbs_result
== 0) {
1964 /* Remember previous entry */
1965 state
->cbs_desc
->cd_cnid
= cnid
;
1966 if (type
== DT_DIR
) {
1967 state
->cbs_desc
->cd_flags
|= CD_ISDIR
;
1969 state
->cbs_desc
->cd_flags
&= ~CD_ISDIR
;
1971 if (state
->cbs_desc
->cd_nameptr
!= NULL
) {
1972 vfs_removename(state
->cbs_desc
->cd_nameptr
);
1975 state
->cbs_desc
->cd_encoding
= xxxx
;
1978 state
->cbs_desc
->cd_namelen
= namelen
;
1979 state
->cbs_desc
->cd_nameptr
= vfs_addname(nameptr
, namelen
, 0, 0);
1981 /* Store unmangled name for the directory hint else it will
1982 * restart readdir at the last location again
1986 size_t tmp_namelen
= 0;
1988 cnp
= (CatalogName
*)&ckp
->hfsPlus
.nodeName
;
1989 bufsize
= 1 + utf8_encodelen(cnp
->ustr
.unicode
,
1990 cnp
->ustr
.length
* sizeof(UniChar
),
1992 MALLOC(new_nameptr
, char *, bufsize
, M_TEMP
, M_WAITOK
);
1993 result
= utf8_encodestr(cnp
->ustr
.unicode
,
1994 cnp
->ustr
.length
* sizeof(UniChar
),
1995 new_nameptr
, &tmp_namelen
,
1998 state
->cbs_desc
->cd_namelen
= tmp_namelen
;
1999 state
->cbs_desc
->cd_nameptr
= vfs_addname(new_nameptr
, tmp_namelen
, 0, 0);
2001 FREE(new_nameptr
, M_TEMP
);
2004 if (state
->cbs_hasprevdirentry
) {
2005 curlinkref
= ilinkref
; /* save current */
2006 ilinkref
= state
->cbs_previlinkref
; /* use previous */
2009 * Record any hard links for post processing.
2011 if ((ilinkref
!= 0) &&
2012 (state
->cbs_result
== 0) &&
2013 (state
->cbs_nlinks
< state
->cbs_maxlinks
)) {
2014 state
->cbs_linkinfo
[state
->cbs_nlinks
].dirent_addr
= uiobase
;
2015 state
->cbs_linkinfo
[state
->cbs_nlinks
].link_ref
= ilinkref
;
2016 state
->cbs_nlinks
++;
2018 if (state
->cbs_hasprevdirentry
) {
2019 ilinkref
= curlinkref
; /* restore current */
2023 if (state
->cbs_extended
) { /* fill the direntry to be used the next time */
2024 if (stop_after_pack
) {
2025 state
->cbs_eof
= true;
2026 return (0); /* stop */
2028 entry
->d_type
= type
;
2029 entry
->d_namlen
= namelen
;
2030 entry
->d_reclen
= EXT_DIRENT_LEN(namelen
);
2032 entry
->d_fileno
= 0; /* file number = 0 means skip entry */
2034 entry
->d_fileno
= cnid
;
2035 /* swap the current and previous entry */
2036 struct direntry
* tmp
;
2037 tmp
= state
->cbs_direntry
;
2038 state
->cbs_direntry
= state
->cbs_prevdirentry
;
2039 state
->cbs_prevdirentry
= tmp
;
2040 state
->cbs_hasprevdirentry
= true;
2041 state
->cbs_previlinkref
= ilinkref
;
2044 /* Continue iteration if there's room */
2045 return (state
->cbs_result
== 0 &&
2046 uio_resid(state
->cbs_uio
) >= SMALL_DIRENTRY_SIZE
);
2051 * Pack a uio buffer with directory entries from the catalog
2055 cat_getdirentries(struct hfsmount
*hfsmp
, int entrycnt
, directoryhint_t
*dirhint
,
2056 uio_t uio
, int extended
, int * items
, int * eofflag
)
2059 BTreeIterator
* iterator
;
2061 struct packdirentry_state state
;
2069 fcb
= GetFileControlBlock(hfsmp
->hfs_catalog_vp
);
2072 * Get a buffer for link info array, btree iterator and a direntry:
2074 maxlinks
= MIN(entrycnt
, uio_resid(uio
) / SMALL_DIRENTRY_SIZE
);
2075 bufsize
= (maxlinks
* sizeof(linkinfo_t
)) + sizeof(*iterator
);
2077 bufsize
+= 2*sizeof(struct direntry
);
2079 MALLOC(buffer
, void *, bufsize
, M_TEMP
, M_WAITOK
);
2080 bzero(buffer
, bufsize
);
2082 state
.cbs_extended
= extended
;
2083 state
.cbs_hasprevdirentry
= false;
2084 state
.cbs_previlinkref
= 0;
2085 state
.cbs_nlinks
= 0;
2086 state
.cbs_maxlinks
= maxlinks
;
2087 state
.cbs_linkinfo
= (linkinfo_t
*) buffer
;
2089 iterator
= (BTreeIterator
*) ((char *)buffer
+ (maxlinks
* sizeof(linkinfo_t
)));
2090 key
= (CatalogKey
*)&iterator
->key
;
2092 index
= dirhint
->dh_index
+ 1;
2094 state
.cbs_direntry
= (struct direntry
*)((char *)iterator
+ sizeof(BTreeIterator
));
2095 state
.cbs_prevdirentry
= state
.cbs_direntry
+ 1;
2096 state
.cbs_eof
= false;
2099 * Attempt to build a key from cached filename
2101 if (dirhint
->dh_desc
.cd_namelen
!= 0) {
2102 if (buildkey(hfsmp
, &dirhint
->dh_desc
, (HFSPlusCatalogKey
*)key
, 0) == 0) {
2108 * If the last entry wasn't cached then position the btree iterator
2110 if ((index
== 0) || !have_key
) {
2112 * Position the iterator at the directory's thread record.
2113 * (i.e. just before the first entry)
2115 buildthreadkey(dirhint
->dh_desc
.cd_parentcnid
, (hfsmp
->hfs_flags
& HFS_STANDARD
), key
);
2116 result
= BTSearchRecord(fcb
, iterator
, NULL
, NULL
, iterator
);
2118 result
= MacToVFSError(result
);
2123 * Iterate until we reach the entry just
2124 * before the one we want to start with.
2127 struct position_state ps
;
2132 ps
.parentID
= dirhint
->dh_desc
.cd_parentcnid
;
2135 result
= BTIterateRecords(fcb
, kBTreeNextRecord
, iterator
,
2136 (IterateCallBackProcPtr
)cat_findposition
, &ps
);
2140 result
= MacToVFSError(result
);
2142 result
= MacToVFSError(result
);
2148 state
.cbs_index
= index
;
2149 state
.cbs_hfsmp
= hfsmp
;
2150 state
.cbs_uio
= uio
;
2151 state
.cbs_desc
= &dirhint
->dh_desc
;
2152 state
.cbs_result
= 0;
2153 state
.cbs_parentID
= dirhint
->dh_desc
.cd_parentcnid
;
2155 enum BTreeIterationOperations op
;
2156 if (extended
&& index
!= 0 && have_key
)
2157 op
= kBTreeCurrentRecord
;
2159 op
= kBTreeNextRecord
;
2162 * Process as many entries as possible starting at iterator->key.
2164 result
= BTIterateRecords(fcb
, op
, iterator
,
2165 (IterateCallBackProcPtr
)cat_packdirentry
, &state
);
2167 /* Note that state.cbs_index is still valid on errors */
2168 *items
= state
.cbs_index
- index
;
2169 index
= state
.cbs_index
;
2171 if (state
.cbs_eof
) {
2175 /* Finish updating the catalog iterator. */
2176 dirhint
->dh_desc
.cd_hint
= iterator
->hint
.nodeNum
;
2177 dirhint
->dh_desc
.cd_flags
|= CD_DECOMPOSED
;
2178 dirhint
->dh_index
= index
- 1;
2181 * Post process any hard links to get the real file id.
2183 if (state
.cbs_nlinks
> 0) {
2184 u_int32_t fileid
= 0;
2185 user_addr_t address
;
2188 for (i
= 0; i
< state
.cbs_nlinks
; ++i
) {
2189 if (resolvelinkid(hfsmp
, state
.cbs_linkinfo
[i
].link_ref
, &fileid
) != 0)
2191 /* This assumes that d_ino is always first field. */
2192 address
= state
.cbs_linkinfo
[i
].dirent_addr
;
2193 if (address
== (user_addr_t
)0)
2195 if (uio_isuserspace(uio
)) {
2196 (void) copyout(&fileid
, address
,
2197 extended
? sizeof(ino64_t
) : sizeof(ino_t
));
2198 } else /* system space */ {
2199 ino64_t
*inoptr
= (ino64_t
*)CAST_DOWN(caddr_t
, address
);
2205 if (state
.cbs_result
)
2206 result
= state
.cbs_result
;
2208 result
= MacToVFSError(result
);
2210 if (result
== ENOENT
) {
2215 FREE(buffer
, M_TEMP
);
2222 * Callback to establish directory position.
2223 * Called with position_state for each item in a directory.
2226 cat_findposition(const CatalogKey
*ckp
, const CatalogRecord
*crp
,
2227 struct position_state
*state
)
2231 if (state
->hfsmp
->hfs_flags
& HFS_STANDARD
)
2232 curID
= ckp
->hfs
.parentID
;
2234 curID
= ckp
->hfsPlus
.parentID
;
2236 /* Make sure parent directory didn't change */
2237 if (state
->parentID
!= curID
) {
2238 state
->error
= EINVAL
;
2239 return (0); /* stop */
2242 /* Count this entry */
2243 switch(crp
->recordType
) {
2244 case kHFSPlusFolderRecord
:
2245 case kHFSPlusFileRecord
:
2246 case kHFSFolderRecord
:
2247 case kHFSFileRecord
:
2251 printf("cat_findposition: invalid record type %d in dir %d\n",
2252 crp
->recordType
, curID
);
2253 state
->error
= EINVAL
;
2254 return (0); /* stop */
2257 return (state
->count
< state
->index
);
2262 * cat_binarykeycompare - compare two HFS Plus catalog keys.
2264 * The name portion of the key is compared using a 16-bit binary comparison.
2265 * This is called from the b-tree code.
2269 cat_binarykeycompare(HFSPlusCatalogKey
*searchKey
, HFSPlusCatalogKey
*trialKey
)
2271 u_int32_t searchParentID
, trialParentID
;
2274 searchParentID
= searchKey
->parentID
;
2275 trialParentID
= trialKey
->parentID
;
2278 if (searchParentID
> trialParentID
) {
2280 } else if (searchParentID
< trialParentID
) {
2283 u_int16_t
* str1
= &searchKey
->nodeName
.unicode
[0];
2284 u_int16_t
* str2
= &trialKey
->nodeName
.unicode
[0];
2285 int length1
= searchKey
->nodeName
.length
;
2286 int length2
= trialKey
->nodeName
.length
;
2290 if (length1
< length2
) {
2293 } else if (length1
> length2
) {
2320 * Compare two standard HFS catalog keys
2322 * Result: +n search key > trial key
2323 * 0 search key = trial key
2324 * -n search key < trial key
2327 CompareCatalogKeys(HFSCatalogKey
*searchKey
, HFSCatalogKey
*trialKey
)
2329 cnid_t searchParentID
, trialParentID
;
2332 searchParentID
= searchKey
->parentID
;
2333 trialParentID
= trialKey
->parentID
;
2335 if (searchParentID
> trialParentID
)
2337 else if (searchParentID
< trialParentID
)
2339 else /* parent dirID's are equal, compare names */
2340 result
= FastRelString(searchKey
->nodeName
, trialKey
->nodeName
);
2347 * Compare two HFS+ catalog keys
2349 * Result: +n search key > trial key
2350 * 0 search key = trial key
2351 * -n search key < trial key
2354 CompareExtendedCatalogKeys(HFSPlusCatalogKey
*searchKey
, HFSPlusCatalogKey
*trialKey
)
2356 cnid_t searchParentID
, trialParentID
;
2359 searchParentID
= searchKey
->parentID
;
2360 trialParentID
= trialKey
->parentID
;
2362 if (searchParentID
> trialParentID
) {
2365 else if (searchParentID
< trialParentID
) {
2368 /* parent node ID's are equal, compare names */
2369 if ( searchKey
->nodeName
.length
== 0 || trialKey
->nodeName
.length
== 0 )
2370 result
= searchKey
->nodeName
.length
- trialKey
->nodeName
.length
;
2372 result
= FastUnicodeCompare(&searchKey
->nodeName
.unicode
[0],
2373 searchKey
->nodeName
.length
,
2374 &trialKey
->nodeName
.unicode
[0],
2375 trialKey
->nodeName
.length
);
2383 * buildkey - build a Catalog b-tree key from a cnode descriptor
2386 buildkey(struct hfsmount
*hfsmp
, struct cat_desc
*descp
,
2387 HFSPlusCatalogKey
*key
, int retry
)
2391 size_t unicodeBytes
= 0;
2393 if (descp
->cd_namelen
== 0 || descp
->cd_nameptr
[0] == '\0')
2394 return (EINVAL
); /* invalid name */
2396 key
->parentID
= descp
->cd_parentcnid
;
2397 key
->nodeName
.length
= 0;
2399 * Convert filename from UTF-8 into Unicode
2402 if ((descp
->cd_flags
& CD_DECOMPOSED
) == 0)
2403 utf8_flags
|= UTF_DECOMPOSED
;
2404 result
= utf8_decodestr(descp
->cd_nameptr
, descp
->cd_namelen
,
2405 key
->nodeName
.unicode
, &unicodeBytes
,
2406 sizeof(key
->nodeName
.unicode
), ':', utf8_flags
);
2407 key
->nodeName
.length
= unicodeBytes
/ sizeof(UniChar
);
2408 key
->keyLength
= kHFSPlusCatalogKeyMinimumLength
+ unicodeBytes
;
2410 if (result
!= ENAMETOOLONG
)
2411 result
= EINVAL
; /* name has invalid characters */
2416 * For HFS volumes convert to an HFS compatible key
2418 * XXX need to save the encoding that succeeded
2420 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) {
2421 HFSCatalogKey hfskey
;
2423 bzero(&hfskey
, sizeof(hfskey
));
2424 hfskey
.keyLength
= kHFSCatalogKeyMinimumLength
;
2425 hfskey
.parentID
= key
->parentID
;
2426 hfskey
.nodeName
[0] = 0;
2427 if (key
->nodeName
.length
> 0) {
2428 if (unicode_to_hfs(HFSTOVCB(hfsmp
),
2429 key
->nodeName
.length
* 2,
2430 key
->nodeName
.unicode
,
2431 &hfskey
.nodeName
[0], retry
) != 0) {
2434 hfskey
.keyLength
+= hfskey
.nodeName
[0];
2436 bcopy(&hfskey
, key
, sizeof(hfskey
));
2443 * Resolve hard link reference to obtain the inode record.
2447 resolvelink(struct hfsmount
*hfsmp
, u_long linkref
, struct HFSPlusCatalogFile
*recp
)
2449 FSBufferDescriptor btdata
;
2450 struct BTreeIterator
*iterator
;
2451 struct cat_desc idesc
;
2455 BDINIT(btdata
, recp
);
2456 MAKE_INODE_NAME(inodename
, linkref
);
2458 /* Get space for iterator */
2459 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
2460 bzero(iterator
, sizeof(*iterator
));
2462 /* Build a descriptor for private dir. */
2463 idesc
.cd_parentcnid
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
2464 idesc
.cd_nameptr
= inodename
;
2465 idesc
.cd_namelen
= strlen(inodename
);
2468 idesc
.cd_encoding
= 0;
2469 (void) buildkey(hfsmp
, &idesc
, (HFSPlusCatalogKey
*)&iterator
->key
, 0);
2471 result
= BTSearchRecord(VTOF(HFSTOVCB(hfsmp
)->catalogRefNum
), iterator
,
2472 &btdata
, NULL
, NULL
);
2475 /* Make sure there's a reference */
2476 if (recp
->bsdInfo
.special
.linkCount
== 0)
2477 recp
->bsdInfo
.special
.linkCount
= 2;
2479 printf("HFS resolvelink: can't find %s\n", inodename
);
2482 FREE(iterator
, M_TEMP
);
2484 return (result
? ENOENT
: 0);
2488 * Resolve hard link reference to obtain the inode number.
2491 resolvelinkid(struct hfsmount
*hfsmp
, u_long linkref
, ino_t
*ino
)
2493 struct HFSPlusCatalogFile record
;
2496 error
= resolvelink(hfsmp
, linkref
, &record
);
2498 if (record
.fileID
== 0)
2501 *ino
= record
.fileID
;
2507 * getkey - get a key from id by doing a thread lookup
2510 getkey(struct hfsmount
*hfsmp
, cnid_t cnid
, CatalogKey
* key
)
2512 struct BTreeIterator
* iterator
;
2513 FSBufferDescriptor btdata
;
2516 CatalogRecord
* recp
;
2520 std_hfs
= (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
);
2522 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
2523 bzero(iterator
, sizeof(*iterator
));
2524 buildthreadkey(cnid
, std_hfs
, (CatalogKey
*)&iterator
->key
);
2526 MALLOC(recp
, CatalogRecord
*, sizeof(CatalogRecord
), M_TEMP
, M_WAITOK
);
2527 BDINIT(btdata
, recp
);
2529 result
= BTSearchRecord(VTOF(HFSTOVCB(hfsmp
)->catalogRefNum
), iterator
,
2530 &btdata
, &datasize
, iterator
);
2534 /* Turn thread record into a cnode key (in place) */
2535 switch (recp
->recordType
) {
2536 case kHFSFileThreadRecord
:
2537 case kHFSFolderThreadRecord
:
2538 keyp
= (CatalogKey
*)((char *)&recp
->hfsThread
.reserved
+ 6);
2539 keyp
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
+ keyp
->hfs
.nodeName
[0];
2540 bcopy(keyp
, key
, keyp
->hfs
.keyLength
+ 1);
2543 case kHFSPlusFileThreadRecord
:
2544 case kHFSPlusFolderThreadRecord
:
2545 keyp
= (CatalogKey
*)&recp
->hfsPlusThread
.reserved
;
2546 keyp
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
+
2547 (keyp
->hfsPlus
.nodeName
.length
* 2);
2548 bcopy(keyp
, key
, keyp
->hfsPlus
.keyLength
+ 2);
2557 FREE(iterator
, M_TEMP
);
2560 return MacToVFSError(result
);
2564 * getkeyplusattr - From id, fetch the key and the bsd attrs for a file/dir (could pass
2565 * null arguments to cat_idlookup instead, but we save around 10% by not building the
2566 * cat_desc here). Both key and attrp must point to real structures.
2570 cat_getkeyplusattr(struct hfsmount
*hfsmp
, cnid_t cnid
, CatalogKey
* key
, struct cat_attr
*attrp
)
2574 result
= getkey(hfsmp
, cnid
, key
);
2577 result
= cat_lookupbykey(hfsmp
, key
, 0, 0, NULL
, attrp
, NULL
, NULL
);
2580 return MacToVFSError(result
);
2585 * buildrecord - build a default catalog directory or file record
2588 buildrecord(struct cat_attr
*attrp
, cnid_t cnid
, int std_hfs
, u_int32_t encoding
,
2589 CatalogRecord
*crp
, int *recordSize
)
2591 int type
= attrp
->ca_mode
& S_IFMT
;
2592 u_int32_t createtime
= to_hfs_time(attrp
->ca_itime
);
2595 createtime
= UTCToLocal(createtime
);
2596 if (type
== S_IFDIR
) {
2597 bzero(crp
, sizeof(HFSCatalogFolder
));
2598 crp
->recordType
= kHFSFolderRecord
;
2599 crp
->hfsFolder
.folderID
= cnid
;
2600 crp
->hfsFolder
.createDate
= createtime
;
2601 crp
->hfsFolder
.modifyDate
= createtime
;
2602 bcopy(attrp
->ca_finderinfo
, &crp
->hfsFolder
.userInfo
, 32);
2603 *recordSize
= sizeof(HFSCatalogFolder
);
2605 bzero(crp
, sizeof(HFSCatalogFile
));
2606 crp
->recordType
= kHFSFileRecord
;
2607 crp
->hfsFile
.fileID
= cnid
;
2608 crp
->hfsFile
.createDate
= createtime
;
2609 crp
->hfsFile
.modifyDate
= createtime
;
2610 bcopy(attrp
->ca_finderinfo
, &crp
->hfsFile
.userInfo
, 16);
2611 bcopy(&attrp
->ca_finderinfo
[16], &crp
->hfsFile
.finderInfo
, 16);
2612 *recordSize
= sizeof(HFSCatalogFile
);
2615 struct HFSPlusBSDInfo
* bsdp
= NULL
;
2616 struct FndrFileInfo
* fip
= NULL
;
2618 if (type
== S_IFDIR
) {
2619 crp
->recordType
= kHFSPlusFolderRecord
;
2620 crp
->hfsPlusFolder
.flags
= 0;
2621 crp
->hfsPlusFolder
.valence
= 0;
2622 crp
->hfsPlusFolder
.folderID
= cnid
;
2623 crp
->hfsPlusFolder
.createDate
= createtime
;
2624 crp
->hfsPlusFolder
.contentModDate
= createtime
;
2625 crp
->hfsPlusFolder
.attributeModDate
= createtime
;
2626 crp
->hfsPlusFolder
.accessDate
= createtime
;
2627 crp
->hfsPlusFolder
.backupDate
= 0;
2628 crp
->hfsPlusFolder
.textEncoding
= encoding
;
2629 crp
->hfsPlusFolder
.attrBlocks
= 0;
2630 bcopy(attrp
->ca_finderinfo
, &crp
->hfsPlusFolder
.userInfo
, 32);
2631 bsdp
= &crp
->hfsPlusFolder
.bsdInfo
;
2632 bsdp
->special
.rawDevice
= 0;
2633 *recordSize
= sizeof(HFSPlusCatalogFolder
);
2635 crp
->recordType
= kHFSPlusFileRecord
;
2636 crp
->hfsPlusFile
.flags
= kHFSThreadExistsMask
;
2637 crp
->hfsPlusFile
.reserved1
= 0;
2638 crp
->hfsPlusFile
.fileID
= cnid
;
2639 crp
->hfsPlusFile
.createDate
= createtime
;
2640 crp
->hfsPlusFile
.contentModDate
= createtime
;
2641 crp
->hfsPlusFile
.accessDate
= createtime
;
2642 crp
->hfsPlusFile
.attributeModDate
= createtime
;
2643 crp
->hfsPlusFile
.backupDate
= 0;
2644 crp
->hfsPlusFile
.textEncoding
= encoding
;
2645 crp
->hfsPlusFile
.attrBlocks
= 0;
2646 bsdp
= &crp
->hfsPlusFile
.bsdInfo
;
2647 bsdp
->special
.rawDevice
= 0;
2651 /* BLK/CHR need to save the device info */
2652 bsdp
->special
.rawDevice
= attrp
->ca_rdev
;
2653 bzero(&crp
->hfsPlusFile
.userInfo
, 32);
2656 /* Hardlink links need to save the linkref */
2657 fip
= (FndrFileInfo
*)&attrp
->ca_finderinfo
;
2658 if ((SWAP_BE32(fip
->fdType
) == kHardLinkFileType
) &&
2659 (SWAP_BE32(fip
->fdCreator
) == kHFSPlusCreator
)) {
2660 bsdp
->special
.iNodeNum
= attrp
->ca_rdev
;
2662 bcopy(attrp
->ca_finderinfo
, &crp
->hfsPlusFile
.userInfo
, 32);
2665 /* Symlinks also have a type and creator */
2666 bcopy(attrp
->ca_finderinfo
, &crp
->hfsPlusFile
.userInfo
, 32);
2669 bzero(&crp
->hfsPlusFile
.dataFork
, 2*sizeof(HFSPlusForkData
));
2670 *recordSize
= sizeof(HFSPlusCatalogFile
);
2672 bsdp
->ownerID
= attrp
->ca_uid
;
2673 bsdp
->groupID
= attrp
->ca_gid
;
2674 bsdp
->fileMode
= attrp
->ca_mode
;
2675 bsdp
->adminFlags
= attrp
->ca_flags
>> 16;
2676 bsdp
->ownerFlags
= attrp
->ca_flags
& 0x000000FF;
2682 * builddesc - build a cnode descriptor from an HFS+ key
2685 builddesc(const HFSPlusCatalogKey
*key
, cnid_t cnid
, u_long hint
, u_long encoding
,
2686 int isdir
, struct cat_desc
*descp
)
2694 /* guess a size... */
2695 bufsize
= (3 * key
->nodeName
.length
) + 1;
2696 if (bufsize
>= sizeof(tmpbuff
) - 1) {
2697 MALLOC(nameptr
, char *, bufsize
, M_TEMP
, M_WAITOK
);
2699 nameptr
= &tmpbuff
[0];
2702 result
= utf8_encodestr(key
->nodeName
.unicode
,
2703 key
->nodeName
.length
* sizeof(UniChar
),
2704 nameptr
, (size_t *)&utf8len
,
2707 if (result
== ENAMETOOLONG
) {
2708 bufsize
= 1 + utf8_encodelen(key
->nodeName
.unicode
,
2709 key
->nodeName
.length
* sizeof(UniChar
),
2711 FREE(nameptr
, M_TEMP
);
2712 MALLOC(nameptr
, char *, bufsize
, M_TEMP
, M_WAITOK
);
2714 result
= utf8_encodestr(key
->nodeName
.unicode
,
2715 key
->nodeName
.length
* sizeof(UniChar
),
2716 nameptr
, (size_t *)&utf8len
,
2719 descp
->cd_parentcnid
= key
->parentID
;
2720 descp
->cd_nameptr
= vfs_addname(nameptr
, utf8len
, 0, 0);
2721 descp
->cd_namelen
= utf8len
;
2722 descp
->cd_cnid
= cnid
;
2723 descp
->cd_hint
= hint
;
2724 descp
->cd_flags
= CD_DECOMPOSED
| CD_HASBUF
;
2726 descp
->cd_flags
|= CD_ISDIR
;
2727 descp
->cd_encoding
= encoding
;
2728 if (nameptr
!= &tmpbuff
[0]) {
2729 FREE(nameptr
, M_TEMP
);
2736 * getbsdattr - get attributes in bsd format
2740 getbsdattr(struct hfsmount
*hfsmp
, const struct HFSPlusCatalogFile
*crp
, struct cat_attr
* attrp
)
2742 int isDirectory
= (crp
->recordType
== kHFSPlusFolderRecord
);
2743 const struct HFSPlusBSDInfo
*bsd
= &crp
->bsdInfo
;
2745 attrp
->ca_recflags
= crp
->flags
;
2746 attrp
->ca_nlink
= 1;
2747 attrp
->ca_atime
= to_bsd_time(crp
->accessDate
);
2748 attrp
->ca_atimeondisk
= attrp
->ca_atime
;
2749 attrp
->ca_mtime
= to_bsd_time(crp
->contentModDate
);
2750 attrp
->ca_ctime
= to_bsd_time(crp
->attributeModDate
);
2751 attrp
->ca_itime
= to_bsd_time(crp
->createDate
);
2752 attrp
->ca_btime
= to_bsd_time(crp
->backupDate
);
2754 if ((bsd
->fileMode
& S_IFMT
) == 0) {
2755 attrp
->ca_flags
= 0;
2756 attrp
->ca_uid
= hfsmp
->hfs_uid
;
2757 attrp
->ca_gid
= hfsmp
->hfs_gid
;
2759 attrp
->ca_mode
= S_IFDIR
| (hfsmp
->hfs_dir_mask
& ACCESSPERMS
);
2761 attrp
->ca_mode
= S_IFREG
| (hfsmp
->hfs_file_mask
& ACCESSPERMS
);
2765 attrp
->ca_uid
= bsd
->ownerID
;
2766 attrp
->ca_gid
= bsd
->groupID
;
2767 attrp
->ca_flags
= bsd
->ownerFlags
| (bsd
->adminFlags
<< 16);
2768 attrp
->ca_mode
= (mode_t
)bsd
->fileMode
;
2769 switch (attrp
->ca_mode
& S_IFMT
) {
2770 case S_IFCHR
: /* fall through */
2772 attrp
->ca_rdev
= bsd
->special
.rawDevice
;
2775 /* Pick up the hard link count */
2776 if (bsd
->special
.linkCount
> 0)
2777 attrp
->ca_nlink
= bsd
->special
.linkCount
;
2781 if (((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
) {
2783 * Override the permissions as determined by the mount auguments
2784 * in ALMOST the same way unset permissions are treated but keep
2785 * track of whether or not the file or folder is hfs locked
2786 * by leaving the h_pflags field unchanged from what was unpacked
2787 * out of the catalog.
2789 attrp
->ca_uid
= hfsmp
->hfs_uid
;
2790 attrp
->ca_gid
= hfsmp
->hfs_gid
;
2795 if (!S_ISDIR(attrp
->ca_mode
)) {
2796 attrp
->ca_mode
&= ~S_IFMT
;
2797 attrp
->ca_mode
|= S_IFDIR
;
2799 attrp
->ca_nlink
= 2 + ((HFSPlusCatalogFolder
*)crp
)->valence
;
2800 attrp
->ca_entries
= ((HFSPlusCatalogFolder
*)crp
)->valence
;
2801 attrp
->ca_attrblks
= ((HFSPlusCatalogFolder
*)crp
)->attrBlocks
;
2803 /* Keep IMMUTABLE bits in sync with HFS locked flag */
2804 if (crp
->flags
& kHFSFileLockedMask
) {
2805 /* The file's supposed to be locked:
2806 Make sure at least one of the IMMUTABLE bits is set: */
2807 if ((attrp
->ca_flags
& (SF_IMMUTABLE
| UF_IMMUTABLE
)) == 0)
2808 attrp
->ca_flags
|= UF_IMMUTABLE
;
2810 /* The file's supposed to be unlocked: */
2811 attrp
->ca_flags
&= ~(SF_IMMUTABLE
| UF_IMMUTABLE
);
2813 /* get total blocks (both forks) */
2814 attrp
->ca_blocks
= crp
->dataFork
.totalBlocks
+ crp
->resourceFork
.totalBlocks
;
2815 attrp
->ca_attrblks
= crp
->attrBlocks
;
2816 /* On HFS+ the ThreadExists flag must always be set. */
2817 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) == 0)
2818 attrp
->ca_recflags
|= kHFSThreadExistsMask
;
2821 attrp
->ca_fileid
= crp
->fileID
;
2823 bcopy(&crp
->userInfo
, attrp
->ca_finderinfo
, 32);
2827 * promotekey - promote hfs key to hfs plus key
2831 promotekey(struct hfsmount
*hfsmp
, const HFSCatalogKey
*hfskey
,
2832 HFSPlusCatalogKey
*keyp
, u_long
*encoding
)
2834 hfs_to_unicode_func_t hfs_get_unicode
= hfsmp
->hfs_get_unicode
;
2838 *encoding
= hfsmp
->hfs_encoding
;
2840 error
= hfs_get_unicode(hfskey
->nodeName
, keyp
->nodeName
.unicode
,
2841 kHFSPlusMaxFileNameChars
, &uniCount
);
2843 * When an HFS name cannot be encoded with the current
2844 * encoding use MacRoman as a fallback.
2846 if (error
&& hfsmp
->hfs_encoding
!= kTextEncodingMacRoman
) {
2848 (void) mac_roman_to_unicode(hfskey
->nodeName
,
2849 keyp
->nodeName
.unicode
,
2850 kHFSPlusMaxFileNameChars
,
2854 keyp
->nodeName
.length
= uniCount
;
2855 keyp
->parentID
= hfskey
->parentID
;
2859 * promotefork - promote hfs fork info to hfs plus
2863 promotefork(struct hfsmount
*hfsmp
, const struct HFSCatalogFile
*filep
,
2864 int resource
, struct cat_fork
* forkp
)
2866 struct HFSPlusExtentDescriptor
*xp
;
2867 u_long blocksize
= HFSTOVCB(hfsmp
)->blockSize
;
2869 bzero(forkp
, sizeof(*forkp
));
2870 xp
= &forkp
->cf_extents
[0];
2872 forkp
->cf_size
= filep
->rsrcLogicalSize
;
2873 forkp
->cf_blocks
= filep
->rsrcPhysicalSize
/ blocksize
;
2874 forkp
->cf_bytesread
= 0;
2875 forkp
->cf_vblocks
= 0;
2876 xp
[0].startBlock
= (u_int32_t
)filep
->rsrcExtents
[0].startBlock
;
2877 xp
[0].blockCount
= (u_int32_t
)filep
->rsrcExtents
[0].blockCount
;
2878 xp
[1].startBlock
= (u_int32_t
)filep
->rsrcExtents
[1].startBlock
;
2879 xp
[1].blockCount
= (u_int32_t
)filep
->rsrcExtents
[1].blockCount
;
2880 xp
[2].startBlock
= (u_int32_t
)filep
->rsrcExtents
[2].startBlock
;
2881 xp
[2].blockCount
= (u_int32_t
)filep
->rsrcExtents
[2].blockCount
;
2883 forkp
->cf_size
= filep
->dataLogicalSize
;
2884 forkp
->cf_blocks
= filep
->dataPhysicalSize
/ blocksize
;
2885 forkp
->cf_bytesread
= 0;
2886 forkp
->cf_vblocks
= 0;
2887 xp
[0].startBlock
= (u_int32_t
)filep
->dataExtents
[0].startBlock
;
2888 xp
[0].blockCount
= (u_int32_t
)filep
->dataExtents
[0].blockCount
;
2889 xp
[1].startBlock
= (u_int32_t
)filep
->dataExtents
[1].startBlock
;
2890 xp
[1].blockCount
= (u_int32_t
)filep
->dataExtents
[1].blockCount
;
2891 xp
[2].startBlock
= (u_int32_t
)filep
->dataExtents
[2].startBlock
;
2892 xp
[2].blockCount
= (u_int32_t
)filep
->dataExtents
[2].blockCount
;
2897 * promoteattr - promote hfs catalog attributes to hfs plus
2901 promoteattr(struct hfsmount
*hfsmp
, const CatalogRecord
*dataPtr
, struct HFSPlusCatalogFile
*crp
)
2903 u_long blocksize
= HFSTOVCB(hfsmp
)->blockSize
;
2905 if (dataPtr
->recordType
== kHFSFolderRecord
) {
2906 struct HFSCatalogFolder
* folder
;
2908 folder
= (struct HFSCatalogFolder
*) dataPtr
;
2909 crp
->recordType
= kHFSPlusFolderRecord
;
2910 crp
->flags
= folder
->flags
;
2911 crp
->fileID
= folder
->folderID
;
2912 crp
->createDate
= LocalToUTC(folder
->createDate
);
2913 crp
->contentModDate
= LocalToUTC(folder
->modifyDate
);
2914 crp
->backupDate
= LocalToUTC(folder
->backupDate
);
2915 crp
->reserved1
= folder
->valence
;
2916 bcopy(&folder
->userInfo
, &crp
->userInfo
, 32);
2918 struct HFSCatalogFile
* file
;
2920 file
= (struct HFSCatalogFile
*) dataPtr
;
2921 crp
->recordType
= kHFSPlusFileRecord
;
2922 crp
->flags
= file
->flags
;
2923 crp
->fileID
= file
->fileID
;
2924 crp
->createDate
= LocalToUTC(file
->createDate
);
2925 crp
->contentModDate
= LocalToUTC(file
->modifyDate
);
2926 crp
->backupDate
= LocalToUTC(file
->backupDate
);
2928 bcopy(&file
->userInfo
, &crp
->userInfo
, 16);
2929 bcopy(&file
->finderInfo
, &crp
->finderInfo
, 16);
2930 crp
->dataFork
.totalBlocks
= file
->dataPhysicalSize
/ blocksize
;
2931 crp
->resourceFork
.totalBlocks
= file
->rsrcPhysicalSize
/ blocksize
;
2933 crp
->textEncoding
= 0;
2934 crp
->attributeModDate
= crp
->contentModDate
;
2935 crp
->accessDate
= crp
->contentModDate
;
2936 bzero(&crp
->bsdInfo
, sizeof(HFSPlusBSDInfo
));
2937 crp
->attrBlocks
= 0;
2941 * Build a catalog node thread record from a catalog key
2942 * and return the size of the record.
2945 buildthread(void *keyp
, void *recp
, int std_hfs
, int directory
)
2950 HFSCatalogKey
*key
= (HFSCatalogKey
*)keyp
;
2951 HFSCatalogThread
*rec
= (HFSCatalogThread
*)recp
;
2953 size
= sizeof(HFSCatalogThread
);
2956 rec
->recordType
= kHFSFolderThreadRecord
;
2958 rec
->recordType
= kHFSFileThreadRecord
;
2959 rec
->parentID
= key
->parentID
;
2960 bcopy(key
->nodeName
, rec
->nodeName
, key
->nodeName
[0]+1);
2963 HFSPlusCatalogKey
*key
= (HFSPlusCatalogKey
*)keyp
;
2964 HFSPlusCatalogThread
*rec
= (HFSPlusCatalogThread
*)recp
;
2966 size
= sizeof(HFSPlusCatalogThread
);
2968 rec
->recordType
= kHFSPlusFolderThreadRecord
;
2970 rec
->recordType
= kHFSPlusFileThreadRecord
;
2972 rec
->parentID
= key
->parentID
;
2973 bcopy(&key
->nodeName
, &rec
->nodeName
,
2974 sizeof(UniChar
) * (key
->nodeName
.length
+ 1));
2976 /* HFS Plus has varaible sized thread records */
2977 size
-= (sizeof(rec
->nodeName
.unicode
) -
2978 (rec
->nodeName
.length
* sizeof(UniChar
)));
2985 * Build a catalog node thread key.
2988 buildthreadkey(HFSCatalogNodeID parentID
, int std_hfs
, CatalogKey
*key
)
2991 key
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
;
2992 key
->hfs
.reserved
= 0;
2993 key
->hfs
.parentID
= parentID
;
2994 key
->hfs
.nodeName
[0] = 0;
2996 key
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
;
2997 key
->hfsPlus
.parentID
= parentID
;
2998 key
->hfsPlus
.nodeName
.length
= 0;
3003 * Extract the text encoding from a catalog node record.
3006 getencoding(const CatalogRecord
*crp
)
3010 if (crp
->recordType
== kHFSPlusFolderRecord
)
3011 encoding
= crp
->hfsPlusFolder
.textEncoding
;
3012 else if (crp
->recordType
== kHFSPlusFileRecord
)
3013 encoding
= crp
->hfsPlusFile
.textEncoding
;
3021 * Extract the CNID from a catalog node record.
3024 getcnid(const CatalogRecord
*crp
)
3028 switch (crp
->recordType
) {
3029 case kHFSFolderRecord
:
3030 cnid
= crp
->hfsFolder
.folderID
;
3032 case kHFSFileRecord
:
3033 cnid
= crp
->hfsFile
.fileID
;
3035 case kHFSPlusFolderRecord
:
3036 cnid
= crp
->hfsPlusFolder
.folderID
;
3038 case kHFSPlusFileRecord
:
3039 cnid
= crp
->hfsPlusFile
.fileID
;
3042 printf("hfs: getcnid: unknown recordType (crp @ 0x%x)\n", crp
);
3050 * Extract the parent ID from a catalog node record.
3053 getparentcnid(const CatalogRecord
*recp
)
3057 switch (recp
->recordType
) {
3058 case kHFSFileThreadRecord
:
3059 case kHFSFolderThreadRecord
:
3060 cnid
= recp
->hfsThread
.parentID
;
3063 case kHFSPlusFileThreadRecord
:
3064 case kHFSPlusFolderThreadRecord
:
3065 cnid
= recp
->hfsPlusThread
.parentID
;
3068 panic("hfs: getparentcnid: unknown recordType (crp @ 0x%x)\n", recp
);
3076 * Determine if a catalog node record is a directory.
3079 isadir(const CatalogRecord
*crp
)
3081 return (crp
->recordType
== kHFSFolderRecord
||
3082 crp
->recordType
== kHFSPlusFolderRecord
);