Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | /* * Copyright (c) 2000-2002, 2004-2005 Apple Computer, Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <sys/param.h> #include <sys/utfconv.h> #include <sys/stat.h> #include <sys/kernel.h> #include <sys/malloc.h> #include <libkern/libkern.h> #include "../headers/FileMgrInternal.h" #include "../headers/BTreesInternal.h" #include "../headers/CatalogPrivate.h" #include "../headers/HFSUnicodeWrappers.h" #include "../headers/BTreesPrivate.h" #include <string.h> // // Routine: LocateCatalogNodeByKey // // Function: Locates the catalog record for an existing folder or file // CNode and returns the key and data records. // OSErr LocateCatalogNodeByKey(const ExtendedVCB *volume, u_int32_t hint, CatalogKey *keyPtr, CatalogRecord *dataPtr, u_int32_t *newHint) { OSErr result; CatalogName *nodeName = NULL; HFSCatalogNodeID threadParentID; u_int16_t tempSize; FSBufferDescriptor btRecord; struct BTreeIterator *searchIterator; FCB *fcb; MALLOC (searchIterator, struct BTreeIterator*, sizeof(struct BTreeIterator), M_TEMP, M_WAITOK); if (searchIterator == NULL) { return memFullErr; // translates to ENOMEM } bzero(searchIterator, sizeof(*searchIterator)); fcb = GetFileControlBlock(volume->catalogRefNum); btRecord.bufferAddress = dataPtr; btRecord.itemCount = 1; btRecord.itemSize = sizeof(CatalogRecord); searchIterator->hint.nodeNum = hint; bcopy(keyPtr, &searchIterator->key, sizeof(CatalogKey)); result = BTSearchRecord( fcb, searchIterator, &btRecord, &tempSize, searchIterator ); if (result == noErr) { *newHint = searchIterator->hint.nodeNum; BlockMoveData(&searchIterator->key, keyPtr, sizeof(CatalogKey)); } if (result == btNotFound) { result = cmNotFound; } if (result) { FREE(searchIterator, M_TEMP); return result; } // if we got a thread record, then go look up real record switch ( dataPtr->recordType ) { #if CONFIG_HFS_STD case kHFSFileThreadRecord: case kHFSFolderThreadRecord: threadParentID = dataPtr->hfsThread.parentID; nodeName = (CatalogName *) &dataPtr->hfsThread.nodeName; break; #endif case kHFSPlusFileThreadRecord: case kHFSPlusFolderThreadRecord: threadParentID = dataPtr->hfsPlusThread.parentID; nodeName = (CatalogName *) &dataPtr->hfsPlusThread.nodeName; break; default: threadParentID = 0; break; } if ( threadParentID ) // found a thread result = LocateCatalogRecord(volume, threadParentID, nodeName, kNoHint, keyPtr, dataPtr, newHint); FREE (searchIterator, M_TEMP); return result; } //******************************************************************************* // Routine: LocateCatalogRecord // // Function: Locates the catalog record associated with folderID and name // //******************************************************************************* OSErr LocateCatalogRecord(const ExtendedVCB *volume, HFSCatalogNodeID folderID, const CatalogName *name, __unused u_int32_t hint, CatalogKey *keyPtr, CatalogRecord *dataPtr, u_int32_t *newHint) { OSErr result; uint16_t tempSize; FSBufferDescriptor btRecord; struct BTreeIterator *searchIterator = NULL; FCB *fcb; BTreeControlBlock *btcb; MALLOC (searchIterator, struct BTreeIterator*, sizeof(struct BTreeIterator), M_TEMP, M_WAITOK); if (searchIterator == NULL) { return memFullErr; // translates to ENOMEM } bzero(searchIterator, sizeof(*searchIterator)); fcb = GetFileControlBlock(volume->catalogRefNum); btcb = (BTreeControlBlock *)fcb->fcbBTCBPtr; btRecord.bufferAddress = dataPtr; btRecord.itemCount = 1; btRecord.itemSize = sizeof(CatalogRecord); BuildCatalogKey(folderID, name, (volume->vcbSigWord == kHFSPlusSigWord), (CatalogKey *)&searchIterator->key); result = BTSearchRecord(fcb, searchIterator, &btRecord, &tempSize, searchIterator); if (result == noErr) { *newHint = searchIterator->hint.nodeNum; BlockMoveData(&searchIterator->key, keyPtr, CalcKeySize(btcb, &searchIterator->key)); } FREE (searchIterator, M_TEMP); return (result == btNotFound ? cmNotFound : result); } /* * Routine: BuildCatalogKey * * Function: Constructs a catalog key record (ckr) given the parent * folder ID and CName. Works for both classic and extended * HFS volumes. * */ void BuildCatalogKey(HFSCatalogNodeID parentID, const CatalogName *cName, Boolean isHFSPlus, CatalogKey *key) { if ( isHFSPlus ) { key->hfsPlus.keyLength = kHFSPlusCatalogKeyMinimumLength; // initial key length (4 + 2) key->hfsPlus.parentID = parentID; // set parent ID key->hfsPlus.nodeName.length = 0; // null CName length if ( cName != NULL ) { CopyCatalogName(cName, (CatalogName *) &key->hfsPlus.nodeName, isHFSPlus); key->hfsPlus.keyLength += sizeof(UniChar) * cName->ustr.length; // add CName size to key length } } #if CONFIG_HFS_STD else { key->hfs.keyLength = kHFSCatalogKeyMinimumLength; // initial key length (1 + 4 + 1) key->hfs.reserved = 0; // clear unused byte key->hfs.parentID = parentID; // set parent ID key->hfs.nodeName[0] = 0; // null CName length if ( cName != NULL ) { UpdateCatalogName(cName->pstr, key->hfs.nodeName); key->hfs.keyLength += key->hfs.nodeName[0]; // add CName size to key length } } #endif } OSErr BuildCatalogKeyUTF8(ExtendedVCB *volume, HFSCatalogNodeID parentID, const unsigned char *name, u_int32_t nameLength, CatalogKey *key, u_int32_t *textEncoding) { OSErr err = 0; if ( name == NULL) nameLength = 0; else if (nameLength == kUndefinedStrLen) nameLength = strlen((const char *)name); if ( volume->vcbSigWord == kHFSPlusSigWord ) { size_t unicodeBytes = 0; key->hfsPlus.keyLength = kHFSPlusCatalogKeyMinimumLength; // initial key length (4 + 2) key->hfsPlus.parentID = parentID; // set parent ID key->hfsPlus.nodeName.length = 0; // null CName length if ( nameLength > 0 ) { err = utf8_decodestr(name, nameLength, key->hfsPlus.nodeName.unicode, &unicodeBytes, sizeof(key->hfsPlus.nodeName.unicode), ':', UTF_DECOMPOSED); key->hfsPlus.nodeName.length = unicodeBytes / sizeof(UniChar); key->hfsPlus.keyLength += unicodeBytes; } if (textEncoding && (*textEncoding != kTextEncodingMacUnicode)) *textEncoding = hfs_pickencoding(key->hfsPlus.nodeName.unicode, key->hfsPlus.nodeName.length); } #if CONFIG_HFS_STD else { key->hfs.keyLength = kHFSCatalogKeyMinimumLength; // initial key length (1 + 4 + 1) key->hfs.reserved = 0; // clear unused byte key->hfs.parentID = parentID; // set parent ID key->hfs.nodeName[0] = 0; // null CName length if ( nameLength > 0 ) { err = utf8_to_hfs(volume, nameLength, name, &key->hfs.nodeName[0]); /* * Retry with MacRoman in case that's how it was exported. * When textEncoding != NULL we know that this is a create * or rename call and can skip the retry (ugly but it works). */ if (err && (textEncoding == NULL)) err = utf8_to_mac_roman(nameLength, name, &key->hfs.nodeName[0]); key->hfs.keyLength += key->hfs.nodeName[0]; // add CName size to key length } if (textEncoding) *textEncoding = 0; } #endif if (err) { if (err == ENAMETOOLONG) err = bdNamErr; /* name is too long */ else err = paramErr; /* name has invalid characters */ } return err; } //******************************************************************************* // Routine: FlushCatalog // // Function: Flushes the catalog for a specified volume. // //******************************************************************************* OSErr FlushCatalog(ExtendedVCB *volume) { FCB * fcb; OSErr result; struct hfsmount *hfsmp = VCBTOHFS (volume); fcb = GetFileControlBlock(volume->catalogRefNum); result = BTFlushPath(fcb); if (result == noErr) { //--- check if catalog's fcb is dirty... if ( 0 /*fcb->fcbFlags & fcbModifiedMask*/ ) { hfs_lock_mount (hfsmp); MarkVCBDirty(volume); // Mark the VCB dirty volume->vcbLsMod = GetTimeUTC(); // update last modified date hfs_unlock_mount (hfsmp); // result = FlushVolumeControlBlock(volume); } } return result; } //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ // Routine: UpdateCatalogName // // Function: Updates a CName. // //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ void UpdateCatalogName(ConstStr31Param srcName, Str31 destName) { Size length = srcName[0]; if (length > CMMaxCName) length = CMMaxCName; // truncate to max destName[0] = length; // set length byte BlockMoveData(&srcName[1], &destName[1], length); } //_______________________________________________________________________ void CopyCatalogName(const CatalogName *srcName, CatalogName *dstName, Boolean isHFSPlus) { u_int32_t length = 0; if ( srcName == NULL ) { if ( dstName != NULL ) dstName->ustr.length = 0; // set length byte to zero (works for both unicode and pascal) return; } if (isHFSPlus) { length = sizeof(UniChar) * (srcName->ustr.length + 1); } #if CONFIG_HFS_STD else { length = sizeof(u_int8_t) + srcName->pstr[0]; } #endif if ( length > 1 ) BlockMoveData(srcName, dstName, length); else dstName->ustr.length = 0; // set length byte to zero (works for both unicode and pascal) } |