/*
- * Copyright (c) 1999-2014 Apple Inc. All rights reserved.
+ * Copyright (c) 1999-2015 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
#include <unistd.h>
#include <wipefs.h>
+#include <TargetConditionals.h>
+
+#if TARGET_OS_IPHONE
+
+// CoreServices is not available, so...
+
+/*
+ * Mac OS Finder flags
+ */
+enum {
+ kHasBeenInited = 0x0100, /* Files only */
+ /* Clear if the file contains desktop database */
+ /* bit 0x0200 was the letter bit for AOCE, but is now reserved for future use */
+ kHasCustomIcon = 0x0400, /* Files and folders */
+ kIsStationery = 0x0800, /* Files only */
+ kNameLocked = 0x1000, /* Files and folders */
+ kHasBundle = 0x2000, /* Files only */
+ kIsInvisible = 0x4000, /* Files and folders */
+ kIsAlias = 0x8000 /* Files only */
+};
+
+enum {
+ kTextEncodingMacUnicode = 0x7E,
+};
+
+#else // !TARGET_OS_IPHONE
+
+#include <CoreServices/CoreServices.h>
+
+#endif // !TARGET_OS_IPHONE
+
/*
* CommonCrypto is meant to be a more stable API than OpenSSL.
* Defining COMMON_DIGEST_FOR_OPENSSL gives API-compatibility
static int ConvertUTF8toUnicode __P((const UInt8* source, size_t bufsize,
UniChar* unibuf, UInt16 *charcount));
-static int getencodinghint(unsigned char *name);
-
#define VOLUMEUUIDVALUESIZE 2
typedef union VolumeUUID {
UInt32 value[VOLUMEUUIDVALUESIZE];
if (header == NULL)
err(1, NULL);
- defaults->encodingHint = getencodinghint(defaults->volumeName);
-
/* VH Initialized in native byte order */
InitVH(defaults, driveInfo->totalSectors, header);
/*--- WRITE FILE ATTRIBUTES B-TREE TO DISK: */
- if (defaults->attributesClumpSize) {
+ if (defaults->attributesInitialSize) {
btNodeSize = defaults->attributesNodeSize;
sectorsPerNode = btNodeSize/kBytesPerSector;
hp->rsrcClumpSize = defaults->rsrcClumpSize;
hp->dataClumpSize = defaults->dataClumpSize;
hp->nextCatalogID = defaults->nextFreeFileID;
- hp->encodingsBitmap = 1 | (1 << ENCODING_TO_BIT(defaults->encodingHint));
+ hp->encodingsBitmap = 1;
/* set up allocation bitmap file */
hp->allocationFile.clumpSize = defaults->allocationClumpSize;
/* set up extents b-tree file */
hp->extentsFile.clumpSize = defaults->extentsClumpSize;
- hp->extentsFile.logicalSize = defaults->extentsClumpSize;
- hp->extentsFile.totalBlocks = defaults->extentsClumpSize / blockSize;
+ hp->extentsFile.logicalSize = defaults->extentsInitialSize;
+ hp->extentsFile.totalBlocks = defaults->extentsInitialSize / blockSize;
if (NEWFS_HFS_DEBUG && defaults->extentsStartBlock)
allocateBlock = defaults->extentsStartBlock;
else {
printf ("extentsFile : (%10u, %10u)\n", hp->extentsFile.extents[0].startBlock, hp->extentsFile.totalBlocks);
/* set up attributes b-tree file */
- if (defaults->attributesClumpSize) {
+ if (defaults->attributesInitialSize) {
hp->attributesFile.clumpSize = defaults->attributesClumpSize;
- hp->attributesFile.logicalSize = defaults->attributesClumpSize;
- hp->attributesFile.totalBlocks = defaults->attributesClumpSize / blockSize;
+ hp->attributesFile.logicalSize = defaults->attributesInitialSize;
+ hp->attributesFile.totalBlocks = defaults->attributesInitialSize / blockSize;
if (NEWFS_HFS_DEBUG && defaults->attributesStartBlock)
allocateBlock = defaults->attributesStartBlock;
else {
/* set up catalog b-tree file */
hp->catalogFile.clumpSize = defaults->catalogClumpSize;
- hp->catalogFile.logicalSize = defaults->catalogClumpSize;
- hp->catalogFile.totalBlocks = defaults->catalogClumpSize / blockSize;
+ hp->catalogFile.logicalSize = defaults->catalogInitialSize;
+ hp->catalogFile.totalBlocks = defaults->catalogInitialSize / blockSize;
if (NEWFS_HFS_DEBUG && defaults->catalogStartBlock)
allocateBlock = defaults->catalogStartBlock;
else {
UInt32 blockCount)
{
size_t bufSize = driveInfo->physSectorSize;
- uint8_t buf[bufSize];
+ void *buf;
uint32_t blocksLeft = blockCount;
uint32_t curBlock = startBlock;
static const int kBitsPerByte = 8;
+ int status = -1;
+
+ buf = valloc(bufSize);
+ if (buf == NULL)
+ err(1, NULL);
/*
* We loop through physSectorSize blocks.
if (nbytes < (ssize_t)bufSize) {
if (nbytes == -1)
err(1, "%s::pread(%d, %p, %zu, %lld)", __FUNCTION__, driveInfo->fd, buf, bufSize, offset);
- return -1;
+ goto exit;
}
if (AllocateExtent(buf, blockOffset, numBlocks) == -1) {
warnx("In-use allocation block in <%u, %u>", blockOffset, numBlocks);
- return -1;
+ goto exit;
}
nwritten = pwrite(driveInfo->fd, buf, bufSize, offset);
/*
* means a return value of 0 or -1, neither of which I could do anything about.
*/
if (nwritten != (ssize_t)bufSize)
- return -1;
+ goto exit;
// And go get the next set, if needed
blocksLeft -= numBlocks;
curBlock += numBlocks;
}
- return 0;
+ status = 0;
+
+exit:
+ free(buf);
+
+ return status;
}
/*
* WriteExtentsFile
SInt16 offset;
*mapNodes = 0;
- fileSize = dp->extentsClumpSize;
+ fileSize = dp->extentsInitialSize;
nodeSize = dp->extentsNodeSize;
bzero(buffer, nodeSize);
bthp->nodeSize = SWAP_BE16 (nodeSize);
bthp->totalNodes = SWAP_BE32 (fileSize / nodeSize);
bthp->freeNodes = SWAP_BE32 (SWAP_BE32 (bthp->totalNodes) - (numOverflowExtents ? 2 : 1)); /* header */
- bthp->clumpSize = SWAP_BE32 (fileSize);
+ bthp->clumpSize = SWAP_BE32 (dp->extentsClumpSize);
bthp->attributes |= SWAP_BE32 (kBTBigKeysMask);
bthp->maxKeyLength = SWAP_BE16 (kHFSPlusExtentKeyMaximumLength);
int set_cp_level = 0;
*mapNodes = 0;
- fileSize = dp->attributesClumpSize;
+ fileSize = dp->attributesInitialSize;
nodeSize = dp->attributesNodeSize;
#ifdef DEBUG_BUILD
/* Take the header into account */
bthp->freeNodes = SWAP_BE32 (SWAP_BE32 (bthp->totalNodes) - 1);
}
- bthp->clumpSize = SWAP_BE32 (fileSize);
+ bthp->clumpSize = SWAP_BE32 (dp->attributesClumpSize);
bthp->attributes |= SWAP_BE32 (kBTBigKeysMask | kBTVariableIndexKeysMask);
bthp->maxKeyLength = SWAP_BE16 (kHFSPlusAttrKeyMaximumLength);
SInt16 offset;
*mapNodes = 0;
- fileSize = dp->catalogClumpSize;
+ fileSize = dp->catalogInitialSize;
nodeSize = dp->catalogNodeSize;
bzero(buffer, nodeSize);
bthp->nodeSize = SWAP_BE16 (nodeSize);
bthp->totalNodes = SWAP_BE32 (fileSize / nodeSize);
bthp->freeNodes = SWAP_BE32 (SWAP_BE32 (bthp->totalNodes) - 2); /* header and root */
- bthp->clumpSize = SWAP_BE32 (fileSize);
+ bthp->clumpSize = SWAP_BE32 (dp->catalogClumpSize);
bthp->attributes |= SWAP_BE32 (kBTVariableIndexKeysMask + kBTBigKeysMask);
cdp->folderID = SWAP_BE32 (kHFSRootFolderID);
cdp->createDate = SWAP_BE32 (dp->createDate);
cdp->contentModDate = SWAP_BE32 (dp->createDate);
- cdp->textEncoding = SWAP_BE32 (dp->encodingHint);
+ cdp->textEncoding = SWAP_BE32 (kTextEncodingMacUnicode);
if (dp->flags & kUseAccessPerms) {
cdp->bsdInfo.ownerID = SWAP_BE32 (dp->owner);
cdp->bsdInfo.groupID = SWAP_BE32 (dp->group);
return (localTime);
}
-#define __kCFUserEncodingFileName ("/.CFUserTextEncoding")
-
-static UInt32
-GetDefaultEncoding()
-{
- struct passwd *passwdp;
-
- if ((passwdp = getpwuid(0))) { // root account
- char buffer[MAXPATHLEN + 1];
- int fd;
-
- strlcpy(buffer, passwdp->pw_dir, sizeof(buffer));
- strlcat(buffer, __kCFUserEncodingFileName, sizeof(buffer));
-
- if ((fd = open(buffer, O_RDONLY, 0)) > 0) {
- ssize_t readSize;
-
- readSize = read(fd, buffer, MAXPATHLEN);
- buffer[(readSize < 0 ? 0 : readSize)] = '\0';
- close(fd);
- return strtol(buffer, NULL, 0);
- }
- }
- return 0;
-}
-
-
static int
ConvertUTF8toUnicode(const UInt8* source, size_t bufsize, UniChar* unibuf,
UInt16 *charcount)
return (0);
}
-/*
- * Derive the encoding hint for the given name.
- */
-static int
-getencodinghint(unsigned char *name)
-{
- int mib[3];
- size_t buflen = sizeof(int);
- struct vfsconf vfc;
- int hint = 0;
-
- if (getvfsbyname("hfs", &vfc) < 0)
- goto error;
-
- mib[0] = CTL_VFS;
- mib[1] = vfc.vfc_typenum;
- mib[2] = HFS_ENCODINGHINT;
-
- if (sysctl(mib, 3, &hint, &buflen, name, strlen((char *)name) + 1) < 0)
- goto error;
- return (hint);
-error:
- hint = GetDefaultEncoding();
- return (hint);
-}
-
-
/* Generate Volume UUID - similar to code existing in hfs_util */
void GenerateVolumeUUID(VolumeUUID *newVolumeID) {
SHA_CTX context;