2 * Copyright (c) 2000-2002, 2004-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <sys/param.h>
29 #include <sys/utfconv.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <libkern/libkern.h>
35 #include "../headers/FileMgrInternal.h"
36 #include "../headers/BTreesInternal.h"
37 #include "../headers/CatalogPrivate.h"
38 #include "../headers/HFSUnicodeWrappers.h"
39 #include "../headers/BTreesPrivate.h"
43 // Routine: LocateCatalogNodeByKey
45 // Function: Locates the catalog record for an existing folder or file
46 // CNode and returns the key and data records.
50 LocateCatalogNodeByKey(const ExtendedVCB
*volume
, u_int32_t hint
, CatalogKey
*keyPtr
,
51 CatalogRecord
*dataPtr
, u_int32_t
*newHint
)
54 CatalogName
*nodeName
= NULL
;
55 HFSCatalogNodeID threadParentID
;
57 FSBufferDescriptor btRecord
;
58 struct BTreeIterator
*searchIterator
;
61 MALLOC (searchIterator
, struct BTreeIterator
*, sizeof(struct BTreeIterator
), M_TEMP
, M_WAITOK
);
62 if (searchIterator
== NULL
) {
63 return memFullErr
; // translates to ENOMEM
66 bzero(searchIterator
, sizeof(*searchIterator
));
68 fcb
= GetFileControlBlock(volume
->catalogRefNum
);
70 btRecord
.bufferAddress
= dataPtr
;
71 btRecord
.itemCount
= 1;
72 btRecord
.itemSize
= sizeof(CatalogRecord
);
74 searchIterator
->hint
.nodeNum
= hint
;
76 bcopy(keyPtr
, &searchIterator
->key
, sizeof(CatalogKey
));
78 result
= BTSearchRecord( fcb
, searchIterator
, &btRecord
, &tempSize
, searchIterator
);
82 *newHint
= searchIterator
->hint
.nodeNum
;
84 BlockMoveData(&searchIterator
->key
, keyPtr
, sizeof(CatalogKey
));
87 if (result
== btNotFound
) {
92 FREE(searchIterator
, M_TEMP
);
96 // if we got a thread record, then go look up real record
97 switch ( dataPtr
->recordType
)
99 case kHFSFileThreadRecord
:
100 case kHFSFolderThreadRecord
:
101 threadParentID
= dataPtr
->hfsThread
.parentID
;
102 nodeName
= (CatalogName
*) &dataPtr
->hfsThread
.nodeName
;
105 case kHFSPlusFileThreadRecord
:
106 case kHFSPlusFolderThreadRecord
:
107 threadParentID
= dataPtr
->hfsPlusThread
.parentID
;
108 nodeName
= (CatalogName
*) &dataPtr
->hfsPlusThread
.nodeName
;
116 if ( threadParentID
) // found a thread
117 result
= LocateCatalogRecord(volume
, threadParentID
, nodeName
, kNoHint
, keyPtr
, dataPtr
, newHint
);
119 FREE (searchIterator
, M_TEMP
);
125 //*******************************************************************************
126 // Routine: LocateCatalogRecord
128 // Function: Locates the catalog record associated with folderID and name
130 //*******************************************************************************
133 LocateCatalogRecord(const ExtendedVCB
*volume
, HFSCatalogNodeID folderID
, const CatalogName
*name
,
134 __unused u_int32_t hint
, CatalogKey
*keyPtr
, CatalogRecord
*dataPtr
, u_int32_t
*newHint
)
138 FSBufferDescriptor btRecord
;
139 struct BTreeIterator
*searchIterator
= NULL
;
141 BTreeControlBlock
*btcb
;
143 MALLOC (searchIterator
, struct BTreeIterator
*, sizeof(struct BTreeIterator
), M_TEMP
, M_WAITOK
);
144 if (searchIterator
== NULL
) {
145 return memFullErr
; // translates to ENOMEM
148 bzero(searchIterator
, sizeof(*searchIterator
));
151 fcb
= GetFileControlBlock(volume
->catalogRefNum
);
152 btcb
= (BTreeControlBlock
*)fcb
->fcbBTCBPtr
;
154 btRecord
.bufferAddress
= dataPtr
;
155 btRecord
.itemCount
= 1;
156 btRecord
.itemSize
= sizeof(CatalogRecord
);
158 BuildCatalogKey(folderID
, name
, (volume
->vcbSigWord
== kHFSPlusSigWord
), (CatalogKey
*)&searchIterator
->key
);
160 result
= BTSearchRecord(fcb
, searchIterator
, &btRecord
, &tempSize
, searchIterator
);
161 if (result
== noErr
) {
162 *newHint
= searchIterator
->hint
.nodeNum
;
163 BlockMoveData(&searchIterator
->key
, keyPtr
, CalcKeySize(btcb
, &searchIterator
->key
));
166 FREE (searchIterator
, M_TEMP
);
167 return (result
== btNotFound
? cmNotFound
: result
);
173 * Routine: BuildCatalogKey
175 * Function: Constructs a catalog key record (ckr) given the parent
176 * folder ID and CName. Works for both classic and extended
182 BuildCatalogKey(HFSCatalogNodeID parentID
, const CatalogName
*cName
, Boolean isHFSPlus
, CatalogKey
*key
)
186 key
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
; // initial key length (4 + 2)
187 key
->hfsPlus
.parentID
= parentID
; // set parent ID
188 key
->hfsPlus
.nodeName
.length
= 0; // null CName length
191 CopyCatalogName(cName
, (CatalogName
*) &key
->hfsPlus
.nodeName
, isHFSPlus
);
192 key
->hfsPlus
.keyLength
+= sizeof(UniChar
) * cName
->ustr
.length
; // add CName size to key length
197 key
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
; // initial key length (1 + 4 + 1)
198 key
->hfs
.reserved
= 0; // clear unused byte
199 key
->hfs
.parentID
= parentID
; // set parent ID
200 key
->hfs
.nodeName
[0] = 0; // null CName length
203 UpdateCatalogName(cName
->pstr
, key
->hfs
.nodeName
);
204 key
->hfs
.keyLength
+= key
->hfs
.nodeName
[0]; // add CName size to key length
210 BuildCatalogKeyUTF8(ExtendedVCB
*volume
, HFSCatalogNodeID parentID
, const unsigned char *name
, u_int32_t nameLength
,
211 CatalogKey
*key
, u_int32_t
*textEncoding
)
217 else if (nameLength
== kUndefinedStrLen
)
218 nameLength
= strlen((const char *)name
);
220 if ( volume
->vcbSigWord
== kHFSPlusSigWord
) {
221 size_t unicodeBytes
= 0;
223 key
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
; // initial key length (4 + 2)
224 key
->hfsPlus
.parentID
= parentID
; // set parent ID
225 key
->hfsPlus
.nodeName
.length
= 0; // null CName length
226 if ( nameLength
> 0 ) {
227 err
= utf8_decodestr(name
, nameLength
, key
->hfsPlus
.nodeName
.unicode
,
228 &unicodeBytes
, sizeof(key
->hfsPlus
.nodeName
.unicode
), ':', UTF_DECOMPOSED
);
229 key
->hfsPlus
.nodeName
.length
= unicodeBytes
/ sizeof(UniChar
);
230 key
->hfsPlus
.keyLength
+= unicodeBytes
;
233 if (textEncoding
&& (*textEncoding
!= kTextEncodingMacUnicode
))
234 *textEncoding
= hfs_pickencoding(key
->hfsPlus
.nodeName
.unicode
,
235 key
->hfsPlus
.nodeName
.length
);
238 key
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
; // initial key length (1 + 4 + 1)
239 key
->hfs
.reserved
= 0; // clear unused byte
240 key
->hfs
.parentID
= parentID
; // set parent ID
241 key
->hfs
.nodeName
[0] = 0; // null CName length
242 if ( nameLength
> 0 ) {
243 err
= utf8_to_hfs(volume
, nameLength
, name
, &key
->hfs
.nodeName
[0]);
245 * Retry with MacRoman in case that's how it was exported.
246 * When textEncoding != NULL we know that this is a create
247 * or rename call and can skip the retry (ugly but it works).
249 if (err
&& (textEncoding
== NULL
))
250 err
= utf8_to_mac_roman(nameLength
, name
, &key
->hfs
.nodeName
[0]);
251 key
->hfs
.keyLength
+= key
->hfs
.nodeName
[0]; // add CName size to key length
258 if (err
== ENAMETOOLONG
)
259 err
= bdNamErr
; /* name is too long */
261 err
= paramErr
; /* name has invalid characters */
268 //*******************************************************************************
269 // Routine: FlushCatalog
271 // Function: Flushes the catalog for a specified volume.
273 //*******************************************************************************
276 FlushCatalog(ExtendedVCB
*volume
)
281 fcb
= GetFileControlBlock(volume
->catalogRefNum
);
282 result
= BTFlushPath(fcb
);
286 //--- check if catalog's fcb is dirty...
288 if ( 0 /*fcb->fcbFlags & fcbModifiedMask*/ )
290 HFS_MOUNT_LOCK(volume
, TRUE
);
291 MarkVCBDirty(volume
); // Mark the VCB dirty
292 volume
->vcbLsMod
= GetTimeUTC(); // update last modified date
293 HFS_MOUNT_UNLOCK(volume
, TRUE
);
295 // result = FlushVolumeControlBlock(volume);
303 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
304 // Routine: UpdateCatalogName
306 // Function: Updates a CName.
308 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
311 UpdateCatalogName(ConstStr31Param srcName
, Str31 destName
)
313 Size length
= srcName
[0];
315 if (length
> CMMaxCName
)
316 length
= CMMaxCName
; // truncate to max
318 destName
[0] = length
; // set length byte
320 BlockMoveData(&srcName
[1], &destName
[1], length
);
323 //_______________________________________________________________________
326 CopyCatalogName(const CatalogName
*srcName
, CatalogName
*dstName
, Boolean isHFSPLus
)
330 if ( srcName
== NULL
)
332 if ( dstName
!= NULL
)
333 dstName
->ustr
.length
= 0; // set length byte to zero (works for both unicode and pascal)
338 length
= sizeof(UniChar
) * (srcName
->ustr
.length
+ 1);
340 length
= sizeof(u_int8_t
) + srcName
->pstr
[0];
343 BlockMoveData(srcName
, dstName
, length
);
345 dstName
->ustr
.length
= 0; // set length byte to zero (works for both unicode and pascal)