1 | /*␊ |
2 | * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | *␊ |
6 | * The contents of this file constitute Original Code as defined in and␊ |
7 | * are subject to the Apple Public Source License Version 2.0 (the␊ |
8 | * "License"). You may not use this file except in compliance with the␊ |
9 | * License. Please obtain a copy of the License at␊ |
10 | * http://www.apple.com/publicsource and read it before using this file.␊ |
11 | * ␊ |
12 | * This Original Code and all software distributed under the License are␊ |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the␊ |
17 | * License for the specific language governing rights and limitations␊ |
18 | * under the License.␊ |
19 | *␊ |
20 | * @APPLE_LICENSE_HEADER_END@␊ |
21 | *␊ |
22 | *␊ |
23 | * hfs.c - File System Module for HFS and HFS+.␊ |
24 | *␊ |
25 | * Copyright (c) 1999-2002 Apple Computer, Inc.␊ |
26 | *␊ |
27 | * DRI: Josh de Cesare␊ |
28 | */␊ |
29 | ␊ |
30 | #include <sl.h>␊ |
31 | #include <hfs/hfs_format.h>␊ |
32 | ␊ |
33 | #include "hfs.h"␊ |
34 | ␊ |
35 | #define kBlockSize (0x200)␊ |
36 | ␊ |
37 | #define kMDBBaseOffset (2 * kBlockSize)␊ |
38 | ␊ |
39 | #define kBTreeCatalog (0)␊ |
40 | #define kBTreeExtents (1)␊ |
41 | ␊ |
42 | #ifdef __i386__␊ |
43 | ␊ |
44 | static CICell gCurrentIH;␊ |
45 | static long long gAllocationOffset;␊ |
46 | static long gIsHFSPlus;␊ |
47 | static long gCaseSensitive;␊ |
48 | static u_int32_t gBlockSize;␊ |
49 | static u_int32_t gCacheBlockSize;␊ |
50 | static char *gBTreeHeaderBuffer;␊ |
51 | static BTHeaderRec *gBTHeaders[2];␊ |
52 | static char *gHFSMdbVib;␊ |
53 | static HFSMasterDirectoryBlock *gHFSMDB;␊ |
54 | static char *gHFSPlusHeader;␊ |
55 | static HFSPlusVolumeHeader *gHFSPlus;␊ |
56 | static char *gLinkTemp;␊ |
57 | static long long gVolID;␊ |
58 | static char *gTempStr;␊ |
59 | ␊ |
60 | #else /* !__i386__ */␊ |
61 | ␊ |
62 | static CICell␉␉␉gCurrentIH;␊ |
63 | static long long␉␉gAllocationOffset;␊ |
64 | static long␉␉␉gIsHFSPlus;␊ |
65 | static long␉␉␉gCaseSensitive;␊ |
66 | static u_int32_t␉␉gBlockSize;␊ |
67 | static u_int32_t␉␉gCacheBlockSize;␊ |
68 | static char␉␉␉gBTreeHeaderBuffer[512];␊ |
69 | static BTHeaderRec␉␉*gBTHeaders[2];␊ |
70 | static char␉␉␉gHFSMdbVib[kBlockSize];␊ |
71 | static HFSMasterDirectoryBlock␉*gHFSMDB =(HFSMasterDirectoryBlock*)gHFSMdbVib;␊ |
72 | static char␉␉␉gHFSPlusHeader[kBlockSize];␊ |
73 | static HFSPlusVolumeHeader␉*gHFSPlus =(HFSPlusVolumeHeader*)gHFSPlusHeader;␊ |
74 | static char␉␉␉gLinkTemp[64];␊ |
75 | static long long␉␉gVolID;␊ |
76 | ␊ |
77 | #endif /* !__i386__ */␊ |
78 | ␊ |
79 | static long ReadFile(void *file, u_int64_t *length, void *base, u_int64_t offset);␊ |
80 | static long GetCatalogEntryInfo(void *entry, long *flags, u_int32_t *time,␊ |
81 | FinderInfo *finderInfo, long *infoValid);␊ |
82 | static long ResolvePathToCatalogEntry(char *filePath, long *flags,␊ |
83 | void *entry, u_int32_t dirID, long long *dirIndex);␊ |
84 | ␊ |
85 | static long GetCatalogEntry(long long *dirIndex, char **name,␊ |
86 | long *flags, u_int32_t *time,␊ |
87 | FinderInfo *finderInfo, long *infoValid);␊ |
88 | static long ReadCatalogEntry(char *fileName, u_int32_t dirID, void *entry,␊ |
89 | long long *dirIndex);␊ |
90 | static long ReadExtentsEntry(u_int32_t fileID, long startBlock, void *entry);␊ |
91 | ␊ |
92 | static long ReadBTreeEntry(long btree, void *key, char *entry, long long *dirIndex);␊ |
93 | static void GetBTreeRecord(u_int16_t index, char *nodeBuffer, u_int16_t nodeSize,␊ |
94 | char **key, char **data);␊ |
95 | ␊ |
96 | static long ReadExtent(char *extent, u_int64_t extentSize, u_int32_t extentFile,␊ |
97 | u_int64_t offset, u_int64_t size, void *buffer, long cache);␊ |
98 | ␊ |
99 | static u_int32_t GetExtentStart(void *extents, u_int32_t index);␊ |
100 | static u_int32_t GetExtentSize(void *extents, u_int32_t index);␊ |
101 | ␊ |
102 | static long CompareHFSCatalogKeys(void *key, void *testKey);␊ |
103 | static long CompareHFSPlusCatalogKeys(void *key, void *testKey);␊ |
104 | static long CompareHFSExtentsKeys(void *key, void *testKey);␊ |
105 | static long CompareHFSPlusExtentsKeys(void *key, void *testKey);␊ |
106 | ␊ |
107 | extern long FastRelString(u_int8_t *str1, u_int8_t *str2);␊ |
108 | extern long BinaryUnicodeCompare(u_int16_t *uniStr1, u_int32_t len1,␊ |
109 | u_int16_t *uniStr2, u_int32_t len2);␊ |
110 | ␊ |
111 | //==============================================================================␊ |
112 | ␊ |
113 | static void SwapFinderInfo(FndrFileInfo *dst, FndrFileInfo *src)␊ |
114 | {␊ |
115 | ␉dst->fdType = SWAP_BE32(src->fdType);␊ |
116 | ␉dst->fdCreator = SWAP_BE32(src->fdCreator);␊ |
117 | ␉dst->fdFlags = SWAP_BE16(src->fdFlags);␊ |
118 | ␉// Don't bother with location␊ |
119 | }␊ |
120 | ␊ |
121 | //==============================================================================␊ |
122 | ␊ |
123 | void HFSFree(CICell ih)␊ |
124 | {␊ |
125 | ␉if(gCurrentIH == ih)␊ |
126 | ␉{␊ |
127 | ␉␉gCurrentIH = 0;␊ |
128 | ␉}␊ |
129 | ␉free(ih);␊ |
130 | }␊ |
131 | ␊ |
132 | //==============================================================================␊ |
133 | ␊ |
134 | bool HFSProbe (const void *buf)␊ |
135 | {␊ |
136 | ␉const HFSMasterDirectoryBlock *mdb;␊ |
137 | ␉const HFSPlusVolumeHeader *header;␊ |
138 | ␉mdb = (const HFSMasterDirectoryBlock *)(((const char*)buf)+kMDBBaseOffset);␊ |
139 | ␉header = (const HFSPlusVolumeHeader *)(((const char*)buf)+kMDBBaseOffset);␊ |
140 | ␉␊ |
141 | ␉if ( SWAP_BE16(mdb->drSigWord) == kHFSSigWord ) {␊ |
142 | ␉␉return true;␊ |
143 | ␉}␊ |
144 | ␊ |
145 | ␉if (SWAP_BE16(header->signature) != kHFSPlusSigWord && SWAP_BE16(header->signature) != kHFSXSigWord) {␊ |
146 | ␉␉return false;␊ |
147 | ␉}␊ |
148 | ␉return true;␊ |
149 | }␊ |
150 | ␊ |
151 | //==============================================================================␊ |
152 | ␊ |
153 | long HFSInitPartition(CICell ih)␊ |
154 | {␊ |
155 | ␉u_int64_t extentSize;␊ |
156 | ␉void *extent;␊ |
157 | ␉u_int32_t extentFile;␊ |
158 | ␉u_int16_t nodeSize;␊ |
159 | ␊ |
160 | ␉if (ih == gCurrentIH)␊ |
161 | ␉{␊ |
162 | #ifdef __i386__␊ |
163 | ␉␉CacheInit(ih, gCacheBlockSize);␊ |
164 | #endif␊ |
165 | ␉return 0;␊ |
166 | ␉}␊ |
167 | ␊ |
168 | #ifdef __i386__␊ |
169 | ␉if (!gTempStr)␊ |
170 | ␉{␊ |
171 | ␉␉gTempStr = (char *)malloc(4096);␊ |
172 | ␉}␊ |
173 | ␉if (!gLinkTemp)␊ |
174 | ␉{␊ |
175 | ␉␉gLinkTemp = (char *)malloc(64);␊ |
176 | ␉}␊ |
177 | ␉if (!gBTreeHeaderBuffer)␊ |
178 | ␉{␊ |
179 | ␉␉gBTreeHeaderBuffer = (char *)malloc(512);␊ |
180 | ␉}␊ |
181 | ␉if (!gHFSMdbVib)␊ |
182 | ␉{␊ |
183 | ␉␉gHFSMdbVib = (char *)malloc(kBlockSize);␊ |
184 | ␉␉gHFSMDB = (HFSMasterDirectoryBlock *)gHFSMdbVib;␊ |
185 | ␉}␊ |
186 | ␉if (!gHFSPlusHeader)␊ |
187 | ␉{␊ |
188 | ␉␉gHFSPlusHeader = (char *)malloc(kBlockSize);␊ |
189 | ␉␉gHFSPlus = (HFSPlusVolumeHeader *)gHFSPlusHeader;␊ |
190 | ␉}␊ |
191 | ␉if (!gTempStr || !gLinkTemp || !gBTreeHeaderBuffer || !gHFSMdbVib || !gHFSPlusHeader)␊ |
192 | ␉{␊ |
193 | ␉␉return -1;␊ |
194 | ␉}␊ |
195 | #endif /* __i386__ */␊ |
196 | ␊ |
197 | ␉gAllocationOffset = 0;␊ |
198 | ␉gIsHFSPlus = 0;␊ |
199 | ␉gCaseSensitive = 0;␊ |
200 | ␉gBTHeaders[0] = 0;␊ |
201 | ␉gBTHeaders[1] = 0;␊ |
202 | ␊ |
203 | ␉// Look for the HFS MDB␊ |
204 | ␉Seek(ih, kMDBBaseOffset);␊ |
205 | ␉Read(ih, (long)gHFSMdbVib, kBlockSize);␊ |
206 | ␊ |
207 | ␉if (SWAP_BE16(gHFSMDB->drSigWord) == kHFSSigWord)␊ |
208 | ␉{␊ |
209 | ␉␉gAllocationOffset = SWAP_BE16(gHFSMDB->drAlBlSt) * kBlockSize;␊ |
210 | ␊ |
211 | ␉␉// See if it is HFSPlus␊ |
212 | ␉␉if (SWAP_BE16(gHFSMDB->drEmbedSigWord) != kHFSPlusSigWord)␊ |
213 | ␉␉{␊ |
214 | ␉␉␉// Normal HFS;␊ |
215 | ␉␉␉gCacheBlockSize = gBlockSize = SWAP_BE32(gHFSMDB->drAlBlkSiz);␊ |
216 | ␉␉␉CacheInit(ih, gCacheBlockSize);␊ |
217 | ␉␉␉gCurrentIH = ih;␊ |
218 | ␊ |
219 | ␉␉␉// grab the 64 bit volume ID␊ |
220 | ␉␉␉bcopy(&gHFSMDB->drFndrInfo[6], &gVolID, 8);␊ |
221 | ␊ |
222 | ␉␉␉// Get the Catalog BTree node size.␊ |
223 | ␉␉␉extent = (HFSExtentDescriptor *)&gHFSMDB->drCTExtRec;␊ |
224 | ␉␉␉extentSize = SWAP_BE32(gHFSMDB->drCTFlSize);␊ |
225 | ␉␉␉extentFile = kHFSCatalogFileID;␊ |
226 | ␉␉␉ReadExtent(extent, extentSize, extentFile, 0, 256, gBTreeHeaderBuffer + kBTreeCatalog * 256, 0);␊ |
227 | ␊ |
228 | ␉␉␉nodeSize = SWAP_BE16(((BTHeaderRec *)(gBTreeHeaderBuffer + kBTreeCatalog * 256 + sizeof(BTNodeDescriptor)))->nodeSize);␊ |
229 | ␊ |
230 | ␉␉␉// If the BTree node size is larger than the block size, reset the cache.␊ |
231 | ␉␉␉if (nodeSize > gBlockSize)␊ |
232 | ␉␉␉{␊ |
233 | ␉␉␉␉gCacheBlockSize = nodeSize;␊ |
234 | ␉␉␉␉CacheInit(ih, gCacheBlockSize);␊ |
235 | ␉␉␉}␊ |
236 | ␊ |
237 | ␉␉return 0;␊ |
238 | ␉␉}␊ |
239 | ␊ |
240 | ␉␉// Calculate the offset to the embeded HFSPlus volume.␊ |
241 | ␉␉gAllocationOffset += (long long)SWAP_BE16(gHFSMDB->drEmbedExtent.startBlock) *␊ |
242 | SWAP_BE32(gHFSMDB->drAlBlkSiz);␊ |
243 | ␉}␊ |
244 | ␊ |
245 | ␉// Look for the HFSPlus Header␊ |
246 | ␉Seek(ih, gAllocationOffset + kMDBBaseOffset);␊ |
247 | ␉Read(ih, (long)gHFSPlusHeader, kBlockSize);␊ |
248 | ␊ |
249 | ␉// Not a HFS+ or HFSX volume.␊ |
250 | ␉if (SWAP_BE16(gHFSPlus->signature) != kHFSPlusSigWord && SWAP_BE16(gHFSPlus->signature) != kHFSXSigWord)␊ |
251 | ␉{␊ |
252 | ␉␉verbose("HFS signature was not present.\n");␊ |
253 | ␉␉gCurrentIH = 0;␊ |
254 | ␉␉return -1;␊ |
255 | ␉}␊ |
256 | ␊ |
257 | ␉gIsHFSPlus = 1;␊ |
258 | ␉gCacheBlockSize = gBlockSize = SWAP_BE32(gHFSPlus->blockSize);␊ |
259 | ␉CacheInit(ih, gCacheBlockSize);␊ |
260 | ␉gCurrentIH = ih;␊ |
261 | ␊ |
262 | ␉ih->modTime = SWAP_BE32(gHFSPlus->modifyDate) - 2082844800;␊ |
263 | ␊ |
264 | ␉// grab the 64 bit volume ID␊ |
265 | ␉bcopy(&gHFSPlus->finderInfo[24], &gVolID, 8);␊ |
266 | ␊ |
267 | ␉// Get the Catalog BTree node size.␊ |
268 | ␉extent = &gHFSPlus->catalogFile.extents;␊ |
269 | ␉extentSize = SWAP_BE64(gHFSPlus->catalogFile.logicalSize);␊ |
270 | ␉extentFile = kHFSCatalogFileID;␊ |
271 | ␊ |
272 | ␉ReadExtent(extent, extentSize, extentFile, 0, 256, gBTreeHeaderBuffer + kBTreeCatalog * 256, 0);␊ |
273 | ␊ |
274 | ␉nodeSize = SWAP_BE16(((BTHeaderRec *)(gBTreeHeaderBuffer + kBTreeCatalog * 256 + sizeof(BTNodeDescriptor)))->nodeSize);␊ |
275 | ␊ |
276 | ␉// If the BTree node size is larger than the block size, reset the cache.␊ |
277 | ␉if (nodeSize > gBlockSize)␊ |
278 | ␉{␊ |
279 | ␉␉gCacheBlockSize = nodeSize;␊ |
280 | ␉␉CacheInit(ih, gCacheBlockSize);␊ |
281 | ␉}␊ |
282 | ␊ |
283 | ␉return 0;␊ |
284 | }␊ |
285 | ␊ |
286 | //==============================================================================␊ |
287 | ␊ |
288 | long HFSLoadFile(CICell ih, char * filePath)␊ |
289 | {␊ |
290 | ␉return HFSReadFile(ih, filePath, (void *)gFSLoadAddress, 0, 0);␊ |
291 | }␊ |
292 | ␊ |
293 | long HFSReadFile(CICell ih, char *filePath, void *base, u_int64_t offset, u_int64_t length)␊ |
294 | {␊ |
295 | ␉char entry[512];␊ |
296 | ␉char devStr[12];␊ |
297 | ␉u_int32_t dirID;␊ |
298 | ␉long result, flags;␊ |
299 | ␊ |
300 | ␉if (HFSInitPartition(ih) == -1)␊ |
301 | ␉{␊ |
302 | ␉␉return -1;␊ |
303 | ␉}␊ |
304 | ␊ |
305 | ␉dirID = kHFSRootFolderID;␊ |
306 | ␉// Skip a lead '/'. Start in the system folder if there are two.␊ |
307 | ␉if (filePath[0] == '/')␊ |
308 | ␉{␊ |
309 | ␉␉if (filePath[1] == '/')␊ |
310 | ␉␉{␊ |
311 | ␉␉␉if (gIsHFSPlus)␊ |
312 | ␉␉␉{␊ |
313 | ␉␉␉␉dirID = SWAP_BE32(((long *)gHFSPlus->finderInfo)[5]);␊ |
314 | ␉␉␉}␊ |
315 | ␉␉␉else␊ |
316 | ␉␉␉{␊ |
317 | ␉␉␉␉dirID = SWAP_BE32(gHFSMDB->drFndrInfo[5]);␊ |
318 | ␉␉␉}␊ |
319 | ␊ |
320 | ␉␉␉if (dirID == 0)␊ |
321 | ␉␉␉{␊ |
322 | ␉␉␉␉return -1;␊ |
323 | ␉␉␉}␊ |
324 | ␊ |
325 | ␉␉␉filePath++;␊ |
326 | ␉␉}␊ |
327 | ␊ |
328 | ␉␉filePath++;␊ |
329 | ␉}␊ |
330 | ␊ |
331 | ␉result = ResolvePathToCatalogEntry(filePath, &flags, entry, dirID, 0);␊ |
332 | ␊ |
333 | ␉if ((result == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
334 | ␉{␊ |
335 | ␉␉return -1;␊ |
336 | ␉}␊ |
337 | ␊ |
338 | #if UNUSED␊ |
339 | ␉// Not yet for Intel. System.config/Default.table will fail this check.␊ |
340 | ␉// Check file owner and permissions.␊ |
341 | ␉if (flags & (kOwnerNotRoot | kPermGroupWrite | kPermOtherWrite))␊ |
342 | ␉{␊ |
343 | ␉␉return -1;␊ |
344 | ␉}␊ |
345 | #endif␊ |
346 | ␊ |
347 | ␉result = ReadFile(entry, &length, base, offset);␊ |
348 | ␉if (result == -1)␊ |
349 | ␉{␊ |
350 | ␉␉return -1;␊ |
351 | ␉}␊ |
352 | ␊ |
353 | ␉getDeviceDescription(ih, devStr);␊ |
354 | /*␊ |
355 | ␉verbose("Read HFS%s file: [%s/%s] %d bytes.\n", (gIsHFSPlus ? "+" : ""), devStr, filePath, (uint32_t)length);␊ |
356 | */␊ |
357 | ␉return length;␊ |
358 | }␊ |
359 | ␊ |
360 | long HFSGetDirEntry(CICell ih, char * dirPath, long long * dirIndex, char ** name, long * flags, u_int32_t * time, FinderInfo * finderInfo, long * infoValid)␊ |
361 | {␊ |
362 | ␉char entry[512];␊ |
363 | ␉u_int32_t dirID;␊ |
364 | ␉long dirFlags;␊ |
365 | ␊ |
366 | ␉if (HFSInitPartition(ih) == -1)␊ |
367 | ␉{␊ |
368 | ␉␉return -1;␊ |
369 | ␉}␊ |
370 | ␊ |
371 | ␉if (*dirIndex == -1)␊ |
372 | ␉{␊ |
373 | ␉␉return -1;␊ |
374 | ␉}␊ |
375 | ␊ |
376 | ␉dirID = kHFSRootFolderID;␊ |
377 | ␊ |
378 | ␉// Skip a lead '/'. Start in the system folder if there are two.␊ |
379 | ␉if (dirPath[0] == '/')␊ |
380 | ␉{␊ |
381 | ␉␉if (dirPath[1] == '/')␊ |
382 | ␉␉{␊ |
383 | ␉␉␉if (gIsHFSPlus)␊ |
384 | ␉␉␉{␊ |
385 | ␉␉␉␉dirID = SWAP_BE32(((long *)gHFSPlus->finderInfo)[5]);␊ |
386 | ␉␉␉}␊ |
387 | ␉␉␉else␊ |
388 | ␉␉␉{␊ |
389 | ␉␉␉␉dirID = SWAP_BE32(gHFSMDB->drFndrInfo[5]);␊ |
390 | ␉␉␉}␊ |
391 | ␊ |
392 | ␉␉␉if (dirID == 0)␊ |
393 | ␉␉␉{␊ |
394 | ␉␉␉␉return -1;␊ |
395 | ␉␉␉}␊ |
396 | ␊ |
397 | ␉␉␉dirPath++;␊ |
398 | ␉␉}␊ |
399 | ␊ |
400 | ␉␉dirPath++;␊ |
401 | ␉}␊ |
402 | ␊ |
403 | ␉if (*dirIndex == 0)␊ |
404 | ␉{␊ |
405 | ␉␉ResolvePathToCatalogEntry(dirPath, &dirFlags, entry, dirID, dirIndex);␊ |
406 | ␊ |
407 | ␉␉if (*dirIndex == 0)␊ |
408 | ␉␉{␊ |
409 | ␉␉␉*dirIndex = -1;␊ |
410 | ␉␉}␊ |
411 | ␊ |
412 | ␉␉if ((dirFlags & kFileTypeMask) != kFileTypeUnknown)␊ |
413 | ␉␉{␊ |
414 | ␉␉␉return -1;␊ |
415 | ␉␉}␊ |
416 | ␉}␊ |
417 | ␊ |
418 | ␉GetCatalogEntry(dirIndex, name, flags, time, finderInfo, infoValid);␊ |
419 | ␊ |
420 | ␉if (*dirIndex == 0)␊ |
421 | ␉{␊ |
422 | ␉␉*dirIndex = -1;␊ |
423 | ␉}␊ |
424 | ␊ |
425 | ␉if ((*flags & kFileTypeMask) == kFileTypeUnknown)␊ |
426 | ␉{␊ |
427 | ␉␉return -1;␊ |
428 | ␉}␊ |
429 | ␊ |
430 | ␉return 0;␊ |
431 | }␊ |
432 | ␊ |
433 | //==============================================================================␊ |
434 | ␊ |
435 | void HFSGetDescription(CICell ih, char *str, long strMaxLen)␊ |
436 | {␊ |
437 | ␉long long dirIndex = 0;␊ |
438 | ␉char *name;␊ |
439 | ␉long flags;␊ |
440 | ␉u_int32_t firstLeafNode;␊ |
441 | ␉u_int32_t time;␊ |
442 | ␉u_int16_t nodeSize;␊ |
443 | ␊ |
444 | ␉if (HFSInitPartition(ih) == -1)␊ |
445 | ␉{␊ |
446 | ␉␉return;␊ |
447 | ␉}␊ |
448 | ␊ |
449 | ␉/* Fill some crucial data structures by side effect. */␊ |
450 | ␉HFSGetDirEntry(ih, "/", &dirIndex, &name, &flags, &time, 0, 0);␊ |
451 | ␊ |
452 | ␉/* Now we can loook up the volume name node. */␊ |
453 | ␉nodeSize = SWAP_BE16(gBTHeaders[kBTreeCatalog]->nodeSize);␊ |
454 | ␉firstLeafNode = SWAP_BE32(gBTHeaders[kBTreeCatalog]->firstLeafNode);␊ |
455 | ␊ |
456 | ␉dirIndex = (long long)firstLeafNode * nodeSize;␊ |
457 | ␊ |
458 | ␉GetCatalogEntry(&dirIndex, &name, &flags, &time, 0, 0);␊ |
459 | ␊ |
460 | ␉strncpy(str, name, strMaxLen);␊ |
461 | ␉str[strMaxLen] = '\0';␊ |
462 | }␊ |
463 | ␊ |
464 | //==============================================================================␊ |
465 | ␊ |
466 | long HFSGetFileBlock(CICell ih, char *filePath, u_int64_t *firstBlock)␊ |
467 | {␊ |
468 | ␉char entry[512];␊ |
469 | ␉long result, flags;␊ |
470 | ␉u_int32_t dirID;␊ |
471 | ␉void *extents;␊ |
472 | ␉HFSCatalogFile *hfsFile = (void *)entry;␊ |
473 | ␉HFSPlusCatalogFile *hfsPlusFile = (void *)entry;␊ |
474 | ␊ |
475 | ␉if (HFSInitPartition(ih) == -1)␊ |
476 | ␉{␊ |
477 | ␉␉return -1;␊ |
478 | ␉}␊ |
479 | ␊ |
480 | ␉dirID = kHFSRootFolderID;␊ |
481 | ␉// Skip a lead '/'. Start in the system folder if there are two.␊ |
482 | ␉if (filePath[0] == '/')␊ |
483 | ␉{␊ |
484 | ␉␉if (filePath[1] == '/')␊ |
485 | ␉␉{␊ |
486 | ␉␉␉if (gIsHFSPlus)␊ |
487 | ␉␉␉{␊ |
488 | ␉␉␉␉dirID = SWAP_BE32(((long *) gHFSPlus->finderInfo)[5]);␊ |
489 | ␉␉␉}␊ |
490 | ␉␉␉else␊ |
491 | ␉␉␉{␊ |
492 | ␉␉␉␉dirID = SWAP_BE32(gHFSMDB->drFndrInfo[5]);␊ |
493 | ␉␉␉}␊ |
494 | ␊ |
495 | ␉␉␉if (dirID == 0)␊ |
496 | ␉␉␉{␊ |
497 | ␉␉␉␉return -1;␊ |
498 | ␉␉␉}␊ |
499 | ␉␉␉filePath++;␊ |
500 | ␉␉}␊ |
501 | ␉␉filePath++;␊ |
502 | ␉}␊ |
503 | ␊ |
504 | ␉result = ResolvePathToCatalogEntry(filePath, &flags, entry, dirID, 0);␊ |
505 | ␊ |
506 | ␉if ((result == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
507 | ␉{␊ |
508 | ␉␉printf("HFS: Resolve path '%s' failed\n", filePath);␊ |
509 | ␉␉return -1;␊ |
510 | ␉}␊ |
511 | ␊ |
512 | ␉if (gIsHFSPlus) {␊ |
513 | ␉␉extents = &hfsPlusFile->dataFork.extents;␊ |
514 | ␉} else {␊ |
515 | ␉␉extents = &hfsFile->dataExtents;␊ |
516 | ␉}␊ |
517 | ␊ |
518 | #if DEBUG␊ |
519 | ␉printf("extent start 0x%x\n", GetExtentStart(extents, 0));␊ |
520 | ␉printf("block size 0x%x\n", gBlockSize);␊ |
521 | ␉printf("Allocation offset 0x%x\n", (unsigned long)gAllocationOffset);␊ |
522 | #endif␊ |
523 | ␉*firstBlock = ((u_int64_t) GetExtentStart(extents, 0) * (u_int64_t) gBlockSize + gAllocationOffset) / 512ULL;␊ |
524 | ␉return 0;␊ |
525 | }␊ |
526 | ␊ |
527 | ␊ |
528 | //==============================================================================␊ |
529 | ␊ |
530 | long HFSGetUUID(CICell ih, char *uuidStr)␊ |
531 | {␊ |
532 | ␉if (HFSInitPartition(ih) == -1)␊ |
533 | ␉{␊ |
534 | ␉␉return -1;␊ |
535 | ␉}␊ |
536 | ␊ |
537 | ␉if (gVolID == 0LL)␊ |
538 | ␉{␊ |
539 | ␉␉return -1;␊ |
540 | ␉}␊ |
541 | ␊ |
542 | ␉return CreateUUIDString((uint8_t*)(&gVolID), sizeof(gVolID), uuidStr);␊ |
543 | }␊ |
544 | ␊ |
545 | //==============================================================================␊ |
546 | // Private Functions␊ |
547 | ␊ |
548 | static long ReadFile(void * file, u_int64_t * length, void * base, u_int64_t offset)␊ |
549 | {␊ |
550 | ␉u_int64_t fileLength;␊ |
551 | ␉void *extents;␊ |
552 | ␉HFSCatalogFile *hfsFile = file;␊ |
553 | ␉HFSPlusCatalogFile *hfsPlusFile = file;␊ |
554 | ␉u_int32_t fileID;␊ |
555 | ␊ |
556 | ␉if (gIsHFSPlus)␊ |
557 | ␉{␊ |
558 | ␉␉fileID = SWAP_BE32(hfsPlusFile->fileID);␊ |
559 | ␉␉fileLength = (uint64_t)SWAP_BE64(hfsPlusFile->dataFork.logicalSize);␊ |
560 | ␉␉extents = &hfsPlusFile->dataFork.extents;␊ |
561 | ␉}␊ |
562 | ␉else␊ |
563 | ␉{␊ |
564 | ␉␉fileID = SWAP_BE32(hfsFile->fileID);␊ |
565 | ␉␉fileLength = SWAP_BE32(hfsFile->dataLogicalSize);␊ |
566 | ␉␉extents = &hfsFile->dataExtents;␊ |
567 | ␉}␊ |
568 | ␊ |
569 | ␉if (offset > fileLength)␊ |
570 | ␉{␊ |
571 | ␉␉printf("ReadFile(HFS%c): Offset is too large.\n", gIsHFSPlus ? "+" : "");␊ |
572 | ␊ |
573 | ␉␉return -1;␊ |
574 | ␉}␊ |
575 | ␊ |
576 | ␉if ((*length == 0) || ((offset + *length) > fileLength))␊ |
577 | ␉{␊ |
578 | ␉␉*length = fileLength - offset;␊ |
579 | ␉}␊ |
580 | ␊ |
581 | /*␊ |
582 | ␉if (*length > kLoadSize)␊ |
583 | ␉{␊ |
584 | ␉␉printf("File is too large.\n");␊ |
585 | ␉␉return -1;␊ |
586 | ␉}␊ |
587 | */␊ |
588 | ␊ |
589 | ␉*length = ReadExtent((char *)extents, fileLength, fileID, offset, *length, (char *)base, 0);␊ |
590 | ␊ |
591 | ␉return 0;␊ |
592 | }␊ |
593 | ␊ |
594 | //==============================================================================␊ |
595 | ␊ |
596 | static long GetCatalogEntryInfo(void * entry, long * flags, u_int32_t * time, FinderInfo * finderInfo, long * infoValid)␊ |
597 | {␊ |
598 | ␉u_int32_t tmpTime = 0;␊ |
599 | ␉long valid = 0;␊ |
600 | ␊ |
601 | ␉// Get information about the file.␊ |
602 | ␊ |
603 | ␉switch ( SWAP_BE16(*(short *)entry) )␊ |
604 | ␉{␊ |
605 | ␉case kHFSFolderRecord :␊ |
606 | ␉␉*flags = kFileTypeDirectory;␊ |
607 | ␉␉tmpTime = SWAP_BE32(((HFSCatalogFolder *)entry)->modifyDate);␊ |
608 | ␉␉break;␊ |
609 | ␊ |
610 | ␉case kHFSPlusFolderRecord :␊ |
611 | ␉␉*flags = kFileTypeDirectory | (SWAP_BE16(((HFSPlusCatalogFolder *)entry)->bsdInfo.fileMode) & kPermMask);␊ |
612 | ␉␉if (SWAP_BE32(((HFSPlusCatalogFolder *)entry)->bsdInfo.ownerID) != 0)␊ |
613 | ␉␉{␊ |
614 | ␉␉␉*flags |= kOwnerNotRoot;␊ |
615 | ␉␉}␊ |
616 | ␉␉tmpTime = SWAP_BE32(((HFSPlusCatalogFolder *)entry)->contentModDate);␊ |
617 | ␉␉break;␊ |
618 | ␊ |
619 | ␉case kHFSFileRecord :␊ |
620 | ␉␉*flags = kFileTypeFlat;␊ |
621 | ␉␉tmpTime = SWAP_BE32(((HFSCatalogFile *)entry)->modifyDate);␊ |
622 | ␉␉if (finderInfo)␊ |
623 | ␉␉{␊ |
624 | ␉␉␉SwapFinderInfo((FndrFileInfo *)finderInfo, &((HFSCatalogFile *)entry)->userInfo);␊ |
625 | ␉␉␉valid = 1;␊ |
626 | ␉␉}␊ |
627 | ␉␉break;␊ |
628 | ␊ |
629 | ␉case kHFSPlusFileRecord :␊ |
630 | ␉␉*flags = kFileTypeFlat | (SWAP_BE16(((HFSPlusCatalogFile *)entry)->bsdInfo.fileMode) & kPermMask);␊ |
631 | ␉␉if (SWAP_BE32(((HFSPlusCatalogFile *)entry)->bsdInfo.ownerID) != 0)␊ |
632 | ␉␉{␊ |
633 | ␉␉␉*flags |= kOwnerNotRoot;␊ |
634 | ␉␉}␊ |
635 | ␉␉tmpTime = SWAP_BE32(((HFSPlusCatalogFile *)entry)->contentModDate);␊ |
636 | ␉␉if (finderInfo)␊ |
637 | ␉␉{␊ |
638 | ␉␉␉SwapFinderInfo((FndrFileInfo *)finderInfo, &((HFSPlusCatalogFile *)entry)->userInfo);␊ |
639 | ␉␉␉valid = 1;␊ |
640 | ␉␉}␊ |
641 | ␉␉break;␊ |
642 | ␊ |
643 | ␉case kHFSFileThreadRecord :␊ |
644 | ␉case kHFSPlusFileThreadRecord :␊ |
645 | ␉case kHFSFolderThreadRecord :␊ |
646 | ␉case kHFSPlusFolderThreadRecord :␊ |
647 | ␉␉*flags = kFileTypeUnknown;␊ |
648 | ␉␉tmpTime = 0;␊ |
649 | ␉␉break;␊ |
650 | ␉}␊ |
651 | ␊ |
652 | ␉if (time != 0)␊ |
653 | ␉{␊ |
654 | ␉␉// Convert base time from 1904 to 1970.␊ |
655 | ␉␉*time = tmpTime - 2082844800;␊ |
656 | ␉}␊ |
657 | ␊ |
658 | ␉if (infoValid)␊ |
659 | ␉{␊ |
660 | ␉␉*infoValid = valid;␊ |
661 | ␉}␊ |
662 | ␊ |
663 | ␉return 0;␊ |
664 | }␊ |
665 | ␊ |
666 | //==============================================================================␊ |
667 | ␊ |
668 | static long ResolvePathToCatalogEntry(char * filePath, long * flags, void * entry, u_int32_t dirID, long long * dirIndex)␊ |
669 | {␊ |
670 | ␉char␉␉␉*restPath;␊ |
671 | ␉long␉␉␉result, cnt;␊ |
672 | ␉u_int32_t␉␉subFolderID = 0;␊ |
673 | ␉long long␉␉tmpDirIndex;␊ |
674 | ␉HFSPlusCatalogFile␉*hfsPlusFile;␊ |
675 | ␊ |
676 | ␉// Copy the file name to gTempStr␊ |
677 | ␉cnt = 0;␊ |
678 | ␉while ((filePath[cnt] != '/') && (filePath[cnt] != '\0'))␊ |
679 | ␉{␊ |
680 | ␉␉cnt++;␊ |
681 | ␉}␊ |
682 | ␊ |
683 | ␉strlcpy(gTempStr, filePath, cnt+1);␊ |
684 | ␊ |
685 | ␉// Move restPath to the right place.␊ |
686 | ␉if (filePath[cnt] != '\0')␊ |
687 | ␉{␊ |
688 | ␉␉cnt++;␊ |
689 | ␉}␊ |
690 | ␊ |
691 | ␉restPath = filePath + cnt;␊ |
692 | ␊ |
693 | ␉// gTempStr is a name in the current Dir.␊ |
694 | ␉// restPath is the rest of the path if any.␊ |
695 | ␊ |
696 | ␉result = ReadCatalogEntry(gTempStr, dirID, entry, dirIndex);␊ |
697 | ␊ |
698 | ␉if (result == -1)␊ |
699 | ␉{␊ |
700 | ␉␉return -1;␊ |
701 | ␉}␊ |
702 | ␊ |
703 | ␉GetCatalogEntryInfo(entry, flags, 0, 0, 0);␊ |
704 | ␊ |
705 | ␉if ((*flags & kFileTypeMask) == kFileTypeDirectory)␊ |
706 | ␉{␊ |
707 | ␉␉if (gIsHFSPlus)␊ |
708 | ␉␉{␊ |
709 | ␉␉␉subFolderID = SWAP_BE32(((HFSPlusCatalogFolder *)entry)->folderID);␊ |
710 | ␉␉}␊ |
711 | ␉␉else␊ |
712 | ␉␉{␊ |
713 | ␉␉␉subFolderID = SWAP_BE32(((HFSCatalogFolder *)entry)->folderID);␊ |
714 | ␉␉}␊ |
715 | ␉}␊ |
716 | ␊ |
717 | ␉if ((*flags & kFileTypeMask) == kFileTypeDirectory)␊ |
718 | ␉{␊ |
719 | ␉␉result = ResolvePathToCatalogEntry(restPath, flags, entry, subFolderID, dirIndex);␊ |
720 | ␉}␊ |
721 | ␊ |
722 | ␉if (gIsHFSPlus && ((*flags & kFileTypeMask) == kFileTypeFlat))␊ |
723 | ␉{␊ |
724 | ␉␉hfsPlusFile = (HFSPlusCatalogFile *)entry;␊ |
725 | ␊ |
726 | ␉␉if ((SWAP_BE32(hfsPlusFile->userInfo.fdType) == kHardLinkFileType) && (SWAP_BE32(hfsPlusFile->userInfo.fdCreator) == kHFSPlusCreator))␊ |
727 | ␉␉{␊ |
728 | ␉␉␉sprintf(gLinkTemp, "%s/%s%ld", HFSPLUSMETADATAFOLDER, HFS_INODE_PREFIX, SWAP_BE32(hfsPlusFile->bsdInfo.special.iNodeNum));␊ |
729 | ␉␉␉result = ResolvePathToCatalogEntry(gLinkTemp, flags, entry, kHFSRootFolderID, &tmpDirIndex);␊ |
730 | ␉␉}␊ |
731 | ␉}␊ |
732 | ␊ |
733 | ␉return result;␊ |
734 | }␊ |
735 | ␊ |
736 | //==============================================================================␊ |
737 | ␊ |
738 | static long GetCatalogEntry(long long * dirIndex, char ** name, long * flags, u_int32_t * time, FinderInfo * finderInfo, long * infoValid)␊ |
739 | {␊ |
740 | ␉u_int64_t extentSize;␊ |
741 | ␉void *extent;␊ |
742 | ␉char *nodeBuf, *testKey, *entry;␊ |
743 | ␉BTNodeDescriptor *node;␊ |
744 | ␉u_int32_t curNode;␊ |
745 | ␉u_int16_t nodeSize, index;␊ |
746 | ␊ |
747 | ␉if (gIsHFSPlus)␊ |
748 | ␉{␊ |
749 | ␉␉extent = &gHFSPlus->catalogFile.extents;␊ |
750 | ␉␉extentSize = SWAP_BE64(gHFSPlus->catalogFile.logicalSize);␊ |
751 | ␉}␊ |
752 | ␉else␊ |
753 | ␉{␊ |
754 | ␉␉extent = (HFSExtentDescriptor *)&gHFSMDB->drCTExtRec;␊ |
755 | ␉␉extentSize = SWAP_BE32(gHFSMDB->drCTFlSize);␊ |
756 | ␉}␊ |
757 | ␊ |
758 | ␉nodeSize = SWAP_BE16(gBTHeaders[kBTreeCatalog]->nodeSize);␊ |
759 | ␉nodeBuf = (char *)malloc(nodeSize);␊ |
760 | ␉node = (BTNodeDescriptor *)nodeBuf;␊ |
761 | ␊ |
762 | ␉index = (u_int16_t) (*dirIndex % nodeSize);␊ |
763 | ␉curNode = (u_int32_t) (*dirIndex / nodeSize);␊ |
764 | ␊ |
765 | ␉// Read the BTree node and get the record for index.␊ |
766 | ␉ReadExtent(extent, extentSize, kHFSCatalogFileID, (long long) curNode * nodeSize, nodeSize, nodeBuf, 1);␊ |
767 | ␊ |
768 | ␉GetBTreeRecord(index, nodeBuf, nodeSize, &testKey, &entry);␊ |
769 | ␊ |
770 | ␉GetCatalogEntryInfo(entry, flags, time, finderInfo, infoValid);␊ |
771 | ␊ |
772 | ␉// Get the file name.␊ |
773 | ␉if (gIsHFSPlus)␊ |
774 | ␉{␊ |
775 | ␉␉utf_encodestr(((HFSPlusCatalogKey *)testKey)->nodeName.unicode,␊ |
776 | SWAP_BE16(((HFSPlusCatalogKey *)testKey)->nodeName.length),␊ |
777 | (u_int8_t *)gTempStr, 256, OSBigEndian);␊ |
778 | ␉}␊ |
779 | ␉else␊ |
780 | ␉{␊ |
781 | ␉␉strncpy(gTempStr, (const char *)&((HFSCatalogKey *)testKey)->nodeName[1], ((HFSCatalogKey *)testKey)->nodeName[0]);␊ |
782 | ␊ |
783 | ␉␉gTempStr[((HFSCatalogKey *)testKey)->nodeName[0]] = '\0';␊ |
784 | ␉}␊ |
785 | ␉*name = gTempStr;␊ |
786 | ␊ |
787 | ␉// Update dirIndex.␊ |
788 | ␉index++;␊ |
789 | ␊ |
790 | ␉if (index == SWAP_BE16(node->numRecords))␊ |
791 | ␉{␊ |
792 | ␉␉index = 0;␊ |
793 | ␉␉curNode = SWAP_BE32(node->fLink);␊ |
794 | ␉}␊ |
795 | ␉*dirIndex = (long long) curNode * nodeSize + index;␊ |
796 | ␊ |
797 | ␉free(nodeBuf);␊ |
798 | ␊ |
799 | ␉return 0;␊ |
800 | }␊ |
801 | ␊ |
802 | //==============================================================================␊ |
803 | ␊ |
804 | static long ReadCatalogEntry(char * fileName, u_int32_t dirID, void * entry, long long * dirIndex)␊ |
805 | {␊ |
806 | ␉long length = strlen(fileName);␊ |
807 | ␉char key[sizeof(HFSPlusCatalogKey)];␊ |
808 | ␉HFSCatalogKey *hfsKey = (HFSCatalogKey *)key;␊ |
809 | ␉HFSPlusCatalogKey *hfsPlusKey = (HFSPlusCatalogKey *)key;␊ |
810 | ␊ |
811 | ␉// Make the catalog key.␊ |
812 | ␉if ( gIsHFSPlus )␊ |
813 | ␉{␊ |
814 | ␉␉if (length > kHFSPlusMaxFileNameChars)␊ |
815 | ␉␉{␊ |
816 | ␉␉␉length = kHFSPlusMaxFileNameChars;␊ |
817 | ␉␉}␊ |
818 | ␊ |
819 | ␉␉hfsPlusKey->parentID = SWAP_BE32(dirID);␊ |
820 | ␊ |
821 | ␉␉utf_decodestr((u_int8_t *)fileName, hfsPlusKey->nodeName.unicode, &(hfsPlusKey->nodeName.length), 512, OSBigEndian);␊ |
822 | ␉}␊ |
823 | ␉else␊ |
824 | ␉{␊ |
825 | ␉␉if (length > kHFSMaxFileNameChars)␊ |
826 | ␉␉{␊ |
827 | ␉␉␉length = kHFSMaxFileNameChars;␊ |
828 | ␉␉}␊ |
829 | ␊ |
830 | ␉␉hfsKey->parentID = SWAP_BE32(dirID);␊ |
831 | ␊ |
832 | ␉␉hfsKey->nodeName[0] = length;␊ |
833 | ␉␉strncpy((char *)(hfsKey->nodeName + 1), fileName, length);␊ |
834 | ␉}␊ |
835 | ␊ |
836 | ␉return ReadBTreeEntry(kBTreeCatalog, &key, entry, dirIndex);␊ |
837 | }␊ |
838 | ␊ |
839 | //==============================================================================␊ |
840 | ␊ |
841 | static long ReadExtentsEntry(u_int32_t fileID, long startBlock, void * entry)␊ |
842 | {␊ |
843 | ␉char key[sizeof(HFSPlusExtentKey)];␊ |
844 | ␉HFSExtentKey *hfsKey = (HFSExtentKey *)key;␊ |
845 | ␉HFSPlusExtentKey *hfsPlusKey = (HFSPlusExtentKey *)key;␊ |
846 | ␊ |
847 | ␉// Make the extents key.␊ |
848 | ␉if (gIsHFSPlus)␊ |
849 | ␉{␊ |
850 | ␉␉hfsPlusKey->forkType = 0;␊ |
851 | ␉␉hfsPlusKey->fileID = SWAP_BE32(fileID);␊ |
852 | ␉␉hfsPlusKey->startBlock = SWAP_BE32(startBlock);␊ |
853 | ␉}␊ |
854 | ␉else␊ |
855 | ␉{␊ |
856 | ␉␉hfsKey->forkType = 0;␊ |
857 | ␉␉hfsKey->fileID = SWAP_BE32(fileID);␊ |
858 | ␉␉hfsKey->startBlock = SWAP_BE16(startBlock);␊ |
859 | ␉}␊ |
860 | ␊ |
861 | ␉return ReadBTreeEntry(kBTreeExtents, &key, entry, 0);␊ |
862 | }␊ |
863 | ␊ |
864 | //==============================================================================␊ |
865 | ␊ |
866 | static long ReadBTreeEntry(long btree, void * key, char * entry, long long * dirIndex)␊ |
867 | {␊ |
868 | ␉u_int64_t extentSize;␊ |
869 | ␉void *extent;␊ |
870 | ␉u_int16_t extentFile;␊ |
871 | ␉char *nodeBuf;␊ |
872 | ␉BTNodeDescriptor *node;␊ |
873 | ␉long result = 0, entrySize = 0;␊ |
874 | ␉u_int32_t curNode;␊ |
875 | ␉char *testKey, *recordData;␊ |
876 | ␉u_int16_t nodeSize, index = 0, lowerBound, upperBound;␊ |
877 | ␊ |
878 | ␉// Figure out which tree is being looked at.␊ |
879 | ␉if (btree == kBTreeCatalog)␊ |
880 | ␉{␊ |
881 | ␉␉if (gIsHFSPlus) ␊ |
882 | ␉␉{␊ |
883 | ␉␉␉extent = &gHFSPlus->catalogFile.extents;␊ |
884 | ␉␉␉extentSize = SWAP_BE64(gHFSPlus->catalogFile.logicalSize);␊ |
885 | ␉␉}␊ |
886 | ␉␉else␊ |
887 | ␉␉{␊ |
888 | ␉␉␉extent = (HFSExtentDescriptor *)&gHFSMDB->drCTExtRec;␊ |
889 | ␉␉␉extentSize = SWAP_BE32(gHFSMDB->drCTFlSize);␊ |
890 | ␉␉}␊ |
891 | ␉␉extentFile = kHFSCatalogFileID;␊ |
892 | ␉}␊ |
893 | ␉else␊ |
894 | ␉{␊ |
895 | ␉␉if (gIsHFSPlus)␊ |
896 | ␉␉{␊ |
897 | ␉␉␉extent = &gHFSPlus->extentsFile.extents;␊ |
898 | ␉␉␉extentSize = SWAP_BE64(gHFSPlus->extentsFile.logicalSize);␊ |
899 | ␉␉}␊ |
900 | ␉␉else␊ |
901 | ␉␉{␊ |
902 | ␉␉␉extent = (HFSExtentDescriptor *)&gHFSMDB->drXTExtRec;␊ |
903 | ␉␉␉extentSize = SWAP_BE32(gHFSMDB->drXTFlSize);␊ |
904 | ␉␉}␊ |
905 | ␉␉extentFile = kHFSExtentsFileID;␊ |
906 | ␉}␊ |
907 | ␊ |
908 | ␉// Read the BTree Header if needed.␊ |
909 | ␉if (gBTHeaders[btree] == 0)␊ |
910 | ␉{␊ |
911 | ␉␉ReadExtent(extent, extentSize, extentFile, 0, 256, gBTreeHeaderBuffer + btree * 256, 0);␊ |
912 | ␉␉gBTHeaders[btree] = (BTHeaderRec *)(gBTreeHeaderBuffer + btree * 256 + sizeof(BTNodeDescriptor));␊ |
913 | ␊ |
914 | ␉␉if ((gIsHFSPlus && btree == kBTreeCatalog) && (gBTHeaders[btree]->keyCompareType == kHFSBinaryCompare))␊ |
915 | ␉␉{␊ |
916 | ␉␉␉gCaseSensitive = 1;␊ |
917 | ␉␉}␊ |
918 | ␉}␊ |
919 | ␊ |
920 | ␉curNode = SWAP_BE32(gBTHeaders[btree]->rootNode);␊ |
921 | ␉nodeSize = SWAP_BE16(gBTHeaders[btree]->nodeSize);␊ |
922 | ␉nodeBuf = (char *)malloc(nodeSize);␊ |
923 | ␉node = (BTNodeDescriptor *)nodeBuf;␊ |
924 | ␊ |
925 | ␉while (1)␊ |
926 | ␉{␊ |
927 | ␉␉// Read the current node.␊ |
928 | ␉ReadExtent(extent, extentSize, extentFile, (long long) curNode * nodeSize, nodeSize, nodeBuf, 1);␊ |
929 | ␊ |
930 | ␉␉// Find the matching key.␊ |
931 | ␉␉lowerBound = 0;␊ |
932 | ␉␉upperBound = SWAP_BE16(node->numRecords) - 1;␊ |
933 | ␊ |
934 | ␉␉while (lowerBound <= upperBound)␊ |
935 | ␉␉{␊ |
936 | ␉␉␉index = (lowerBound + upperBound) / 2;␊ |
937 | ␊ |
938 | ␉␉␉GetBTreeRecord(index, nodeBuf, nodeSize, &testKey, &recordData);␊ |
939 | ␊ |
940 | ␉␉␉if (gIsHFSPlus)␊ |
941 | ␉␉␉{␊ |
942 | ␉␉␉␉if (btree == kBTreeCatalog)␊ |
943 | ␉␉␉␉{␊ |
944 | ␉␉␉␉␉result = CompareHFSPlusCatalogKeys(key, testKey);␊ |
945 | ␉␉␉␉}␊ |
946 | ␉␉␉␉else␊ |
947 | ␉␉␉␉{␊ |
948 | ␉␉␉␉␉result = CompareHFSPlusExtentsKeys(key, testKey);␊ |
949 | ␉␉␉␉}␊ |
950 | ␉␉␉}␊ |
951 | ␉␉␉else␊ |
952 | ␉␉␉{␊ |
953 | ␉␉␉␉if (btree == kBTreeCatalog)␊ |
954 | ␉␉␉␉{␊ |
955 | ␉␉␉␉␉result = CompareHFSCatalogKeys(key, testKey);␊ |
956 | ␉␉␉␉}␊ |
957 | ␉␉␉␉else␊ |
958 | ␉␉␉␉{␊ |
959 | ␉␉␉␉␉result = CompareHFSExtentsKeys(key, testKey);␊ |
960 | ␉␉␉␉}␊ |
961 | ␉␉␉}␊ |
962 | ␊ |
963 | ␉␉␉if (result < 0)␊ |
964 | ␉␉␉{␊ |
965 | ␉␉␉␉upperBound = index - 1;␉// search < trial␊ |
966 | ␉␉␉}␊ |
967 | ␉␉␉else if (result > 0)␊ |
968 | ␉␉␉{␊ |
969 | ␉␉␉␉lowerBound = index + 1;␉// search > trial␊ |
970 | ␉␉␉}␊ |
971 | ␉␉␉else␊ |
972 | ␉␉␉{␊ |
973 | ␉␉␉␉break;␉␉␉// search = trial␊ |
974 | ␉␉␉}␊ |
975 | ␉␉}␊ |
976 | ␊ |
977 | ␉␉if (result < 0)␊ |
978 | ␉␉{␊ |
979 | ␉␉␉index = upperBound;␊ |
980 | ␉␉␉GetBTreeRecord(index, nodeBuf, nodeSize, &testKey, &recordData);␊ |
981 | ␉␉}␊ |
982 | ␊ |
983 | ␉␉// Found the closest key... Recurse on it if this is an index node.␊ |
984 | ␉␉if (node->kind == kBTIndexNode)␊ |
985 | ␉␉{␊ |
986 | ␉␉␉curNode = SWAP_BE32( *((long *)recordData) );␊ |
987 | ␉␉}␊ |
988 | ␉␉else␊ |
989 | ␉␉{␊ |
990 | ␉␉␉break;␊ |
991 | ␉␉}␊ |
992 | ␉}␊ |
993 | ␊ |
994 | ␉// Return error if the file was not found.␊ |
995 | ␉if (result != 0)␊ |
996 | ␉{␊ |
997 | ␉␉free(nodeBuf);␊ |
998 | ␉␉return -1;␊ |
999 | ␉}␊ |
1000 | ␊ |
1001 | ␉if (btree == kBTreeCatalog)␊ |
1002 | ␉{␊ |
1003 | ␉␉switch (SWAP_BE16(*(short *)recordData))␊ |
1004 | ␉␉{␊ |
1005 | ␉␉␉case kHFSFolderRecord : entrySize = 70;␊ |
1006 | ␉␉␉␉break;␊ |
1007 | ␉␉␉case kHFSFileRecord : entrySize = 102;␊ |
1008 | ␉␉␉␉break;␊ |
1009 | ␉␉␉case kHFSFolderThreadRecord : entrySize = 46;␊ |
1010 | ␉␉␉␉break;␊ |
1011 | ␉␉␉case kHFSFileThreadRecord : entrySize = 46;␊ |
1012 | ␉␉␉␉break;␊ |
1013 | ␉␉␉case kHFSPlusFolderRecord : entrySize = 88;␊ |
1014 | ␉␉␉␉break;␊ |
1015 | ␉␉␉case kHFSPlusFileRecord : entrySize = 248;␊ |
1016 | ␉␉␉␉break;␊ |
1017 | ␉␉␉case kHFSPlusFolderThreadRecord : entrySize = 264;␊ |
1018 | ␉␉␉␉break;␊ |
1019 | ␉␉␉case kHFSPlusFileThreadRecord : entrySize = 264;␊ |
1020 | ␉␉␉␉break;␊ |
1021 | ␉␉}␊ |
1022 | ␉}␊ |
1023 | ␉else␊ |
1024 | ␉{␊ |
1025 | ␉␉if (gIsHFSPlus)␊ |
1026 | ␉␉{␊ |
1027 | ␉␉␉entrySize = sizeof(HFSPlusExtentRecord);␊ |
1028 | ␉␉}␊ |
1029 | ␉␉else␊ |
1030 | ␉␉{␊ |
1031 | ␉␉␉entrySize = sizeof(HFSExtentRecord);␊ |
1032 | ␉␉}␊ |
1033 | ␉}␊ |
1034 | ␊ |
1035 | ␉bcopy(recordData, entry, entrySize);␊ |
1036 | ␊ |
1037 | ␉// Update dirIndex.␊ |
1038 | ␉if (dirIndex != 0)␊ |
1039 | ␉{␊ |
1040 | ␉␉index++;␊ |
1041 | ␉␉if (index == SWAP_BE16(node->numRecords))␊ |
1042 | ␉␉{␊ |
1043 | ␉␉␉index = 0;␊ |
1044 | ␉␉␉curNode = SWAP_BE32(node->fLink);␊ |
1045 | ␉␉}␊ |
1046 | ␉␉*dirIndex = (long long) curNode * nodeSize + index;␊ |
1047 | ␉}␊ |
1048 | ␊ |
1049 | ␉free(nodeBuf);␊ |
1050 | ␊ |
1051 | ␉return 0;␊ |
1052 | }␊ |
1053 | ␊ |
1054 | //==============================================================================␊ |
1055 | ␊ |
1056 | static void GetBTreeRecord(u_int16_t index, char * nodeBuffer, u_int16_t nodeSize, char ** key, char ** data)␊ |
1057 | {␊ |
1058 | ␉u_int16_t recordOffset, keySize;␊ |
1059 | ␊ |
1060 | ␉recordOffset = SWAP_BE16(*((u_int16_t *)(nodeBuffer + (nodeSize - 2 * index - 2))));␊ |
1061 | ␉*key = nodeBuffer + recordOffset;␊ |
1062 | ␊ |
1063 | ␉if (gIsHFSPlus)␊ |
1064 | ␉{␊ |
1065 | ␉␉keySize = SWAP_BE16(*(u_int16_t *)*key);␊ |
1066 | ␉␉*data = *key + 2 + keySize;␊ |
1067 | ␉}␊ |
1068 | ␉else␊ |
1069 | ␉{␊ |
1070 | ␉␉keySize = **key;␊ |
1071 | ␉␉*data = *key + 2 + keySize - (keySize & 1);␊ |
1072 | ␉}␊ |
1073 | }␊ |
1074 | ␊ |
1075 | //==============================================================================␊ |
1076 | ␊ |
1077 | static long ReadExtent(char * extent, u_int64_t extentSize, u_int32_t extentFile, u_int64_t offset, u_int64_t size, void * buffer, long cache)␊ |
1078 | {␊ |
1079 | ␉u_int64_t lastOffset;␊ |
1080 | ␉u_int64_t blockNumber, countedBlocks = 0;␊ |
1081 | ␉u_int64_t nextExtent = 0, sizeRead = 0, readSize;␊ |
1082 | ␉u_int64_t nextExtentBlock, currentExtentBlock = 0;␊ |
1083 | ␉u_int64_t readOffset;␊ |
1084 | ␉u_int64_t extentDensity, sizeofExtent, currentExtentSize;␊ |
1085 | ␉char *currentExtent, *extentBuffer = 0, *bufferPos = buffer;␊ |
1086 | ␊ |
1087 | ␉if (offset >= extentSize)␊ |
1088 | ␉{␊ |
1089 | ␉␉return 0;␊ |
1090 | ␉}␊ |
1091 | ␊ |
1092 | ␉if (gIsHFSPlus)␊ |
1093 | ␉{␊ |
1094 | ␉␉extentDensity = kHFSPlusExtentDensity;␊ |
1095 | ␉␉sizeofExtent = sizeof(HFSPlusExtentDescriptor);␊ |
1096 | ␉}␊ |
1097 | ␉else␊ |
1098 | ␉{␊ |
1099 | ␉␉extentDensity = kHFSExtentDensity;␊ |
1100 | ␉␉sizeofExtent = sizeof(HFSExtentDescriptor);␊ |
1101 | ␉}␊ |
1102 | ␊ |
1103 | ␉lastOffset = offset + size;␊ |
1104 | ␊ |
1105 | ␉while (offset < lastOffset)␊ |
1106 | ␉{␊ |
1107 | ␉␉blockNumber = offset / gBlockSize;␊ |
1108 | ␊ |
1109 | ␉␉// Find the extent for the offset.␊ |
1110 | ␉␉for (; ; nextExtent++)␊ |
1111 | ␉␉{␊ |
1112 | ␉␉␉if (nextExtent < extentDensity)␊ |
1113 | ␉␉␉{␊ |
1114 | ␉␉␉␉if ((countedBlocks + GetExtentSize(extent, nextExtent) -1) < blockNumber)␊ |
1115 | ␉␉␉␉{␊ |
1116 | ␉␉␉␉␉countedBlocks += GetExtentSize(extent, nextExtent);␊ |
1117 | ␉␉␉␉␉continue;␊ |
1118 | ␉␉␉␉}␊ |
1119 | ␊ |
1120 | ␉␉␉␉currentExtent = extent + nextExtent * sizeofExtent;␊ |
1121 | ␉␉␉␉break;␊ |
1122 | ␉␉␉}␊ |
1123 | ␊ |
1124 | ␉␉␉if (extentBuffer == 0)␊ |
1125 | ␉␉␉{␊ |
1126 | ␉␉␉␉extentBuffer = malloc(sizeofExtent * extentDensity);␊ |
1127 | ␊ |
1128 | ␉␉␉␉if (extentBuffer == 0)␊ |
1129 | ␉␉␉␉{␊ |
1130 | ␉␉␉␉␉return -1;␊ |
1131 | ␉␉␉␉}␊ |
1132 | ␉␉␉}␊ |
1133 | ␊ |
1134 | ␉␉␉nextExtentBlock = nextExtent / extentDensity;␊ |
1135 | ␊ |
1136 | ␉␉␉if (currentExtentBlock != nextExtentBlock)␊ |
1137 | ␉␉␉{␊ |
1138 | ␉␉␉␉ReadExtentsEntry(extentFile, countedBlocks, extentBuffer);␊ |
1139 | ␉␉␉␉currentExtentBlock = nextExtentBlock;␊ |
1140 | ␉␉␉}␊ |
1141 | ␊ |
1142 | ␉␉␉currentExtentSize = GetExtentSize(extentBuffer, nextExtent % extentDensity);␊ |
1143 | ␊ |
1144 | ␉␉␉if ((countedBlocks + currentExtentSize - 1) >= blockNumber)␊ |
1145 | ␉␉␉{␊ |
1146 | ␉␉␉␉currentExtent = extentBuffer + sizeofExtent * (nextExtent % extentDensity);␊ |
1147 | ␉␉␉␉break;␊ |
1148 | ␉␉␉}␊ |
1149 | ␊ |
1150 | ␉␉␉countedBlocks += currentExtentSize;␊ |
1151 | ␉␉}␊ |
1152 | ␊ |
1153 | ␉␉readOffset = ((blockNumber - countedBlocks) * gBlockSize) + (offset % gBlockSize);␊ |
1154 | ␊ |
1155 | ␉␉// MacWen: fix overflow in multiplication by forcing 64bit multiplication␊ |
1156 | ␉␉readSize = (long long)GetExtentSize(currentExtent, 0) * gBlockSize - readOffset;␊ |
1157 | ␊ |
1158 | ␉␉if (readSize > (size - sizeRead))␊ |
1159 | ␉␉{␊ |
1160 | ␉␉␉readSize = size - sizeRead;␊ |
1161 | ␉␉}␊ |
1162 | ␊ |
1163 | ␉␉readOffset += (long long)GetExtentStart(currentExtent, 0) * gBlockSize;␊ |
1164 | ␊ |
1165 | ␉␉CacheRead(gCurrentIH, bufferPos, gAllocationOffset + readOffset, readSize, cache);␊ |
1166 | ␊ |
1167 | ␉␉sizeRead += readSize;␊ |
1168 | ␉␉offset += readSize;␊ |
1169 | ␉␉bufferPos += readSize;␊ |
1170 | ␉}␊ |
1171 | ␊ |
1172 | ␉if (extentBuffer)␊ |
1173 | ␉{␊ |
1174 | ␉␉free(extentBuffer);␊ |
1175 | ␉}␊ |
1176 | ␊ |
1177 | ␉return sizeRead;␊ |
1178 | }␊ |
1179 | ␊ |
1180 | //==============================================================================␊ |
1181 | ␊ |
1182 | static u_int32_t GetExtentStart(void * extents, u_int32_t index)␊ |
1183 | {␊ |
1184 | ␉u_int32_t start;␊ |
1185 | ␊ |
1186 | ␉HFSExtentDescriptor␉␉*hfsExtents␉␉= extents;␊ |
1187 | ␉HFSPlusExtentDescriptor␉*hfsPlusExtents␉= extents;␊ |
1188 | ␊ |
1189 | ␉if (gIsHFSPlus)␊ |
1190 | ␉{␊ |
1191 | ␉␉start = SWAP_BE32(hfsPlusExtents[index].startBlock);␊ |
1192 | ␉}␊ |
1193 | ␉else␊ |
1194 | ␉{␊ |
1195 | ␉␉start = SWAP_BE16(hfsExtents[index].startBlock);␊ |
1196 | ␉}␊ |
1197 | ␊ |
1198 | ␉return start;␊ |
1199 | }␊ |
1200 | ␊ |
1201 | //==============================================================================␊ |
1202 | ␊ |
1203 | static u_int32_t GetExtentSize(void * extents, u_int32_t index)␊ |
1204 | {␊ |
1205 | ␉u_int32_t size = 0;␊ |
1206 | ␊ |
1207 | ␉HFSExtentDescriptor *hfsExtents = extents;␊ |
1208 | ␉HFSPlusExtentDescriptor *hfsPlusExtents = extents;␊ |
1209 | ␊ |
1210 | ␉if (gIsHFSPlus)␊ |
1211 | ␉{␊ |
1212 | ␉␉size = SWAP_BE32(hfsPlusExtents[index].blockCount);␊ |
1213 | ␉}␊ |
1214 | ␉else␊ |
1215 | ␉{␊ |
1216 | ␉␉size = SWAP_BE16(hfsExtents[index].blockCount);␊ |
1217 | ␉}␊ |
1218 | ␊ |
1219 | ␉return size;␊ |
1220 | }␊ |
1221 | ␊ |
1222 | //==============================================================================␊ |
1223 | ␊ |
1224 | static long CompareHFSCatalogKeys(void * key, void * testKey)␊ |
1225 | {␊ |
1226 | ␉HFSCatalogKey *searchKey, *trialKey;␊ |
1227 | ␉long result, searchParentID, trialParentID;␊ |
1228 | ␊ |
1229 | ␉searchKey = key;␊ |
1230 | ␉trialKey = testKey;␊ |
1231 | ␊ |
1232 | ␉searchParentID = SWAP_BE32(searchKey->parentID);␊ |
1233 | ␉trialParentID = SWAP_BE32(trialKey->parentID);␊ |
1234 | ␊ |
1235 | ␉// parent dirID is unsigned␊ |
1236 | ␉if (searchParentID > trialParentID)␊ |
1237 | ␉{␊ |
1238 | ␉␉result = 1;␊ |
1239 | ␉}␊ |
1240 | ␉else if (searchParentID < trialParentID)␊ |
1241 | ␉{␊ |
1242 | ␉␉result = -1;␊ |
1243 | ␉}␊ |
1244 | ␉else␊ |
1245 | ␉{␊ |
1246 | ␉␉// parent dirID's are equal, compare names␊ |
1247 | ␉␉result = FastRelString(searchKey->nodeName, trialKey->nodeName);␊ |
1248 | ␉}␊ |
1249 | ␊ |
1250 | ␉return result;␊ |
1251 | }␊ |
1252 | ␊ |
1253 | //==============================================================================␊ |
1254 | ␊ |
1255 | static long CompareHFSPlusCatalogKeys(void * key, void * testKey)␊ |
1256 | {␊ |
1257 | ␉HFSPlusCatalogKey *searchKey, *trialKey;␊ |
1258 | ␉long result, searchParentID, trialParentID;␊ |
1259 | ␊ |
1260 | ␉searchKey = key;␊ |
1261 | ␉trialKey = testKey;␊ |
1262 | ␊ |
1263 | ␉searchParentID = SWAP_BE32(searchKey->parentID);␊ |
1264 | ␉trialParentID = SWAP_BE32(trialKey->parentID);␊ |
1265 | ␊ |
1266 | ␉// parent dirID is unsigned␊ |
1267 | ␉if (searchParentID > trialParentID)␊ |
1268 | ␉{␊ |
1269 | ␉␉result = 1;␊ |
1270 | ␉}␊ |
1271 | ␉else if (searchParentID < trialParentID) ␊ |
1272 | ␉{␊ |
1273 | ␉␉result = -1;␊ |
1274 | ␉}␊ |
1275 | ␉else␊ |
1276 | ␉{␊ |
1277 | ␉␉// parent dirID's are equal, compare names␊ |
1278 | ␉␉if ((searchKey->nodeName.length == 0) || (trialKey->nodeName.length == 0))␊ |
1279 | ␉␉{␊ |
1280 | ␉␉␉result = searchKey->nodeName.length - trialKey->nodeName.length;␊ |
1281 | ␉␉}␊ |
1282 | ␉␉else if (gCaseSensitive)␊ |
1283 | ␉␉{␊ |
1284 | ␉␉␉result = BinaryUnicodeCompare(&searchKey->nodeName.unicode[0],␊ |
1285 | SWAP_BE16(searchKey->nodeName.length),␊ |
1286 | &trialKey->nodeName.unicode[0],␊ |
1287 | SWAP_BE16(trialKey->nodeName.length));␊ |
1288 | ␉␉}␊ |
1289 | ␉␉else␊ |
1290 | ␉␉{␊ |
1291 | ␉␉␉result = FastUnicodeCompare(&searchKey->nodeName.unicode[0],␊ |
1292 | SWAP_BE16(searchKey->nodeName.length),␊ |
1293 | &trialKey->nodeName.unicode[0],␊ |
1294 | SWAP_BE16(trialKey->nodeName.length), OSBigEndian);␊ |
1295 | ␉␉}␊ |
1296 | ␉}␊ |
1297 | ␊ |
1298 | ␉return result;␊ |
1299 | }␊ |
1300 | ␊ |
1301 | //==============================================================================␊ |
1302 | ␊ |
1303 | static long CompareHFSExtentsKeys(void * key, void * testKey)␊ |
1304 | {␊ |
1305 | ␉HFSExtentKey *searchKey, *trialKey;␊ |
1306 | ␉long result;␊ |
1307 | ␊ |
1308 | ␉searchKey = key;␊ |
1309 | ␉trialKey = testKey;␊ |
1310 | ␊ |
1311 | ␉// assume searchKey < trialKey␊ |
1312 | ␉result = -1; ␊ |
1313 | ␊ |
1314 | ␉if (searchKey->fileID == trialKey->fileID)␊ |
1315 | ␉{␊ |
1316 | ␉␉// FileNum's are equal; compare fork types␊ |
1317 | ␉␉if (searchKey->forkType == trialKey->forkType)␊ |
1318 | ␉␉{␊ |
1319 | ␉␉␉// Fork types are equal; compare allocation block number␊ |
1320 | ␉␉␉if (searchKey->startBlock == trialKey->startBlock)␊ |
1321 | ␉␉␉{␊ |
1322 | ␉␉␉␉// Everything is equal␊ |
1323 | ␉␉␉␉result = 0;␊ |
1324 | ␉␉␉}␊ |
1325 | ␉␉␉else␊ |
1326 | ␉␉␉{␊ |
1327 | ␉␉␉␉// Allocation block numbers differ; determine sign␊ |
1328 | ␉␉␉␉if (SWAP_BE16(searchKey->startBlock) > SWAP_BE16(trialKey->startBlock))␊ |
1329 | ␉␉␉␉{␊ |
1330 | ␉␉␉␉␉result = 1;␊ |
1331 | ␉␉␉␉}␊ |
1332 | ␉␉␉}␊ |
1333 | ␉}␊ |
1334 | ␉␉else␊ |
1335 | ␉␉{␊ |
1336 | ␉␉␉// Fork types differ; determine sign␊ |
1337 | ␉␉␉if (searchKey->forkType > trialKey->forkType)␊ |
1338 | ␉␉␉{␊ |
1339 | ␉␉␉␉result = 1;␊ |
1340 | ␉␉␉}␊ |
1341 | ␉␉}␊ |
1342 | ␉}␊ |
1343 | ␉else␊ |
1344 | ␉{␊ |
1345 | ␉␉// FileNums differ; determine sign␊ |
1346 | ␉␉if (SWAP_BE32(searchKey->fileID) > SWAP_BE32(trialKey->fileID))␊ |
1347 | ␉␉{␊ |
1348 | ␉␉␉result = 1;␊ |
1349 | ␉␉}␊ |
1350 | ␉}␊ |
1351 | ␊ |
1352 | ␉return result;␊ |
1353 | }␊ |
1354 | ␊ |
1355 | ␊ |
1356 | //==============================================================================␊ |
1357 | ␊ |
1358 | static long CompareHFSPlusExtentsKeys(void * key, void * testKey)␊ |
1359 | {␊ |
1360 | ␉HFSPlusExtentKey␉*searchKey, *trialKey;␊ |
1361 | ␊ |
1362 | ␉long␉␉␉result;␊ |
1363 | ␊ |
1364 | ␉searchKey = key;␊ |
1365 | ␉trialKey = testKey;␊ |
1366 | ␊ |
1367 | ␉// assume searchKey < trialKey␊ |
1368 | ␉result = -1;␊ |
1369 | ␊ |
1370 | ␉if (searchKey->fileID == trialKey->fileID)␊ |
1371 | ␉{␊ |
1372 | ␉␉// FileNum's are equal; compare fork types␊ |
1373 | ␉␉if (searchKey->forkType == trialKey->forkType)␊ |
1374 | ␉␉{␊ |
1375 | ␉␉␉// Fork types are equal; compare allocation block number␊ |
1376 | ␉␉␉if (searchKey->startBlock == trialKey->startBlock)␊ |
1377 | ␉␉␉{␊ |
1378 | ␉␉␉␉// Everything is equal␊ |
1379 | ␉␉␉␉result = 0;␊ |
1380 | ␉␉␉}␊ |
1381 | ␉␉␉else␊ |
1382 | ␉␉␉{␊ |
1383 | ␉␉␉␉// Allocation block numbers differ; determine sign␊ |
1384 | ␉␉␉␉if (SWAP_BE32(searchKey->startBlock) > SWAP_BE32(trialKey->startBlock))␊ |
1385 | ␉␉␉␉{␊ |
1386 | ␉␉␉␉␉result = 1;␊ |
1387 | ␉␉␉␉}␊ |
1388 | ␉␉␉}␊ |
1389 | ␉␉}␊ |
1390 | ␉␉else␊ |
1391 | ␉␉{␊ |
1392 | ␉␉␉// Fork types differ; determine sign␊ |
1393 | ␉␉␉if (searchKey->forkType > trialKey->forkType)␊ |
1394 | ␉␉␉{␊ |
1395 | ␉␉␉␉result = 1;␊ |
1396 | ␉␉␉}␊ |
1397 | ␉␉}␊ |
1398 | ␉}␊ |
1399 | ␉else␊ |
1400 | ␉{␊ |
1401 | ␉␉// FileNums differ; determine sign␊ |
1402 | ␉␉if (SWAP_BE32(searchKey->fileID) > SWAP_BE32(trialKey->fileID))␊ |
1403 | ␉␉{␊ |
1404 | ␉␉␉result = 1;␊ |
1405 | ␉␉}␊ |
1406 | ␉}␊ |
1407 | ␊ |
1408 | return result;␊ |
1409 | }␊ |
1410 | ␊ |
1411 | |