2 * Copyright (c) 2000-2002 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>
32 #include "../headers/FileMgrInternal.h"
33 #include "../headers/BTreesInternal.h"
34 #include "../headers/CatalogPrivate.h"
35 #include "../headers/HFSUnicodeWrappers.h"
39 //*******************************************************************************
40 // Routine: LocateCatalogNode
42 // Function: Locates the catalog record for an existing folder or file
43 // CNode and returns pointers to the key and data records.
45 //*******************************************************************************
48 LocateCatalogNode(const ExtendedVCB
*volume
, HFSCatalogNodeID folderID
, const CatalogName
*name
,
49 UInt32 hint
, CatalogKey
*keyPtr
, CatalogRecord
*dataPtr
, UInt32
*newHint
)
52 CatalogName
*nodeName
= NULL
; /* To ward off uninitialized use warnings from compiler */
53 HFSCatalogNodeID threadParentID
;
56 result
= LocateCatalogRecord(volume
, folderID
, name
, hint
, keyPtr
, dataPtr
, newHint
);
57 ReturnIfError(result
);
59 // if we got a thread record, then go look up real record
60 switch ( dataPtr
->recordType
)
62 case kHFSFileThreadRecord
:
63 case kHFSFolderThreadRecord
:
64 threadParentID
= dataPtr
->hfsThread
.parentID
;
65 nodeName
= (CatalogName
*) &dataPtr
->hfsThread
.nodeName
;
68 case kHFSPlusFileThreadRecord
:
69 case kHFSPlusFolderThreadRecord
:
70 threadParentID
= dataPtr
->hfsPlusThread
.parentID
;
71 nodeName
= (CatalogName
*) &dataPtr
->hfsPlusThread
.nodeName
;
79 if ( threadParentID
) // found a thread
80 result
= LocateCatalogRecord(volume
, threadParentID
, nodeName
, kNoHint
, keyPtr
, dataPtr
, newHint
);
86 // Routine: LocateCatalogNodeByKey
88 // Function: Locates the catalog record for an existing folder or file
89 // CNode and returns the key and data records.
93 LocateCatalogNodeByKey(const ExtendedVCB
*volume
, UInt32 hint
, CatalogKey
*keyPtr
,
94 CatalogRecord
*dataPtr
, UInt32
*newHint
)
97 CatalogName
*nodeName
= NULL
;
98 HFSCatalogNodeID threadParentID
;
100 FSBufferDescriptor btRecord
;
101 BTreeIterator searchIterator
= {0};
104 fcb
= GetFileControlBlock(volume
->catalogRefNum
);
106 btRecord
.bufferAddress
= dataPtr
;
107 btRecord
.itemCount
= 1;
108 btRecord
.itemSize
= sizeof(CatalogRecord
);
110 searchIterator
.hint
.nodeNum
= hint
;
112 bcopy(keyPtr
, &searchIterator
.key
, sizeof(CatalogKey
));
114 result
= BTSearchRecord( fcb
, &searchIterator
, &btRecord
, &tempSize
, &searchIterator
);
118 *newHint
= searchIterator
.hint
.nodeNum
;
120 BlockMoveData(&searchIterator
.key
, keyPtr
, sizeof(CatalogKey
));
123 if (result
== btNotFound
)
125 ReturnIfError(result
);
127 // if we got a thread record, then go look up real record
128 switch ( dataPtr
->recordType
)
130 case kHFSFileThreadRecord
:
131 case kHFSFolderThreadRecord
:
132 threadParentID
= dataPtr
->hfsThread
.parentID
;
133 nodeName
= (CatalogName
*) &dataPtr
->hfsThread
.nodeName
;
136 case kHFSPlusFileThreadRecord
:
137 case kHFSPlusFolderThreadRecord
:
138 threadParentID
= dataPtr
->hfsPlusThread
.parentID
;
139 nodeName
= (CatalogName
*) &dataPtr
->hfsPlusThread
.nodeName
;
147 if ( threadParentID
) // found a thread
148 result
= LocateCatalogRecord(volume
, threadParentID
, nodeName
, kNoHint
, keyPtr
, dataPtr
, newHint
);
155 //*******************************************************************************
156 // Routine: LocateCatalogRecord
158 // Function: Locates the catalog record associated with folderID and name
160 //*******************************************************************************
163 LocateCatalogRecord(const ExtendedVCB
*volume
, HFSCatalogNodeID folderID
, const CatalogName
*name
,
164 UInt32 hint
, CatalogKey
*keyPtr
, CatalogRecord
*dataPtr
, UInt32
*newHint
)
167 CatalogKey tempKey
; // 518 bytes
170 BuildCatalogKey(folderID
, name
, (volume
->vcbSigWord
== kHFSPlusSigWord
), &tempKey
);
173 hint
= kNoHint
; // no CName given so clear the hint
175 result
= SearchBTreeRecord(volume
->catalogRefNum
, &tempKey
, hint
, keyPtr
, dataPtr
, &tempSize
, newHint
);
177 return (result
== btNotFound
? cmNotFound
: result
);
183 * Routine: BuildCatalogKey
185 * Function: Constructs a catalog key record (ckr) given the parent
186 * folder ID and CName. Works for both classic and extended
192 BuildCatalogKey(HFSCatalogNodeID parentID
, const CatalogName
*cName
, Boolean isHFSPlus
, CatalogKey
*key
)
196 key
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
; // initial key length (4 + 2)
197 key
->hfsPlus
.parentID
= parentID
; // set parent ID
198 key
->hfsPlus
.nodeName
.length
= 0; // null CName length
201 CopyCatalogName(cName
, (CatalogName
*) &key
->hfsPlus
.nodeName
, isHFSPlus
);
202 key
->hfsPlus
.keyLength
+= sizeof(UniChar
) * cName
->ustr
.length
; // add CName size to key length
207 key
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
; // initial key length (1 + 4 + 1)
208 key
->hfs
.reserved
= 0; // clear unused byte
209 key
->hfs
.parentID
= parentID
; // set parent ID
210 key
->hfs
.nodeName
[0] = 0; // null CName length
213 UpdateCatalogName(cName
->pstr
, key
->hfs
.nodeName
);
214 key
->hfs
.keyLength
+= key
->hfs
.nodeName
[0]; // add CName size to key length
220 BuildCatalogKeyUTF8(ExtendedVCB
*volume
, HFSCatalogNodeID parentID
, const char *name
, UInt32 nameLength
,
221 CatalogKey
*key
, UInt32
*textEncoding
)
227 else if (nameLength
== kUndefinedStrLen
)
228 nameLength
= strlen(name
);
230 if ( volume
->vcbSigWord
== kHFSPlusSigWord
) {
231 size_t unicodeBytes
= 0;
233 key
->hfsPlus
.keyLength
= kHFSPlusCatalogKeyMinimumLength
; // initial key length (4 + 2)
234 key
->hfsPlus
.parentID
= parentID
; // set parent ID
235 key
->hfsPlus
.nodeName
.length
= 0; // null CName length
236 if ( nameLength
> 0 ) {
237 err
= utf8_decodestr(name
, nameLength
, key
->hfsPlus
.nodeName
.unicode
,
238 &unicodeBytes
, sizeof(key
->hfsPlus
.nodeName
.unicode
), ':', UTF_DECOMPOSED
);
239 key
->hfsPlus
.nodeName
.length
= unicodeBytes
/ sizeof(UniChar
);
240 key
->hfsPlus
.keyLength
+= unicodeBytes
;
243 if (textEncoding
&& (*textEncoding
!= kTextEncodingMacUnicode
))
244 *textEncoding
= hfs_pickencoding(key
->hfsPlus
.nodeName
.unicode
,
245 key
->hfsPlus
.nodeName
.length
);
248 key
->hfs
.keyLength
= kHFSCatalogKeyMinimumLength
; // initial key length (1 + 4 + 1)
249 key
->hfs
.reserved
= 0; // clear unused byte
250 key
->hfs
.parentID
= parentID
; // set parent ID
251 key
->hfs
.nodeName
[0] = 0; // null CName length
252 if ( nameLength
> 0 ) {
253 err
= utf8_to_hfs(volume
, nameLength
, name
, &key
->hfs
.nodeName
[0]);
255 * Retry with MacRoman in case that's how it was exported.
256 * When textEncoding != NULL we know that this is a create
257 * or rename call and can skip the retry (ugly but it works).
259 if (err
&& (textEncoding
== NULL
))
260 err
= utf8_to_mac_roman(nameLength
, name
, &key
->hfs
.nodeName
[0]);
261 key
->hfs
.keyLength
+= key
->hfs
.nodeName
[0]; // add CName size to key length
268 if (err
== ENAMETOOLONG
)
269 err
= bdNamErr
; /* name is too long */
271 err
= paramErr
; /* name has invalid characters */
278 //*******************************************************************************
279 // Routine: FlushCatalog
281 // Function: Flushes the catalog for a specified volume.
283 //*******************************************************************************
286 FlushCatalog(ExtendedVCB
*volume
)
291 fcb
= GetFileControlBlock(volume
->catalogRefNum
);
292 result
= BTFlushPath(fcb
);
296 //--- check if catalog's fcb is dirty...
298 if ( 0 /*fcb->fcbFlags & fcbModifiedMask*/ )
300 HFS_MOUNT_LOCK(volume
, TRUE
);
301 volume
->vcbFlags
|= 0xFF00; // Mark the VCB dirty
302 volume
->vcbLsMod
= GetTimeUTC(); // update last modified date
303 HFS_MOUNT_UNLOCK(volume
, TRUE
);
305 // result = FlushVolumeControlBlock(volume);
313 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
314 // Routine: UpdateCatalogName
316 // Function: Updates a CName.
318 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
321 UpdateCatalogName(ConstStr31Param srcName
, Str31 destName
)
323 Size length
= srcName
[0];
325 if (length
> CMMaxCName
)
326 length
= CMMaxCName
; // truncate to max
328 destName
[0] = length
; // set length byte
330 BlockMoveData(&srcName
[1], &destName
[1], length
);
333 //_______________________________________________________________________
336 CopyCatalogName(const CatalogName
*srcName
, CatalogName
*dstName
, Boolean isHFSPLus
)
340 if ( srcName
== NULL
)
342 if ( dstName
!= NULL
)
343 dstName
->ustr
.length
= 0; // set length byte to zero (works for both unicode and pascal)
348 length
= sizeof(UniChar
) * (srcName
->ustr
.length
+ 1);
350 length
= sizeof(UInt8
) + srcName
->pstr
[0];
353 BlockMoveData(srcName
, dstName
, length
);
355 dstName
->ustr
.length
= 0; // set length byte to zero (works for both unicode and pascal)