+ /* Get space for iterator, key and data */
+ MALLOC(bto, struct btobj *, sizeof(struct btobj), M_TEMP, M_WAITOK);
+ bto->iterator.hint.nodeNum = 0;
+ rsrcforkp = &bto->data.hfsPlusFile.resourceFork;
+
+ result = buildkey(hfsmp, descp, &bto->key, 0);
+ if (result) {
+ printf("cat_createlink: err %d from buildkey\n", result);
+ goto exit;
+ }
+
+ /* This is our only chance to set the encoding (other than a rename). */
+ encoding = hfs_pickencoding(bto->key.nodeName.unicode, bto->key.nodeName.length);
+
+ /* Insert the thread record first. */
+ datalen = buildthread((void*)&bto->key, &bto->data, 0, 0);
+ btdata.bufferAddress = &bto->data;
+ btdata.itemSize = datalen;
+ btdata.itemCount = 1;
+
+ for (;;) {
+ buildthreadkey(nextCNID, 0, (CatalogKey *) &bto->iterator.key);
+
+ result = BTInsertRecord(fcb, &bto->iterator, &btdata, datalen);
+ if ((result == btExists) && (hfsmp->vcbAtrb & kHFSCatalogNodeIDsReusedMask)) {
+ /*
+ * Allow CNIDs on HFS Plus volumes to wrap around
+ */
+ if (++nextCNID < kHFSFirstUserCatalogNodeID) {
+ nextCNID = kHFSFirstUserCatalogNodeID;
+ }
+ continue;
+ }
+ if (result == 0) {
+ thread_inserted = 1;
+ }
+ break;
+ }
+ if (result)
+ goto exit;
+
+ /*
+ * CNID is now established. If we have wrapped then
+ * update the vcbNxtCNID.
+ */
+ if ((hfsmp->vcbAtrb & kHFSCatalogNodeIDsReusedMask)) {
+ hfsmp->vcbNxtCNID = nextCNID + 1;
+ if (hfsmp->vcbNxtCNID < kHFSFirstUserCatalogNodeID) {
+ hfsmp->vcbNxtCNID = kHFSFirstUserCatalogNodeID;
+ }
+ }
+
+ /*
+ * Now insert the link record.
+ */
+ buildrecord(attrp, nextCNID, 0, encoding, &bto->data, &datalen);
+
+ bto->data.hfsPlusFile.hl_prevLinkID = 0;
+ bto->data.hfsPlusFile.hl_nextLinkID = nextlinkid;
+ bto->data.hfsPlusFile.hl_linkReference = attrp->ca_linkref;
+
+ /* For directory hard links, create alias in resource fork */
+ if (descp->cd_flags & CD_ISDIR) {
+ if ((result = cat_makealias(hfsmp, attrp->ca_linkref, &bto->data.hfsPlusFile))) {
+ goto exit;
+ }
+ alias_allocated = 1;
+ }
+ btdata.bufferAddress = &bto->data;
+ btdata.itemSize = datalen;
+ btdata.itemCount = 1;
+
+ bcopy(&bto->key, &bto->iterator.key, sizeof(bto->key));
+
+ result = BTInsertRecord(fcb, &bto->iterator, &btdata, datalen);
+ if (result) {
+ if (result == btExists)
+ result = EEXIST;
+ goto exit;
+ }
+ if (linkfileid != NULL) {
+ *linkfileid = nextCNID;
+ }
+exit:
+ if (result) {
+ if (thread_inserted) {
+ printf("cat_createlink: err %d from BTInsertRecord\n", MacToVFSError(result));
+
+ buildthreadkey(nextCNID, 0, (CatalogKey *)&bto->iterator.key);
+ if (BTDeleteRecord(fcb, &bto->iterator)) {
+ printf("hfs: cat_createlink() failed to delete thread record on volume %s\n", hfsmp->vcbVN);
+ hfs_mark_volume_inconsistent(hfsmp);
+ }
+ }
+ if (alias_allocated && rsrcforkp->extents[0].startBlock != 0) {
+ (void) BlockDeallocate(hfsmp, rsrcforkp->extents[0].startBlock,
+ rsrcforkp->extents[0].blockCount);
+ rsrcforkp->extents[0].startBlock = 0;
+ rsrcforkp->extents[0].blockCount = 0;
+ }
+ }
+ (void) BTFlushPath(fcb);
+ FREE(bto, M_TEMP);
+
+ return MacToVFSError(result);
+}
+
+/* Directory hard links are visible as aliases on pre-Leopard systems and
+ * as normal directories on Leopard or later. All directory hard link aliases
+ * have the same resource fork content except for the three uniquely
+ * identifying values that are updated in the resource fork data when the alias
+ * is created. The following array is the constant resource fork data used
+ * only for creating directory hard link aliases.
+ */
+static const char hfs_dirlink_alias_rsrc[] = {
+ 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x9e, 0x00, 0x00, 0x00, 0x9e, 0x00, 0x00, 0x00, 0x32,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x9a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9a, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x2b,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
+ 0x01, 0x00, 0x00, 0x00, 0x01, 0x9e, 0x00, 0x00, 0x00, 0x9e, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x32, 0x00, 0x00, 0x61, 0x6c, 0x69, 0x73,
+ 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+/* Constants for directory hard link alias */
+enum {
+ /* Size of resource fork data array for directory hard link alias */
+ kHFSAliasSize = 0x1d0,
+
+ /* Volume type for ejectable devices like disk image */
+ kHFSAliasVolTypeEjectable = 0x5,
+
+ /* Offset for volume create date, in Mac OS local time */
+ kHFSAliasVolCreateDateOffset = 0x12a,
+
+ /* Offset for the type of volume */
+ kHFSAliasVolTypeOffset = 0x130,
+
+ /* Offset for folder ID of the parent directory of the directory inode */
+ kHFSAliasParentIDOffset = 0x132,
+
+ /* Offset for folder ID of the directory inode */
+ kHFSAliasTargetIDOffset = 0x176,
+};
+
+/* Create and write an alias that points at the directory represented by given
+ * inode number on the same volume. Directory hard links are visible as
+ * aliases in pre-Leopard systems and this function creates these aliases.
+ *
+ * Note: This code is very specific to creating alias for the purpose
+ * of directory hard links only, and should not be generalized.
+ */
+static int
+cat_makealias(struct hfsmount *hfsmp, u_int32_t inode_num, struct HFSPlusCatalogFile *crp)
+{
+ struct buf *bp;
+ daddr64_t blkno;
+ u_int32_t blkcount;
+ int blksize;
+ int sectorsize;
+ int result;
+ HFSPlusForkData *rsrcforkp;
+ char *alias;
+ uint32_t *valptr;
+
+ rsrcforkp = &(crp->resourceFork);
+
+ blksize = hfsmp->blockSize;
+ blkcount = howmany(kHFSAliasSize, blksize);
+ sectorsize = hfsmp->hfs_logical_block_size;
+ bzero(rsrcforkp, sizeof(HFSPlusForkData));
+
+ /* Allocate some disk space for the alias content. */
+ result = BlockAllocate(hfsmp, 0, blkcount, blkcount, 1, 1,
+ &rsrcforkp->extents[0].startBlock,
+ &rsrcforkp->extents[0].blockCount);
+ if (result) {
+ rsrcforkp->extents[0].startBlock = 0;
+ goto exit;
+ }
+
+ /* Acquire a buffer cache block for our block. */
+ blkno = ((u_int64_t)rsrcforkp->extents[0].startBlock * (u_int64_t)blksize) / sectorsize;
+ blkno += hfsmp->hfsPlusIOPosOffset / sectorsize;
+
+ bp = buf_getblk(hfsmp->hfs_devvp, blkno, roundup(kHFSAliasSize, hfsmp->hfs_logical_block_size), 0, 0, BLK_META);
+ if (hfsmp->jnl) {
+ journal_modify_block_start(hfsmp->jnl, bp);
+ }
+
+ /* Generate alias content */
+ alias = (char *)buf_dataptr(bp);
+ bzero(alias, buf_size(bp));
+ bcopy(hfs_dirlink_alias_rsrc, alias, kHFSAliasSize);
+
+ /* Set the volume create date, local time in Mac OS format */
+ valptr = (uint32_t *)(alias + kHFSAliasVolCreateDateOffset);
+ *valptr = OSSwapHostToBigInt32(hfsmp->localCreateDate);
+
+ /* If the file system is on a virtual device like disk image,
+ * update the volume type to be ejectable device.
+ */
+ if (hfsmp->hfs_flags & HFS_VIRTUAL_DEVICE) {
+ *(uint16_t *)(alias + kHFSAliasVolTypeOffset) =
+ OSSwapHostToBigInt16(kHFSAliasVolTypeEjectable);
+ }
+
+ /* Set id of the parent of the target directory */
+ valptr = (uint32_t *)(alias + kHFSAliasParentIDOffset);
+ *valptr = OSSwapHostToBigInt32(hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid);
+
+ /* Set id of the target directory */
+ valptr = (uint32_t *)(alias + kHFSAliasTargetIDOffset);
+ *valptr = OSSwapHostToBigInt32(inode_num);
+
+ /* Write alias content to disk. */
+ if (hfsmp->jnl) {
+ journal_modify_block_end(hfsmp->jnl, bp, NULL, NULL);
+ } else if ((result = buf_bwrite(bp))) {
+ goto exit;
+ }
+
+ /* Finish initializing the fork data. */
+ rsrcforkp->logicalSize = kHFSAliasSize;
+ rsrcforkp->totalBlocks = rsrcforkp->extents[0].blockCount;
+
+exit:
+ if (result && rsrcforkp->extents[0].startBlock != 0) {
+ (void) BlockDeallocate(hfsmp, rsrcforkp->extents[0].startBlock, rsrcforkp->extents[0].blockCount);
+ rsrcforkp->extents[0].startBlock = 0;
+ rsrcforkp->extents[0].blockCount = 0;
+ rsrcforkp->logicalSize = 0;
+ rsrcforkp->totalBlocks = 0;
+ }
+ return (result);
+}
+
+/*
+ * cat_deletelink - delete a link from the catalog
+ */
+__private_extern__
+int
+cat_deletelink(struct hfsmount *hfsmp, struct cat_desc *descp)
+{
+ struct HFSPlusCatalogFile file;
+ struct cat_attr cattr;
+ uint32_t totalBlocks;
+ int i;
+ int result;
+
+ bzero(&file, sizeof (file));
+ bzero(&cattr, sizeof (cattr));
+ cattr.ca_fileid = descp->cd_cnid;
+
+ /* Directory links have alias content to remove. */
+ if (descp->cd_flags & CD_ISDIR) {
+ FCB * fcb;
+ BTreeIterator * iterator;
+ struct FSBufferDescriptor btdata;
+
+ fcb = hfsmp->hfs_catalog_cp->c_datafork;
+
+ /* Borrow the btcb iterator since we have an exclusive catalog lock. */
+ iterator = &((BTreeControlBlockPtr)(fcb->ff_sysfileinfo))->iterator;
+ iterator->hint.nodeNum = 0;
+
+ if ((result = buildkey(hfsmp, descp, (HFSPlusCatalogKey *)&iterator->key, 0))) {
+ goto exit;
+ }
+ BDINIT(btdata, &file);
+
+ if ((result = BTSearchRecord(fcb, iterator, &btdata, NULL, NULL))) {
+ goto exit;
+ }
+ }
+
+ result = cat_delete(hfsmp, descp, &cattr);
+
+ if ((result == 0) &&
+ (descp->cd_flags & CD_ISDIR) &&
+ (file.recordType == kHFSPlusFileRecord)) {
+
+ totalBlocks = file.resourceFork.totalBlocks;
+
+ for (i = 0; (i < 8) && (totalBlocks > 0); i++) {
+ if ((file.resourceFork.extents[i].blockCount == 0) &&
+ (file.resourceFork.extents[i].startBlock == 0)) {
+ break;
+ }
+
+ (void) BlockDeallocate(hfsmp,
+ file.resourceFork.extents[i].startBlock,
+ file.resourceFork.extents[i].blockCount);
+
+ totalBlocks -= file.resourceFork.extents[i].blockCount;
+ file.resourceFork.extents[i].startBlock = 0;
+ file.resourceFork.extents[i].blockCount = 0;
+ }
+ }
+exit:
+ return (result);
+}
+
+
+/*
+ * Callback to collect directory entries.
+ * Called with readattr_state for each item in a directory.
+ */
+struct readattr_state {
+ struct hfsmount *hfsmp;
+ struct cat_entrylist *list;
+ cnid_t dir_cnid;
+ int stdhfs;
+ int error;
+};
+
+static int
+getentriesattr_callback(const CatalogKey *key, const CatalogRecord *rec,
+ struct readattr_state *state)
+{
+ struct cat_entrylist *list = state->list;
+ struct hfsmount *hfsmp = state->hfsmp;
+ struct cat_entry *cep;
+ cnid_t parentcnid;
+
+ if (list->realentries >= list->maxentries)
+ return (0); /* stop */
+
+ parentcnid = state->stdhfs ? key->hfs.parentID : key->hfsPlus.parentID;
+
+ switch(rec->recordType) {
+ case kHFSPlusFolderRecord:
+ case kHFSPlusFileRecord:
+ case kHFSFolderRecord:
+ case kHFSFileRecord:
+ if (parentcnid != state->dir_cnid) {
+ state->error = ENOENT;
+ return (0); /* stop */
+ }
+ break;
+ default:
+ state->error = ENOENT;
+ return (0); /* stop */
+ }
+
+ /* Hide the private system directories and journal files */
+ if (parentcnid == kHFSRootFolderID) {
+ if (rec->recordType == kHFSPlusFolderRecord) {
+ if (rec->hfsPlusFolder.folderID == hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid ||
+ rec->hfsPlusFolder.folderID == hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid) {
+ list->skipentries++;
+ return (1); /* continue */
+ }
+ }
+ if ((hfsmp->jnl || ((HFSTOVCB(hfsmp)->vcbAtrb & kHFSVolumeJournaledMask) && (hfsmp->hfs_flags & HFS_READ_ONLY))) &&
+ (rec->recordType == kHFSPlusFileRecord) &&
+ ((rec->hfsPlusFile.fileID == hfsmp->hfs_jnlfileid) ||
+ (rec->hfsPlusFile.fileID == hfsmp->hfs_jnlinfoblkid))) {
+ list->skipentries++;
+ return (1); /* continue */
+ }
+ }
+
+ cep = &list->entry[list->realentries++];
+
+ if (state->stdhfs) {
+ struct HFSPlusCatalogFile cnoderec;
+ HFSPlusCatalogKey * pluskey;
+ u_long encoding;
+
+ promoteattr(hfsmp, rec, &cnoderec);
+ getbsdattr(hfsmp, &cnoderec, &cep->ce_attr);
+
+ MALLOC(pluskey, HFSPlusCatalogKey *, sizeof(HFSPlusCatalogKey), M_TEMP, M_WAITOK);
+ promotekey(hfsmp, (const HFSCatalogKey *)key, pluskey, &encoding);
+ builddesc(pluskey, getcnid(rec), 0, encoding, isadir(rec), &cep->ce_desc);
+ FREE(pluskey, M_TEMP);
+
+ if (rec->recordType == kHFSFileRecord) {
+ int blksize = HFSTOVCB(hfsmp)->blockSize;
+
+ cep->ce_datasize = rec->hfsFile.dataLogicalSize;
+ cep->ce_datablks = rec->hfsFile.dataPhysicalSize / blksize;
+ cep->ce_rsrcsize = rec->hfsFile.rsrcLogicalSize;
+ cep->ce_rsrcblks = rec->hfsFile.rsrcPhysicalSize / blksize;
+ }
+ } else {
+ getbsdattr(hfsmp, (const struct HFSPlusCatalogFile *)rec, &cep->ce_attr);
+ builddesc((const HFSPlusCatalogKey *)key, getcnid(rec), 0, getencoding(rec),
+ isadir(rec), &cep->ce_desc);
+
+ if (rec->recordType == kHFSPlusFileRecord) {
+ cep->ce_datasize = rec->hfsPlusFile.dataFork.logicalSize;
+ cep->ce_datablks = rec->hfsPlusFile.dataFork.totalBlocks;
+ cep->ce_rsrcsize = rec->hfsPlusFile.resourceFork.logicalSize;
+ cep->ce_rsrcblks = rec->hfsPlusFile.resourceFork.totalBlocks;
+
+ /* Save link reference for later processing. */
+ if ((SWAP_BE32(rec->hfsPlusFile.userInfo.fdType) == kHardLinkFileType) &&
+ (SWAP_BE32(rec->hfsPlusFile.userInfo.fdCreator) == kHFSPlusCreator)) {
+ cep->ce_attr.ca_linkref = rec->hfsPlusFile.bsdInfo.special.iNodeNum;
+ } else if ((rec->hfsPlusFile.flags & kHFSHasLinkChainMask) &&
+ (SWAP_BE32(rec->hfsPlusFile.userInfo.fdType) == kHFSAliasType) &&
+ (SWAP_BE32(rec->hfsPlusFile.userInfo.fdCreator) == kHFSAliasCreator)) {
+ cep->ce_attr.ca_linkref = rec->hfsPlusFile.bsdInfo.special.iNodeNum;
+ }
+ }
+ }
+
+ return (list->realentries < list->maxentries);
+}
+
+/*
+ * Pack a cat_entrylist buffer with attributes from the catalog
+ *
+ * Note: index is zero relative
+ */
+__private_extern__
+int
+cat_getentriesattr(struct hfsmount *hfsmp, directoryhint_t *dirhint, struct cat_entrylist *ce_list)
+{
+ FCB* fcb;
+ CatalogKey * key;
+ BTreeIterator * iterator;
+ struct readattr_state state;
+ cnid_t parentcnid;
+ int i;
+ int std_hfs;
+ int index;
+ int have_key;
+ int result = 0;
+
+ ce_list->realentries = 0;
+
+ fcb = GetFileControlBlock(HFSTOVCB(hfsmp)->catalogRefNum);
+ std_hfs = (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord);
+ parentcnid = dirhint->dh_desc.cd_parentcnid;
+
+ state.hfsmp = hfsmp;
+ state.list = ce_list;
+ state.dir_cnid = parentcnid;
+ state.stdhfs = std_hfs;
+ state.error = 0;
+
+ MALLOC(iterator, BTreeIterator *, sizeof(*iterator), M_TEMP, M_WAITOK);
+ bzero(iterator, sizeof(*iterator));
+ key = (CatalogKey *)&iterator->key;
+ have_key = 0;
+ iterator->hint.nodeNum = dirhint->dh_desc.cd_hint;
+ index = dirhint->dh_index + 1;
+
+ /*
+ * Attempt to build a key from cached filename
+ */
+ if (dirhint->dh_desc.cd_namelen != 0) {
+ if (buildkey(hfsmp, &dirhint->dh_desc, (HFSPlusCatalogKey *)key, 0) == 0) {
+ have_key = 1;
+ }
+ }
+
+ /*
+ * If the last entry wasn't cached then position the btree iterator
+ */
+ if ((index == 0) || !have_key) {
+ /*
+ * Position the iterator at the directory's thread record.
+ * (i.e. just before the first entry)
+ */
+ buildthreadkey(dirhint->dh_desc.cd_parentcnid, (hfsmp->hfs_flags & HFS_STANDARD), key);
+ result = BTSearchRecord(fcb, iterator, NULL, NULL, iterator);
+ if (result) {
+ result = MacToVFSError(result);
+ goto exit;
+ }
+
+ /*
+ * Iterate until we reach the entry just
+ * before the one we want to start with.
+ */
+ if (index > 0) {
+ struct position_state ps;
+
+ ps.error = 0;
+ ps.count = 0;
+ ps.index = index;
+ ps.parentID = dirhint->dh_desc.cd_parentcnid;
+ ps.hfsmp = hfsmp;
+
+ result = BTIterateRecords(fcb, kBTreeNextRecord, iterator,
+ (IterateCallBackProcPtr)cat_findposition, &ps);
+ if (ps.error)
+ result = ps.error;
+ else
+ result = MacToVFSError(result);
+ if (result) {
+ result = MacToVFSError(result);
+ goto exit;
+ }
+ }
+ }
+
+ /* Fill list with entries starting at iterator->key. */
+ result = BTIterateRecords(fcb, kBTreeNextRecord, iterator,
+ (IterateCallBackProcPtr)getentriesattr_callback, &state);
+
+ if (state.error)
+ result = state.error;
+ else if (ce_list->realentries == 0)
+ result = ENOENT;
+ else
+ result = MacToVFSError(result);
+
+ if (std_hfs)
+ goto exit;
+
+ /*
+ * Resolve any hard links.
+ */
+ for (i = 0; i < (int)ce_list->realentries; ++i) {
+ struct FndrFileInfo *fip;
+ struct cat_entry *cep;
+ struct HFSPlusCatalogFile filerec;
+ int isdirlink = 0;
+ int isfilelink = 0;
+
+ cep = &ce_list->entry[i];
+ if (cep->ce_attr.ca_linkref == 0)
+ continue;
+
+ /* Note: Finder info is still in Big Endian */
+ fip = (struct FndrFileInfo *)&cep->ce_attr.ca_finderinfo;
+
+ if (S_ISREG(cep->ce_attr.ca_mode) &&
+ (SWAP_BE32(fip->fdType) == kHardLinkFileType) &&
+ (SWAP_BE32(fip->fdCreator) == kHFSPlusCreator)) {
+ isfilelink = 1;
+ }
+ if (S_ISREG(cep->ce_attr.ca_mode) &&
+ (SWAP_BE32(fip->fdType) == kHFSAliasType) &&
+ (SWAP_BE32(fip->fdCreator) == kHFSAliasCreator) &&
+ (cep->ce_attr.ca_recflags & kHFSHasLinkChainMask)) {
+ isdirlink = 1;
+ }
+ if (isfilelink || isdirlink) {
+ if (cat_resolvelink(hfsmp, cep->ce_attr.ca_linkref, isdirlink, &filerec) != 0)
+ continue;
+ /* Repack entry from inode record. */
+ getbsdattr(hfsmp, &filerec, &cep->ce_attr);
+ cep->ce_datasize = filerec.dataFork.logicalSize;
+ cep->ce_datablks = filerec.dataFork.totalBlocks;
+ cep->ce_rsrcsize = filerec.resourceFork.logicalSize;
+ cep->ce_rsrcblks = filerec.resourceFork.totalBlocks;
+ }
+ }
+exit:
+ FREE(iterator, M_TEMP);
+
+ return MacToVFSError(result);
+}
+
+#define SMALL_DIRENTRY_SIZE (int)(sizeof(struct dirent) - (MAXNAMLEN + 1) + 8)
+
+/*
+ * Callback to pack directory entries.
+ * Called with packdirentry_state for each item in a directory.
+ */
+
+/* Hard link information collected during cat_getdirentries. */
+struct linkinfo {
+ u_long link_ref;
+ user_addr_t dirent_addr;
+};
+typedef struct linkinfo linkinfo_t;
+
+/* State information for the getdirentries_callback function. */
+struct packdirentry_state {
+ int cbs_extended;
+ u_int32_t cbs_parentID;
+ u_int32_t cbs_index;
+ uio_t cbs_uio;
+ ExtendedVCB * cbs_hfsmp;
+ int cbs_result;
+ int32_t cbs_nlinks;
+ int32_t cbs_maxlinks;
+ linkinfo_t * cbs_linkinfo;
+ struct cat_desc * cbs_desc;
+ u_int8_t * cbs_namebuf;
+ /*
+ * The following fields are only used for NFS readdir, which
+ * uses the next file id as the seek offset of each entry.
+ */
+ struct direntry * cbs_direntry;
+ struct direntry * cbs_prevdirentry;
+ u_int32_t cbs_previlinkref;
+ Boolean cbs_hasprevdirentry;
+ Boolean cbs_eof;
+};
+
+/*
+ * getdirentries callback for HFS Plus directories.
+ */
+static int
+getdirentries_callback(const CatalogKey *ckp, const CatalogRecord *crp,
+ struct packdirentry_state *state)
+{
+ struct hfsmount *hfsmp;
+ const CatalogName *cnp;
+ cnid_t curID;
+ OSErr result;
+ struct dirent catent;
+ struct direntry * entry = NULL;
+ time_t itime;
+ u_int32_t ilinkref = 0;
+ u_int32_t curlinkref = 0;
+ cnid_t cnid;
+ int hide = 0;
+ u_int8_t type = DT_UNKNOWN;
+ u_int8_t is_mangled = 0;
+ u_int8_t *nameptr;
+ user_addr_t uiobase = USER_ADDR_NULL;
+ size_t namelen = 0;
+ size_t maxnamelen;
+ size_t uiosize = 0;
+ caddr_t uioaddr;
+ Boolean stop_after_pack = false;
+
+ hfsmp = state->cbs_hfsmp;
+ curID = ckp->hfsPlus.parentID;
+
+ /* We're done when parent directory changes */
+ if (state->cbs_parentID != curID) {
+ if (state->cbs_extended) {
+ /* The last record has not been returned yet, so we
+ * want to stop after packing the last item
+ */
+ if (state->cbs_hasprevdirentry) {
+ stop_after_pack = true;
+ } else {
+ state->cbs_result = ENOENT;
+ return (0); /* stop */
+ }
+ } else {
+ state->cbs_result = ENOENT;
+ return (0); /* stop */
+ }
+ }
+
+ if (state->cbs_extended) {
+ entry = state->cbs_direntry;
+ nameptr = (u_int8_t *)&entry->d_name[0];
+ maxnamelen = NAME_MAX;
+ } else {
+ nameptr = (u_int8_t *)&catent.d_name[0];
+ maxnamelen = NAME_MAX;
+ }
+
+ if (state->cbs_extended && stop_after_pack) {
+ /* The last item returns a non-zero invalid cookie */
+ cnid = INT_MAX;
+ } else {
+ switch(crp->recordType) {
+ case kHFSPlusFolderRecord:
+ type = DT_DIR;
+ cnid = crp->hfsPlusFolder.folderID;
+ /* Hide our private system directories. */
+ if (curID == kHFSRootFolderID) {
+ if (cnid == hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid ||
+ cnid == hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid) {
+ hide = 1;
+ }
+ }
+ break;
+ case kHFSPlusFileRecord:
+ itime = to_bsd_time(crp->hfsPlusFile.createDate);
+ type = MODE_TO_DT(crp->hfsPlusFile.bsdInfo.fileMode);
+ cnid = crp->hfsPlusFile.fileID;
+ /*
+ * When a hardlink link is encountered save its link ref.
+ */
+ if ((SWAP_BE32(crp->hfsPlusFile.userInfo.fdType) == kHardLinkFileType) &&
+ (SWAP_BE32(crp->hfsPlusFile.userInfo.fdCreator) == kHFSPlusCreator) &&
+ ((itime == (time_t)hfsmp->hfs_itime) ||
+ (itime == (time_t)hfsmp->hfs_metadata_createdate))) {
+ /* If link ref is inode's file id then use it directly. */
+ if (crp->hfsPlusFile.flags & kHFSHasLinkChainMask) {
+ cnid = crp->hfsPlusFile.hl_linkReference;
+ } else {
+ ilinkref = crp->hfsPlusFile.hl_linkReference;
+ }
+ } else if ((SWAP_BE32(crp->hfsPlusFile.userInfo.fdType) == kHFSAliasType) &&
+ (SWAP_BE32(crp->hfsPlusFile.userInfo.fdCreator) == kHFSAliasCreator) &&
+ (crp->hfsPlusFile.flags & kHFSHasLinkChainMask) &&
+ (crp->hfsPlusFile.hl_linkReference >= kHFSFirstUserCatalogNodeID) &&
+ ((itime == (time_t)hfsmp->hfs_itime) ||
+ (itime == (time_t)hfsmp->hfs_metadata_createdate))) {
+ /* A directory's link resolves to a directory. */
+ type = DT_DIR;
+ /* A directory's link ref is always inode's file id. */
+ cnid = crp->hfsPlusFile.hl_linkReference;
+ }
+ /* Hide the journal files */
+ if ((curID == kHFSRootFolderID) &&
+ ((hfsmp->jnl || ((HFSTOVCB(hfsmp)->vcbAtrb & kHFSVolumeJournaledMask) && (hfsmp->hfs_flags & HFS_READ_ONLY)))) &&
+ ((cnid == hfsmp->hfs_jnlfileid) ||
+ (cnid == hfsmp->hfs_jnlinfoblkid))) {
+ hide = 1;
+ }
+ break;
+ default:
+ return (0); /* stop */
+ };
+
+ cnp = (const CatalogName*) &ckp->hfsPlus.nodeName;
+
+ namelen = cnp->ustr.length;
+ /*
+ * For MacRoman encoded names, assume that its ascii and
+ * convert it directly in an attempt to avoid the more
+ * expensive utf8_encodestr conversion.
+ */
+ if ((namelen < maxnamelen) && (crp->hfsPlusFile.textEncoding == 0)) {
+ int i;
+ u_int16_t ch;
+ const u_int16_t *chp;
+
+ chp = &cnp->ustr.unicode[0];
+ for (i = 0; i < (int)namelen; ++i) {
+ ch = *chp++;
+ if (ch > 0x007f || ch == 0x0000) {
+ /* Perform expensive utf8_encodestr conversion */
+ goto encodestr;
+ }
+ nameptr[i] = (ch == '/') ? ':' : (u_int8_t)ch;
+ }
+ nameptr[namelen] = '\0';
+ result = 0;
+ } else {
+encodestr:
+ result = utf8_encodestr(cnp->ustr.unicode, namelen * sizeof(UniChar),
+ nameptr, &namelen, maxnamelen + 1, ':', 0);
+ }
+
+ /* Check result returned from encoding the filename to utf8 */
+ if (result == ENAMETOOLONG) {
+ result = ConvertUnicodeToUTF8Mangled(cnp->ustr.length * sizeof(UniChar),
+ cnp->ustr.unicode, maxnamelen + 1,
+ (ByteCount*)&namelen, nameptr, cnid);
+ is_mangled = 1;
+ }
+ }
+
+ if (state->cbs_extended) {
+ /*
+ * The index is 1 relative and includes "." and ".."
+ *
+ * Also stuff the cnid in the upper 32 bits of the cookie.
+ * The cookie is stored to the previous entry, which will
+ * be packed and copied this time
+ */
+ state->cbs_prevdirentry->d_seekoff = (state->cbs_index + 3) | ((u_int64_t)cnid << 32);
+ uiosize = state->cbs_prevdirentry->d_reclen;
+ uioaddr = (caddr_t) state->cbs_prevdirentry;
+ } else {
+ catent.d_type = type;
+ catent.d_namlen = namelen;
+ catent.d_reclen = uiosize = STD_DIRENT_LEN(namelen);
+ if (hide)
+ catent.d_fileno = 0; /* file number = 0 means skip entry */
+ else
+ catent.d_fileno = cnid;
+ uioaddr = (caddr_t) &catent;
+ }
+
+ /* Save current base address for post processing of hard-links. */
+ if (ilinkref || state->cbs_previlinkref) {
+ uiobase = uio_curriovbase(state->cbs_uio);
+ }
+ /* If this entry won't fit then we're done */
+ if ((uiosize > uio_resid(state->cbs_uio)) ||
+ (ilinkref != 0 && state->cbs_nlinks == state->cbs_maxlinks)) {
+ return (0); /* stop */
+ }
+
+ if (!state->cbs_extended || state->cbs_hasprevdirentry) {
+ state->cbs_result = uiomove(uioaddr, uiosize, state->cbs_uio);
+ if (state->cbs_result == 0) {
+ ++state->cbs_index;
+
+ /* Remember previous entry */
+ state->cbs_desc->cd_cnid = cnid;
+ if (type == DT_DIR) {
+ state->cbs_desc->cd_flags |= CD_ISDIR;
+ } else {
+ state->cbs_desc->cd_flags &= ~CD_ISDIR;
+ }
+ if (state->cbs_desc->cd_nameptr != NULL) {
+ state->cbs_desc->cd_namelen = 0;
+ }
+#if 0
+ state->cbs_desc->cd_encoding = xxxx;
+#endif
+ if (!is_mangled) {
+ state->cbs_desc->cd_namelen = namelen;
+ bcopy(nameptr, state->cbs_namebuf, namelen + 1);
+ } else {
+ /* Store unmangled name for the directory hint else it will
+ * restart readdir at the last location again
+ */
+ u_int8_t *new_nameptr;
+ size_t bufsize;
+ size_t tmp_namelen = 0;
+
+ cnp = (const CatalogName *)&ckp->hfsPlus.nodeName;
+ bufsize = 1 + utf8_encodelen(cnp->ustr.unicode,
+ cnp->ustr.length * sizeof(UniChar),
+ ':', 0);
+ MALLOC(new_nameptr, u_int8_t *, bufsize, M_TEMP, M_WAITOK);
+ result = utf8_encodestr(cnp->ustr.unicode,
+ cnp->ustr.length * sizeof(UniChar),
+ new_nameptr, &tmp_namelen, bufsize, ':', 0);
+
+ state->cbs_desc->cd_namelen = tmp_namelen;
+ bcopy(new_nameptr, state->cbs_namebuf, tmp_namelen + 1);
+
+ FREE(new_nameptr, M_TEMP);
+ }
+ }
+ if (state->cbs_hasprevdirentry) {
+ curlinkref = ilinkref; /* save current */
+ ilinkref = state->cbs_previlinkref; /* use previous */
+ }
+ /*
+ * Record any hard links for post processing.
+ */
+ if ((ilinkref != 0) &&
+ (state->cbs_result == 0) &&
+ (state->cbs_nlinks < state->cbs_maxlinks)) {
+ state->cbs_linkinfo[state->cbs_nlinks].dirent_addr = uiobase;
+ state->cbs_linkinfo[state->cbs_nlinks].link_ref = ilinkref;
+ state->cbs_nlinks++;
+ }
+ if (state->cbs_hasprevdirentry) {
+ ilinkref = curlinkref; /* restore current */
+ }
+ }
+
+ /* Fill the direntry to be used the next time */
+ if (state->cbs_extended) {
+ if (stop_after_pack) {
+ state->cbs_eof = true;
+ return (0); /* stop */
+ }
+ entry->d_type = type;
+ entry->d_namlen = namelen;
+ entry->d_reclen = EXT_DIRENT_LEN(namelen);
+ if (hide) {
+ /* File number = 0 means skip entry */
+ entry->d_fileno = 0;
+ } else {
+ entry->d_fileno = cnid;
+ }
+ /* swap the current and previous entry */
+ struct direntry * tmp;
+ tmp = state->cbs_direntry;
+ state->cbs_direntry = state->cbs_prevdirentry;
+ state->cbs_prevdirentry = tmp;
+ state->cbs_hasprevdirentry = true;
+ state->cbs_previlinkref = ilinkref;
+ }
+
+ /* Continue iteration if there's room */
+ return (state->cbs_result == 0 &&
+ uio_resid(state->cbs_uio) >= SMALL_DIRENTRY_SIZE);
+}
+
+/*
+ * getdirentries callback for standard HFS (non HFS+) directories.
+ */
+static int
+getdirentries_std_callback(const CatalogKey *ckp, const CatalogRecord *crp,
+ struct packdirentry_state *state)
+{
+ struct hfsmount *hfsmp;
+ const CatalogName *cnp;
+ cnid_t curID;
+ OSErr result;
+ struct dirent catent;
+ cnid_t cnid;
+ u_int8_t type = DT_UNKNOWN;
+ u_int8_t *nameptr;
+ size_t namelen = 0;
+ size_t maxnamelen;
+ size_t uiosize = 0;
+ caddr_t uioaddr;
+
+ hfsmp = state->cbs_hfsmp;
+
+ curID = ckp->hfs.parentID;
+
+ /* We're done when parent directory changes */
+ if (state->cbs_parentID != curID) {
+ state->cbs_result = ENOENT;
+ return (0); /* stop */
+ }
+
+ nameptr = (u_int8_t *)&catent.d_name[0];
+ maxnamelen = NAME_MAX;
+
+ switch(crp->recordType) {
+ case kHFSFolderRecord:
+ type = DT_DIR;
+ cnid = crp->hfsFolder.folderID;
+ break;
+ case kHFSFileRecord:
+ type = DT_REG;
+ cnid = crp->hfsFile.fileID;
+ break;
+ default:
+ return (0); /* stop */
+ };
+
+ cnp = (const CatalogName*) ckp->hfs.nodeName;
+ result = hfs_to_utf8(hfsmp, cnp->pstr, maxnamelen + 1, (ByteCount *)&namelen, nameptr);
+ /*
+ * When an HFS name cannot be encoded with the current
+ * volume encoding we use MacRoman as a fallback.
+ */
+ if (result) {
+ result = mac_roman_to_utf8(cnp->pstr, maxnamelen + 1, (ByteCount *)&namelen, nameptr);
+ }
+ catent.d_type = type;
+ catent.d_namlen = namelen;
+ catent.d_reclen = uiosize = STD_DIRENT_LEN(namelen);
+ catent.d_fileno = cnid;
+ uioaddr = (caddr_t) &catent;
+
+ /* If this entry won't fit then we're done */
+ if (uiosize > uio_resid(state->cbs_uio)) {
+ return (0); /* stop */
+ }
+
+ state->cbs_result = uiomove(uioaddr, uiosize, state->cbs_uio);
+ if (state->cbs_result == 0) {
+ ++state->cbs_index;
+
+ /* Remember previous entry */
+ state->cbs_desc->cd_cnid = cnid;
+ if (type == DT_DIR) {
+ state->cbs_desc->cd_flags |= CD_ISDIR;
+ } else {
+ state->cbs_desc->cd_flags &= ~CD_ISDIR;
+ }
+ if (state->cbs_desc->cd_nameptr != NULL) {
+ state->cbs_desc->cd_namelen = 0;
+ }
+ state->cbs_desc->cd_namelen = namelen;
+ bcopy(nameptr, state->cbs_namebuf, namelen + 1);
+ }
+
+ /* Continue iteration if there's room */
+ return (state->cbs_result == 0 && uio_resid(state->cbs_uio) >= SMALL_DIRENTRY_SIZE);
+}
+
+/*
+ * Pack a uio buffer with directory entries from the catalog
+ */
+__private_extern__
+int
+cat_getdirentries(struct hfsmount *hfsmp, int entrycnt, directoryhint_t *dirhint,
+ uio_t uio, int extended, int * items, int * eofflag)
+{
+ FCB* fcb;
+ BTreeIterator * iterator;
+ CatalogKey * key;
+ struct packdirentry_state state;
+ void * buffer;
+ int bufsize;
+ int maxlinks;
+ int result;
+ int index;
+ int have_key;
+
+ if (extended && (hfsmp->hfs_flags & HFS_STANDARD)) {
+ return (ENOTSUP);
+ }
+ fcb = hfsmp->hfs_catalog_cp->c_datafork;
+
+ /*
+ * Get a buffer for link info array, btree iterator and a direntry:
+ */
+ maxlinks = MIN(entrycnt, uio_resid(uio) / SMALL_DIRENTRY_SIZE);
+ bufsize = MAXPATHLEN + (maxlinks * sizeof(linkinfo_t)) + sizeof(*iterator);
+ if (extended) {
+ bufsize += 2*sizeof(struct direntry);
+ }
+ MALLOC(buffer, void *, bufsize, M_TEMP, M_WAITOK);
+ bzero(buffer, bufsize);
+
+ state.cbs_extended = extended;
+ state.cbs_hasprevdirentry = false;
+ state.cbs_previlinkref = 0;
+ state.cbs_nlinks = 0;
+ state.cbs_maxlinks = maxlinks;
+ state.cbs_linkinfo = (linkinfo_t *)((char *)buffer + MAXPATHLEN);
+
+ iterator = (BTreeIterator *) ((char *)state.cbs_linkinfo + (maxlinks * sizeof(linkinfo_t)));
+ key = (CatalogKey *)&iterator->key;
+ have_key = 0;
+ index = dirhint->dh_index + 1;
+ if (extended) {
+ state.cbs_direntry = (struct direntry *)((char *)iterator + sizeof(BTreeIterator));
+ state.cbs_prevdirentry = state.cbs_direntry + 1;
+ state.cbs_eof = false;
+ }
+ /*
+ * Attempt to build a key from cached filename
+ */
+ if (dirhint->dh_desc.cd_namelen != 0) {
+ if (buildkey(hfsmp, &dirhint->dh_desc, (HFSPlusCatalogKey *)key, 0) == 0) {
+ iterator->hint.nodeNum = dirhint->dh_desc.cd_hint;
+ have_key = 1;
+ }
+ }