]>
Commit | Line | Data |
---|---|---|
9bccf70c | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved. |
9bccf70c A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (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. | |
9bccf70c | 11 | * |
e5568f75 A |
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 | |
9bccf70c A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 A |
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. | |
9bccf70c A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | ||
23 | #include <sys/systm.h> | |
24 | #include <sys/kernel.h> | |
25 | #include <sys/malloc.h> | |
26 | #include <sys/stat.h> | |
27 | #include <sys/mount.h> | |
28 | #include <sys/vnode.h> | |
9bccf70c A |
29 | #include <sys/dirent.h> |
30 | #include <vfs/vfs_support.h> | |
31 | #include <libkern/libkern.h> | |
32 | ||
33 | #include <sys/utfconv.h> | |
34 | ||
35 | #include "hfs.h" | |
36 | #include "hfs_catalog.h" | |
37 | #include "hfs_format.h" | |
38 | #include "hfs_endian.h" | |
39 | ||
40 | #include "hfscommon/headers/BTreesInternal.h" | |
9bccf70c A |
41 | #include "hfscommon/headers/HFSUnicodeWrappers.h" |
42 | ||
9bccf70c A |
43 | |
44 | /* | |
45 | * Initialization of an FSBufferDescriptor structure. | |
46 | */ | |
47 | #define BDINIT(bd, addr) { \ | |
48 | (bd).bufferAddress = (addr); \ | |
49 | (bd).itemSize = sizeof(*(addr)); \ | |
50 | (bd).itemCount = 1; \ | |
51 | } | |
52 | ||
53 | ||
54 | struct btobj { | |
55 | BTreeIterator iterator; | |
56 | HFSPlusCatalogKey key; | |
57 | CatalogRecord data; | |
58 | }; | |
59 | ||
60 | struct update_state { | |
61 | struct cat_desc * s_desc; | |
62 | struct cat_attr * s_attr; | |
63 | struct cat_fork * s_datafork; | |
64 | struct cat_fork * s_rsrcfork; | |
65 | struct hfsmount * s_hfsmp; | |
66 | }; | |
67 | ||
91447636 A |
68 | struct position_state { |
69 | int error; | |
70 | u_int32_t count; | |
71 | u_int32_t index; | |
72 | u_int32_t parentID; | |
73 | struct hfsmount *hfsmp; | |
74 | }; | |
75 | ||
76 | /* Map file mode type to directory entry types */ | |
77 | u_char modetodirtype[16] = { | |
78 | DT_REG, DT_FIFO, DT_CHR, DT_UNKNOWN, | |
79 | DT_DIR, DT_UNKNOWN, DT_BLK, DT_UNKNOWN, | |
80 | DT_REG, DT_UNKNOWN, DT_LNK, DT_UNKNOWN, | |
81 | DT_SOCK, DT_UNKNOWN, DT_WHT, DT_UNKNOWN | |
82 | }; | |
83 | #define MODE_TO_DT(mode) (modetodirtype[((mode) & S_IFMT) >> 12]) | |
84 | ||
9bccf70c A |
85 | |
86 | static int cat_lookupbykey(struct hfsmount *hfsmp, CatalogKey *keyp, u_long hint, int wantrsrc, | |
91447636 | 87 | struct cat_desc *descp, struct cat_attr *attrp, struct cat_fork *forkp, cnid_t *desc_cnid); |
9bccf70c A |
88 | |
89 | static int cat_lookupmangled(struct hfsmount *hfsmp, struct cat_desc *descp, int wantrsrc, | |
90 | struct cat_desc *outdescp, struct cat_attr *attrp, struct cat_fork *forkp); | |
91 | ||
92 | extern int mac_roman_to_unicode(const Str31 hfs_str, UniChar *uni_str, | |
93 | UInt32 maxCharLen, UInt32 *unicodeChars); | |
94 | ||
95 | extern int unicode_to_hfs(ExtendedVCB *vcb, ByteCount srcLen, | |
96 | const u_int16_t* srcStr, Str31 dstStr, int retry); | |
97 | ||
98 | ||
99 | /* Internal catalog support routines */ | |
100 | ||
91447636 A |
101 | static int cat_findposition(const CatalogKey *ckp, const CatalogRecord *crp, |
102 | struct position_state *state); | |
9bccf70c | 103 | |
55e303ae A |
104 | static int resolvelinkid(struct hfsmount *hfsmp, u_long linkref, ino_t *ino); |
105 | ||
9bccf70c A |
106 | static int getkey(struct hfsmount *hfsmp, cnid_t cnid, CatalogKey * key); |
107 | ||
108 | static int buildkey(struct hfsmount *hfsmp, struct cat_desc *descp, | |
109 | HFSPlusCatalogKey *key, int retry); | |
110 | ||
111 | static void buildthreadkey(HFSCatalogNodeID parentID, int std_hfs, CatalogKey *key); | |
112 | ||
113 | static void buildrecord(struct cat_attr *attrp, cnid_t cnid, int std_hfs, u_int32_t encoding, CatalogRecord *crp, int *recordSize); | |
114 | ||
91447636 | 115 | static int catrec_update(const CatalogKey *ckp, CatalogRecord *crp, struct update_state *state); |
9bccf70c A |
116 | |
117 | static int builddesc(const HFSPlusCatalogKey *key, cnid_t cnid, u_long hint, u_long encoding, | |
118 | int isdir, struct cat_desc *descp); | |
119 | ||
120 | static void getbsdattr(struct hfsmount *hfsmp, const struct HFSPlusCatalogFile *crp, struct cat_attr * attrp); | |
121 | ||
122 | static void promotekey(struct hfsmount *hfsmp, const HFSCatalogKey *hfskey, HFSPlusCatalogKey *keyp, u_long *encoding); | |
123 | static void promotefork(struct hfsmount *hfsmp, const struct HFSCatalogFile *file, int resource, struct cat_fork * forkp); | |
124 | static void promoteattr(struct hfsmount *hfsmp, const CatalogRecord *dataPtr, struct HFSPlusCatalogFile *crp); | |
125 | ||
126 | static cnid_t getcnid(const CatalogRecord *crp); | |
127 | static u_long getencoding(const CatalogRecord *crp); | |
128 | static cnid_t getparentcnid(const CatalogRecord *recp); | |
129 | ||
130 | static int isadir(const CatalogRecord *crp); | |
131 | ||
132 | static int buildthread(void *keyp, void *recp, int std_hfs, int directory); | |
133 | ||
134 | ||
55e303ae A |
135 | __private_extern__ |
136 | int | |
137 | cat_preflight(struct hfsmount *hfsmp, catops_t ops, cat_cookie_t *cookie, struct proc *p) | |
138 | { | |
139 | FCB *fcb; | |
91447636 | 140 | int lockflags; |
55e303ae A |
141 | int result; |
142 | ||
91447636 | 143 | fcb = GetFileControlBlock(hfsmp->hfs_catalog_vp); |
55e303ae | 144 | |
91447636 | 145 | lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK); |
55e303ae A |
146 | |
147 | result = BTReserveSpace(fcb, ops, (void*)cookie); | |
148 | ||
91447636 | 149 | hfs_systemfile_unlock(hfsmp, lockflags); |
55e303ae | 150 | |
91447636 | 151 | return MacToVFSError(result); |
55e303ae A |
152 | } |
153 | ||
154 | __private_extern__ | |
155 | void | |
156 | cat_postflight(struct hfsmount *hfsmp, cat_cookie_t *cookie, struct proc *p) | |
157 | { | |
158 | FCB *fcb; | |
91447636 | 159 | int lockflags; |
55e303ae | 160 | |
91447636 A |
161 | fcb = GetFileControlBlock(hfsmp->hfs_catalog_vp); |
162 | ||
163 | lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK); | |
55e303ae | 164 | |
55e303ae | 165 | (void) BTReleaseReserve(fcb, (void*)cookie); |
91447636 A |
166 | |
167 | hfs_systemfile_unlock(hfsmp, lockflags); | |
55e303ae A |
168 | } |
169 | ||
9bccf70c | 170 | |
55e303ae | 171 | __private_extern__ |
9bccf70c A |
172 | void |
173 | cat_convertattr( | |
174 | struct hfsmount *hfsmp, | |
175 | CatalogRecord * recp, | |
176 | struct cat_attr *attrp, | |
177 | struct cat_fork *datafp, | |
178 | struct cat_fork *rsrcfp) | |
179 | { | |
180 | int std_hfs = HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord; | |
181 | ||
182 | if (std_hfs) { | |
183 | struct HFSPlusCatalogFile cnoderec; | |
184 | ||
185 | promoteattr(hfsmp, recp, &cnoderec); | |
186 | getbsdattr(hfsmp, &cnoderec, attrp); | |
187 | } else { | |
188 | getbsdattr(hfsmp, (struct HFSPlusCatalogFile *)recp, attrp); | |
189 | } | |
190 | ||
191 | if (isadir(recp)) | |
192 | bzero(datafp, sizeof(*datafp)); | |
193 | else if (std_hfs) { | |
194 | promotefork(hfsmp, (HFSCatalogFile *)&recp->hfsFile, 0, datafp); | |
195 | promotefork(hfsmp, (HFSCatalogFile *)&recp->hfsFile, 1, rsrcfp); | |
196 | } else { | |
55e303ae A |
197 | /* Convert the data fork. */ |
198 | datafp->cf_size = recp->hfsPlusFile.dataFork.logicalSize; | |
199 | datafp->cf_blocks = recp->hfsPlusFile.dataFork.totalBlocks; | |
200 | if ((hfsmp->hfc_stage == HFC_RECORDING) && | |
201 | (attrp->ca_atime >= hfsmp->hfc_timebase)) { | |
202 | datafp->cf_bytesread = | |
203 | recp->hfsPlusFile.dataFork.clumpSize * | |
204 | HFSTOVCB(hfsmp)->blockSize; | |
205 | } else { | |
206 | datafp->cf_bytesread = 0; | |
207 | } | |
208 | datafp->cf_vblocks = 0; | |
209 | bcopy(&recp->hfsPlusFile.dataFork.extents[0], | |
210 | &datafp->cf_extents[0], sizeof(HFSPlusExtentRecord)); | |
211 | ||
212 | /* Convert the resource fork. */ | |
213 | rsrcfp->cf_size = recp->hfsPlusFile.resourceFork.logicalSize; | |
214 | rsrcfp->cf_blocks = recp->hfsPlusFile.resourceFork.totalBlocks; | |
215 | if ((hfsmp->hfc_stage == HFC_RECORDING) && | |
216 | (attrp->ca_atime >= hfsmp->hfc_timebase)) { | |
217 | datafp->cf_bytesread = | |
218 | recp->hfsPlusFile.resourceFork.clumpSize * | |
219 | HFSTOVCB(hfsmp)->blockSize; | |
220 | } else { | |
221 | datafp->cf_bytesread = 0; | |
222 | } | |
223 | rsrcfp->cf_vblocks = 0; | |
224 | bcopy(&recp->hfsPlusFile.resourceFork.extents[0], | |
225 | &rsrcfp->cf_extents[0], sizeof(HFSPlusExtentRecord)); | |
9bccf70c A |
226 | } |
227 | } | |
228 | ||
55e303ae | 229 | __private_extern__ |
9bccf70c A |
230 | int |
231 | cat_convertkey( | |
232 | struct hfsmount *hfsmp, | |
233 | CatalogKey *key, | |
234 | CatalogRecord * recp, | |
235 | struct cat_desc *descp) | |
236 | { | |
237 | int std_hfs = HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord; | |
238 | HFSPlusCatalogKey * pluskey = NULL; | |
239 | u_long encoding; | |
240 | ||
241 | if (std_hfs) { | |
242 | MALLOC(pluskey, HFSPlusCatalogKey *, sizeof(HFSPlusCatalogKey), M_TEMP, M_WAITOK); | |
243 | promotekey(hfsmp, (HFSCatalogKey *)key, pluskey, &encoding); | |
244 | ||
245 | } else { | |
246 | pluskey = (HFSPlusCatalogKey *)key; | |
247 | encoding = getencoding(recp); | |
248 | } | |
249 | ||
250 | builddesc(pluskey, getcnid(recp), 0, encoding, isadir(recp), descp); | |
251 | if (std_hfs) { | |
252 | FREE(pluskey, M_TEMP); | |
253 | } | |
254 | return (0); | |
255 | } | |
256 | ||
257 | ||
258 | /* | |
259 | * cat_releasedesc | |
260 | */ | |
55e303ae | 261 | __private_extern__ |
9bccf70c A |
262 | void |
263 | cat_releasedesc(struct cat_desc *descp) | |
264 | { | |
265 | char * name; | |
266 | ||
267 | if (descp == NULL) | |
268 | return; | |
269 | ||
270 | if ((descp->cd_flags & CD_HASBUF) && | |
271 | (descp->cd_nameptr != NULL)) { | |
272 | name = descp->cd_nameptr; | |
273 | descp->cd_nameptr = NULL; | |
274 | descp->cd_namelen = 0; | |
275 | descp->cd_flags &= ~CD_HASBUF; | |
91447636 | 276 | vfs_removename(name); |
9bccf70c A |
277 | } |
278 | descp->cd_nameptr = NULL; | |
279 | descp->cd_namelen = 0; | |
280 | } | |
281 | ||
282 | /* | |
283 | * These Catalog functions allow access to the HFS Catalog (database). | |
284 | * The catalog b-tree lock must be aquired before calling any of these routines. | |
285 | */ | |
286 | ||
287 | /* | |
288 | * cat_lookup - lookup a catalog node using a cnode decriptor | |
289 | */ | |
55e303ae | 290 | __private_extern__ |
9bccf70c A |
291 | int |
292 | cat_lookup(struct hfsmount *hfsmp, struct cat_desc *descp, int wantrsrc, | |
293 | struct cat_desc *outdescp, struct cat_attr *attrp, | |
91447636 | 294 | struct cat_fork *forkp, cnid_t *desc_cnid) |
9bccf70c A |
295 | { |
296 | CatalogKey * keyp; | |
297 | int std_hfs; | |
298 | int result; | |
299 | ||
300 | std_hfs = (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord); | |
301 | ||
302 | MALLOC(keyp, CatalogKey *, sizeof(CatalogKey), M_TEMP, M_WAITOK); | |
303 | ||
304 | result = buildkey(hfsmp, descp, (HFSPlusCatalogKey *)keyp, 1); | |
305 | if (result) | |
306 | goto exit; | |
307 | ||
91447636 | 308 | result = cat_lookupbykey(hfsmp, keyp, descp->cd_hint, wantrsrc, outdescp, attrp, forkp, desc_cnid); |
9bccf70c A |
309 | |
310 | if (result == ENOENT) { | |
311 | if (!std_hfs) { | |
91447636 A |
312 | struct cat_desc temp_desc; |
313 | if (outdescp == NULL) { | |
314 | bzero(&temp_desc, sizeof(temp_desc)); | |
315 | outdescp = &temp_desc; | |
316 | } | |
9bccf70c | 317 | result = cat_lookupmangled(hfsmp, descp, wantrsrc, outdescp, attrp, forkp); |
91447636 A |
318 | if (desc_cnid) { |
319 | *desc_cnid = outdescp->cd_cnid; | |
320 | } | |
321 | if (outdescp == &temp_desc) { | |
322 | /* Release the local copy of desc */ | |
323 | cat_releasedesc(outdescp); | |
324 | } | |
9bccf70c A |
325 | } else if (hfsmp->hfs_encoding != kTextEncodingMacRoman) { |
326 | // make MacRoman key from utf-8 | |
327 | // result = cat_lookupbykey(hfsmp, keyp, descp->cd_hint, attrp, forkp); | |
328 | // update desc text encoding so that other catalog ops succeed | |
329 | } | |
330 | } | |
331 | exit: | |
332 | FREE(keyp, M_TEMP); | |
333 | ||
334 | return (result); | |
335 | } | |
336 | ||
55e303ae | 337 | __private_extern__ |
9bccf70c A |
338 | int |
339 | cat_insertfilethread(struct hfsmount *hfsmp, struct cat_desc *descp) | |
340 | { | |
341 | struct BTreeIterator *iterator; | |
342 | struct FSBufferDescriptor file_data; | |
343 | struct HFSCatalogFile file_rec; | |
344 | UInt16 datasize; | |
345 | FCB *fcb; | |
346 | int result; | |
347 | ||
348 | if (HFSTOVCB(hfsmp)->vcbSigWord != kHFSSigWord) | |
349 | return (EINVAL); | |
350 | ||
351 | fcb = GetFileControlBlock(HFSTOVCB(hfsmp)->catalogRefNum); | |
352 | ||
353 | MALLOC(iterator, BTreeIterator *, 2 * sizeof(*iterator), M_TEMP, M_WAITOK); | |
354 | bzero(&iterator[0], 2* sizeof(*iterator)); | |
355 | result = buildkey(hfsmp, descp, (HFSPlusCatalogKey *)&iterator[0].key, 0); | |
356 | if (result) | |
357 | goto exit; | |
358 | ||
359 | BDINIT(file_data, &file_rec); | |
360 | result = BTSearchRecord(fcb, &iterator[0], &file_data, &datasize, &iterator[0]); | |
361 | if (result) | |
362 | goto exit; | |
363 | ||
364 | if (file_rec.recordType != kHFSFileRecord) { | |
365 | result = EISDIR; | |
366 | goto exit; | |
367 | } | |
368 | ||
369 | if ((file_rec.flags & kHFSThreadExistsMask) == 0) { | |
370 | struct FSBufferDescriptor thread_data; | |
371 | struct HFSCatalogThread thread_rec; | |
372 | ||
373 | file_rec.flags |= kHFSThreadExistsMask; | |
374 | BDINIT(thread_data, &thread_rec); | |
375 | thread_data.itemSize = buildthread(&iterator[0].key, &thread_rec, 1, 0); | |
376 | buildthreadkey(file_rec.fileID, 1, (CatalogKey *)&iterator[1].key); | |
377 | ||
378 | result = BTInsertRecord(fcb, &iterator[1], &thread_data, thread_data.itemSize); | |
379 | if (result) | |
380 | goto exit; | |
381 | ||
382 | (void) BTReplaceRecord(fcb, &iterator[0], &file_data, datasize); | |
383 | (void) BTFlushPath(fcb); | |
384 | } | |
385 | exit: | |
b4c24cb9 | 386 | (void) BTFlushPath(fcb); |
9bccf70c A |
387 | FREE(iterator, M_TEMP); |
388 | ||
389 | return MacToVFSError(result); | |
390 | } | |
391 | ||
392 | ||
91447636 A |
393 | /* |
394 | * cat_findname - obtain a descriptor from cnid | |
395 | * | |
396 | * Only a thread lookup is performed. | |
397 | */ | |
398 | __private_extern__ | |
399 | int | |
400 | cat_findname(struct hfsmount *hfsmp, cnid_t cnid, struct cat_desc *outdescp) | |
401 | { | |
402 | struct BTreeIterator * iterator; | |
403 | FSBufferDescriptor btdata; | |
404 | CatalogKey * keyp; | |
405 | CatalogRecord * recp; | |
406 | int isdir; | |
407 | int result; | |
408 | int std_hfs; | |
409 | ||
410 | isdir = 0; | |
411 | std_hfs = (hfsmp->hfs_flags & HFS_STANDARD); | |
412 | ||
413 | MALLOC(iterator, BTreeIterator *, sizeof(*iterator), M_TEMP, M_WAITOK); | |
414 | buildthreadkey(cnid, std_hfs, (CatalogKey *)&iterator->key); | |
415 | iterator->hint.nodeNum = 0; | |
416 | ||
417 | MALLOC(recp, CatalogRecord *, sizeof(CatalogRecord), M_TEMP, M_WAITOK); | |
418 | BDINIT(btdata, recp); | |
419 | ||
420 | result = BTSearchRecord(VTOF(hfsmp->hfs_catalog_vp), iterator, &btdata, NULL, NULL); | |
421 | if (result) | |
422 | goto exit; | |
423 | ||
424 | /* Turn thread record into a cnode key (in place). */ | |
425 | switch (recp->recordType) { | |
426 | case kHFSFolderThreadRecord: | |
427 | isdir = 1; | |
428 | /* fall through */ | |
429 | case kHFSFileThreadRecord: | |
430 | keyp = (CatalogKey *)((char *)&recp->hfsThread.reserved + 6); | |
431 | keyp->hfs.keyLength = kHFSCatalogKeyMinimumLength + keyp->hfs.nodeName[0]; | |
432 | break; | |
433 | ||
434 | case kHFSPlusFolderThreadRecord: | |
435 | isdir = 1; | |
436 | /* fall through */ | |
437 | case kHFSPlusFileThreadRecord: | |
438 | keyp = (CatalogKey *)&recp->hfsPlusThread.reserved; | |
439 | keyp->hfsPlus.keyLength = kHFSPlusCatalogKeyMinimumLength + | |
440 | (keyp->hfsPlus.nodeName.length * 2); | |
441 | break; | |
442 | default: | |
443 | result = ENOENT; | |
444 | goto exit; | |
445 | } | |
446 | if (std_hfs) { | |
447 | HFSPlusCatalogKey * pluskey = NULL; | |
448 | u_long encoding; | |
449 | ||
450 | MALLOC(pluskey, HFSPlusCatalogKey *, sizeof(HFSPlusCatalogKey), M_TEMP, M_WAITOK); | |
451 | promotekey(hfsmp, &keyp->hfs, pluskey, &encoding); | |
452 | builddesc(pluskey, cnid, 0, encoding, isdir, outdescp); | |
453 | FREE(pluskey, M_TEMP); | |
454 | ||
455 | } else { | |
456 | builddesc((HFSPlusCatalogKey *)keyp, cnid, 0, 0, isdir, outdescp); | |
457 | } | |
458 | exit: | |
459 | FREE(recp, M_TEMP); | |
460 | FREE(iterator, M_TEMP); | |
461 | ||
462 | return MacToVFSError(result); | |
463 | } | |
464 | ||
9bccf70c A |
465 | /* |
466 | * cat_idlookup - lookup a catalog node using a cnode id | |
467 | */ | |
55e303ae | 468 | __private_extern__ |
9bccf70c A |
469 | int |
470 | cat_idlookup(struct hfsmount *hfsmp, cnid_t cnid, struct cat_desc *outdescp, | |
471 | struct cat_attr *attrp, struct cat_fork *forkp) | |
472 | { | |
473 | struct BTreeIterator * iterator; | |
474 | FSBufferDescriptor btdata; | |
475 | UInt16 datasize; | |
476 | CatalogKey * keyp; | |
477 | CatalogRecord * recp; | |
478 | int result; | |
479 | int std_hfs; | |
480 | ||
481 | std_hfs = (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord); | |
482 | ||
483 | MALLOC(iterator, BTreeIterator *, sizeof(*iterator), M_TEMP, M_WAITOK); | |
484 | bzero(iterator, sizeof(*iterator)); | |
485 | buildthreadkey(cnid, std_hfs, (CatalogKey *)&iterator->key); | |
486 | ||
487 | MALLOC(recp, CatalogRecord *, sizeof(CatalogRecord), M_TEMP, M_WAITOK); | |
488 | BDINIT(btdata, recp); | |
489 | ||
490 | result = BTSearchRecord(VTOF(HFSTOVCB(hfsmp)->catalogRefNum), iterator, | |
491 | &btdata, &datasize, iterator); | |
492 | if (result) | |
493 | goto exit; | |
494 | ||
495 | /* Turn thread record into a cnode key (in place) */ | |
496 | switch (recp->recordType) { | |
497 | case kHFSFileThreadRecord: | |
498 | case kHFSFolderThreadRecord: | |
499 | keyp = (CatalogKey *)((char *)&recp->hfsThread.reserved + 6); | |
500 | keyp->hfs.keyLength = kHFSCatalogKeyMinimumLength + keyp->hfs.nodeName[0]; | |
501 | break; | |
502 | ||
503 | case kHFSPlusFileThreadRecord: | |
504 | case kHFSPlusFolderThreadRecord: | |
505 | keyp = (CatalogKey *)&recp->hfsPlusThread.reserved; | |
506 | keyp->hfsPlus.keyLength = kHFSPlusCatalogKeyMinimumLength + | |
507 | (keyp->hfsPlus.nodeName.length * 2); | |
508 | break; | |
509 | ||
510 | default: | |
511 | result = ENOENT; | |
512 | goto exit; | |
513 | } | |
514 | ||
91447636 | 515 | result = cat_lookupbykey(hfsmp, keyp, 0, 0, outdescp, attrp, forkp, NULL); |
9bccf70c A |
516 | exit: |
517 | FREE(recp, M_TEMP); | |
518 | FREE(iterator, M_TEMP); | |
519 | ||
520 | return MacToVFSError(result); | |
521 | } | |
522 | ||
523 | ||
524 | /* | |
525 | * cat_lookupmangled - lookup a catalog node using a mangled name | |
526 | */ | |
527 | static int | |
528 | cat_lookupmangled(struct hfsmount *hfsmp, struct cat_desc *descp, int wantrsrc, | |
529 | struct cat_desc *outdescp, struct cat_attr *attrp, struct cat_fork *forkp) | |
530 | { | |
531 | cnid_t fileID; | |
532 | int prefixlen; | |
533 | int result; | |
534 | ||
535 | if (wantrsrc) | |
536 | return (ENOENT); | |
537 | ||
538 | fileID = GetEmbeddedFileID(descp->cd_nameptr, descp->cd_namelen, &prefixlen); | |
539 | if (fileID < kHFSFirstUserCatalogNodeID) | |
540 | return (ENOENT); | |
541 | ||
542 | result = cat_idlookup(hfsmp, fileID, outdescp, attrp, forkp); | |
543 | if (result) | |
544 | return (ENOENT); | |
545 | ||
546 | /* It must be in the correct directory */ | |
547 | if (descp->cd_parentcnid != outdescp->cd_parentcnid) | |
548 | goto falsematch; | |
549 | ||
550 | if ((outdescp->cd_namelen < prefixlen) || | |
551 | bcmp(outdescp->cd_nameptr, descp->cd_nameptr, prefixlen-6) != 0) | |
552 | goto falsematch; | |
553 | ||
554 | return (0); | |
555 | ||
556 | falsematch: | |
557 | cat_releasedesc(outdescp); | |
558 | return (ENOENT); | |
559 | } | |
560 | ||
561 | ||
562 | /* | |
563 | * cat_lookupbykey - lookup a catalog node using a cnode key | |
564 | */ | |
565 | static int | |
566 | cat_lookupbykey(struct hfsmount *hfsmp, CatalogKey *keyp, u_long hint, int wantrsrc, | |
91447636 | 567 | struct cat_desc *descp, struct cat_attr *attrp, struct cat_fork *forkp, cnid_t *desc_cnid) |
9bccf70c A |
568 | { |
569 | struct BTreeIterator * iterator; | |
570 | FSBufferDescriptor btdata; | |
571 | CatalogRecord * recp; | |
572 | UInt16 datasize; | |
573 | int result; | |
574 | int std_hfs; | |
575 | u_long ilink = 0; | |
576 | cnid_t cnid = 0; | |
577 | u_long encoding = 0; | |
578 | ||
579 | std_hfs = (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord); | |
580 | ||
581 | MALLOC(recp, CatalogRecord *, sizeof(CatalogRecord), M_TEMP, M_WAITOK); | |
582 | BDINIT(btdata, recp); | |
583 | MALLOC(iterator, BTreeIterator *, sizeof(*iterator), M_TEMP, M_WAITOK); | |
584 | bzero(iterator, sizeof(*iterator)); | |
585 | iterator->hint.nodeNum = hint; | |
586 | bcopy(keyp, &iterator->key, sizeof(CatalogKey)); | |
587 | ||
588 | result = BTSearchRecord(VTOF(HFSTOVCB(hfsmp)->catalogRefNum), iterator, | |
589 | &btdata, &datasize, iterator); | |
590 | if (result) | |
591 | goto exit; | |
592 | ||
593 | /* Save the cnid now in case there's a hard link */ | |
594 | cnid = getcnid(recp); | |
595 | encoding = getencoding(recp); | |
596 | hint = iterator->hint.nodeNum; | |
597 | ||
b4c24cb9 A |
598 | /* Hide the journal files (if any) */ |
599 | if (hfsmp->jnl && | |
600 | ((cnid == hfsmp->hfs_jnlfileid) || | |
601 | (cnid == hfsmp->hfs_jnlinfoblkid))) { | |
602 | ||
603 | result = ENOENT; | |
604 | goto exit; | |
605 | } | |
606 | ||
9bccf70c A |
607 | /* |
608 | * When a hardlink link is encountered, auto resolve it | |
609 | */ | |
610 | if (!std_hfs | |
611 | && (attrp || forkp) | |
612 | && (recp->recordType == kHFSPlusFileRecord) | |
613 | && (SWAP_BE32(recp->hfsPlusFile.userInfo.fdType) == kHardLinkFileType) | |
614 | && (SWAP_BE32(recp->hfsPlusFile.userInfo.fdCreator) == kHFSPlusCreator) | |
91447636 A |
615 | && ((to_bsd_time(recp->hfsPlusFile.createDate) == (time_t)HFSTOVCB(hfsmp)->vcbCrDate) || |
616 | (to_bsd_time(recp->hfsPlusFile.createDate) == (time_t)hfsmp->hfs_metadata_createdate))) { | |
9bccf70c A |
617 | |
618 | ilink = recp->hfsPlusFile.bsdInfo.special.iNodeNum; | |
619 | ||
620 | (void) resolvelink(hfsmp, ilink, (struct HFSPlusCatalogFile *)recp); | |
621 | } | |
622 | ||
623 | if (attrp != NULL) { | |
624 | if (std_hfs) { | |
625 | struct HFSPlusCatalogFile cnoderec; | |
626 | ||
627 | promoteattr(hfsmp, recp, &cnoderec); | |
628 | getbsdattr(hfsmp, &cnoderec, attrp); | |
629 | } else { | |
630 | getbsdattr(hfsmp, (struct HFSPlusCatalogFile *)recp, attrp); | |
631 | if (ilink) | |
632 | attrp->ca_rdev = ilink; | |
633 | } | |
634 | } | |
635 | if (forkp != NULL) { | |
55e303ae | 636 | if (isadir(recp)) { |
9bccf70c | 637 | bzero(forkp, sizeof(*forkp)); |
55e303ae | 638 | } else if (std_hfs) { |
9bccf70c | 639 | promotefork(hfsmp, (HFSCatalogFile *)&recp->hfsFile, wantrsrc, forkp); |
55e303ae A |
640 | } else if (wantrsrc) { |
641 | /* Convert the resource fork. */ | |
642 | forkp->cf_size = recp->hfsPlusFile.resourceFork.logicalSize; | |
643 | forkp->cf_blocks = recp->hfsPlusFile.resourceFork.totalBlocks; | |
644 | if ((hfsmp->hfc_stage == HFC_RECORDING) && | |
645 | (to_bsd_time(recp->hfsPlusFile.accessDate) >= hfsmp->hfc_timebase)) { | |
646 | forkp->cf_bytesread = | |
647 | recp->hfsPlusFile.resourceFork.clumpSize * | |
648 | HFSTOVCB(hfsmp)->blockSize; | |
649 | } else { | |
650 | forkp->cf_bytesread = 0; | |
651 | } | |
652 | forkp->cf_vblocks = 0; | |
653 | bcopy(&recp->hfsPlusFile.resourceFork.extents[0], | |
654 | &forkp->cf_extents[0], sizeof(HFSPlusExtentRecord)); | |
655 | } else { | |
656 | /* Convert the data fork. */ | |
657 | forkp->cf_size = recp->hfsPlusFile.dataFork.logicalSize; | |
658 | forkp->cf_blocks = recp->hfsPlusFile.dataFork.totalBlocks; | |
659 | if ((hfsmp->hfc_stage == HFC_RECORDING) && | |
660 | (to_bsd_time(recp->hfsPlusFile.accessDate) >= hfsmp->hfc_timebase)) { | |
661 | forkp->cf_bytesread = | |
662 | recp->hfsPlusFile.dataFork.clumpSize * | |
663 | HFSTOVCB(hfsmp)->blockSize; | |
664 | } else { | |
665 | forkp->cf_bytesread = 0; | |
666 | } | |
667 | forkp->cf_vblocks = 0; | |
668 | bcopy(&recp->hfsPlusFile.dataFork.extents[0], | |
669 | &forkp->cf_extents[0], sizeof(HFSPlusExtentRecord)); | |
670 | } | |
9bccf70c A |
671 | } |
672 | if (descp != NULL) { | |
673 | HFSPlusCatalogKey * pluskey = NULL; | |
674 | ||
675 | if (std_hfs) { | |
676 | MALLOC(pluskey, HFSPlusCatalogKey *, sizeof(HFSPlusCatalogKey), M_TEMP, M_WAITOK); | |
677 | promotekey(hfsmp, (HFSCatalogKey *)&iterator->key, pluskey, &encoding); | |
678 | ||
679 | } else | |
680 | pluskey = (HFSPlusCatalogKey *)&iterator->key; | |
681 | ||
682 | builddesc(pluskey, cnid, hint, encoding, isadir(recp), descp); | |
683 | if (std_hfs) { | |
684 | FREE(pluskey, M_TEMP); | |
685 | } | |
686 | } | |
91447636 A |
687 | |
688 | if (desc_cnid != NULL) { | |
689 | *desc_cnid = cnid; | |
690 | } | |
9bccf70c A |
691 | exit: |
692 | FREE(iterator, M_TEMP); | |
693 | FREE(recp, M_TEMP); | |
694 | ||
695 | return MacToVFSError(result); | |
696 | } | |
697 | ||
698 | ||
699 | /* | |
700 | * cat_create - create a node in the catalog | |
701 | */ | |
55e303ae | 702 | __private_extern__ |
9bccf70c A |
703 | int |
704 | cat_create(struct hfsmount *hfsmp, struct cat_desc *descp, struct cat_attr *attrp, | |
705 | struct cat_desc *out_descp) | |
706 | { | |
707 | ExtendedVCB * vcb; | |
708 | FCB * fcb; | |
709 | struct btobj * bto; | |
710 | FSBufferDescriptor btdata; | |
711 | u_int32_t nextCNID; | |
712 | u_int32_t datalen; | |
713 | int std_hfs; | |
91447636 | 714 | int result = 0; |
9bccf70c A |
715 | u_long encoding; |
716 | int modeformat; | |
91447636 | 717 | int mntlock = 0; |
9bccf70c A |
718 | |
719 | modeformat = attrp->ca_mode & S_IFMT; | |
720 | ||
721 | vcb = HFSTOVCB(hfsmp); | |
722 | fcb = GetFileControlBlock(vcb->catalogRefNum); | |
9bccf70c A |
723 | std_hfs = (vcb->vcbSigWord == kHFSSigWord); |
724 | ||
91447636 A |
725 | /* |
726 | * Atomically get the next CNID. If we have wrapped the CNIDs | |
727 | * then keep the hfsmp lock held until we have found a CNID. | |
728 | */ | |
729 | HFS_MOUNT_LOCK(hfsmp, TRUE); | |
730 | mntlock = 1; | |
731 | nextCNID = hfsmp->vcbNxtCNID; | |
732 | if (nextCNID == 0xFFFFFFFF) { | |
733 | if (std_hfs) { | |
734 | result = ENOSPC; | |
735 | } else { | |
736 | hfsmp->vcbNxtCNID = kHFSFirstUserCatalogNodeID; | |
737 | hfsmp->vcbAtrb |= kHFSCatalogNodeIDsReusedMask; | |
738 | } | |
739 | } else { | |
740 | hfsmp->vcbNxtCNID++; | |
741 | } | |
742 | hfsmp->vcbFlags |= 0xFF00; | |
743 | /* OK to drop lock if CNIDs are not wrapping */ | |
744 | if ((hfsmp->vcbAtrb & kHFSCatalogNodeIDsReusedMask) == 0) { | |
745 | HFS_MOUNT_UNLOCK(hfsmp, TRUE); | |
746 | mntlock = 0; | |
747 | if (result) | |
748 | return (result); /* HFS only exit */ | |
749 | } | |
9bccf70c A |
750 | |
751 | /* Get space for iterator, key and data */ | |
752 | MALLOC(bto, struct btobj *, sizeof(struct btobj), M_TEMP, M_WAITOK); | |
91447636 | 753 | bto->iterator.hint.nodeNum = 0; |
9bccf70c A |
754 | |
755 | result = buildkey(hfsmp, descp, &bto->key, 0); | |
756 | if (result) | |
757 | goto exit; | |
758 | ||
759 | if (!std_hfs) { | |
760 | encoding = hfs_pickencoding(bto->key.nodeName.unicode, | |
761 | bto->key.nodeName.length); | |
762 | hfs_setencodingbits(hfsmp, encoding); | |
763 | } | |
764 | ||
765 | /* | |
766 | * Insert the thread record first | |
767 | */ | |
768 | if (!std_hfs || (modeformat == S_IFDIR)) { | |
769 | datalen = buildthread((void*)&bto->key, &bto->data, std_hfs, | |
770 | S_ISDIR(attrp->ca_mode)); | |
771 | btdata.bufferAddress = &bto->data; | |
772 | btdata.itemSize = datalen; | |
773 | btdata.itemCount = 1; | |
774 | ||
775 | for (;;) { | |
776 | buildthreadkey(nextCNID, std_hfs, (CatalogKey *) &bto->iterator.key); | |
777 | ||
778 | result = BTInsertRecord(fcb, &bto->iterator, &btdata, datalen); | |
91447636 | 779 | if ((result == btExists) && !std_hfs && mntlock) { |
9bccf70c A |
780 | /* |
781 | * Allow CNIDs on HFS Plus volumes to wrap around | |
782 | */ | |
91447636 | 783 | if (++nextCNID < kHFSFirstUserCatalogNodeID) { |
9bccf70c A |
784 | nextCNID = kHFSFirstUserCatalogNodeID; |
785 | } | |
786 | continue; | |
787 | } | |
788 | break; | |
789 | } | |
790 | if (result) goto exit; | |
791 | } | |
91447636 A |
792 | |
793 | /* | |
794 | * CNID is now established. If we have wrapped then | |
795 | * update the vcbNxtCNID and drop the vcb lock. | |
796 | */ | |
797 | if (mntlock) { | |
798 | hfsmp->vcbNxtCNID = nextCNID + 1; | |
799 | if (hfsmp->vcbNxtCNID < kHFSFirstUserCatalogNodeID) { | |
800 | hfsmp->vcbNxtCNID = kHFSFirstUserCatalogNodeID; | |
801 | } | |
802 | HFS_MOUNT_UNLOCK(hfsmp, TRUE); | |
803 | mntlock = 0; | |
804 | } | |
9bccf70c A |
805 | |
806 | /* | |
807 | * Now insert the file/directory record | |
808 | */ | |
809 | buildrecord(attrp, nextCNID, std_hfs, encoding, &bto->data, &datalen); | |
810 | btdata.bufferAddress = &bto->data; | |
811 | btdata.itemSize = datalen; | |
812 | btdata.itemCount = 1; | |
813 | ||
814 | bcopy(&bto->key, &bto->iterator.key, sizeof(bto->key)); | |
815 | ||
816 | result = BTInsertRecord(fcb, &bto->iterator, &btdata, datalen); | |
817 | if (result) { | |
818 | if (result == btExists) | |
819 | result = EEXIST; | |
820 | ||
821 | /* Back out the thread record */ | |
822 | if (!std_hfs || S_ISDIR(attrp->ca_mode)) { | |
823 | buildthreadkey(nextCNID, std_hfs, (CatalogKey *)&bto->iterator.key); | |
824 | (void) BTDeleteRecord(fcb, &bto->iterator); | |
825 | } | |
826 | goto exit; | |
827 | } | |
828 | ||
829 | /* | |
830 | * Insert was Successfull, update name, parent and volume | |
831 | */ | |
832 | ||
833 | ||
834 | if (out_descp != NULL) { | |
835 | HFSPlusCatalogKey * pluskey = NULL; | |
836 | ||
837 | if (std_hfs) { | |
838 | MALLOC(pluskey, HFSPlusCatalogKey *, sizeof(HFSPlusCatalogKey), M_TEMP, M_WAITOK); | |
839 | promotekey(hfsmp, (HFSCatalogKey *)&bto->iterator.key, pluskey, &encoding); | |
840 | ||
841 | } else | |
842 | pluskey = (HFSPlusCatalogKey *)&bto->iterator.key; | |
843 | ||
844 | builddesc(pluskey, nextCNID, bto->iterator.hint.nodeNum, | |
845 | encoding, S_ISDIR(attrp->ca_mode), out_descp); | |
846 | if (std_hfs) { | |
847 | FREE(pluskey, M_TEMP); | |
848 | } | |
849 | } | |
850 | attrp->ca_fileid = nextCNID; | |
851 | ||
9bccf70c | 852 | exit: |
91447636 A |
853 | if (mntlock) |
854 | HFS_MOUNT_UNLOCK(hfsmp, TRUE); | |
855 | ||
b4c24cb9 | 856 | (void) BTFlushPath(fcb); |
9bccf70c A |
857 | FREE(bto, M_TEMP); |
858 | ||
859 | return MacToVFSError(result); | |
860 | } | |
861 | ||
862 | ||
863 | /* | |
864 | * cnode_rename - rename a catalog node | |
865 | * | |
866 | * Assumes that the target's directory exists. | |
867 | * | |
868 | * Order of B-tree operations: | |
869 | * 1. BTSearchRecord(from_cnode, &data); | |
870 | * 2. BTInsertRecord(to_cnode, &data); | |
871 | * 3. BTDeleteRecord(from_cnode); | |
872 | * 4. BTDeleteRecord(from_thread); | |
873 | * 5. BTInsertRecord(to_thread); | |
874 | */ | |
55e303ae | 875 | __private_extern__ |
9bccf70c A |
876 | int |
877 | cat_rename ( | |
878 | struct hfsmount * hfsmp, | |
879 | struct cat_desc * from_cdp, | |
880 | struct cat_desc * todir_cdp, | |
881 | struct cat_desc * to_cdp, | |
882 | struct cat_desc * out_cdp ) | |
883 | { | |
884 | struct BTreeIterator * to_iterator = NULL; | |
885 | struct BTreeIterator * from_iterator = NULL; | |
886 | FSBufferDescriptor btdata; | |
887 | CatalogRecord * recp = NULL; | |
888 | HFSPlusCatalogKey * to_key; | |
889 | ExtendedVCB * vcb; | |
890 | FCB * fcb; | |
891 | UInt16 datasize; | |
892 | int result = 0; | |
893 | int sourcegone = 0; | |
894 | int skipthread = 0; | |
895 | int directory = from_cdp->cd_flags & CD_ISDIR; | |
896 | int std_hfs; | |
897 | u_long encoding = 0; | |
898 | ||
899 | vcb = HFSTOVCB(hfsmp); | |
900 | fcb = GetFileControlBlock(vcb->catalogRefNum); | |
901 | std_hfs = (vcb->vcbSigWord == kHFSSigWord); | |
902 | ||
903 | if (from_cdp->cd_namelen == 0 || to_cdp->cd_namelen == 0) | |
904 | return (EINVAL); | |
905 | ||
906 | MALLOC(from_iterator, BTreeIterator *, sizeof(*from_iterator), M_TEMP, M_WAITOK); | |
907 | bzero(from_iterator, sizeof(*from_iterator)); | |
908 | if ((result = buildkey(hfsmp, from_cdp, (HFSPlusCatalogKey *)&from_iterator->key, 0))) | |
909 | goto exit; | |
910 | ||
911 | MALLOC(to_iterator, BTreeIterator *, sizeof(*to_iterator), M_TEMP, M_WAITOK); | |
912 | bzero(to_iterator, sizeof(*to_iterator)); | |
913 | if ((result = buildkey(hfsmp, to_cdp, (HFSPlusCatalogKey *)&to_iterator->key, 0))) | |
914 | goto exit; | |
915 | ||
916 | to_key = (HFSPlusCatalogKey *)&to_iterator->key; | |
917 | MALLOC(recp, CatalogRecord *, sizeof(CatalogRecord), M_TEMP, M_WAITOK); | |
918 | BDINIT(btdata, recp); | |
919 | ||
920 | /* | |
921 | * When moving a directory, make sure its a valid move. | |
922 | */ | |
923 | if (directory && (from_cdp->cd_parentcnid != to_cdp->cd_parentcnid)) { | |
91447636 | 924 | struct BTreeIterator iterator; |
9bccf70c A |
925 | cnid_t cnid = from_cdp->cd_cnid; |
926 | cnid_t pathcnid = todir_cdp->cd_parentcnid; | |
927 | ||
928 | /* First check the obvious ones */ | |
929 | if (cnid == fsRtDirID || | |
930 | cnid == to_cdp->cd_parentcnid || | |
931 | cnid == pathcnid) { | |
932 | result = EINVAL; | |
933 | goto exit; | |
934 | } | |
91447636 | 935 | bzero(&iterator, sizeof(iterator)); |
9bccf70c A |
936 | /* |
937 | * Traverese destination path all the way back to the root | |
938 | * making sure that source directory is not encountered. | |
939 | * | |
940 | */ | |
941 | while (pathcnid > fsRtDirID) { | |
942 | buildthreadkey(pathcnid, std_hfs, | |
943 | (CatalogKey *)&iterator.key); | |
944 | result = BTSearchRecord(fcb, &iterator, &btdata, | |
945 | &datasize, NULL); | |
946 | if (result) goto exit; | |
947 | ||
948 | pathcnid = getparentcnid(recp); | |
949 | if (pathcnid == cnid) { | |
950 | result = EINVAL; | |
951 | goto exit; | |
952 | } | |
953 | } | |
954 | } | |
955 | ||
956 | /* | |
957 | * Step 1: Find cnode data at old location | |
958 | */ | |
959 | result = BTSearchRecord(fcb, from_iterator, &btdata, | |
960 | &datasize, from_iterator); | |
91447636 A |
961 | if (result) { |
962 | if (std_hfs || (result != btNotFound)) | |
963 | goto exit; | |
964 | ||
965 | struct cat_desc temp_desc; | |
966 | ||
967 | /* Probably the node has mangled name */ | |
968 | result = cat_lookupmangled(hfsmp, from_cdp, 0, &temp_desc, NULL, NULL); | |
969 | if (result) | |
970 | goto exit; | |
971 | ||
972 | /* The file has mangled name. Search the cnode data using full name */ | |
973 | bzero(from_iterator, sizeof(*from_iterator)); | |
974 | result = buildkey(hfsmp, &temp_desc, (HFSPlusCatalogKey *)&from_iterator->key, 0); | |
975 | if (result) { | |
976 | cat_releasedesc(&temp_desc); | |
977 | goto exit; | |
978 | } | |
979 | ||
980 | result = BTSearchRecord(fcb, from_iterator, &btdata, &datasize, from_iterator); | |
981 | if (result) { | |
982 | cat_releasedesc(&temp_desc); | |
983 | goto exit; | |
984 | } | |
985 | ||
986 | cat_releasedesc(&temp_desc); | |
987 | } | |
9bccf70c | 988 | |
55e303ae | 989 | /* Update the text encoding (on disk and in descriptor) */ |
9bccf70c A |
990 | if (!std_hfs) { |
991 | encoding = hfs_pickencoding(to_key->nodeName.unicode, | |
992 | to_key->nodeName.length); | |
993 | hfs_setencodingbits(hfsmp, encoding); | |
994 | recp->hfsPlusFile.textEncoding = encoding; | |
995 | if (out_cdp) | |
996 | out_cdp->cd_encoding = encoding; | |
997 | } | |
998 | ||
999 | if (std_hfs && !directory && | |
1000 | !(recp->hfsFile.flags & kHFSThreadExistsMask)) | |
1001 | skipthread = 1; | |
1002 | #if 0 | |
1003 | /* | |
1004 | * If the keys are identical then there's nothing left to do! | |
1005 | * | |
1006 | * update the hint and exit | |
1007 | * | |
1008 | */ | |
1009 | if (std_hfs && hfskeycompare(to_key, iter->key) == 0) | |
1010 | goto exit; | |
1011 | if (!std_hfs && hfspluskeycompare(to_key, iter->key) == 0) | |
1012 | goto exit; | |
1013 | #endif | |
1014 | ||
9bccf70c A |
1015 | /* Step 2: Insert cnode at new location */ |
1016 | result = BTInsertRecord(fcb, to_iterator, &btdata, datasize); | |
1017 | if (result == btExists) { | |
1018 | int fromtype = recp->recordType; | |
1019 | ||
1020 | if (from_cdp->cd_parentcnid != to_cdp->cd_parentcnid) | |
1021 | goto exit; /* EEXIST */ | |
1022 | ||
1023 | /* Find cnode data at new location */ | |
1024 | result = BTSearchRecord(fcb, to_iterator, &btdata, &datasize, NULL); | |
1025 | ||
1026 | if ((fromtype != recp->recordType) || | |
1027 | (from_cdp->cd_cnid != getcnid(recp))) | |
1028 | goto exit; /* EEXIST */ | |
1029 | ||
1030 | /* The old name is a case variant and must be removed */ | |
1031 | result = BTDeleteRecord(fcb, from_iterator); | |
1032 | if (result) | |
1033 | goto exit; | |
1034 | ||
1035 | /* Insert cnode (now that case duplicate is gone) */ | |
1036 | result = BTInsertRecord(fcb, to_iterator, &btdata, datasize); | |
1037 | if (result) { | |
1038 | /* Try and restore original before leaving */ | |
b4c24cb9 A |
1039 | // XXXdbg |
1040 | #if 1 | |
1041 | { | |
1042 | int err; | |
1043 | err = BTInsertRecord(fcb, from_iterator, &btdata, datasize); | |
1044 | if (err) | |
1045 | panic("cat_create: could not undo (BTInsert = %d)", err); | |
1046 | } | |
1047 | #else | |
9bccf70c | 1048 | (void) BTInsertRecord(fcb, from_iterator, &btdata, datasize); |
b4c24cb9 | 1049 | #endif |
9bccf70c A |
1050 | goto exit; |
1051 | } | |
1052 | sourcegone = 1; | |
1053 | } | |
1054 | if (result) | |
1055 | goto exit; | |
1056 | ||
1057 | /* Step 3: Remove cnode from old location */ | |
1058 | if (!sourcegone) { | |
1059 | result = BTDeleteRecord(fcb, from_iterator); | |
1060 | if (result) { | |
1061 | /* Try and delete new record before leaving */ | |
b4c24cb9 A |
1062 | // XXXdbg |
1063 | #if 1 | |
1064 | { | |
1065 | int err; | |
1066 | err = BTDeleteRecord(fcb, to_iterator); | |
1067 | if (err) | |
1068 | panic("cat_create: could not undo (BTDelete = %d)", err); | |
1069 | } | |
1070 | #else | |
9bccf70c | 1071 | (void) BTDeleteRecord(fcb, to_iterator); |
b4c24cb9 | 1072 | #endif |
9bccf70c A |
1073 | goto exit; |
1074 | } | |
1075 | } | |
1076 | ||
1077 | /* #### POINT OF NO RETURN #### */ | |
1078 | ||
1079 | /* | |
1080 | * Step 4: Remove cnode's old thread record | |
1081 | */ | |
1082 | buildthreadkey(from_cdp->cd_cnid, std_hfs, (CatalogKey *)&from_iterator->key); | |
1083 | (void) BTDeleteRecord(fcb, from_iterator); | |
1084 | ||
1085 | /* | |
1086 | * Step 5: Insert cnode's new thread record | |
1087 | * (optional for HFS files) | |
1088 | */ | |
1089 | if (!skipthread) { | |
1090 | datasize = buildthread(&to_iterator->key, recp, std_hfs, directory); | |
1091 | btdata.itemSize = datasize; | |
1092 | buildthreadkey(from_cdp->cd_cnid, std_hfs, (CatalogKey *)&from_iterator->key); | |
1093 | result = BTInsertRecord(fcb, from_iterator, &btdata, datasize); | |
1094 | } | |
1095 | ||
1096 | if (out_cdp) { | |
1097 | HFSPlusCatalogKey * pluskey = NULL; | |
1098 | ||
1099 | if (std_hfs) { | |
1100 | MALLOC(pluskey, HFSPlusCatalogKey *, sizeof(HFSPlusCatalogKey), M_TEMP, M_WAITOK); | |
1101 | promotekey(hfsmp, (HFSCatalogKey *)&to_iterator->key, pluskey, &encoding); | |
55e303ae A |
1102 | |
1103 | /* Save the real encoding hint in the Finder Info (field 4). */ | |
1104 | if (directory && from_cdp->cd_cnid == kHFSRootFolderID) { | |
1105 | u_long realhint; | |
1106 | ||
1107 | realhint = hfs_pickencoding(pluskey->nodeName.unicode, pluskey->nodeName.length); | |
1108 | vcb->vcbFndrInfo[4] = SET_HFS_TEXT_ENCODING(realhint); | |
1109 | } | |
9bccf70c A |
1110 | |
1111 | } else | |
1112 | pluskey = (HFSPlusCatalogKey *)&to_iterator->key; | |
1113 | ||
1114 | builddesc(pluskey, from_cdp->cd_cnid, to_iterator->hint.nodeNum, | |
1115 | encoding, directory, out_cdp); | |
1116 | if (std_hfs) { | |
1117 | FREE(pluskey, M_TEMP); | |
1118 | } | |
1119 | } | |
9bccf70c | 1120 | exit: |
b4c24cb9 | 1121 | (void) BTFlushPath(fcb); |
9bccf70c A |
1122 | if (from_iterator) |
1123 | FREE(from_iterator, M_TEMP); | |
1124 | if (to_iterator) | |
1125 | FREE(to_iterator, M_TEMP); | |
1126 | if (recp) | |
1127 | FREE(recp, M_TEMP); | |
1128 | return MacToVFSError(result); | |
1129 | } | |
1130 | ||
1131 | ||
1132 | /* | |
1133 | * cat_delete - delete a node from the catalog | |
1134 | * | |
1135 | * Order of B-tree operations: | |
1136 | * 1. BTDeleteRecord(cnode); | |
1137 | * 2. BTDeleteRecord(thread); | |
1138 | * 3. BTUpdateRecord(parent); | |
1139 | */ | |
55e303ae | 1140 | __private_extern__ |
9bccf70c A |
1141 | int |
1142 | cat_delete(struct hfsmount *hfsmp, struct cat_desc *descp, struct cat_attr *attrp) | |
1143 | { | |
1144 | ExtendedVCB * vcb; | |
1145 | FCB * fcb; | |
1146 | BTreeIterator *iterator; | |
1147 | cnid_t cnid; | |
1148 | int std_hfs; | |
1149 | int result; | |
1150 | ||
1151 | vcb = HFSTOVCB(hfsmp); | |
1152 | fcb = GetFileControlBlock(vcb->catalogRefNum); | |
1153 | std_hfs = (vcb->vcbSigWord == kHFSSigWord); | |
1154 | ||
1155 | /* Preflight check: | |
1156 | * | |
1157 | * The root directory cannot be deleted | |
1158 | * A directory must be empty | |
1159 | * A file must be zero length (no blocks) | |
1160 | */ | |
9bccf70c | 1161 | if (descp->cd_cnid < kHFSFirstUserCatalogNodeID || |
91447636 | 1162 | descp->cd_parentcnid == kHFSRootParentID) |
9bccf70c A |
1163 | return (EINVAL); |
1164 | ||
1165 | /* XXX Preflight Missing */ | |
1166 | ||
1167 | /* Get space for iterator */ | |
1168 | MALLOC(iterator, BTreeIterator *, sizeof(*iterator), M_TEMP, M_WAITOK); | |
91447636 | 1169 | iterator->hint.nodeNum = 0; |
9bccf70c A |
1170 | |
1171 | /* | |
1172 | * Derive a key from either the file ID (for a virtual inode) | |
1173 | * or the descriptor. | |
1174 | */ | |
1175 | if (descp->cd_namelen == 0) { | |
91447636 A |
1176 | result = getkey(hfsmp, attrp->ca_fileid, (CatalogKey *)&iterator->key); |
1177 | cnid = attrp->ca_fileid; | |
9bccf70c A |
1178 | } else { |
1179 | result = buildkey(hfsmp, descp, (HFSPlusCatalogKey *)&iterator->key, 0); | |
1180 | cnid = descp->cd_cnid; | |
1181 | } | |
1182 | if (result) | |
1183 | goto exit; | |
1184 | ||
1185 | /* Delete record */ | |
1186 | result = BTDeleteRecord(fcb, iterator); | |
91447636 A |
1187 | if (result) { |
1188 | if (std_hfs || (result != btNotFound)) | |
1189 | goto exit; | |
1190 | ||
1191 | struct cat_desc temp_desc; | |
1192 | ||
1193 | /* Probably the node has mangled name */ | |
1194 | result = cat_lookupmangled(hfsmp, descp, 0, &temp_desc, attrp, NULL); | |
1195 | if (result) | |
1196 | goto exit; | |
1197 | ||
1198 | /* The file has mangled name. Delete the file using full name */ | |
1199 | bzero(iterator, sizeof(*iterator)); | |
1200 | result = buildkey(hfsmp, &temp_desc, (HFSPlusCatalogKey *)&iterator->key, 0); | |
1201 | cnid = temp_desc.cd_cnid; | |
1202 | if (result) { | |
1203 | cat_releasedesc(&temp_desc); | |
1204 | goto exit; | |
1205 | } | |
1206 | ||
1207 | result = BTDeleteRecord(fcb, iterator); | |
1208 | if (result) { | |
1209 | cat_releasedesc(&temp_desc); | |
1210 | goto exit; | |
1211 | } | |
1212 | ||
1213 | cat_releasedesc(&temp_desc); | |
1214 | } | |
9bccf70c A |
1215 | |
1216 | /* Delete thread record, ignore errors */ | |
1217 | buildthreadkey(cnid, std_hfs, (CatalogKey *)&iterator->key); | |
1218 | (void) BTDeleteRecord(fcb, iterator); | |
1219 | ||
9bccf70c | 1220 | exit: |
b4c24cb9 | 1221 | (void) BTFlushPath(fcb); |
9bccf70c A |
1222 | FREE(iterator, M_TEMP); |
1223 | ||
1224 | return MacToVFSError(result); | |
1225 | } | |
1226 | ||
1227 | ||
1228 | /* | |
1229 | * cnode_update - update the catalog node described by descp | |
1230 | * using the data from attrp and forkp. | |
1231 | */ | |
55e303ae | 1232 | __private_extern__ |
9bccf70c A |
1233 | int |
1234 | cat_update(struct hfsmount *hfsmp, struct cat_desc *descp, struct cat_attr *attrp, | |
1235 | struct cat_fork *dataforkp, struct cat_fork *rsrcforkp) | |
1236 | { | |
1237 | ExtendedVCB * vcb; | |
1238 | FCB * fcb; | |
1239 | BTreeIterator * iterator; | |
1240 | struct update_state state; | |
1241 | int std_hfs; | |
1242 | int result; | |
1243 | ||
1244 | vcb = HFSTOVCB(hfsmp); | |
1245 | fcb = GetFileControlBlock(vcb->catalogRefNum); | |
1246 | std_hfs = (vcb->vcbSigWord == kHFSSigWord); | |
1247 | ||
1248 | state.s_desc = descp; | |
1249 | state.s_attr = attrp; | |
1250 | state.s_datafork = dataforkp; | |
1251 | state.s_rsrcfork = rsrcforkp; | |
1252 | state.s_hfsmp = hfsmp; | |
1253 | ||
1254 | /* Get space for iterator */ | |
1255 | MALLOC(iterator, BTreeIterator *, sizeof(*iterator), M_TEMP, M_WAITOK); | |
9bccf70c A |
1256 | |
1257 | /* | |
1258 | * For open-deleted files we need to do a lookup by cnid | |
1259 | * (using thread rec). | |
1260 | * | |
1261 | * For hard links, the target of the update is the inode | |
1262 | * itself (not the link record) so a lookup by fileid | |
1263 | * (i.e. thread rec) is needed. | |
1264 | */ | |
1265 | if ((descp->cd_cnid != attrp->ca_fileid) || (descp->cd_namelen == 0)) | |
1266 | result = getkey(hfsmp, attrp->ca_fileid, (CatalogKey *)&iterator->key); | |
1267 | else | |
1268 | result = buildkey(hfsmp, descp, (HFSPlusCatalogKey *)&iterator->key, 0); | |
1269 | if (result) | |
1270 | goto exit; | |
1271 | ||
1272 | /* Pass a node hint */ | |
1273 | iterator->hint.nodeNum = descp->cd_hint; | |
1274 | ||
1275 | result = BTUpdateRecord(fcb, iterator, | |
1276 | (IterateCallBackProcPtr)catrec_update, &state); | |
1277 | if (result) | |
1278 | goto exit; | |
1279 | ||
1280 | /* Update the node hint. */ | |
1281 | descp->cd_hint = iterator->hint.nodeNum; | |
1282 | ||
9bccf70c | 1283 | exit: |
b4c24cb9 | 1284 | (void) BTFlushPath(fcb); |
9bccf70c A |
1285 | FREE(iterator, M_TEMP); |
1286 | ||
1287 | return MacToVFSError(result); | |
1288 | } | |
1289 | ||
1290 | /* | |
1291 | * catrec_update - Update the fields of a catalog record | |
1292 | * This is called from within BTUpdateRecord. | |
1293 | */ | |
1294 | static int | |
91447636 | 1295 | catrec_update(const CatalogKey *ckp, CatalogRecord *crp, struct update_state *state) |
9bccf70c A |
1296 | { |
1297 | struct cat_desc *descp; | |
1298 | struct cat_attr *attrp; | |
1299 | struct cat_fork *forkp; | |
1300 | struct hfsmount *hfsmp; | |
1301 | long blksize; | |
1302 | int i; | |
1303 | ||
1304 | descp = state->s_desc; | |
1305 | attrp = state->s_attr; | |
1306 | hfsmp = state->s_hfsmp; | |
1307 | blksize = HFSTOVCB(hfsmp)->blockSize; | |
1308 | ||
1309 | switch (crp->recordType) { | |
1310 | case kHFSFolderRecord: { | |
1311 | HFSCatalogFolder *dir; | |
1312 | ||
1313 | dir = (struct HFSCatalogFolder *)crp; | |
1314 | /* Do a quick sanity check */ | |
1315 | if ((ckp->hfs.parentID != descp->cd_parentcnid) || | |
1316 | (dir->folderID != descp->cd_cnid)) | |
1317 | return (btNotFound); | |
1318 | dir->valence = attrp->ca_entries; | |
1319 | dir->createDate = UTCToLocal(to_hfs_time(attrp->ca_itime)); | |
1320 | dir->modifyDate = UTCToLocal(to_hfs_time(attrp->ca_mtime)); | |
1321 | dir->backupDate = UTCToLocal(to_hfs_time(attrp->ca_btime)); | |
1322 | bcopy(&attrp->ca_finderinfo[0], &dir->userInfo, 16); | |
1323 | bcopy(&attrp->ca_finderinfo[16], &dir->finderInfo, 16); | |
1324 | break; | |
1325 | } | |
1326 | case kHFSFileRecord: { | |
1327 | HFSCatalogFile *file; | |
1328 | ||
1329 | file = (struct HFSCatalogFile *)crp; | |
1330 | /* Do a quick sanity check */ | |
1331 | if ((ckp->hfs.parentID != descp->cd_parentcnid) || | |
1332 | (file->fileID != attrp->ca_fileid)) | |
1333 | return (btNotFound); | |
1334 | file->createDate = UTCToLocal(to_hfs_time(attrp->ca_itime)); | |
1335 | file->modifyDate = UTCToLocal(to_hfs_time(attrp->ca_mtime)); | |
1336 | file->backupDate = UTCToLocal(to_hfs_time(attrp->ca_btime)); | |
1337 | bcopy(&attrp->ca_finderinfo[0], &file->userInfo, 16); | |
1338 | bcopy(&attrp->ca_finderinfo[16], &file->finderInfo, 16); | |
1339 | if (state->s_rsrcfork) { | |
1340 | forkp = state->s_rsrcfork; | |
1341 | file->rsrcLogicalSize = forkp->cf_size; | |
1342 | file->rsrcPhysicalSize = forkp->cf_blocks * blksize; | |
1343 | for (i = 0; i < kHFSExtentDensity; ++i) { | |
1344 | file->rsrcExtents[i].startBlock = | |
1345 | (u_int16_t)forkp->cf_extents[i].startBlock; | |
1346 | file->rsrcExtents[i].blockCount = | |
1347 | (u_int16_t)forkp->cf_extents[i].blockCount; | |
1348 | } | |
1349 | } | |
1350 | if (state->s_datafork) { | |
1351 | forkp = state->s_datafork; | |
1352 | file->dataLogicalSize = forkp->cf_size; | |
1353 | file->dataPhysicalSize = forkp->cf_blocks * blksize; | |
1354 | for (i = 0; i < kHFSExtentDensity; ++i) { | |
1355 | file->dataExtents[i].startBlock = | |
1356 | (u_int16_t)forkp->cf_extents[i].startBlock; | |
1357 | file->dataExtents[i].blockCount = | |
1358 | (u_int16_t)forkp->cf_extents[i].blockCount; | |
1359 | } | |
1360 | } | |
1361 | break; | |
1362 | } | |
1363 | case kHFSPlusFolderRecord: { | |
1364 | HFSPlusCatalogFolder *dir; | |
1365 | ||
1366 | dir = (struct HFSPlusCatalogFolder *)crp; | |
1367 | /* Do a quick sanity check */ | |
1368 | if ((ckp->hfsPlus.parentID != descp->cd_parentcnid) || | |
91447636 | 1369 | (dir->folderID != descp->cd_cnid)) |
9bccf70c | 1370 | return (btNotFound); |
91447636 | 1371 | dir->flags = attrp->ca_recflags; |
9bccf70c A |
1372 | dir->valence = attrp->ca_entries; |
1373 | dir->createDate = to_hfs_time(attrp->ca_itime); | |
1374 | dir->contentModDate = to_hfs_time(attrp->ca_mtime); | |
1375 | dir->backupDate = to_hfs_time(attrp->ca_btime); | |
1376 | dir->accessDate = to_hfs_time(attrp->ca_atime); | |
91447636 | 1377 | attrp->ca_atimeondisk = attrp->ca_atime; |
9bccf70c A |
1378 | dir->attributeModDate = to_hfs_time(attrp->ca_ctime); |
1379 | dir->textEncoding = descp->cd_encoding; | |
91447636 | 1380 | dir->attrBlocks = attrp->ca_attrblks; |
9bccf70c A |
1381 | bcopy(&attrp->ca_finderinfo[0], &dir->userInfo, 32); |
1382 | /* | |
1383 | * Update the BSD Info if it was already initialized on | |
1384 | * disk or if the runtime values have been modified. | |
1385 | * | |
1386 | * If the BSD info was already initialized, but | |
1387 | * MNT_UNKNOWNPERMISSIONS is set, then the runtime IDs are | |
1388 | * probably different than what was on disk. We don't want | |
1389 | * to overwrite the on-disk values (so if we turn off | |
1390 | * MNT_UNKNOWNPERMISSIONS, the old IDs get used again). | |
1391 | * This way, we can still change fields like the mode or | |
1392 | * dates even when MNT_UNKNOWNPERMISSIONS is set. | |
1393 | * | |
1394 | * Note that if MNT_UNKNOWNPERMISSIONS is set, hfs_chown | |
1395 | * won't change the uid or gid from their defaults. So, if | |
1396 | * the BSD info wasn't set, and the runtime values are not | |
1397 | * default, then what changed was the mode or flags. We | |
1398 | * have to set the uid and gid to something, so use the | |
1399 | * supplied values (which will be default), which has the | |
1400 | * same effect as creating a new file while | |
1401 | * MNT_UNKNOWNPERMISSIONS is set. | |
1402 | */ | |
1403 | if ((dir->bsdInfo.fileMode != 0) || | |
1404 | (attrp->ca_flags != 0) || | |
1405 | (attrp->ca_uid != hfsmp->hfs_uid) || | |
1406 | (attrp->ca_gid != hfsmp->hfs_gid) || | |
1407 | ((attrp->ca_mode & ALLPERMS) != | |
1408 | (hfsmp->hfs_dir_mask & ACCESSPERMS))) { | |
1409 | if ((dir->bsdInfo.fileMode == 0) || | |
91447636 | 1410 | (((unsigned int)vfs_flags(HFSTOVFS(hfsmp))) & MNT_UNKNOWNPERMISSIONS) == 0) { |
9bccf70c A |
1411 | dir->bsdInfo.ownerID = attrp->ca_uid; |
1412 | dir->bsdInfo.groupID = attrp->ca_gid; | |
1413 | } | |
1414 | dir->bsdInfo.ownerFlags = attrp->ca_flags & 0x000000FF; | |
1415 | dir->bsdInfo.adminFlags = attrp->ca_flags >> 16; | |
1416 | dir->bsdInfo.fileMode = attrp->ca_mode; | |
1417 | } | |
1418 | break; | |
1419 | } | |
1420 | case kHFSPlusFileRecord: { | |
1421 | HFSPlusCatalogFile *file; | |
1422 | ||
1423 | file = (struct HFSPlusCatalogFile *)crp; | |
1424 | /* Do a quick sanity check */ | |
1425 | if (file->fileID != attrp->ca_fileid) | |
1426 | return (btNotFound); | |
91447636 | 1427 | file->flags = attrp->ca_recflags; |
9bccf70c A |
1428 | file->createDate = to_hfs_time(attrp->ca_itime); |
1429 | file->contentModDate = to_hfs_time(attrp->ca_mtime); | |
1430 | file->backupDate = to_hfs_time(attrp->ca_btime); | |
1431 | file->accessDate = to_hfs_time(attrp->ca_atime); | |
91447636 | 1432 | attrp->ca_atimeondisk = attrp->ca_atime; |
9bccf70c A |
1433 | file->attributeModDate = to_hfs_time(attrp->ca_ctime); |
1434 | file->textEncoding = descp->cd_encoding; | |
91447636 | 1435 | file->attrBlocks = attrp->ca_attrblks; |
9bccf70c A |
1436 | bcopy(&attrp->ca_finderinfo[0], &file->userInfo, 32); |
1437 | /* | |
1438 | * Update the BSD Info if it was already initialized on | |
1439 | * disk or if the runtime values have been modified. | |
1440 | * | |
1441 | * If the BSD info was already initialized, but | |
1442 | * MNT_UNKNOWNPERMISSIONS is set, then the runtime IDs are | |
1443 | * probably different than what was on disk. We don't want | |
1444 | * to overwrite the on-disk values (so if we turn off | |
1445 | * MNT_UNKNOWNPERMISSIONS, the old IDs get used again). | |
1446 | * This way, we can still change fields like the mode or | |
1447 | * dates even when MNT_UNKNOWNPERMISSIONS is set. | |
1448 | * | |
1449 | * Note that if MNT_UNKNOWNPERMISSIONS is set, hfs_chown | |
1450 | * won't change the uid or gid from their defaults. So, if | |
1451 | * the BSD info wasn't set, and the runtime values are not | |
1452 | * default, then what changed was the mode or flags. We | |
1453 | * have to set the uid and gid to something, so use the | |
1454 | * supplied values (which will be default), which has the | |
1455 | * same effect as creating a new file while | |
1456 | * MNT_UNKNOWNPERMISSIONS is set. | |
1457 | */ | |
1458 | if ((file->bsdInfo.fileMode != 0) || | |
1459 | (attrp->ca_flags != 0) || | |
1460 | (attrp->ca_uid != hfsmp->hfs_uid) || | |
1461 | (attrp->ca_gid != hfsmp->hfs_gid) || | |
1462 | ((attrp->ca_mode & ALLPERMS) != | |
1463 | (hfsmp->hfs_file_mask & ACCESSPERMS))) { | |
1464 | if ((file->bsdInfo.fileMode == 0) || | |
91447636 | 1465 | (((unsigned int)vfs_flags(HFSTOVFS(hfsmp))) & MNT_UNKNOWNPERMISSIONS) == 0) { |
9bccf70c A |
1466 | file->bsdInfo.ownerID = attrp->ca_uid; |
1467 | file->bsdInfo.groupID = attrp->ca_gid; | |
1468 | } | |
1469 | file->bsdInfo.ownerFlags = attrp->ca_flags & 0x000000FF; | |
1470 | file->bsdInfo.adminFlags = attrp->ca_flags >> 16; | |
1471 | file->bsdInfo.fileMode = attrp->ca_mode; | |
1472 | } | |
1473 | if (state->s_rsrcfork) { | |
1474 | forkp = state->s_rsrcfork; | |
1475 | file->resourceFork.logicalSize = forkp->cf_size; | |
1476 | file->resourceFork.totalBlocks = forkp->cf_blocks; | |
1477 | bcopy(&forkp->cf_extents[0], &file->resourceFork.extents, | |
1478 | sizeof(HFSPlusExtentRecord)); | |
55e303ae A |
1479 | /* Push blocks read to disk */ |
1480 | file->resourceFork.clumpSize = | |
1481 | howmany(forkp->cf_bytesread, blksize); | |
9bccf70c A |
1482 | } |
1483 | if (state->s_datafork) { | |
1484 | forkp = state->s_datafork; | |
1485 | file->dataFork.logicalSize = forkp->cf_size; | |
1486 | file->dataFork.totalBlocks = forkp->cf_blocks; | |
1487 | bcopy(&forkp->cf_extents[0], &file->dataFork.extents, | |
1488 | sizeof(HFSPlusExtentRecord)); | |
55e303ae | 1489 | /* Push blocks read to disk */ |
91447636 | 1490 | file->dataFork.clumpSize = |
55e303ae | 1491 | howmany(forkp->cf_bytesread, blksize); |
9bccf70c A |
1492 | } |
1493 | ||
1494 | if ((file->resourceFork.extents[0].startBlock != 0) && | |
1495 | (file->resourceFork.extents[0].startBlock == | |
1496 | file->dataFork.extents[0].startBlock)) | |
1497 | panic("catrec_update: rsrc fork == data fork"); | |
1498 | ||
1499 | /* Synchronize the lock state */ | |
1500 | if (attrp->ca_flags & (SF_IMMUTABLE | UF_IMMUTABLE)) | |
1501 | file->flags |= kHFSFileLockedMask; | |
1502 | else | |
1503 | file->flags &= ~kHFSFileLockedMask; | |
1504 | ||
1505 | /* Push out special field if necessary */ | |
1506 | if (S_ISBLK(attrp->ca_mode) || S_ISCHR(attrp->ca_mode)) | |
1507 | file->bsdInfo.special.rawDevice = attrp->ca_rdev; | |
1508 | else if (descp->cd_cnid != attrp->ca_fileid | |
1509 | || attrp->ca_nlink == 2) | |
1510 | file->bsdInfo.special.linkCount = attrp->ca_nlink; | |
1511 | break; | |
1512 | } | |
1513 | default: | |
1514 | return (btNotFound); | |
1515 | } | |
1516 | return (0); | |
1517 | } | |
1518 | ||
1519 | /* | |
91447636 A |
1520 | * Callback to collect directory entries. |
1521 | * Called with readattr_state for each item in a directory. | |
9bccf70c A |
1522 | */ |
1523 | struct readattr_state { | |
1524 | struct hfsmount *hfsmp; | |
1525 | struct cat_entrylist *list; | |
1526 | cnid_t dir_cnid; | |
1527 | int stdhfs; | |
1528 | int error; | |
1529 | }; | |
1530 | ||
1531 | static int | |
91447636 A |
1532 | cat_readattr(const CatalogKey *key, const CatalogRecord *rec, |
1533 | struct readattr_state *state) | |
9bccf70c A |
1534 | { |
1535 | struct cat_entrylist *list = state->list; | |
1536 | struct hfsmount *hfsmp = state->hfsmp; | |
1537 | struct cat_entry *cep; | |
1538 | cnid_t parentcnid; | |
1539 | ||
1540 | if (list->realentries >= list->maxentries) | |
1541 | return (0); /* stop */ | |
1542 | ||
1543 | parentcnid = state->stdhfs ? key->hfs.parentID : key->hfsPlus.parentID; | |
1544 | ||
1545 | switch(rec->recordType) { | |
1546 | case kHFSPlusFolderRecord: | |
1547 | case kHFSPlusFileRecord: | |
1548 | case kHFSFolderRecord: | |
1549 | case kHFSFileRecord: | |
1550 | if (parentcnid != state->dir_cnid) { | |
1551 | state->error = ENOENT; | |
1552 | return (0); /* stop */ | |
1553 | } | |
1554 | break; | |
1555 | default: | |
1556 | state->error = ENOENT; | |
1557 | return (0); /* stop */ | |
1558 | } | |
1559 | ||
b4c24cb9 | 1560 | /* Hide the private meta data directory and journal files */ |
91447636 | 1561 | if (parentcnid == kHFSRootFolderID) { |
b4c24cb9 | 1562 | if ((rec->recordType == kHFSPlusFolderRecord) && |
55e303ae | 1563 | (rec->hfsPlusFolder.folderID == hfsmp->hfs_privdir_desc.cd_cnid)) { |
b4c24cb9 A |
1564 | return (1); /* continue */ |
1565 | } | |
1566 | if (hfsmp->jnl && | |
1567 | (rec->recordType == kHFSPlusFileRecord) && | |
1568 | ((rec->hfsPlusFile.fileID == hfsmp->hfs_jnlfileid) || | |
1569 | (rec->hfsPlusFile.fileID == hfsmp->hfs_jnlinfoblkid))) { | |
1570 | ||
1571 | return (1); /* continue */ | |
1572 | } | |
9bccf70c A |
1573 | } |
1574 | ||
1575 | cep = &list->entry[list->realentries++]; | |
1576 | ||
1577 | if (state->stdhfs) { | |
1578 | struct HFSPlusCatalogFile cnoderec; | |
1579 | HFSPlusCatalogKey * pluskey; | |
1580 | long encoding; | |
1581 | ||
1582 | promoteattr(hfsmp, rec, &cnoderec); | |
1583 | getbsdattr(hfsmp, &cnoderec, &cep->ce_attr); | |
1584 | ||
1585 | MALLOC(pluskey, HFSPlusCatalogKey *, sizeof(HFSPlusCatalogKey), M_TEMP, M_WAITOK); | |
1586 | promotekey(hfsmp, (HFSCatalogKey *)key, pluskey, &encoding); | |
91447636 | 1587 | builddesc(pluskey, getcnid(rec), 0, encoding, isadir(rec), &cep->ce_desc); |
9bccf70c A |
1588 | FREE(pluskey, M_TEMP); |
1589 | ||
1590 | if (rec->recordType == kHFSFileRecord) { | |
1591 | int blksize = HFSTOVCB(hfsmp)->blockSize; | |
1592 | ||
1593 | cep->ce_datasize = rec->hfsFile.dataLogicalSize; | |
1594 | cep->ce_datablks = rec->hfsFile.dataPhysicalSize / blksize; | |
1595 | cep->ce_rsrcsize = rec->hfsFile.rsrcLogicalSize; | |
1596 | cep->ce_rsrcblks = rec->hfsFile.rsrcPhysicalSize / blksize; | |
1597 | } | |
1598 | } else { | |
1599 | getbsdattr(hfsmp, (struct HFSPlusCatalogFile *)rec, &cep->ce_attr); | |
91447636 | 1600 | builddesc((HFSPlusCatalogKey *)key, getcnid(rec), 0, getencoding(rec), |
9bccf70c A |
1601 | isadir(rec), &cep->ce_desc); |
1602 | ||
1603 | if (rec->recordType == kHFSPlusFileRecord) { | |
1604 | cep->ce_datasize = rec->hfsPlusFile.dataFork.logicalSize; | |
1605 | cep->ce_datablks = rec->hfsPlusFile.dataFork.totalBlocks; | |
1606 | cep->ce_rsrcsize = rec->hfsPlusFile.resourceFork.logicalSize; | |
1607 | cep->ce_rsrcblks = rec->hfsPlusFile.resourceFork.totalBlocks; | |
1608 | ||
1609 | /* Save link reference for later processing. */ | |
1610 | if ((SWAP_BE32(rec->hfsPlusFile.userInfo.fdType) == kHardLinkFileType) | |
1611 | && (SWAP_BE32(rec->hfsPlusFile.userInfo.fdCreator) == kHFSPlusCreator)) | |
1612 | cep->ce_attr.ca_rdev = rec->hfsPlusFile.bsdInfo.special.iNodeNum; | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | return (list->realentries < list->maxentries); | |
1617 | } | |
1618 | ||
1619 | /* | |
91447636 A |
1620 | * Pack a cat_entrylist buffer with attributes from the catalog |
1621 | * | |
9bccf70c A |
1622 | * Note: index is zero relative |
1623 | */ | |
55e303ae | 1624 | __private_extern__ |
9bccf70c | 1625 | int |
91447636 | 1626 | cat_getentriesattr(struct hfsmount *hfsmp, directoryhint_t *dirhint, struct cat_entrylist *ce_list) |
9bccf70c A |
1627 | { |
1628 | FCB* fcb; | |
1629 | CatalogKey * key; | |
1630 | BTreeIterator * iterator; | |
1631 | struct readattr_state state; | |
1632 | cnid_t parentcnid; | |
1633 | int i; | |
1634 | int std_hfs; | |
91447636 A |
1635 | int index; |
1636 | int have_key; | |
9bccf70c A |
1637 | int result = 0; |
1638 | ||
1639 | ce_list->realentries = 0; | |
1640 | ||
1641 | fcb = GetFileControlBlock(HFSTOVCB(hfsmp)->catalogRefNum); | |
1642 | std_hfs = (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord); | |
91447636 | 1643 | parentcnid = dirhint->dh_desc.cd_parentcnid; |
9bccf70c A |
1644 | |
1645 | state.hfsmp = hfsmp; | |
1646 | state.list = ce_list; | |
1647 | state.dir_cnid = parentcnid; | |
1648 | state.stdhfs = std_hfs; | |
1649 | state.error = 0; | |
1650 | ||
1651 | MALLOC(iterator, BTreeIterator *, sizeof(*iterator), M_TEMP, M_WAITOK); | |
1652 | bzero(iterator, sizeof(*iterator)); | |
1653 | key = (CatalogKey *)&iterator->key; | |
91447636 A |
1654 | have_key = 0; |
1655 | iterator->hint.nodeNum = dirhint->dh_desc.cd_hint; | |
1656 | index = dirhint->dh_index + 1; | |
9bccf70c A |
1657 | |
1658 | /* | |
91447636 | 1659 | * Attempt to build a key from cached filename |
9bccf70c | 1660 | */ |
91447636 A |
1661 | if (dirhint->dh_desc.cd_namelen != 0) { |
1662 | if (buildkey(hfsmp, &dirhint->dh_desc, (HFSPlusCatalogKey *)key, 0) == 0) { | |
1663 | have_key = 1; | |
1664 | } | |
1665 | } | |
1666 | ||
1667 | /* | |
1668 | * If the last entry wasn't cached then position the btree iterator | |
1669 | */ | |
1670 | if ((index == 0) || !have_key) { | |
9bccf70c | 1671 | /* |
91447636 A |
1672 | * Position the iterator at the directory's thread record. |
1673 | * (i.e. just before the first entry) | |
9bccf70c | 1674 | */ |
91447636 | 1675 | buildthreadkey(dirhint->dh_desc.cd_parentcnid, (hfsmp->hfs_flags & HFS_STANDARD), key); |
9bccf70c | 1676 | result = BTSearchRecord(fcb, iterator, NULL, NULL, iterator); |
91447636 A |
1677 | if (result) { |
1678 | result = MacToVFSError(result); | |
1679 | goto exit; | |
1680 | } | |
1681 | ||
9bccf70c A |
1682 | /* |
1683 | * Iterate until we reach the entry just | |
1684 | * before the one we want to start with. | |
1685 | */ | |
91447636 A |
1686 | if (index > 0) { |
1687 | struct position_state ps; | |
1688 | ||
1689 | ps.error = 0; | |
1690 | ps.count = 0; | |
1691 | ps.index = index; | |
1692 | ps.parentID = dirhint->dh_desc.cd_parentcnid; | |
1693 | ps.hfsmp = hfsmp; | |
1694 | ||
1695 | result = BTIterateRecords(fcb, kBTreeNextRecord, iterator, | |
1696 | (IterateCallBackProcPtr)cat_findposition, &ps); | |
1697 | if (ps.error) | |
1698 | result = ps.error; | |
1699 | else | |
1700 | result = MacToVFSError(result); | |
1701 | if (result) { | |
1702 | result = MacToVFSError(result); | |
1703 | goto exit; | |
1704 | } | |
9bccf70c A |
1705 | } |
1706 | } | |
1707 | ||
91447636 | 1708 | /* Fill list with entries starting at iterator->key. */ |
9bccf70c | 1709 | result = BTIterateRecords(fcb, kBTreeNextRecord, iterator, |
91447636 | 1710 | (IterateCallBackProcPtr)cat_readattr, &state); |
9bccf70c A |
1711 | |
1712 | if (state.error) | |
1713 | result = state.error; | |
1714 | else if (ce_list->realentries == 0) | |
1715 | result = ENOENT; | |
1716 | else | |
1717 | result = MacToVFSError(result); | |
1718 | ||
1719 | if (std_hfs) | |
1720 | goto exit; | |
1721 | ||
1722 | /* | |
1723 | * Resolve any hard links. | |
1724 | */ | |
91447636 | 1725 | for (i = 0; i < (int)ce_list->realentries; ++i) { |
9bccf70c A |
1726 | struct FndrFileInfo *fip; |
1727 | struct cat_entry *cep; | |
1728 | struct HFSPlusCatalogFile filerec; | |
1729 | ||
1730 | cep = &ce_list->entry[i]; | |
1731 | if (!S_ISREG(cep->ce_attr.ca_mode)) | |
1732 | continue; | |
1733 | ||
1734 | /* Note: Finder info is still in Big Endian */ | |
1735 | fip = (struct FndrFileInfo *)&cep->ce_attr.ca_finderinfo; | |
1736 | ||
1737 | /* Check for hard link signature. */ | |
1738 | if ((cep->ce_attr.ca_rdev != 0) | |
1739 | && (SWAP_BE32(fip->fdType) == kHardLinkFileType) | |
1740 | && (SWAP_BE32(fip->fdCreator) == kHFSPlusCreator) | |
91447636 A |
1741 | && ((cep->ce_attr.ca_itime == (time_t)HFSTOVCB(hfsmp)->vcbCrDate) || |
1742 | (cep->ce_attr.ca_itime == (time_t)hfsmp->hfs_metadata_createdate))) { | |
9bccf70c A |
1743 | |
1744 | if (resolvelink(hfsmp, cep->ce_attr.ca_rdev, &filerec) != 0) | |
1745 | continue; | |
1746 | /* Repack entry from inode record. */ | |
1747 | getbsdattr(hfsmp, &filerec, &cep->ce_attr); | |
1748 | cep->ce_datasize = filerec.dataFork.logicalSize; | |
1749 | cep->ce_datablks = filerec.dataFork.totalBlocks; | |
1750 | cep->ce_rsrcsize = filerec.resourceFork.logicalSize; | |
1751 | cep->ce_rsrcblks = filerec.resourceFork.totalBlocks; | |
1752 | } | |
1753 | } | |
1754 | exit: | |
1755 | FREE(iterator, M_TEMP); | |
1756 | ||
1757 | return MacToVFSError(result); | |
1758 | } | |
1759 | ||
91447636 | 1760 | #define SMALL_DIRENTRY_SIZE (int)(sizeof(struct dirent) - (MAXNAMLEN + 1) + 8) |
9bccf70c | 1761 | |
91447636 A |
1762 | /* |
1763 | * Callback to pack directory entries. | |
1764 | * Called with packdirentry_state for each item in a directory. | |
1765 | */ | |
9bccf70c | 1766 | |
91447636 A |
1767 | /* Hard link information collected during cat_getdirentries. */ |
1768 | struct linkinfo { | |
1769 | u_long link_ref; | |
1770 | user_addr_t dirent_addr; | |
1771 | }; | |
1772 | typedef struct linkinfo linkinfo_t; | |
1773 | ||
1774 | /* State information for the cat_packdirentry callback function. */ | |
1775 | struct packdirentry_state { | |
1776 | int cbs_extended; | |
1777 | u_int32_t cbs_parentID; | |
1778 | u_int32_t cbs_index; | |
1779 | uio_t cbs_uio; | |
1780 | ExtendedVCB * cbs_hfsmp; | |
1781 | int cbs_result; | |
1782 | int32_t cbs_nlinks; | |
1783 | int32_t cbs_maxlinks; | |
1784 | linkinfo_t * cbs_linkinfo; | |
1785 | struct cat_desc * cbs_desc; | |
1786 | // struct dirent * cbs_stdentry; | |
1787 | struct direntry * cbs_direntry; | |
55e303ae | 1788 | }; |
9bccf70c A |
1789 | |
1790 | static int | |
91447636 A |
1791 | cat_packdirentry(const CatalogKey *ckp, const CatalogRecord *crp, |
1792 | struct packdirentry_state *state) | |
9bccf70c | 1793 | { |
55e303ae | 1794 | struct hfsmount *hfsmp; |
9bccf70c | 1795 | CatalogName *cnp; |
91447636 | 1796 | cnid_t curID; |
9bccf70c A |
1797 | OSErr result; |
1798 | struct dirent catent; | |
91447636 | 1799 | struct direntry * entry = NULL; |
55e303ae A |
1800 | time_t itime; |
1801 | u_long ilinkref = 0; | |
91447636 A |
1802 | cnid_t cnid; |
1803 | int hide = 0; | |
1804 | u_int8_t type; | |
1805 | u_int8_t is_mangled = 0; | |
1806 | char *nameptr; | |
1807 | user_addr_t uiobase; | |
1808 | size_t namelen = 0; | |
1809 | size_t maxnamelen; | |
1810 | size_t uiosize = 0; | |
1811 | caddr_t uioaddr; | |
9bccf70c | 1812 | |
91447636 A |
1813 | hfsmp = state->cbs_hfsmp; |
1814 | ||
1815 | if (hfsmp->hfs_flags & HFS_STANDARD) | |
9bccf70c | 1816 | curID = ckp->hfs.parentID; |
91447636 A |
1817 | else |
1818 | curID = ckp->hfsPlus.parentID; | |
9bccf70c A |
1819 | |
1820 | /* We're done when parent directory changes */ | |
1821 | if (state->cbs_parentID != curID) { | |
9bccf70c | 1822 | state->cbs_result = ENOENT; |
9bccf70c A |
1823 | return (0); /* stop */ |
1824 | } | |
1825 | ||
91447636 A |
1826 | if (state->cbs_extended) { |
1827 | entry = state->cbs_direntry; | |
1828 | nameptr = &entry->d_name[0]; | |
1829 | maxnamelen = NAME_MAX; | |
1830 | } else { | |
1831 | nameptr = &catent.d_name[0]; | |
1832 | maxnamelen = NAME_MAX; | |
1833 | } | |
1834 | ||
1835 | if (!(hfsmp->hfs_flags & HFS_STANDARD)) { | |
9bccf70c A |
1836 | switch(crp->recordType) { |
1837 | case kHFSPlusFolderRecord: | |
91447636 A |
1838 | type = DT_DIR; |
1839 | cnid = crp->hfsPlusFolder.folderID; | |
1840 | /* Hide our private meta data directory */ | |
1841 | if ((curID == kHFSRootFolderID) && | |
1842 | (cnid == hfsmp->hfs_privdir_desc.cd_cnid)) { | |
1843 | hide = 1; | |
1844 | } | |
1845 | ||
9bccf70c A |
1846 | break; |
1847 | case kHFSPlusFileRecord: | |
55e303ae | 1848 | itime = to_bsd_time(crp->hfsPlusFile.createDate); |
55e303ae A |
1849 | /* |
1850 | * When a hardlink link is encountered save its link ref. | |
1851 | */ | |
1852 | if ((SWAP_BE32(crp->hfsPlusFile.userInfo.fdType) == kHardLinkFileType) && | |
1853 | (SWAP_BE32(crp->hfsPlusFile.userInfo.fdCreator) == kHFSPlusCreator) && | |
91447636 A |
1854 | ((itime == (time_t)hfsmp->hfs_itime) || |
1855 | (itime == (time_t)hfsmp->hfs_metadata_createdate))) { | |
55e303ae A |
1856 | ilinkref = crp->hfsPlusFile.bsdInfo.special.iNodeNum; |
1857 | } | |
91447636 A |
1858 | type = MODE_TO_DT(crp->hfsPlusFile.bsdInfo.fileMode); |
1859 | cnid = crp->hfsPlusFile.fileID; | |
1860 | /* Hide the journal files */ | |
1861 | if ((curID == kHFSRootFolderID) && | |
1862 | (hfsmp->jnl) && | |
1863 | ((cnid == hfsmp->hfs_jnlfileid) || | |
1864 | (cnid == hfsmp->hfs_jnlinfoblkid))) { | |
1865 | hide = 1; | |
1866 | } | |
9bccf70c A |
1867 | break; |
1868 | default: | |
1869 | return (0); /* stop */ | |
1870 | }; | |
1871 | ||
1872 | cnp = (CatalogName*) &ckp->hfsPlus.nodeName; | |
1873 | result = utf8_encodestr(cnp->ustr.unicode, cnp->ustr.length * sizeof(UniChar), | |
91447636 | 1874 | nameptr, &namelen, maxnamelen + 1, ':', 0); |
9bccf70c A |
1875 | if (result == ENAMETOOLONG) { |
1876 | result = ConvertUnicodeToUTF8Mangled(cnp->ustr.length * sizeof(UniChar), | |
91447636 A |
1877 | cnp->ustr.unicode, maxnamelen + 1, |
1878 | (ByteCount*)&namelen, nameptr, | |
1879 | cnid); | |
1880 | is_mangled = 1; | |
9bccf70c A |
1881 | } |
1882 | } else { /* hfs */ | |
1883 | switch(crp->recordType) { | |
1884 | case kHFSFolderRecord: | |
91447636 A |
1885 | type = DT_DIR; |
1886 | cnid = crp->hfsFolder.folderID; | |
9bccf70c A |
1887 | break; |
1888 | case kHFSFileRecord: | |
91447636 A |
1889 | type = DT_REG; |
1890 | cnid = crp->hfsFile.fileID; | |
9bccf70c A |
1891 | break; |
1892 | default: | |
1893 | return (0); /* stop */ | |
1894 | }; | |
1895 | ||
1896 | cnp = (CatalogName*) ckp->hfs.nodeName; | |
91447636 A |
1897 | result = hfs_to_utf8(hfsmp, cnp->pstr, maxnamelen + 1, |
1898 | (ByteCount *)&namelen, nameptr); | |
9bccf70c A |
1899 | /* |
1900 | * When an HFS name cannot be encoded with the current | |
1901 | * volume encoding we use MacRoman as a fallback. | |
1902 | */ | |
1903 | if (result) | |
91447636 A |
1904 | result = mac_roman_to_utf8(cnp->pstr, maxnamelen + 1, |
1905 | (ByteCount *)&namelen, nameptr); | |
9bccf70c A |
1906 | } |
1907 | ||
91447636 A |
1908 | if (state->cbs_extended) { |
1909 | entry->d_type = type; | |
1910 | entry->d_namlen = namelen; | |
1911 | entry->d_reclen = uiosize = EXT_DIRENT_LEN(namelen); | |
1912 | if (hide) | |
1913 | entry->d_fileno = 0; /* file number = 0 means skip entry */ | |
1914 | else | |
1915 | entry->d_fileno = cnid; | |
55e303ae | 1916 | |
91447636 A |
1917 | /* |
1918 | * The index is 1 relative and includes "." and ".." | |
1919 | * | |
1920 | * Also stuff the cnid in the upper 32 bits of the cookie. | |
1921 | */ | |
1922 | entry->d_seekoff = (state->cbs_index + 3) | ((u_int64_t)cnid << 32); | |
1923 | uioaddr = (caddr_t) entry; | |
1924 | } else { | |
1925 | catent.d_type = type; | |
1926 | catent.d_namlen = namelen; | |
1927 | catent.d_reclen = uiosize = STD_DIRENT_LEN(namelen); | |
1928 | if (hide) | |
1929 | catent.d_fileno = 0; /* file number = 0 means skip entry */ | |
1930 | else | |
1931 | catent.d_fileno = cnid; | |
1932 | uioaddr = (caddr_t) &catent; | |
b4c24cb9 A |
1933 | } |
1934 | ||
91447636 A |
1935 | /* Save current base address for post processing of hard-links. */ |
1936 | uiobase = uio_curriovbase(state->cbs_uio); | |
9bccf70c | 1937 | |
91447636 A |
1938 | /* If this entry won't fit then we're done */ |
1939 | if ((uiosize > uio_resid(state->cbs_uio)) || | |
1940 | (ilinkref != 0 && state->cbs_nlinks == state->cbs_maxlinks)) { | |
9bccf70c | 1941 | return (0); /* stop */ |
91447636 | 1942 | } |
9bccf70c | 1943 | |
91447636 A |
1944 | state->cbs_result = uiomove(uioaddr, uiosize, state->cbs_uio); |
1945 | if (state->cbs_result == 0) { | |
1946 | ++state->cbs_index; | |
9bccf70c | 1947 | |
91447636 A |
1948 | /* Remember previous entry */ |
1949 | state->cbs_desc->cd_cnid = cnid; | |
1950 | if (type == DT_DIR) { | |
1951 | state->cbs_desc->cd_flags |= CD_ISDIR; | |
1952 | } else { | |
1953 | state->cbs_desc->cd_flags &= ~CD_ISDIR; | |
1954 | } | |
1955 | if (state->cbs_desc->cd_nameptr != NULL) { | |
1956 | vfs_removename(state->cbs_desc->cd_nameptr); | |
1957 | } | |
1958 | #if 0 | |
1959 | state->cbs_desc->cd_encoding = xxxx; | |
1960 | #endif | |
1961 | if (!is_mangled) { | |
1962 | state->cbs_desc->cd_namelen = namelen; | |
1963 | state->cbs_desc->cd_nameptr = vfs_addname(nameptr, namelen, 0, 0); | |
1964 | } else { | |
1965 | /* Store unmangled name for the directory hint else it will | |
1966 | * restart readdir at the last location again | |
1967 | */ | |
1968 | char *new_nameptr; | |
1969 | size_t bufsize; | |
1970 | ||
1971 | cnp = (CatalogName *)&ckp->hfsPlus.nodeName; | |
1972 | bufsize = 1 + utf8_encodelen(cnp->ustr.unicode, | |
1973 | cnp->ustr.length * sizeof(UniChar), | |
1974 | ':', 0); | |
1975 | MALLOC(new_nameptr, char *, bufsize, M_TEMP, M_WAITOK); | |
1976 | result = utf8_encodestr(cnp->ustr.unicode, | |
1977 | cnp->ustr.length * sizeof(UniChar), | |
1978 | new_nameptr, &namelen, | |
1979 | bufsize, ':', 0); | |
1980 | ||
1981 | state->cbs_desc->cd_namelen = namelen; | |
1982 | state->cbs_desc->cd_nameptr = vfs_addname(new_nameptr, namelen, 0, 0); | |
1983 | ||
1984 | FREE(new_nameptr, M_TEMP); | |
1985 | } | |
1986 | } | |
55e303ae A |
1987 | /* |
1988 | * Record any hard links for post processing. | |
1989 | */ | |
1990 | if ((ilinkref != 0) && | |
1991 | (state->cbs_result == 0) && | |
1992 | (state->cbs_nlinks < state->cbs_maxlinks)) { | |
1993 | state->cbs_linkinfo[state->cbs_nlinks].dirent_addr = uiobase; | |
1994 | state->cbs_linkinfo[state->cbs_nlinks].link_ref = ilinkref; | |
1995 | state->cbs_nlinks++; | |
1996 | } | |
1997 | ||
91447636 | 1998 | /* Continue iteration if there's room */ |
9bccf70c | 1999 | return (state->cbs_result == 0 && |
91447636 | 2000 | uio_resid(state->cbs_uio) >= SMALL_DIRENTRY_SIZE); |
9bccf70c A |
2001 | } |
2002 | ||
91447636 | 2003 | |
9bccf70c | 2004 | /* |
91447636 | 2005 | * Pack a uio buffer with directory entries from the catalog |
9bccf70c | 2006 | */ |
55e303ae | 2007 | __private_extern__ |
9bccf70c | 2008 | int |
91447636 A |
2009 | cat_getdirentries(struct hfsmount *hfsmp, int entrycnt, directoryhint_t *dirhint, |
2010 | uio_t uio, int extended, int * items) | |
9bccf70c | 2011 | { |
91447636 | 2012 | FCB* fcb; |
9bccf70c | 2013 | BTreeIterator * iterator; |
91447636 A |
2014 | CatalogKey * key; |
2015 | struct packdirentry_state state; | |
55e303ae A |
2016 | void * buffer; |
2017 | int bufsize; | |
91447636 | 2018 | int maxlinks; |
9bccf70c | 2019 | int result; |
91447636 A |
2020 | int index; |
2021 | int have_key; | |
2022 | ||
2023 | fcb = GetFileControlBlock(hfsmp->hfs_catalog_vp); | |
9bccf70c | 2024 | |
55e303ae | 2025 | /* Get a buffer for collecting link info and for a btree iterator */ |
91447636 A |
2026 | maxlinks = MIN(entrycnt, uio_resid(uio) / SMALL_DIRENTRY_SIZE); |
2027 | bufsize = (maxlinks * sizeof(linkinfo_t)) + sizeof(*iterator); | |
2028 | if (extended) { | |
2029 | bufsize += sizeof(struct direntry); | |
2030 | } | |
55e303ae A |
2031 | MALLOC(buffer, void *, bufsize, M_TEMP, M_WAITOK); |
2032 | bzero(buffer, bufsize); | |
2033 | ||
91447636 | 2034 | state.cbs_extended = extended; |
55e303ae | 2035 | state.cbs_nlinks = 0; |
91447636 A |
2036 | state.cbs_maxlinks = maxlinks; |
2037 | state.cbs_linkinfo = (linkinfo_t *) buffer; | |
9bccf70c | 2038 | |
91447636 A |
2039 | iterator = (BTreeIterator *) ((char *)buffer + (maxlinks * sizeof(linkinfo_t))); |
2040 | key = (CatalogKey *)&iterator->key; | |
2041 | have_key = 0; | |
2042 | index = dirhint->dh_index + 1; | |
2043 | if (extended) { | |
2044 | state.cbs_direntry = (struct direntry *)((char *)buffer + sizeof(BTreeIterator)); | |
2045 | } | |
2046 | /* | |
2047 | * Attempt to build a key from cached filename | |
2048 | */ | |
2049 | if (dirhint->dh_desc.cd_namelen != 0) { | |
2050 | if (buildkey(hfsmp, &dirhint->dh_desc, (HFSPlusCatalogKey *)key, 0) == 0) { | |
2051 | have_key = 1; | |
2052 | } | |
2053 | } | |
9bccf70c | 2054 | |
91447636 A |
2055 | /* |
2056 | * If the last entry wasn't cached then position the btree iterator | |
2057 | */ | |
2058 | if ((index == 0) || !have_key) { | |
2059 | /* | |
2060 | * Position the iterator at the directory's thread record. | |
2061 | * (i.e. just before the first entry) | |
2062 | */ | |
2063 | buildthreadkey(dirhint->dh_desc.cd_parentcnid, (hfsmp->hfs_flags & HFS_STANDARD), key); | |
2064 | result = BTSearchRecord(fcb, iterator, NULL, NULL, iterator); | |
2065 | if (result) { | |
2066 | result = MacToVFSError(result); | |
2067 | goto cleanup; | |
2068 | } | |
2069 | ||
2070 | /* | |
2071 | * Iterate until we reach the entry just | |
2072 | * before the one we want to start with. | |
2073 | */ | |
2074 | if (index > 0) { | |
2075 | struct position_state ps; | |
2076 | ||
2077 | ps.error = 0; | |
2078 | ps.count = 0; | |
2079 | ps.index = index; | |
2080 | ps.parentID = dirhint->dh_desc.cd_parentcnid; | |
2081 | ps.hfsmp = hfsmp; | |
2082 | ||
2083 | result = BTIterateRecords(fcb, kBTreeNextRecord, iterator, | |
2084 | (IterateCallBackProcPtr)cat_findposition, &ps); | |
2085 | if (ps.error) | |
2086 | result = ps.error; | |
2087 | else | |
2088 | result = MacToVFSError(result); | |
2089 | if (result) { | |
2090 | result = MacToVFSError(result); | |
2091 | goto cleanup; | |
2092 | } | |
2093 | } | |
b4c24cb9 A |
2094 | } |
2095 | ||
91447636 A |
2096 | state.cbs_index = index; |
2097 | state.cbs_hfsmp = hfsmp; | |
9bccf70c | 2098 | state.cbs_uio = uio; |
91447636 | 2099 | state.cbs_desc = &dirhint->dh_desc; |
9bccf70c | 2100 | state.cbs_result = 0; |
91447636 | 2101 | state.cbs_parentID = dirhint->dh_desc.cd_parentcnid; |
9bccf70c | 2102 | |
91447636 A |
2103 | /* |
2104 | * Process as many entries as possible starting at iterator->key. | |
2105 | */ | |
2106 | result = BTIterateRecords(fcb, kBTreeNextRecord, iterator, | |
2107 | (IterateCallBackProcPtr)cat_packdirentry, &state); | |
9bccf70c | 2108 | |
91447636 A |
2109 | /* Note that state.cbs_index is still valid on errors */ |
2110 | *items = state.cbs_index - index; | |
2111 | index = state.cbs_index; | |
2112 | ||
2113 | /* Finish updating the catalog iterator. */ | |
2114 | dirhint->dh_desc.cd_hint = iterator->hint.nodeNum; | |
2115 | dirhint->dh_desc.cd_flags |= CD_DECOMPOSED; | |
2116 | dirhint->dh_index = index - 1; | |
2117 | ||
55e303ae A |
2118 | /* |
2119 | * Post process any hard links to get the real file id. | |
2120 | */ | |
2121 | if (state.cbs_nlinks > 0) { | |
91447636 A |
2122 | u_int32_t fileid = 0; |
2123 | user_addr_t address; | |
55e303ae | 2124 | int i; |
55e303ae A |
2125 | |
2126 | for (i = 0; i < state.cbs_nlinks; ++i) { | |
55e303ae A |
2127 | if (resolvelinkid(hfsmp, state.cbs_linkinfo[i].link_ref, &fileid) != 0) |
2128 | continue; | |
91447636 A |
2129 | /* This assumes that d_ino is always first field. */ |
2130 | address = state.cbs_linkinfo[i].dirent_addr; | |
2131 | if (address == (user_addr_t)0) | |
2132 | continue; | |
2133 | if (uio_isuserspace(uio)) { | |
2134 | (void) copyout(&fileid, address, | |
2135 | extended ? sizeof(ino64_t) : sizeof(ino_t)); | |
2136 | } else /* system space */ { | |
2137 | ino64_t *inoptr = (ino64_t *)CAST_DOWN(caddr_t, address); | |
2138 | *inoptr = fileid; | |
2139 | } | |
55e303ae A |
2140 | } |
2141 | } | |
91447636 | 2142 | |
9bccf70c A |
2143 | if (state.cbs_result) |
2144 | result = state.cbs_result; | |
2145 | else | |
2146 | result = MacToVFSError(result); | |
2147 | ||
2148 | if (result == ENOENT) { | |
9bccf70c A |
2149 | result = 0; |
2150 | } | |
2151 | ||
9bccf70c | 2152 | cleanup: |
55e303ae | 2153 | FREE(buffer, M_TEMP); |
9bccf70c A |
2154 | |
2155 | return (result); | |
2156 | } | |
2157 | ||
2158 | ||
91447636 A |
2159 | /* |
2160 | * Callback to establish directory position. | |
2161 | * Called with position_state for each item in a directory. | |
2162 | */ | |
2163 | static int | |
2164 | cat_findposition(const CatalogKey *ckp, const CatalogRecord *crp, | |
2165 | struct position_state *state) | |
2166 | { | |
2167 | cnid_t curID; | |
2168 | ||
2169 | if (state->hfsmp->hfs_flags & HFS_STANDARD) | |
2170 | curID = ckp->hfs.parentID; | |
2171 | else | |
2172 | curID = ckp->hfsPlus.parentID; | |
2173 | ||
2174 | /* Make sure parent directory didn't change */ | |
2175 | if (state->parentID != curID) { | |
2176 | state->error = EINVAL; | |
2177 | return (0); /* stop */ | |
2178 | } | |
2179 | ||
2180 | /* Count this entry */ | |
2181 | switch(crp->recordType) { | |
2182 | case kHFSPlusFolderRecord: | |
2183 | case kHFSPlusFileRecord: | |
2184 | case kHFSFolderRecord: | |
2185 | case kHFSFileRecord: | |
2186 | ++state->count; | |
2187 | break; | |
2188 | default: | |
2189 | printf("cat_findposition: invalid record type %d in dir %d\n", | |
2190 | crp->recordType, curID); | |
2191 | state->error = EINVAL; | |
2192 | return (0); /* stop */ | |
2193 | }; | |
2194 | ||
2195 | return (state->count < state->index); | |
2196 | } | |
2197 | ||
2198 | ||
55e303ae A |
2199 | /* |
2200 | * cat_binarykeycompare - compare two HFS Plus catalog keys. | |
2201 | ||
91447636 | 2202 | * The name portion of the key is compared using a 16-bit binary comparison. |
55e303ae A |
2203 | * This is called from the b-tree code. |
2204 | */ | |
2205 | __private_extern__ | |
2206 | int | |
2207 | cat_binarykeycompare(HFSPlusCatalogKey *searchKey, HFSPlusCatalogKey *trialKey) | |
2208 | { | |
2209 | u_int32_t searchParentID, trialParentID; | |
2210 | int result; | |
2211 | ||
2212 | searchParentID = searchKey->parentID; | |
2213 | trialParentID = trialKey->parentID; | |
2214 | result = 0; | |
2215 | ||
2216 | if (searchParentID > trialParentID) { | |
2217 | ++result; | |
2218 | } else if (searchParentID < trialParentID) { | |
2219 | --result; | |
2220 | } else { | |
2221 | u_int16_t * str1 = &searchKey->nodeName.unicode[0]; | |
2222 | u_int16_t * str2 = &trialKey->nodeName.unicode[0]; | |
2223 | int length1 = searchKey->nodeName.length; | |
2224 | int length2 = trialKey->nodeName.length; | |
2225 | u_int16_t c1, c2; | |
2226 | int length; | |
2227 | ||
2228 | if (length1 < length2) { | |
2229 | length = length1; | |
2230 | --result; | |
2231 | } else if (length1 > length2) { | |
2232 | length = length2; | |
2233 | ++result; | |
2234 | } else { | |
2235 | length = length1; | |
2236 | } | |
2237 | ||
2238 | while (length--) { | |
2239 | c1 = *(str1++); | |
2240 | c2 = *(str2++); | |
2241 | ||
2242 | if (c1 > c2) { | |
2243 | result = 1; | |
2244 | break; | |
2245 | } | |
2246 | if (c1 < c2) { | |
2247 | result = -1; | |
2248 | break; | |
2249 | } | |
2250 | } | |
2251 | } | |
2252 | ||
2253 | return result; | |
2254 | } | |
2255 | ||
2256 | ||
91447636 A |
2257 | /* |
2258 | * Compare two standard HFS catalog keys | |
2259 | * | |
2260 | * Result: +n search key > trial key | |
2261 | * 0 search key = trial key | |
2262 | * -n search key < trial key | |
2263 | */ | |
2264 | int | |
2265 | CompareCatalogKeys(HFSCatalogKey *searchKey, HFSCatalogKey *trialKey) | |
2266 | { | |
2267 | cnid_t searchParentID, trialParentID; | |
2268 | int result; | |
2269 | ||
2270 | searchParentID = searchKey->parentID; | |
2271 | trialParentID = trialKey->parentID; | |
2272 | ||
2273 | if (searchParentID > trialParentID) | |
2274 | result = 1; | |
2275 | else if (searchParentID < trialParentID) | |
2276 | result = -1; | |
2277 | else /* parent dirID's are equal, compare names */ | |
2278 | result = FastRelString(searchKey->nodeName, trialKey->nodeName); | |
2279 | ||
2280 | return result; | |
2281 | } | |
2282 | ||
2283 | ||
2284 | /* | |
2285 | * Compare two HFS+ catalog keys | |
2286 | * | |
2287 | * Result: +n search key > trial key | |
2288 | * 0 search key = trial key | |
2289 | * -n search key < trial key | |
2290 | */ | |
2291 | int | |
2292 | CompareExtendedCatalogKeys(HFSPlusCatalogKey *searchKey, HFSPlusCatalogKey *trialKey) | |
2293 | { | |
2294 | cnid_t searchParentID, trialParentID; | |
2295 | int result; | |
2296 | ||
2297 | searchParentID = searchKey->parentID; | |
2298 | trialParentID = trialKey->parentID; | |
2299 | ||
2300 | if (searchParentID > trialParentID) { | |
2301 | result = 1; | |
2302 | } | |
2303 | else if (searchParentID < trialParentID) { | |
2304 | result = -1; | |
2305 | } else { | |
2306 | /* parent node ID's are equal, compare names */ | |
2307 | if ( searchKey->nodeName.length == 0 || trialKey->nodeName.length == 0 ) | |
2308 | result = searchKey->nodeName.length - trialKey->nodeName.length; | |
2309 | else | |
2310 | result = FastUnicodeCompare(&searchKey->nodeName.unicode[0], | |
2311 | searchKey->nodeName.length, | |
2312 | &trialKey->nodeName.unicode[0], | |
2313 | trialKey->nodeName.length); | |
2314 | } | |
2315 | ||
2316 | return result; | |
2317 | } | |
2318 | ||
2319 | ||
9bccf70c A |
2320 | /* |
2321 | * buildkey - build a Catalog b-tree key from a cnode descriptor | |
2322 | */ | |
2323 | static int | |
2324 | buildkey(struct hfsmount *hfsmp, struct cat_desc *descp, | |
2325 | HFSPlusCatalogKey *key, int retry) | |
2326 | { | |
2327 | int utf8_flags = 0; | |
2328 | int result = 0; | |
2329 | size_t unicodeBytes = 0; | |
2330 | ||
2331 | if (descp->cd_namelen == 0 || descp->cd_nameptr[0] == '\0') | |
2332 | return (EINVAL); /* invalid name */ | |
2333 | ||
2334 | key->parentID = descp->cd_parentcnid; | |
2335 | key->nodeName.length = 0; | |
2336 | /* | |
2337 | * Convert filename from UTF-8 into Unicode | |
2338 | */ | |
2339 | ||
2340 | if ((descp->cd_flags & CD_DECOMPOSED) == 0) | |
2341 | utf8_flags |= UTF_DECOMPOSED; | |
2342 | result = utf8_decodestr(descp->cd_nameptr, descp->cd_namelen, | |
2343 | key->nodeName.unicode, &unicodeBytes, | |
2344 | sizeof(key->nodeName.unicode), ':', utf8_flags); | |
2345 | key->nodeName.length = unicodeBytes / sizeof(UniChar); | |
2346 | key->keyLength = kHFSPlusCatalogKeyMinimumLength + unicodeBytes; | |
2347 | if (result) { | |
2348 | if (result != ENAMETOOLONG) | |
2349 | result = EINVAL; /* name has invalid characters */ | |
2350 | return (result); | |
2351 | } | |
2352 | ||
2353 | /* | |
2354 | * For HFS volumes convert to an HFS compatible key | |
2355 | * | |
2356 | * XXX need to save the encoding that succeeded | |
2357 | */ | |
2358 | if (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord) { | |
2359 | HFSCatalogKey hfskey; | |
2360 | ||
2361 | bzero(&hfskey, sizeof(hfskey)); | |
2362 | hfskey.keyLength = kHFSCatalogKeyMinimumLength; | |
2363 | hfskey.parentID = key->parentID; | |
2364 | hfskey.nodeName[0] = 0; | |
2365 | if (key->nodeName.length > 0) { | |
2366 | if (unicode_to_hfs(HFSTOVCB(hfsmp), | |
2367 | key->nodeName.length * 2, | |
2368 | key->nodeName.unicode, | |
2369 | &hfskey.nodeName[0], retry) != 0) { | |
2370 | return (EINVAL); | |
2371 | } | |
2372 | hfskey.keyLength += hfskey.nodeName[0]; | |
2373 | } | |
2374 | bcopy(&hfskey, key, sizeof(hfskey)); | |
2375 | } | |
2376 | return (0); | |
2377 | } | |
2378 | ||
2379 | ||
2380 | /* | |
2381 | * Resolve hard link reference to obtain the inode record. | |
2382 | */ | |
2383 | __private_extern__ | |
2384 | int | |
2385 | resolvelink(struct hfsmount *hfsmp, u_long linkref, struct HFSPlusCatalogFile *recp) | |
2386 | { | |
2387 | FSBufferDescriptor btdata; | |
2388 | struct BTreeIterator *iterator; | |
2389 | struct cat_desc idesc; | |
2390 | char inodename[32]; | |
2391 | int result = 0; | |
2392 | ||
2393 | BDINIT(btdata, recp); | |
2394 | MAKE_INODE_NAME(inodename, linkref); | |
2395 | ||
2396 | /* Get space for iterator */ | |
2397 | MALLOC(iterator, BTreeIterator *, sizeof(*iterator), M_TEMP, M_WAITOK); | |
2398 | bzero(iterator, sizeof(*iterator)); | |
2399 | ||
2400 | /* Build a descriptor for private dir. */ | |
55e303ae | 2401 | idesc.cd_parentcnid = hfsmp->hfs_privdir_desc.cd_cnid; |
9bccf70c A |
2402 | idesc.cd_nameptr = inodename; |
2403 | idesc.cd_namelen = strlen(inodename); | |
2404 | idesc.cd_flags = 0; | |
2405 | idesc.cd_hint = 0; | |
2406 | idesc.cd_encoding = 0; | |
2407 | (void) buildkey(hfsmp, &idesc, (HFSPlusCatalogKey *)&iterator->key, 0); | |
2408 | ||
2409 | result = BTSearchRecord(VTOF(HFSTOVCB(hfsmp)->catalogRefNum), iterator, | |
2410 | &btdata, NULL, NULL); | |
2411 | ||
2412 | if (result == 0) { | |
2413 | /* Make sure there's a reference */ | |
2414 | if (recp->bsdInfo.special.linkCount == 0) | |
2415 | recp->bsdInfo.special.linkCount = 2; | |
2416 | } else { | |
2417 | printf("HFS resolvelink: can't find %s\n", inodename); | |
2418 | } | |
2419 | ||
2420 | FREE(iterator, M_TEMP); | |
2421 | ||
2422 | return (result ? ENOENT : 0); | |
2423 | } | |
2424 | ||
55e303ae A |
2425 | /* |
2426 | * Resolve hard link reference to obtain the inode number. | |
2427 | */ | |
2428 | static int | |
2429 | resolvelinkid(struct hfsmount *hfsmp, u_long linkref, ino_t *ino) | |
2430 | { | |
2431 | struct HFSPlusCatalogFile record; | |
2432 | int error; | |
2433 | ||
2434 | error = resolvelink(hfsmp, linkref, &record); | |
2435 | if (error == 0) { | |
2436 | if (record.fileID == 0) | |
2437 | error = ENOENT; | |
2438 | else | |
2439 | *ino = record.fileID; | |
2440 | } | |
2441 | return (error); | |
2442 | } | |
2443 | ||
9bccf70c A |
2444 | /* |
2445 | * getkey - get a key from id by doing a thread lookup | |
2446 | */ | |
2447 | static int | |
2448 | getkey(struct hfsmount *hfsmp, cnid_t cnid, CatalogKey * key) | |
2449 | { | |
2450 | struct BTreeIterator * iterator; | |
2451 | FSBufferDescriptor btdata; | |
2452 | UInt16 datasize; | |
2453 | CatalogKey * keyp; | |
2454 | CatalogRecord * recp; | |
2455 | int result; | |
2456 | int std_hfs; | |
2457 | ||
2458 | std_hfs = (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord); | |
2459 | ||
2460 | MALLOC(iterator, BTreeIterator *, sizeof(*iterator), M_TEMP, M_WAITOK); | |
2461 | bzero(iterator, sizeof(*iterator)); | |
2462 | buildthreadkey(cnid, std_hfs, (CatalogKey *)&iterator->key); | |
2463 | ||
2464 | MALLOC(recp, CatalogRecord *, sizeof(CatalogRecord), M_TEMP, M_WAITOK); | |
2465 | BDINIT(btdata, recp); | |
2466 | ||
2467 | result = BTSearchRecord(VTOF(HFSTOVCB(hfsmp)->catalogRefNum), iterator, | |
2468 | &btdata, &datasize, iterator); | |
2469 | if (result) | |
2470 | goto exit; | |
2471 | ||
2472 | /* Turn thread record into a cnode key (in place) */ | |
2473 | switch (recp->recordType) { | |
2474 | case kHFSFileThreadRecord: | |
2475 | case kHFSFolderThreadRecord: | |
2476 | keyp = (CatalogKey *)((char *)&recp->hfsThread.reserved + 6); | |
2477 | keyp->hfs.keyLength = kHFSCatalogKeyMinimumLength + keyp->hfs.nodeName[0]; | |
2478 | bcopy(keyp, key, keyp->hfs.keyLength + 1); | |
2479 | break; | |
2480 | ||
2481 | case kHFSPlusFileThreadRecord: | |
2482 | case kHFSPlusFolderThreadRecord: | |
2483 | keyp = (CatalogKey *)&recp->hfsPlusThread.reserved; | |
2484 | keyp->hfsPlus.keyLength = kHFSPlusCatalogKeyMinimumLength + | |
2485 | (keyp->hfsPlus.nodeName.length * 2); | |
2486 | bcopy(keyp, key, keyp->hfsPlus.keyLength + 2); | |
2487 | break; | |
2488 | ||
2489 | default: | |
2490 | result = ENOENT; | |
2491 | break; | |
2492 | } | |
2493 | ||
2494 | exit: | |
2495 | FREE(iterator, M_TEMP); | |
2496 | FREE(recp, M_TEMP); | |
2497 | ||
2498 | return MacToVFSError(result); | |
2499 | } | |
2500 | ||
91447636 A |
2501 | /* |
2502 | * getkeyplusattr - From id, fetch the key and the bsd attrs for a file/dir (could pass | |
2503 | * null arguments to cat_idlookup instead, but we save around 10% by not building the | |
2504 | * cat_desc here). Both key and attrp must point to real structures. | |
2505 | */ | |
2506 | __private_extern__ | |
2507 | int | |
2508 | cat_getkeyplusattr(struct hfsmount *hfsmp, cnid_t cnid, CatalogKey * key, struct cat_attr *attrp) | |
2509 | { | |
2510 | int result; | |
2511 | ||
2512 | result = getkey(hfsmp, cnid, key); | |
2513 | ||
2514 | if (result == 0) { | |
2515 | result = cat_lookupbykey(hfsmp, key, 0, 0, NULL, attrp, NULL, NULL); | |
2516 | } | |
2517 | ||
2518 | return MacToVFSError(result); | |
2519 | } | |
2520 | ||
9bccf70c A |
2521 | |
2522 | /* | |
2523 | * buildrecord - build a default catalog directory or file record | |
2524 | */ | |
2525 | static void | |
2526 | buildrecord(struct cat_attr *attrp, cnid_t cnid, int std_hfs, u_int32_t encoding, | |
2527 | CatalogRecord *crp, int *recordSize) | |
2528 | { | |
2529 | int type = attrp->ca_mode & S_IFMT; | |
2530 | u_int32_t createtime = to_hfs_time(attrp->ca_itime); | |
2531 | ||
2532 | if (std_hfs) { | |
2533 | createtime = UTCToLocal(createtime); | |
2534 | if (type == S_IFDIR) { | |
2535 | bzero(crp, sizeof(HFSCatalogFolder)); | |
2536 | crp->recordType = kHFSFolderRecord; | |
2537 | crp->hfsFolder.folderID = cnid; | |
2538 | crp->hfsFolder.createDate = createtime; | |
2539 | crp->hfsFolder.modifyDate = createtime; | |
2540 | bcopy(attrp->ca_finderinfo, &crp->hfsFolder.userInfo, 32); | |
2541 | *recordSize = sizeof(HFSCatalogFolder); | |
2542 | } else { | |
2543 | bzero(crp, sizeof(HFSCatalogFile)); | |
2544 | crp->recordType = kHFSFileRecord; | |
2545 | crp->hfsFile.fileID = cnid; | |
2546 | crp->hfsFile.createDate = createtime; | |
2547 | crp->hfsFile.modifyDate = createtime; | |
2548 | bcopy(attrp->ca_finderinfo, &crp->hfsFile.userInfo, 16); | |
2549 | bcopy(&attrp->ca_finderinfo[16], &crp->hfsFile.finderInfo, 16); | |
2550 | *recordSize = sizeof(HFSCatalogFile); | |
2551 | } | |
2552 | } else { | |
2553 | struct HFSPlusBSDInfo * bsdp = NULL; | |
2554 | struct FndrFileInfo * fip = NULL; | |
2555 | ||
2556 | if (type == S_IFDIR) { | |
9bccf70c | 2557 | crp->recordType = kHFSPlusFolderRecord; |
91447636 A |
2558 | crp->hfsPlusFolder.flags = 0; |
2559 | crp->hfsPlusFolder.valence = 0; | |
9bccf70c A |
2560 | crp->hfsPlusFolder.folderID = cnid; |
2561 | crp->hfsPlusFolder.createDate = createtime; | |
2562 | crp->hfsPlusFolder.contentModDate = createtime; | |
9bccf70c | 2563 | crp->hfsPlusFolder.attributeModDate = createtime; |
91447636 A |
2564 | crp->hfsPlusFolder.accessDate = createtime; |
2565 | crp->hfsPlusFolder.backupDate = 0; | |
9bccf70c | 2566 | crp->hfsPlusFolder.textEncoding = encoding; |
91447636 | 2567 | crp->hfsPlusFolder.attrBlocks = 0; |
9bccf70c A |
2568 | bcopy(attrp->ca_finderinfo, &crp->hfsPlusFolder.userInfo, 32); |
2569 | bsdp = &crp->hfsPlusFolder.bsdInfo; | |
91447636 | 2570 | bsdp->special.rawDevice = 0; |
9bccf70c A |
2571 | *recordSize = sizeof(HFSPlusCatalogFolder); |
2572 | } else { | |
9bccf70c | 2573 | crp->recordType = kHFSPlusFileRecord; |
91447636 A |
2574 | crp->hfsPlusFile.flags = kHFSThreadExistsMask; |
2575 | crp->hfsPlusFile.reserved1 = 0; | |
9bccf70c A |
2576 | crp->hfsPlusFile.fileID = cnid; |
2577 | crp->hfsPlusFile.createDate = createtime; | |
2578 | crp->hfsPlusFile.contentModDate = createtime; | |
2579 | crp->hfsPlusFile.accessDate = createtime; | |
2580 | crp->hfsPlusFile.attributeModDate = createtime; | |
91447636 | 2581 | crp->hfsPlusFile.backupDate = 0; |
9bccf70c | 2582 | crp->hfsPlusFile.textEncoding = encoding; |
91447636 | 2583 | crp->hfsPlusFile.attrBlocks = 0; |
9bccf70c | 2584 | bsdp = &crp->hfsPlusFile.bsdInfo; |
91447636 | 2585 | bsdp->special.rawDevice = 0; |
9bccf70c A |
2586 | switch(type) { |
2587 | case S_IFBLK: | |
2588 | case S_IFCHR: | |
2589 | /* BLK/CHR need to save the device info */ | |
2590 | bsdp->special.rawDevice = attrp->ca_rdev; | |
91447636 | 2591 | bzero(&crp->hfsPlusFile.userInfo, 32); |
9bccf70c A |
2592 | break; |
2593 | case S_IFREG: | |
2594 | /* Hardlink links need to save the linkref */ | |
2595 | fip = (FndrFileInfo *)&attrp->ca_finderinfo; | |
2596 | if ((SWAP_BE32(fip->fdType) == kHardLinkFileType) && | |
2597 | (SWAP_BE32(fip->fdCreator) == kHFSPlusCreator)) { | |
2598 | bsdp->special.iNodeNum = attrp->ca_rdev; | |
2599 | } | |
2600 | bcopy(attrp->ca_finderinfo, &crp->hfsPlusFile.userInfo, 32); | |
2601 | break; | |
2602 | case S_IFLNK: | |
2603 | /* Symlinks also have a type and creator */ | |
2604 | bcopy(attrp->ca_finderinfo, &crp->hfsPlusFile.userInfo, 32); | |
2605 | break; | |
2606 | } | |
91447636 | 2607 | bzero(&crp->hfsPlusFile.dataFork, 2*sizeof(HFSPlusForkData)); |
9bccf70c A |
2608 | *recordSize = sizeof(HFSPlusCatalogFile); |
2609 | } | |
2610 | bsdp->ownerID = attrp->ca_uid; | |
2611 | bsdp->groupID = attrp->ca_gid; | |
2612 | bsdp->fileMode = attrp->ca_mode; | |
2613 | bsdp->adminFlags = attrp->ca_flags >> 16; | |
2614 | bsdp->ownerFlags = attrp->ca_flags & 0x000000FF; | |
2615 | } | |
2616 | } | |
2617 | ||
2618 | ||
2619 | /* | |
2620 | * builddesc - build a cnode descriptor from an HFS+ key | |
2621 | */ | |
2622 | static int | |
2623 | builddesc(const HFSPlusCatalogKey *key, cnid_t cnid, u_long hint, u_long encoding, | |
2624 | int isdir, struct cat_desc *descp) | |
2625 | { | |
2626 | int result = 0; | |
2627 | char * nameptr; | |
91447636 | 2628 | size_t bufsize; |
9bccf70c | 2629 | size_t utf8len; |
55e303ae | 2630 | char tmpbuff[128]; |
9bccf70c A |
2631 | |
2632 | /* guess a size... */ | |
2633 | bufsize = (3 * key->nodeName.length) + 1; | |
91447636 | 2634 | if (bufsize >= sizeof(tmpbuff) - 1) { |
55e303ae A |
2635 | MALLOC(nameptr, char *, bufsize, M_TEMP, M_WAITOK); |
2636 | } else { | |
2637 | nameptr = &tmpbuff[0]; | |
2638 | } | |
9bccf70c A |
2639 | |
2640 | result = utf8_encodestr(key->nodeName.unicode, | |
2641 | key->nodeName.length * sizeof(UniChar), | |
2642 | nameptr, (size_t *)&utf8len, | |
2643 | bufsize, ':', 0); | |
2644 | ||
2645 | if (result == ENAMETOOLONG) { | |
2646 | bufsize = 1 + utf8_encodelen(key->nodeName.unicode, | |
2647 | key->nodeName.length * sizeof(UniChar), | |
2648 | ':', 0); | |
2649 | FREE(nameptr, M_TEMP); | |
2650 | MALLOC(nameptr, char *, bufsize, M_TEMP, M_WAITOK); | |
2651 | ||
2652 | result = utf8_encodestr(key->nodeName.unicode, | |
2653 | key->nodeName.length * sizeof(UniChar), | |
2654 | nameptr, (size_t *)&utf8len, | |
2655 | bufsize, ':', 0); | |
2656 | } | |
2657 | descp->cd_parentcnid = key->parentID; | |
91447636 | 2658 | descp->cd_nameptr = vfs_addname(nameptr, utf8len, 0, 0); |
9bccf70c A |
2659 | descp->cd_namelen = utf8len; |
2660 | descp->cd_cnid = cnid; | |
2661 | descp->cd_hint = hint; | |
2662 | descp->cd_flags = CD_DECOMPOSED | CD_HASBUF; | |
2663 | if (isdir) | |
55e303ae | 2664 | descp->cd_flags |= CD_ISDIR; |
9bccf70c | 2665 | descp->cd_encoding = encoding; |
55e303ae A |
2666 | if (nameptr != &tmpbuff[0]) { |
2667 | FREE(nameptr, M_TEMP); | |
2668 | } | |
9bccf70c A |
2669 | return result; |
2670 | } | |
2671 | ||
2672 | ||
2673 | /* | |
2674 | * getbsdattr - get attributes in bsd format | |
2675 | * | |
2676 | */ | |
2677 | static void | |
2678 | getbsdattr(struct hfsmount *hfsmp, const struct HFSPlusCatalogFile *crp, struct cat_attr * attrp) | |
2679 | { | |
2680 | int isDirectory = (crp->recordType == kHFSPlusFolderRecord); | |
2681 | const struct HFSPlusBSDInfo *bsd = &crp->bsdInfo; | |
2682 | ||
91447636 | 2683 | attrp->ca_recflags = crp->flags; |
9bccf70c A |
2684 | attrp->ca_nlink = 1; |
2685 | attrp->ca_atime = to_bsd_time(crp->accessDate); | |
91447636 | 2686 | attrp->ca_atimeondisk = attrp->ca_atime; |
9bccf70c | 2687 | attrp->ca_mtime = to_bsd_time(crp->contentModDate); |
9bccf70c A |
2688 | attrp->ca_ctime = to_bsd_time(crp->attributeModDate); |
2689 | attrp->ca_itime = to_bsd_time(crp->createDate); | |
2690 | attrp->ca_btime = to_bsd_time(crp->backupDate); | |
2691 | ||
2692 | if ((bsd->fileMode & S_IFMT) == 0) { | |
2693 | attrp->ca_flags = 0; | |
2694 | attrp->ca_uid = hfsmp->hfs_uid; | |
2695 | attrp->ca_gid = hfsmp->hfs_gid; | |
2696 | if (isDirectory) | |
2697 | attrp->ca_mode = S_IFDIR | (hfsmp->hfs_dir_mask & ACCESSPERMS); | |
2698 | else | |
2699 | attrp->ca_mode = S_IFREG | (hfsmp->hfs_file_mask & ACCESSPERMS); | |
2700 | attrp->ca_rdev = 0; | |
2701 | } else { | |
2702 | attrp->ca_rdev = 0; | |
2703 | attrp->ca_uid = bsd->ownerID; | |
2704 | attrp->ca_gid = bsd->groupID; | |
2705 | attrp->ca_flags = bsd->ownerFlags | (bsd->adminFlags << 16); | |
2706 | attrp->ca_mode = (mode_t)bsd->fileMode; | |
2707 | switch (attrp->ca_mode & S_IFMT) { | |
2708 | case S_IFCHR: /* fall through */ | |
2709 | case S_IFBLK: | |
2710 | attrp->ca_rdev = bsd->special.rawDevice; | |
2711 | break; | |
2712 | case S_IFREG: | |
2713 | /* Pick up the hard link count */ | |
2714 | if (bsd->special.linkCount > 0) | |
2715 | attrp->ca_nlink = bsd->special.linkCount; | |
2716 | break; | |
2717 | } | |
2718 | ||
91447636 | 2719 | if (((unsigned int)vfs_flags(HFSTOVFS(hfsmp))) & MNT_UNKNOWNPERMISSIONS) { |
9bccf70c A |
2720 | /* |
2721 | * Override the permissions as determined by the mount auguments | |
2722 | * in ALMOST the same way unset permissions are treated but keep | |
2723 | * track of whether or not the file or folder is hfs locked | |
2724 | * by leaving the h_pflags field unchanged from what was unpacked | |
2725 | * out of the catalog. | |
2726 | */ | |
2727 | attrp->ca_uid = hfsmp->hfs_uid; | |
2728 | attrp->ca_gid = hfsmp->hfs_gid; | |
2729 | } | |
2730 | } | |
2731 | ||
2732 | if (isDirectory) { | |
2733 | if (!S_ISDIR(attrp->ca_mode)) { | |
2734 | attrp->ca_mode &= ~S_IFMT; | |
2735 | attrp->ca_mode |= S_IFDIR; | |
2736 | } | |
2737 | attrp->ca_nlink = 2 + ((HFSPlusCatalogFolder *)crp)->valence; | |
2738 | attrp->ca_entries = ((HFSPlusCatalogFolder *)crp)->valence; | |
91447636 | 2739 | attrp->ca_attrblks = ((HFSPlusCatalogFolder *)crp)->attrBlocks; |
9bccf70c A |
2740 | } else { |
2741 | /* Keep IMMUTABLE bits in sync with HFS locked flag */ | |
2742 | if (crp->flags & kHFSFileLockedMask) { | |
2743 | /* The file's supposed to be locked: | |
2744 | Make sure at least one of the IMMUTABLE bits is set: */ | |
2745 | if ((attrp->ca_flags & (SF_IMMUTABLE | UF_IMMUTABLE)) == 0) | |
2746 | attrp->ca_flags |= UF_IMMUTABLE; | |
2747 | } else { | |
2748 | /* The file's supposed to be unlocked: */ | |
2749 | attrp->ca_flags &= ~(SF_IMMUTABLE | UF_IMMUTABLE); | |
2750 | } | |
2751 | /* get total blocks (both forks) */ | |
2752 | attrp->ca_blocks = crp->dataFork.totalBlocks + crp->resourceFork.totalBlocks; | |
91447636 | 2753 | attrp->ca_attrblks = crp->attrBlocks; |
b36670ce A |
2754 | /* On HFS+ the ThreadExists flag must always be set. */ |
2755 | if ((hfsmp->hfs_flags & HFS_STANDARD) == 0) | |
2756 | attrp->ca_recflags |= kHFSThreadExistsMask; | |
9bccf70c A |
2757 | } |
2758 | ||
2759 | attrp->ca_fileid = crp->fileID; | |
2760 | ||
2761 | bcopy(&crp->userInfo, attrp->ca_finderinfo, 32); | |
2762 | } | |
2763 | ||
2764 | /* | |
2765 | * promotekey - promote hfs key to hfs plus key | |
2766 | * | |
2767 | */ | |
2768 | static void | |
2769 | promotekey(struct hfsmount *hfsmp, const HFSCatalogKey *hfskey, | |
2770 | HFSPlusCatalogKey *keyp, u_long *encoding) | |
2771 | { | |
2772 | hfs_to_unicode_func_t hfs_get_unicode = hfsmp->hfs_get_unicode; | |
2773 | UInt32 uniCount; | |
2774 | int error; | |
2775 | ||
2776 | *encoding = hfsmp->hfs_encoding; | |
2777 | ||
2778 | error = hfs_get_unicode(hfskey->nodeName, keyp->nodeName.unicode, | |
2779 | kHFSPlusMaxFileNameChars, &uniCount); | |
2780 | /* | |
2781 | * When an HFS name cannot be encoded with the current | |
2782 | * encoding use MacRoman as a fallback. | |
2783 | */ | |
2784 | if (error && hfsmp->hfs_encoding != kTextEncodingMacRoman) { | |
2785 | *encoding = 0; | |
2786 | (void) mac_roman_to_unicode(hfskey->nodeName, | |
2787 | keyp->nodeName.unicode, | |
2788 | kHFSPlusMaxFileNameChars, | |
2789 | &uniCount); | |
2790 | } | |
2791 | ||
2792 | keyp->nodeName.length = uniCount; | |
2793 | keyp->parentID = hfskey->parentID; | |
2794 | } | |
2795 | ||
2796 | /* | |
2797 | * promotefork - promote hfs fork info to hfs plus | |
2798 | * | |
2799 | */ | |
2800 | static void | |
2801 | promotefork(struct hfsmount *hfsmp, const struct HFSCatalogFile *filep, | |
2802 | int resource, struct cat_fork * forkp) | |
2803 | { | |
2804 | struct HFSPlusExtentDescriptor *xp; | |
2805 | u_long blocksize = HFSTOVCB(hfsmp)->blockSize; | |
2806 | ||
2807 | bzero(forkp, sizeof(*forkp)); | |
2808 | xp = &forkp->cf_extents[0]; | |
2809 | if (resource) { | |
2810 | forkp->cf_size = filep->rsrcLogicalSize; | |
2811 | forkp->cf_blocks = filep->rsrcPhysicalSize / blocksize; | |
55e303ae A |
2812 | forkp->cf_bytesread = 0; |
2813 | forkp->cf_vblocks = 0; | |
9bccf70c A |
2814 | xp[0].startBlock = (u_int32_t)filep->rsrcExtents[0].startBlock; |
2815 | xp[0].blockCount = (u_int32_t)filep->rsrcExtents[0].blockCount; | |
2816 | xp[1].startBlock = (u_int32_t)filep->rsrcExtents[1].startBlock; | |
2817 | xp[1].blockCount = (u_int32_t)filep->rsrcExtents[1].blockCount; | |
2818 | xp[2].startBlock = (u_int32_t)filep->rsrcExtents[2].startBlock; | |
2819 | xp[2].blockCount = (u_int32_t)filep->rsrcExtents[2].blockCount; | |
2820 | } else { | |
2821 | forkp->cf_size = filep->dataLogicalSize; | |
2822 | forkp->cf_blocks = filep->dataPhysicalSize / blocksize; | |
55e303ae A |
2823 | forkp->cf_bytesread = 0; |
2824 | forkp->cf_vblocks = 0; | |
9bccf70c A |
2825 | xp[0].startBlock = (u_int32_t)filep->dataExtents[0].startBlock; |
2826 | xp[0].blockCount = (u_int32_t)filep->dataExtents[0].blockCount; | |
2827 | xp[1].startBlock = (u_int32_t)filep->dataExtents[1].startBlock; | |
2828 | xp[1].blockCount = (u_int32_t)filep->dataExtents[1].blockCount; | |
2829 | xp[2].startBlock = (u_int32_t)filep->dataExtents[2].startBlock; | |
2830 | xp[2].blockCount = (u_int32_t)filep->dataExtents[2].blockCount; | |
2831 | } | |
2832 | } | |
2833 | ||
2834 | /* | |
2835 | * promoteattr - promote hfs catalog attributes to hfs plus | |
2836 | * | |
2837 | */ | |
2838 | static void | |
2839 | promoteattr(struct hfsmount *hfsmp, const CatalogRecord *dataPtr, struct HFSPlusCatalogFile *crp) | |
2840 | { | |
2841 | u_long blocksize = HFSTOVCB(hfsmp)->blockSize; | |
2842 | ||
2843 | if (dataPtr->recordType == kHFSFolderRecord) { | |
2844 | struct HFSCatalogFolder * folder; | |
2845 | ||
2846 | folder = (struct HFSCatalogFolder *) dataPtr; | |
2847 | crp->recordType = kHFSPlusFolderRecord; | |
2848 | crp->flags = folder->flags; | |
2849 | crp->fileID = folder->folderID; | |
2850 | crp->createDate = LocalToUTC(folder->createDate); | |
2851 | crp->contentModDate = LocalToUTC(folder->modifyDate); | |
2852 | crp->backupDate = LocalToUTC(folder->backupDate); | |
2853 | crp->reserved1 = folder->valence; | |
2854 | bcopy(&folder->userInfo, &crp->userInfo, 32); | |
2855 | } else /* file */ { | |
2856 | struct HFSCatalogFile * file; | |
2857 | ||
2858 | file = (struct HFSCatalogFile *) dataPtr; | |
2859 | crp->recordType = kHFSPlusFileRecord; | |
2860 | crp->flags = file->flags; | |
2861 | crp->fileID = file->fileID; | |
2862 | crp->createDate = LocalToUTC(file->createDate); | |
2863 | crp->contentModDate = LocalToUTC(file->modifyDate); | |
2864 | crp->backupDate = LocalToUTC(file->backupDate); | |
2865 | crp->reserved1 = 0; | |
2866 | bcopy(&file->userInfo, &crp->userInfo, 16); | |
2867 | bcopy(&file->finderInfo, &crp->finderInfo, 16); | |
2868 | crp->dataFork.totalBlocks = file->dataPhysicalSize / blocksize; | |
2869 | crp->resourceFork.totalBlocks = file->rsrcPhysicalSize / blocksize; | |
2870 | } | |
2871 | crp->textEncoding = 0; | |
2872 | crp->attributeModDate = crp->contentModDate; | |
2873 | crp->accessDate = crp->contentModDate; | |
2874 | bzero(&crp->bsdInfo, sizeof(HFSPlusBSDInfo)); | |
91447636 | 2875 | crp->attrBlocks = 0; |
9bccf70c A |
2876 | } |
2877 | ||
2878 | /* | |
2879 | * Build a catalog node thread record from a catalog key | |
2880 | * and return the size of the record. | |
2881 | */ | |
2882 | static int | |
2883 | buildthread(void *keyp, void *recp, int std_hfs, int directory) | |
2884 | { | |
2885 | int size = 0; | |
2886 | ||
2887 | if (std_hfs) { | |
2888 | HFSCatalogKey *key = (HFSCatalogKey *)keyp; | |
2889 | HFSCatalogThread *rec = (HFSCatalogThread *)recp; | |
2890 | ||
2891 | size = sizeof(HFSCatalogThread); | |
2892 | bzero(rec, size); | |
2893 | if (directory) | |
2894 | rec->recordType = kHFSFolderThreadRecord; | |
2895 | else | |
2896 | rec->recordType = kHFSFileThreadRecord; | |
2897 | rec->parentID = key->parentID; | |
2898 | bcopy(key->nodeName, rec->nodeName, key->nodeName[0]+1); | |
2899 | ||
2900 | } else /* HFS+ */ { | |
2901 | HFSPlusCatalogKey *key = (HFSPlusCatalogKey *)keyp; | |
2902 | HFSPlusCatalogThread *rec = (HFSPlusCatalogThread *)recp; | |
2903 | ||
2904 | size = sizeof(HFSPlusCatalogThread); | |
2905 | if (directory) | |
2906 | rec->recordType = kHFSPlusFolderThreadRecord; | |
2907 | else | |
2908 | rec->recordType = kHFSPlusFileThreadRecord; | |
2909 | rec->reserved = 0; | |
2910 | rec->parentID = key->parentID; | |
2911 | bcopy(&key->nodeName, &rec->nodeName, | |
2912 | sizeof(UniChar) * (key->nodeName.length + 1)); | |
2913 | ||
2914 | /* HFS Plus has varaible sized thread records */ | |
2915 | size -= (sizeof(rec->nodeName.unicode) - | |
2916 | (rec->nodeName.length * sizeof(UniChar))); | |
2917 | } | |
2918 | ||
2919 | return (size); | |
2920 | } | |
2921 | ||
2922 | /* | |
2923 | * Build a catalog node thread key. | |
2924 | */ | |
2925 | static void | |
2926 | buildthreadkey(HFSCatalogNodeID parentID, int std_hfs, CatalogKey *key) | |
2927 | { | |
2928 | if (std_hfs) { | |
2929 | key->hfs.keyLength = kHFSCatalogKeyMinimumLength; | |
2930 | key->hfs.reserved = 0; | |
2931 | key->hfs.parentID = parentID; | |
2932 | key->hfs.nodeName[0] = 0; | |
2933 | } else { | |
2934 | key->hfsPlus.keyLength = kHFSPlusCatalogKeyMinimumLength; | |
2935 | key->hfsPlus.parentID = parentID; | |
2936 | key->hfsPlus.nodeName.length = 0; | |
2937 | } | |
2938 | } | |
2939 | ||
2940 | /* | |
2941 | * Extract the text encoding from a catalog node record. | |
2942 | */ | |
2943 | static u_long | |
2944 | getencoding(const CatalogRecord *crp) | |
2945 | { | |
2946 | u_long encoding; | |
2947 | ||
2948 | if (crp->recordType == kHFSPlusFolderRecord) | |
2949 | encoding = crp->hfsPlusFolder.textEncoding; | |
2950 | else if (crp->recordType == kHFSPlusFileRecord) | |
2951 | encoding = crp->hfsPlusFile.textEncoding; | |
2952 | else | |
2953 | encoding = 0; | |
2954 | ||
2955 | return (encoding); | |
2956 | } | |
2957 | ||
2958 | /* | |
2959 | * Extract the CNID from a catalog node record. | |
2960 | */ | |
2961 | static cnid_t | |
2962 | getcnid(const CatalogRecord *crp) | |
2963 | { | |
2964 | cnid_t cnid = 0; | |
2965 | ||
2966 | switch (crp->recordType) { | |
2967 | case kHFSFolderRecord: | |
2968 | cnid = crp->hfsFolder.folderID; | |
2969 | break; | |
2970 | case kHFSFileRecord: | |
2971 | cnid = crp->hfsFile.fileID; | |
2972 | break; | |
2973 | case kHFSPlusFolderRecord: | |
2974 | cnid = crp->hfsPlusFolder.folderID; | |
2975 | break; | |
2976 | case kHFSPlusFileRecord: | |
2977 | cnid = crp->hfsPlusFile.fileID; | |
2978 | break; | |
b4c24cb9 | 2979 | default: |
91447636 | 2980 | printf("hfs: getcnid: unknown recordType (crp @ 0x%x)\n", crp); |
b4c24cb9 | 2981 | break; |
9bccf70c | 2982 | } |
b4c24cb9 | 2983 | |
9bccf70c A |
2984 | return (cnid); |
2985 | } | |
2986 | ||
2987 | /* | |
2988 | * Extract the parent ID from a catalog node record. | |
2989 | */ | |
2990 | static cnid_t | |
2991 | getparentcnid(const CatalogRecord *recp) | |
2992 | { | |
2993 | cnid_t cnid = 0; | |
2994 | ||
2995 | switch (recp->recordType) { | |
2996 | case kHFSFileThreadRecord: | |
2997 | case kHFSFolderThreadRecord: | |
2998 | cnid = recp->hfsThread.parentID; | |
2999 | break; | |
3000 | ||
3001 | case kHFSPlusFileThreadRecord: | |
3002 | case kHFSPlusFolderThreadRecord: | |
3003 | cnid = recp->hfsPlusThread.parentID; | |
3004 | break; | |
b4c24cb9 A |
3005 | default: |
3006 | panic("hfs: getparentcnid: unknown recordType (crp @ 0x%x)\n", recp); | |
3007 | break; | |
9bccf70c | 3008 | } |
b4c24cb9 | 3009 | |
9bccf70c A |
3010 | return (cnid); |
3011 | } | |
3012 | ||
3013 | /* | |
3014 | * Determine if a catalog node record is a directory. | |
3015 | */ | |
3016 | static int | |
3017 | isadir(const CatalogRecord *crp) | |
3018 | { | |
3019 | return (crp->recordType == kHFSFolderRecord || | |
3020 | crp->recordType == kHFSPlusFolderRecord); | |
3021 | } | |
3022 |