#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: LocateCatalogNode
-//
-// Function: Locates the catalog record for an existing folder or file
-// CNode and returns pointers to the key and data records.
-//
-//*******************************************************************************
-
-OSErr
-LocateCatalogNode(const ExtendedVCB *volume, HFSCatalogNodeID folderID, const CatalogName *name,
- u_int32_t hint, CatalogKey *keyPtr, CatalogRecord *dataPtr, u_int32_t *newHint)
-{
- OSErr result;
- CatalogName *nodeName = NULL; /* To ward off uninitialized use warnings from compiler */
- HFSCatalogNodeID threadParentID;
-
-
- result = LocateCatalogRecord(volume, folderID, name, hint, keyPtr, dataPtr, newHint);
- ReturnIfError(result);
-
- // if we got a thread record, then go look up real record
- switch ( dataPtr->recordType )
- {
- case kHFSFileThreadRecord:
- case kHFSFolderThreadRecord:
- threadParentID = dataPtr->hfsThread.parentID;
- nodeName = (CatalogName *) &dataPtr->hfsThread.nodeName;
- break;
-
- 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);
-
- return result;
-}
-
//
// Routine: LocateCatalogNodeByKey
//
HFSCatalogNodeID threadParentID;
u_int16_t tempSize;
FSBufferDescriptor btRecord;
- BTreeIterator searchIterator;
+ struct BTreeIterator *searchIterator;
FCB *fcb;
- bzero(&searchIterator, sizeof(searchIterator));
+ 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.itemCount = 1;
btRecord.itemSize = sizeof(CatalogRecord);
- searchIterator.hint.nodeNum = hint;
+ searchIterator->hint.nodeNum = hint;
- bcopy(keyPtr, &searchIterator.key, sizeof(CatalogKey));
+ bcopy(keyPtr, &searchIterator->key, sizeof(CatalogKey));
- result = BTSearchRecord( fcb, &searchIterator, &btRecord, &tempSize, &searchIterator );
+ result = BTSearchRecord( fcb, searchIterator, &btRecord, &tempSize, searchIterator );
if (result == noErr)
{
- *newHint = searchIterator.hint.nodeNum;
+ *newHint = searchIterator->hint.nodeNum;
- BlockMoveData(&searchIterator.key, keyPtr, sizeof(CatalogKey));
+ BlockMoveData(&searchIterator->key, keyPtr, sizeof(CatalogKey));
}
- if (result == btNotFound)
- result = cmNotFound;
- ReturnIfError(result);
+ 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 ( threadParentID ) // found a thread
result = LocateCatalogRecord(volume, threadParentID, nodeName, kNoHint, keyPtr, dataPtr, newHint);
+ FREE (searchIterator, M_TEMP);
return result;
}
OSErr
LocateCatalogRecord(const ExtendedVCB *volume, HFSCatalogNodeID folderID, const CatalogName *name,
- u_int32_t hint, CatalogKey *keyPtr, CatalogRecord *dataPtr, u_int32_t *newHint)
+ __unused u_int32_t hint, CatalogKey *keyPtr, CatalogRecord *dataPtr, u_int32_t *newHint)
{
- OSErr result;
- CatalogKey tempKey; // 518 bytes
- u_int16_t tempSize;
+ 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
+ }
- BuildCatalogKey(folderID, name, (volume->vcbSigWord == kHFSPlusSigWord), &tempKey);
+ bzero(searchIterator, sizeof(*searchIterator));
- if ( name == NULL )
- hint = kNoHint; // no CName given so clear the hint
- result = SearchBTreeRecord(volume->catalogRefNum, &tempKey, hint, keyPtr, dataPtr, &tempSize, newHint);
+ fcb = GetFileControlBlock(volume->catalogRefNum);
+ btcb = (BTreeControlBlock *)fcb->fcbBTCBPtr;
- return (result == btNotFound ? cmNotFound : result);
+ 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);
}