2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 #include <sys/param.h>
31 #include <sys/utfconv.h>
34 #include "../headers/FileMgrInternal.h"
35 #include "../headers/BTreesInternal.h"
36 #include "../headers/CatalogPrivate.h"
37 #include "../headers/HFSUnicodeWrappers.h"
41 //*******************************************************************************
42 // Routine: LocateCatalogNode
44 // Function: Locates the catalog record for an existing folder or file
45 // CNode and returns pointers to the key and data records.
47 //*******************************************************************************
50 LocateCatalogNode(const ExtendedVCB
*volume
, HFSCatalogNodeID folderID
, const CatalogName
*name
,
51 UInt32 hint
, CatalogKey
*keyPtr
, CatalogRecord
*dataPtr
, UInt32
*newHint
)
54 CatalogName
*nodeName
= NULL
; /* To ward off uninitialized use warnings from compiler */
55 HFSCatalogNodeID threadParentID
;
58 result
= LocateCatalogRecord(volume
, folderID
, name
, hint
, keyPtr
, dataPtr
, newHint
);
59 ReturnIfError(result
);
61 // if we got a thread record, then go look up real record
62 switch ( dataPtr
->recordType
)
64 case kHFSFileThreadRecord
:
65 case kHFSFolderThreadRecord
:
66 threadParentID
= dataPtr
->hfsThread
.parentID
;
67 nodeName
= (CatalogName
*) &dataPtr
->hfsThread
.nodeName
;
70 case kHFSPlusFileThreadRecord
:
71 case kHFSPlusFolderThreadRecord
:
72 threadParentID
= dataPtr
->hfsPlusThread
.parentID
;
73 nodeName
= (CatalogName
*) &dataPtr
->hfsPlusThread
.nodeName
;
81 if ( threadParentID
) // found a thread
82 result
= LocateCatalogRecord(volume
, threadParentID
, nodeName
, kNoHint
, keyPtr
, dataPtr
, newHint
);
88 // Routine: LocateCatalogNodeByKey
90 // Function: Locates the catalog record for an existing folder or file
91 // CNode and returns the key and data records.
95 LocateCatalogNodeByKey(const ExtendedVCB
*volume
, UInt32 hint
, CatalogKey
*keyPtr
,
96 CatalogRecord
*dataPtr
, UInt32
*newHint
)
99 CatalogName
*nodeName
= NULL
;
100 HFSCatalogNodeID threadParentID
;
102 FSBufferDescriptor btRecord
;
103 BTreeIterator searchIterator
= {0};
106 fcb
= GetFileControlBlock(volume
->catalogRefNum
);
108 btRecord
.bufferAddress
= dataPtr
;
109 btRecord
.itemCount
= 1;
110 btRecord
.itemSize
= sizeof(CatalogRecord
);
112 searchIterator
.hint
.nodeNum
= hint
;
114 bcopy(keyPtr
, &searchIterator
.key
, sizeof(CatalogKey
));
116 result
= BTSearchRecord( fcb
, &searchIterator
, &btRecord
, &tempSize
, &searchIterator
);
120 *newHint
= searchIterator
.hint
.nodeNum
;
122 BlockMoveData(&searchIterator
.key
, keyPtr
, sizeof(CatalogKey
));
125 if (result
== btNotFound
)
127 ReturnIfError(result
);
129 // if we got a thread record, then go look up real record
130 switch ( dataPtr
->recordType
)
132 case kHFSFileThreadRecord
:
133 case kHFSFolderThreadRecord
:
134 threadParentID
= dataPtr
->hfsThread
.parentID
;
135 nodeName
= (CatalogName
*) &dataPtr
->hfsThread
.nodeName
;
138 case kHFSPlusFileThreadRecord
:
139 case kHFSPlusFolderThreadRecord
:
140 threadParentID
= dataPtr
->hfsPlusThread
.parentID
;
141 nodeName
= (CatalogName
*) &dataPtr
->hfsPlusThread
.nodeName
;
149 if ( threadParentID
) // found a thread
150 result
= LocateCatalogRecord(volume
, threadParentID
, nodeName
, kNoHint
, keyPtr
, dataPtr
, newHint
);
157 //*******************************************************************************
158 // Routine: LocateCatalogRecord
160 // Function: Locates the catalog record associated with folderID and name
162 //*******************************************************************************
165 LocateCatalogRecord(const ExtendedVCB
*volume
, HFSCatalogNodeID folderID
, const CatalogName
*name
,
166 UInt32 hint
, CatalogKey
*keyPtr
, CatalogRecord
*dataPtr
, UInt32
*newHint
)
169 CatalogKey tempKey
; // 518 bytes
172 BuildCatalogKey(folderID
, name
, (volume
->vcbSigWord
== kHFSPlusSigWord
), &tempKey
);
175 hint
= kNoHint
; // no CName given so clear the hint
177 result
= SearchBTreeRecord(volume
->catalogRefNum
, &tempKey
, hint
, keyPtr
, dataPtr
, &tempSize
, newHint
);
179 return (result
== btNotFound
? cmNotFound
: result
);
185 * Routine: BuildCatalogKey
187 * Function: Constructs a catalog key record (ckr) given the parent
188 * folder ID and CName. Works for both classic and extended
194 BuildCatalogKey(HFSCatalogNodeID parentID
, const CatalogName
*cName
, Boolean isHFSPlus
, CatalogKey
*key
)
198 key
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
; // initial key length (4 + 2)
199 key
->hfsPlus
.parentID
= parentID
; // set parent ID
200 key
->hfsPlus
.nodeName
.length
= 0; // null CName length
203 CopyCatalogName(cName
, (CatalogName
*) &key
->hfsPlus
.nodeName
, isHFSPlus
);
204 key
->hfsPlus
.keyLength
+= sizeof(UniChar
) * cName
->ustr
.length
; // add CName size to key length
209 key
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
; // initial key length (1 + 4 + 1)
210 key
->hfs
.reserved
= 0; // clear unused byte
211 key
->hfs
.parentID
= parentID
; // set parent ID
212 key
->hfs
.nodeName
[0] = 0; // null CName length
215 UpdateCatalogName(cName
->pstr
, key
->hfs
.nodeName
);
216 key
->hfs
.keyLength
+= key
->hfs
.nodeName
[0]; // add CName size to key length
222 BuildCatalogKeyUTF8(ExtendedVCB
*volume
, HFSCatalogNodeID parentID
, const char *name
, UInt32 nameLength
,
223 CatalogKey
*key
, UInt32
*textEncoding
)
229 else if (nameLength
== kUndefinedStrLen
)
230 nameLength
= strlen(name
);
232 if ( volume
->vcbSigWord
== kHFSPlusSigWord
) {
233 size_t unicodeBytes
= 0;
235 key
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
; // initial key length (4 + 2)
236 key
->hfsPlus
.parentID
= parentID
; // set parent ID
237 key
->hfsPlus
.nodeName
.length
= 0; // null CName length
238 if ( nameLength
> 0 ) {
239 err
= utf8_decodestr(name
, nameLength
, key
->hfsPlus
.nodeName
.unicode
,
240 &unicodeBytes
, sizeof(key
->hfsPlus
.nodeName
.unicode
), ':', UTF_DECOMPOSED
);
241 key
->hfsPlus
.nodeName
.length
= unicodeBytes
/ sizeof(UniChar
);
242 key
->hfsPlus
.keyLength
+= unicodeBytes
;
245 if (textEncoding
&& (*textEncoding
!= kTextEncodingMacUnicode
))
246 *textEncoding
= hfs_pickencoding(key
->hfsPlus
.nodeName
.unicode
,
247 key
->hfsPlus
.nodeName
.length
);
250 key
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
; // initial key length (1 + 4 + 1)
251 key
->hfs
.reserved
= 0; // clear unused byte
252 key
->hfs
.parentID
= parentID
; // set parent ID
253 key
->hfs
.nodeName
[0] = 0; // null CName length
254 if ( nameLength
> 0 ) {
255 err
= utf8_to_hfs(volume
, nameLength
, name
, &key
->hfs
.nodeName
[0]);
257 * Retry with MacRoman in case that's how it was exported.
258 * When textEncoding != NULL we know that this is a create
259 * or rename call and can skip the retry (ugly but it works).
261 if (err
&& (textEncoding
== NULL
))
262 err
= utf8_to_mac_roman(nameLength
, name
, &key
->hfs
.nodeName
[0]);
263 key
->hfs
.keyLength
+= key
->hfs
.nodeName
[0]; // add CName size to key length
270 if (err
== ENAMETOOLONG
)
271 err
= bdNamErr
; /* name is too long */
273 err
= paramErr
; /* name has invalid characters */
280 //*******************************************************************************
281 // Routine: FlushCatalog
283 // Function: Flushes the catalog for a specified volume.
285 //*******************************************************************************
288 FlushCatalog(ExtendedVCB
*volume
)
293 fcb
= GetFileControlBlock(volume
->catalogRefNum
);
294 result
= BTFlushPath(fcb
);
298 //--- check if catalog's fcb is dirty...
300 if ( 0 /*fcb->fcbFlags & fcbModifiedMask*/ )
302 HFS_MOUNT_LOCK(volume
, TRUE
);
303 volume
->vcbFlags
|= 0xFF00; // Mark the VCB dirty
304 volume
->vcbLsMod
= GetTimeUTC(); // update last modified date
305 HFS_MOUNT_UNLOCK(volume
, TRUE
);
307 // result = FlushVolumeControlBlock(volume);
315 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
316 // Routine: UpdateCatalogName
318 // Function: Updates a CName.
320 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
323 UpdateCatalogName(ConstStr31Param srcName
, Str31 destName
)
325 Size length
= srcName
[0];
327 if (length
> CMMaxCName
)
328 length
= CMMaxCName
; // truncate to max
330 destName
[0] = length
; // set length byte
332 BlockMoveData(&srcName
[1], &destName
[1], length
);
335 //_______________________________________________________________________
338 CopyCatalogName(const CatalogName
*srcName
, CatalogName
*dstName
, Boolean isHFSPLus
)
342 if ( srcName
== NULL
)
344 if ( dstName
!= NULL
)
345 dstName
->ustr
.length
= 0; // set length byte to zero (works for both unicode and pascal)
350 length
= sizeof(UniChar
) * (srcName
->ustr
.length
+ 1);
352 length
= sizeof(UInt8
) + srcName
->pstr
[0];
355 BlockMoveData(srcName
, dstName
, length
);
357 dstName
->ustr
.length
= 0; // set length byte to zero (works for both unicode and pascal)