ciderpress/diskimg/HFS.cpp

2297 lines
70 KiB
C++
Raw Normal View History

2007-03-27 17:47:10 +00:00
/*
* CiderPress
* Copyright (C) 2007 by faddenSoft, LLC. All Rights Reserved.
* See the file LICENSE for distribution terms.
*/
/*
* Implementation of the Macintosh HFS filesystem.
*
* Most of the stuff lives in libhfs. To avoid problems that could arise
* from people ejecting floppies or trying to use a disk image while
* CiderPress is still open, we call hfs_flush() to force updates to be
* written. (Even with the "no caching" flag set, the master dir block and
* volume bitmap aren't written until flush is called.)
*
* The libhfs code is licensed under the full GPL, making it awkward to
* use in a commercial product. Support for libhfs can be removed with
* the EXCISE_GPL_CODE ifdefs. A stub will remain that can recognize HFS
* volumes, which is useful when dealing with Apple II hard drive and CFFA
* images.
*/
#include "StdAfx.h"
#include "DiskImgPriv.h"
/*
* ===========================================================================
* DiskFSHFS
2007-03-27 17:47:10 +00:00
* ===========================================================================
*/
const int kBlkSize = 512;
const int kMasterDirBlock = 2; // also a copy in next-to-last block
const uint16_t kSignature = 0x4244; // or 0xd2d7 for MFS
2007-03-27 17:47:10 +00:00
const int kMaxDirectoryDepth = 128; // not sure what HFS limit is
//namespace DiskImgLib {
/* extent descriptor */
typedef struct ExtDescriptor {
uint16_t xdrStABN; // first allocation block
uint16_t xdrNumABlks; // #of allocation blocks
2007-03-27 17:47:10 +00:00
} ExtDescriptor;
/* extent data record */
typedef struct ExtDataRec {
ExtDescriptor extDescriptor[3];
2007-03-27 17:47:10 +00:00
} ExtDataRec;
/*
* Contents of the HFS MDB. Information comes from "Inside Macintosh: Files",
* chapter 2 ("Data Organization on Volumes"), pages 2-60 to 2-62.
*/
typedef struct DiskFSHFS::MasterDirBlock {
uint16_t drSigWord; // volume signature
uint32_t drCrDate; // date/time of volume creation
uint32_t drLsMod; // date/time of last modification
uint16_t drAtrb; // volume attributes
uint16_t drNmPls; // #of files in root directory
uint16_t drVBMSt; // first block of volume bitmap
uint16_t drAllocPtr; // start of next allocation search
uint16_t drNmAlBlks; // number of allocation blocks in volume
uint32_t drAlBlkSiz; // size (bytes) of allocation blocks
uint32_t drClpSiz; // default clump size
uint16_t drAlBlSt; // first allocation block in volume
uint32_t drNxtCNID; // next unused catalog node ID
uint16_t drFreeBks; // number of unused allocation blocks
uint8_t drVN[28]; // volume name (pascal string)
uint32_t drVolBkUp; // date/time of last backup
uint16_t drVSeqNum; // volume backup sequence number
uint32_t drWrCnt; // volume write count
uint32_t drXTClpSiz; // clump size for extents overflow file
uint32_t drCTClpSiz; // clump size for catalog file
uint16_t drNmRtDirs; // #of directories in root directory
uint32_t drFilCnt; // #of files in volume
uint32_t drDirCnt; // #of directories in volume
uint32_t drFndrInfo[8]; // information used by the Finder
uint16_t drVCSize; // size (blocks) of volume cache
uint16_t drVBMCSize; // size (blocks) of volume bitmap cache
uint16_t drCtlCSize; // size (blocks) of common volume cache
uint32_t drXTFlSize; // size (bytes) of extents overflow file
ExtDataRec drXTExtRec; // extent record for extents overflow file
uint32_t drCTFlSize; // size (bytes) of catalog file
ExtDataRec drCTExtRec; // extent record for catalog file
2007-03-27 17:47:10 +00:00
} MasterDirBlock;
//}; // namespace DiskImgLib
/*
* Extract fields from a Master Directory Block.
*/
/*static*/ void DiskFSHFS::UnpackMDB(const uint8_t* buf, MasterDirBlock* pMDB)
2007-03-27 17:47:10 +00:00
{
pMDB->drSigWord = GetShortBE(&buf[0x00]);
pMDB->drCrDate = GetLongBE(&buf[0x02]);
pMDB->drLsMod = GetLongBE(&buf[0x06]);
pMDB->drAtrb = GetShortBE(&buf[0x0a]);
pMDB->drNmPls = GetShortBE(&buf[0x0c]);
pMDB->drVBMSt = GetShortBE(&buf[0x0e]);
pMDB->drAllocPtr = GetShortBE(&buf[0x10]);
pMDB->drNmAlBlks = GetShortBE(&buf[0x12]);
pMDB->drAlBlkSiz = GetLongBE(&buf[0x14]);
pMDB->drClpSiz = GetLongBE(&buf[0x18]);
pMDB->drAlBlSt = GetShortBE(&buf[0x1c]);
pMDB->drNxtCNID = GetLongBE(&buf[0x1e]);
pMDB->drFreeBks = GetShortBE(&buf[0x22]);
memcpy(pMDB->drVN, &buf[0x24], sizeof(pMDB->drVN));
pMDB->drVolBkUp = GetLongBE(&buf[0x40]);
pMDB->drVSeqNum = GetShortBE(&buf[0x44]);
pMDB->drWrCnt = GetLongBE(&buf[0x46]);
pMDB->drXTClpSiz = GetLongBE(&buf[0x4a]);
pMDB->drCTClpSiz = GetLongBE(&buf[0x4e]);
pMDB->drNmRtDirs = GetShortBE(&buf[0x52]);
pMDB->drFilCnt = GetLongBE(&buf[0x54]);
pMDB->drDirCnt = GetLongBE(&buf[0x58]);
for (int i = 0; i < (int) NELEM(pMDB->drFndrInfo); i++)
pMDB->drFndrInfo[i] = GetLongBE(&buf[0x5c + i * 4]);
pMDB->drVCSize = GetShortBE(&buf[0x7c]);
pMDB->drVBMCSize = GetShortBE(&buf[0x7e]);
pMDB->drCtlCSize = GetShortBE(&buf[0x80]);
pMDB->drXTFlSize = GetLongBE(&buf[0x82]);
//UnpackExtDataRec(&pMDB->drXTExtRec, &buf[0x86]); // 12 bytes
pMDB->drCTFlSize = GetLongBE(&buf[0x92]);
//UnpackExtDataRec(&pMDB->drXTExtRec, &buf[0x96]);
// next field at 0xa2
2007-03-27 17:47:10 +00:00
}
/*
* See if this looks like an HFS volume.
*
* We test a few fields in the master directory block for validity.
*/
/*static*/ DIError DiskFSHFS::TestImage(DiskImg* pImg, DiskImg::SectorOrder imageOrder)
2007-03-27 17:47:10 +00:00
{
DIError dierr = kDIErrNone;
MasterDirBlock mdb;
uint8_t blkBuf[kBlkSize];
dierr = pImg->ReadBlockSwapped(kMasterDirBlock, blkBuf, imageOrder,
DiskImg::kSectorOrderProDOS);
if (dierr != kDIErrNone)
goto bail;
UnpackMDB(blkBuf, &mdb);
if (mdb.drSigWord != kSignature) {
dierr = kDIErrFilesystemNotFound;
goto bail;
}
if ((mdb.drAlBlkSiz & 0x1ff) != 0) {
// allocation block size must be a multiple of 512
LOGI(" HFS: found allocation block size = %lu, rejecting",
mdb.drAlBlkSiz);
dierr = kDIErrFilesystemNotFound;
goto bail;
}
if (mdb.drVN[0] == 0 || mdb.drVN[0] > kMaxVolumeName) {
LOGI(" HFS: volume name has len = %d, rejecting", mdb.drVN[0]);
dierr = kDIErrFilesystemNotFound;
goto bail;
}
long minBlocks;
minBlocks = mdb.drNmAlBlks * (mdb.drAlBlkSiz / kBlkSize) + mdb.drAlBlSt + 2;
if (minBlocks > pImg->GetNumBlocks()) {
// We're probably trying to open a 1GB volume as if it were only
// 32MB. Maybe this is a full HFS partition and we're trying to
// see if it's a CFFA image. Whatever the case, we can't do this.
LOGI("HFS: volume exceeds disk image size (%ld vs %ld)",
minBlocks, pImg->GetNumBlocks());
dierr = kDIErrFilesystemNotFound;
goto bail;
}
// looks good!
2007-03-27 17:47:10 +00:00
bail:
return dierr;
2007-03-27 17:47:10 +00:00
}
/*
* Test to see if the image is an HFS disk.
*/
/*static*/ DIError DiskFSHFS::TestFS(DiskImg* pImg, DiskImg::SectorOrder* pOrder,
DiskImg::FSFormat* pFormat, FSLeniency leniency)
2007-03-27 17:47:10 +00:00
{
//return kDIErrFilesystemNotFound; // DEBUG DEBUG DEBUG
/* must be block format, should be at least 720K */
if (!pImg->GetHasBlocks() || pImg->GetNumBlocks() < kExpectedMinBlocks)
return kDIErrFilesystemNotFound;
DiskImg::SectorOrder ordering[DiskImg::kSectorOrderMax];
DiskImg::GetSectorOrderArray(ordering, *pOrder);
for (int i = 0; i < DiskImg::kSectorOrderMax; i++) {
if (ordering[i] == DiskImg::kSectorOrderUnknown)
continue;
if (TestImage(pImg, ordering[i]) == kDIErrNone) {
*pOrder = ordering[i];
*pFormat = DiskImg::kFormatMacHFS;
return kDIErrNone;
}
}
LOGI(" HFS didn't find valid FS");
return kDIErrFilesystemNotFound;
2007-03-27 17:47:10 +00:00
}
/*
* Load some stuff from the volume header.
*/
DIError DiskFSHFS::LoadVolHeader(void)
2007-03-27 17:47:10 +00:00
{
DIError dierr = kDIErrNone;
MasterDirBlock mdb;
uint8_t blkBuf[kBlkSize];
if (fLocalTimeOffset == -1) {
struct tm* ptm;
struct tm tmWhen;
time_t when;
int isDst;
2014-11-18 05:13:13 +00:00
when = time(NULL);
isDst = localtime(&when)->tm_isdst;
ptm = gmtime(&when);
2014-11-18 05:13:13 +00:00
if (ptm != NULL) {
tmWhen = *ptm; // make a copy -- static buffers in time functions
tmWhen.tm_isdst = isDst;
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
fLocalTimeOffset = (long) (when - mktime(&tmWhen));
} else
fLocalTimeOffset = 0;
LOGI(" HFS computed local time offset = %.3f hours",
fLocalTimeOffset / 3600.0);
}
dierr = fpImg->ReadBlock(kMasterDirBlock, blkBuf);
if (dierr != kDIErrNone)
goto bail;
UnpackMDB(blkBuf, &mdb);
/*
* The minimum size of the volume is "number of allocation blocks" plus
* "first allocation block" (to avoid the OS overhead) plus 2 (because
* there's a backup copy of the MDB in the next-to-last block, and
* nothing at all in the very last block).
2007-03-27 17:47:10 +00:00
*
* This isn't the total size, because on larger volumes there can be
* some padding between the last usable block and the backup MDB. The
* only way to find the MDB is to take the DiskImg's block size and
* subtract 2.
*/
assert((mdb.drAlBlkSiz % kBlkSize) == 0);
fNumAllocationBlocks = mdb.drNmAlBlks;
fAllocationBlockSize = mdb.drAlBlkSiz;
2007-03-27 17:47:10 +00:00
fTotalBlocks = fpImg->GetNumBlocks();
uint32_t minBlocks;
minBlocks = mdb.drNmAlBlks * (mdb.drAlBlkSiz / kBlkSize) + mdb.drAlBlSt + 2;
2007-03-27 17:47:10 +00:00
assert(fTotalBlocks >= minBlocks); // verified during fs tests
int volNameLen;
volNameLen = mdb.drVN[0];
if (volNameLen > kMaxVolumeName) {
assert(false); // should've been trapped earlier
volNameLen = kMaxVolumeName;
}
memcpy(fVolumeName, &mdb.drVN[1], volNameLen);
fVolumeName[volNameLen] = '\0';
SetVolumeID();
fNumFiles = mdb.drFilCnt;
fNumDirectories = mdb.drDirCnt;
fCreatedDateTime = mdb.drCrDate;
fModifiedDateTime = mdb.drLsMod;
/*
* Create a "magic" directory entry for the volume directory. This
2007-03-27 17:47:10 +00:00
* must come first in the file list.
*/
A2FileHFS* pFile;
pFile = new A2FileHFS(this);
2014-11-18 05:13:13 +00:00
if (pFile == NULL) {
dierr = kDIErrMalloc;
goto bail;
}
2007-03-27 17:47:10 +00:00
pFile->fIsDir = true;
pFile->fIsVolumeDir = true;
pFile->fType = 0;
pFile->fCreator = 0;
strcpy(pFile->fFileName, fVolumeName); // vol names are shorter than
pFile->SetPathName(":", fVolumeName); // filenames, so it fits
2007-03-27 17:47:10 +00:00
pFile->fDataLength = 0;
pFile->fRsrcLength = -1;
pFile->fCreateWhen =
(time_t) (fCreatedDateTime - kDateTimeOffset) - fLocalTimeOffset;
pFile->fModWhen =
(time_t) (fModifiedDateTime - kDateTimeOffset) - fLocalTimeOffset;
pFile->fAccess = DiskFS::kFileAccessUnlocked;
2007-03-27 17:47:10 +00:00
//LOGI("GOT *** '%s' '%s'", pFile->fFileName, pFile->fPathName);
2007-03-27 17:47:10 +00:00
AddFileToList(pFile);
2007-03-27 17:47:10 +00:00
bail:
return dierr;
2007-03-27 17:47:10 +00:00
}
/*
* Set the volume ID based on fVolumeName.
*/
void DiskFSHFS::SetVolumeID(void)
2007-03-27 17:47:10 +00:00
{
strcpy(fVolumeID, "HFS ");
strcat(fVolumeID, fVolumeName);
2007-03-27 17:47:10 +00:00
}
/*
* Blank out the volume usage map. The HFS volume bitmap is not yet supported.
*/
void DiskFSHFS::SetVolumeUsageMap(void)
2007-03-27 17:47:10 +00:00
{
VolumeUsage::ChunkState cstate;
long block;
2007-03-27 17:47:10 +00:00
fVolumeUsage.Create(fpImg->GetNumBlocks());
2007-03-27 17:47:10 +00:00
cstate.isUsed = true;
cstate.isMarkedUsed = true;
cstate.purpose = VolumeUsage::kChunkPurposeUnknown;
2007-03-27 17:47:10 +00:00
for (block = fTotalBlocks-1; block >= 0; block--)
fVolumeUsage.SetChunkState(block, &cstate);
2007-03-27 17:47:10 +00:00
}
/*
* Print some interesting fields to the debug log.
*/
void DiskFSHFS::DumpVolHeader(void)
2007-03-27 17:47:10 +00:00
{
LOGI("HFS volume header read:");
LOGI(" volume name = '%s'", fVolumeName);
LOGI(" total blocks = %ld (allocSize=%ld [x%lu], numAllocs=%lu)",
fTotalBlocks, fAllocationBlockSize, fAllocationBlockSize / kBlkSize,
fNumAllocationBlocks);
LOGI(" num directories=%ld, num files=%ld",
fNumDirectories, fNumFiles);
time_t when;
when = (time_t) (fCreatedDateTime - kDateTimeOffset - fLocalTimeOffset);
LOGI(" cre date=0x%08lx %.24s", fCreatedDateTime, ctime(&when));
when = (time_t) (fModifiedDateTime - kDateTimeOffset - fLocalTimeOffset);
LOGI(" mod date=0x%08lx %.24s", fModifiedDateTime, ctime(&when));
2007-03-27 17:47:10 +00:00
}
#ifndef EXCISE_GPL_CODE
/*
* Get things rolling.
*
* Since we're assured that this is a valid disk, errors encountered from here
* on out must be handled somehow, possibly by claiming that the disk is
* completely full and has no files on it.
*/
DIError DiskFSHFS::Initialize(InitMode initMode)
2007-03-27 17:47:10 +00:00
{
DIError dierr = kDIErrNone;
char msg[kMaxVolumeName + 32];
dierr = LoadVolHeader();
if (dierr != kDIErrNone)
goto bail;
DumpVolHeader();
if (initMode == kInitHeaderOnly) {
LOGI(" HFS - headerOnly set, skipping file load");
goto bail;
}
sprintf(msg, "Scanning %s", fVolumeName);
if (!fpImg->UpdateScanProgress(msg)) {
LOGI(" HFS cancelled by user");
dierr = kDIErrCancelled;
goto bail;
}
/*
* Open the volume with libhfs. We used to set HFS_OPT_NOCACHE to avoid
* consistency problems and reduce the risk of disk corruption should
* CiderPress fail, but it turns out libhfs doesn't write the volume
* bitmap or master dir block unless explicitly flushed anyway. Since
* the caching helps us a lot when just reading -- 4 seconds vs. 9 for
* a CD-ROM over gigabit Ethernet -- we leave it on, and explicitly
* flush every time we make a change.
*/
2007-03-27 17:47:10 +00:00
fHfsVol = hfs_callback_open(LibHFSCB, this, /*HFS_OPT_NOCACHE |*/
(fpImg->GetReadOnly() ? HFS_MODE_RDONLY : HFS_MODE_RDWR));
2014-11-18 05:13:13 +00:00
if (fHfsVol == NULL) {
LOGI("ERROR: hfs_opencallback failed: %s", hfs_error);
2007-03-27 17:47:10 +00:00
return kDIErrGeneric;
}
/* volume dir is guaranteed to come first; if not, we need a lookup func */
A2FileHFS* pVolumeDir;
2014-11-18 05:13:13 +00:00
pVolumeDir = (A2FileHFS*) GetNextFile(NULL);
2007-03-27 17:47:10 +00:00
dierr = RecursiveDirAdd(pVolumeDir, ":", 0);
if (dierr != kDIErrNone)
goto bail;
SetVolumeUsageMap();
2007-03-27 17:47:10 +00:00
/*
* Make sure there's nothing lingering. libhfs will fiddle around with
* the MDB if it looks like the volume wasn't unmounted cleanly last time.
*/
hfs_flush(fHfsVol);
2007-03-27 17:47:10 +00:00
bail:
return dierr;
2007-03-27 17:47:10 +00:00
}
/*
* Callback function from libhfs. Can read/write/seek.
*
* This is a little clumsy, but it allows us to maintain a separation from
* the libhfs code (which is GPLed).
*
* Returns -1 on failure.
*/
unsigned long DiskFSHFS::LibHFSCB(void* vThis, int op, unsigned long arg1, void* arg2)
2007-03-27 17:47:10 +00:00
{
DiskFSHFS* pThis = (DiskFSHFS*) vThis;
unsigned long result = (unsigned long) -1;
2014-11-18 05:13:13 +00:00
assert(pThis != NULL);
2007-03-27 17:47:10 +00:00
switch (op) {
case HFS_CB_VOLSIZE:
//LOGI(" HFSCB vol size = %ld blocks", pThis->fTotalBlocks);
2007-03-27 17:47:10 +00:00
result = pThis->fTotalBlocks;
break;
case HFS_CB_READ: // arg1=block, arg2=buffer
//LOGI(" HFSCB read block %lu", arg1);
2014-11-18 05:13:13 +00:00
if (arg1 < pThis->fTotalBlocks && arg2 != NULL) {
2007-03-27 17:47:10 +00:00
DIError err = pThis->fpImg->ReadBlock(arg1, arg2);
if (err == kDIErrNone)
result = 0;
else {
LOGI(" HFSCB read %lu failed", arg1);
2007-03-27 17:47:10 +00:00
}
}
break;
case HFS_CB_WRITE:
LOGI(" HFSCB write block %lu", arg1);
2014-11-18 05:13:13 +00:00
if (arg1 < pThis->fTotalBlocks && arg2 != NULL) {
2007-03-27 17:47:10 +00:00
DIError err = pThis->fpImg->WriteBlock(arg1, arg2);
if (err == kDIErrNone)
result = 0;
else {
LOGI(" HFSCB write %lu failed", arg1);
2007-03-27 17:47:10 +00:00
}
}
break;
case HFS_CB_SEEK: // arg1=block, arg2=unused
/* just verify that the seek is legal */
//LOGI(" HFSCB seek block %lu", arg1);
2007-03-27 17:47:10 +00:00
if (arg1 < pThis->fTotalBlocks)
result = arg1;
break;
default:
assert(false);
}
//LOGI("--- HFSCB returning %lu", result);
2007-03-27 17:47:10 +00:00
return result;
}
/*
* Determine the amount of free space on the disk.
*/
DIError DiskFSHFS::GetFreeSpaceCount(long* pTotalUnits, long* pFreeUnits,
int* pUnitSize) const
2007-03-27 17:47:10 +00:00
{
2014-11-18 05:13:13 +00:00
assert(fHfsVol != NULL);
2007-03-27 17:47:10 +00:00
hfsvolent volEnt;
if (hfs_vstat(fHfsVol, &volEnt) != 0)
return kDIErrGeneric;
2007-03-27 17:47:10 +00:00
*pTotalUnits = volEnt.totbytes / 512;
*pFreeUnits = volEnt.freebytes / 512;
*pUnitSize = 512;
2007-03-27 17:47:10 +00:00
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Recursively traverse the filesystem.
*/
DIError DiskFSHFS::RecursiveDirAdd(A2File* pParent, const char* basePath, int depth)
2007-03-27 17:47:10 +00:00
{
DIError dierr = kDIErrNone;
hfsdir* dir;
hfsdirent dirEntry;
2014-11-18 05:13:13 +00:00
char* pathBuf = NULL;
2007-03-27 17:47:10 +00:00
int nameOffset;
/* if we get too deep, assume it's a loop */
if (depth > kMaxDirectoryDepth) {
dierr = kDIErrDirectoryLoop;
goto bail;
}
//LOGI(" HFS RecursiveDirAdd '%s'", basePath);
2007-03-27 17:47:10 +00:00
dir = hfs_opendir(fHfsVol, basePath);
2014-11-18 05:13:13 +00:00
if (dir == NULL) {
2007-03-27 17:47:10 +00:00
printf(" HFS unable to open dir '%s'\n", basePath);
LOGI(" HFS unable to open dir '%s'", basePath);
2007-03-27 17:47:10 +00:00
dierr = kDIErrGeneric;
goto bail;
}
if (strcmp(basePath, ":") == 0)
basePath = "";
nameOffset = strlen(basePath) +1;
pathBuf = new char[nameOffset + A2FileHFS::kMaxFileName +1];
2014-11-18 05:13:13 +00:00
if (pathBuf == NULL) {
2007-03-27 17:47:10 +00:00
dierr = kDIErrMalloc;
goto bail;
}
strcpy(pathBuf, basePath);
pathBuf[nameOffset-1] = A2FileHFS::kFssep;
pathBuf[nameOffset] = '\0'; // not needed
while (hfs_readdir(dir, &dirEntry) != -1) {
A2FileHFS* pFile;
2007-03-27 17:47:10 +00:00
pFile = new A2FileHFS(this);
pFile->InitEntry(&dirEntry);
2007-03-27 17:47:10 +00:00
pFile->SetPathName(basePath, pFile->fFileName);
pFile->SetParent(pParent);
2007-03-27 17:47:10 +00:00
AddFileToList(pFile);
2014-11-18 05:13:13 +00:00
if (!fpImg->UpdateScanProgress(NULL)) {
LOGI(" HFS cancelled by user");
dierr = kDIErrCancelled;
goto bail;
}
2007-03-27 17:47:10 +00:00
if (dirEntry.flags & HFS_ISDIR) {
strcpy(pathBuf + nameOffset, dirEntry.name);
dierr = RecursiveDirAdd(pFile, pathBuf, depth+1);
if (dierr != kDIErrNone)
goto bail;
}
}
bail:
delete[] pathBuf;
2007-03-27 17:47:10 +00:00
return dierr;
}
/*
* Initialize an A2FileHFS structure from the stuff in an hfsdirent.
*/
void A2FileHFS::InitEntry(const hfsdirent* dirEntry)
{
//printf("--- File '%s' flags=0x%08x fdflags=0x%08x type='%s'\n",
// dirEntry.name, dirEntry.flags, dirEntry.fdflags,
// dirEntry.u.file.type);
fIsVolumeDir = false;
memcpy(fFileName, dirEntry->name, A2FileHFS::kMaxFileName+1);
fFileName[A2FileHFS::kMaxFileName] = '\0'; // make sure
if (dirEntry->flags & HFS_ISLOCKED)
fAccess = DiskFS::kFileAccessLocked;
else
fAccess = DiskFS::kFileAccessUnlocked;
if (dirEntry->fdflags & HFS_FNDR_ISINVISIBLE)
fAccess |= A2FileProDOS::kAccessInvisible;
2007-03-27 17:47:10 +00:00
if (dirEntry->flags & HFS_ISDIR) {
fIsDir = true;
fType = fCreator = 0;
fDataLength = 0;
fRsrcLength = -1;
} else {
uint8_t* pType;
2007-03-27 17:47:10 +00:00
fIsDir = false;
pType = (uint8_t*) dirEntry->u.file.type;
2007-03-27 17:47:10 +00:00
fType =
pType[0] << 24 | pType[1] << 16 | pType[2] << 8 | pType[3];
pType = (uint8_t*) dirEntry->u.file.creator;
2007-03-27 17:47:10 +00:00
fCreator =
pType[0] << 24 | pType[1] << 16 | pType[2] << 8 | pType[3];
fDataLength = dirEntry->u.file.dsize;
fRsrcLength = dirEntry->u.file.rsize;
/*
* Resource fork must be at least 512 bytes for Finder, so if
* it has zero length then the file must not have one.
*/
2007-03-27 17:47:10 +00:00
if (fRsrcLength == 0)
fRsrcLength = -1;
}
/*
* Create/modified dates (we ignore the "last backup" date). The
* hfslib functions convert to time_t for us.
*/
fCreateWhen = dirEntry->crdate;
fModWhen = dirEntry->mddate;
2007-03-27 17:47:10 +00:00
}
/*
* Return "true" if "name" is valid for use as an HFS volume name.
*/
/*static*/ bool DiskFSHFS::IsValidVolumeName(const char* name)
2007-03-27 17:47:10 +00:00
{
2014-11-18 05:13:13 +00:00
if (name == NULL)
return false;
2007-03-27 17:47:10 +00:00
int len = strlen(name);
if (len < 1 || len > kMaxVolumeName)
return false;
2007-03-27 17:47:10 +00:00
while (*name != '\0') {
if (*name == A2FileHFS::kFssep)
return false;
name++;
}
2007-03-27 17:47:10 +00:00
return true;
2007-03-27 17:47:10 +00:00
}
/*
* Return "true" if "name" is valid for use as an HFS file name.
*/
/*static*/ bool DiskFSHFS::IsValidFileName(const char* name)
2007-03-27 17:47:10 +00:00
{
2014-11-18 05:13:13 +00:00
if (name == NULL)
return false;
2007-03-27 17:47:10 +00:00
int len = strlen(name);
if (len < 1 || len > A2FileHFS::kMaxFileName)
return false;
2007-03-27 17:47:10 +00:00
while (*name != '\0') {
if (*name == A2FileHFS::kFssep)
return false;
name++;
}
2007-03-27 17:47:10 +00:00
return true;
2007-03-27 17:47:10 +00:00
}
/*
* Format the current volume with HFS.
*/
DIError DiskFSHFS::Format(DiskImg* pDiskImg, const char* volName)
2007-03-27 17:47:10 +00:00
{
assert(strlen(volName) > 0 && strlen(volName) <= kMaxVolumeName);
2007-03-27 17:47:10 +00:00
if (!IsValidVolumeName(volName))
return kDIErrInvalidArg;
2007-03-27 17:47:10 +00:00
/* set fpImg so calls that rely on it will work; we un-set it later */
2014-11-18 05:13:13 +00:00
assert(fpImg == NULL);
SetDiskImg(pDiskImg);
2007-03-27 17:47:10 +00:00
/* need this for callback function */
fTotalBlocks = fpImg->GetNumBlocks();
2007-03-27 17:47:10 +00:00
// need HFS_OPT_2048 for CD-ROM?
if (hfs_callback_format(LibHFSCB, this, 0, volName) != 0) {
LOGI("hfs_callback_format failed (%s)", hfs_error);
return kDIErrGeneric;
}
2007-03-27 17:47:10 +00:00
// no need to flush; HFS volume is closed
2007-03-27 17:47:10 +00:00
2014-11-18 05:13:13 +00:00
SetDiskImg(NULL); // shouldn't really be set by us
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Normalize an HFS path. Invokes DoNormalizePath and handles the buffer
* management (if the normalized path doesn't fit in "*pNormalizedBufLen"
* bytes, we set "*pNormalizedBufLen to the required length).
*
* This is invoked from the generalized "add" function in CiderPress, which
* doesn't want to understand the ins and outs of pathnames.
*/
DIError DiskFSHFS::NormalizePath(const char* path, char fssep,
char* normalizedBuf, int* pNormalizedBufLen)
2007-03-27 17:47:10 +00:00
{
DIError dierr = kDIErrNone;
2014-11-18 05:13:13 +00:00
char* normalizedPath = NULL;
int len;
2007-03-27 17:47:10 +00:00
2014-11-18 05:13:13 +00:00
assert(pNormalizedBufLen != NULL);
assert(normalizedBuf != NULL || *pNormalizedBufLen == 0);
2007-03-27 17:47:10 +00:00
dierr = DoNormalizePath(path, fssep, &normalizedPath);
if (dierr != kDIErrNone)
goto bail;
2007-03-27 17:47:10 +00:00
2014-11-18 05:13:13 +00:00
assert(normalizedPath != NULL);
len = strlen(normalizedPath);
2014-11-18 05:13:13 +00:00
if (normalizedBuf == NULL || *pNormalizedBufLen <= len) {
/* too short */
dierr = kDIErrDataOverrun;
} else {
/* fits */
strcpy(normalizedBuf, normalizedPath);
}
2007-03-27 17:47:10 +00:00
*pNormalizedBufLen = len+1; // alloc room for the '\0'
2007-03-27 17:47:10 +00:00
bail:
delete[] normalizedPath;
return dierr;
2007-03-27 17:47:10 +00:00
}
/*
* Normalize an HFS path. This requires separating each path component
* out, making it HFS-compliant, and then putting it back in.
* The fssep could be anything, so we need to change it to kFssep.
*
* The caller must delete[] "*pNormalizedPath".
*/
DIError DiskFSHFS::DoNormalizePath(const char* path, char fssep,
char** pNormalizedPath)
2007-03-27 17:47:10 +00:00
{
DIError dierr = kDIErrNone;
2014-11-18 05:13:13 +00:00
char* workBuf = NULL;
char* partBuf = NULL;
char* outputBuf = NULL;
char* start;
char* end;
char* outPtr;
2014-11-18 05:13:13 +00:00
assert(path != NULL);
workBuf = new char[strlen(path)+1];
partBuf = new char[strlen(path)+1 +1]; // need +1 for prepending letter
outputBuf = new char[strlen(path) * 2];
2014-11-18 05:13:13 +00:00
if (workBuf == NULL || partBuf == NULL || outputBuf == NULL) {
dierr = kDIErrMalloc;
goto bail;
}
strcpy(workBuf, path);
outputBuf[0] = '\0';
outPtr = outputBuf;
start = workBuf;
while (*start != '\0') {
//char* origStart = start; // need for debug msg
int partIdx;
if (fssep == '\0') {
2014-11-18 05:13:13 +00:00
end = NULL;
} else {
end = strchr(start, fssep);
2014-11-18 05:13:13 +00:00
if (end != NULL)
*end = '\0';
}
partIdx = 0;
/*
* Copy, converting colons to underscores. We should strip out any
* illegal characters here, but there's not much in HFS that's
* considered illegal.
*/
while (*start != '\0') {
if (*start == A2FileHFS::kFssep)
partBuf[partIdx++] = '_';
else
partBuf[partIdx++] = *start;
start++;
}
/*
* Truncate at 31 chars, preserving anything that looks like a
* filename extension. "partIdx" represents the length of the
* string at this point. "partBuf" holds the string, which we
* want to null-terminate before proceeding.
*
* Try to keep the filename extension, if any.
*/
partBuf[partIdx] = '\0';
if (partIdx > A2FileHFS::kMaxFileName) {
const char* pDot = strrchr(partBuf, '.');
//int DEBUGDOTLEN = pDot - partBuf;
2014-11-18 05:13:13 +00:00
if (pDot != NULL && partIdx - (pDot-partBuf) <= kMaxExtensionLen) {
int dotLen = partIdx - (pDot-partBuf);
memmove(partBuf + (A2FileProDOS::kMaxFileName - dotLen),
pDot, dotLen); // don't use memcpy, move might overlap
}
partIdx = A2FileProDOS::kMaxFileName;
}
partBuf[partIdx] = '\0';
//LOGI(" HFS Converted component '%s' to '%s'",
// origStart, partBuf);
if (outPtr != outputBuf)
*outPtr++ = A2FileHFS::kFssep;
strcpy(outPtr, partBuf);
outPtr += partIdx;
/*
* Continue with next segment.
*/
2014-11-18 05:13:13 +00:00
if (end == NULL)
break;
start = end+1;
}
*outPtr = '\0';
LOGI(" HFS Converted path '%s' to '%s' (fssep='%c')",
path, outputBuf, fssep);
assert(*outputBuf != '\0');
*pNormalizedPath = outputBuf;
2014-11-18 05:13:13 +00:00
outputBuf = NULL;
2007-03-27 17:47:10 +00:00
bail:
delete[] workBuf;
delete[] partBuf;
delete[] outputBuf;
return dierr;
2007-03-27 17:47:10 +00:00
}
/*
* Compare two Macintosh filename strings.
*
* This requires some effort because the Macintosh Roman character set
* doesn't sort the same way that ASCII does. HFS is case-insensitive but
* case-preserving, so we need to deal with that too. The hfs_charorder
* table takes care of it.
*
* Returns <0, ==0, or >0 depending on whether sstr1 is lexically less than,
* equal to, or greater than sstr2.
*/
/*static*/ int DiskFSHFS::CompareMacFileNames(const char* sstr1, const char* sstr2)
2007-03-27 17:47:10 +00:00
{
const uint8_t* str1 = (const uint8_t*) sstr1;
const uint8_t* str2 = (const uint8_t*) sstr2;
int diff;
2007-03-27 17:47:10 +00:00
while (*str1 && *str2) {
diff = hfs_charorder[*str1] - hfs_charorder[*str2];
2007-03-27 17:47:10 +00:00
if (diff != 0)
return diff;
2007-03-27 17:47:10 +00:00
str1++;
str2++;
}
2007-03-27 17:47:10 +00:00
return *str1 - *str2;
2007-03-27 17:47:10 +00:00
}
/*
* Keep tweaking the filename until it no longer matches an existing file.
* The first time this is called we don't know if the name is unique or not,
* so we need to start by checking that.
*
* We have our choice between the DiskFS GetFileByName(), which traverses
* a linear list, and hfs_stat(), which uses more efficient data structures
* but may require disk reads. We use the DiskFS interface, on the assumption
* that someday we'll switch the linear list to a tree structure.
*/
DIError DiskFSHFS::MakeFileNameUnique(const char* pathName, char** pUniqueName)
2007-03-27 17:47:10 +00:00
{
A2File* pFile;
const int kMaxExtra = 3;
const int kMaxDigits = 999;
char* uniqueName;
char* fileName; // points inside uniqueName
2014-11-18 05:13:13 +00:00
assert(pathName != NULL);
assert(pathName[0] == A2FileHFS::kFssep);
/* see if it exists */
pFile = GetFileByName(pathName+1);
2014-11-18 05:13:13 +00:00
if (pFile == NULL) {
*pUniqueName = NULL;
return kDIErrNone;
}
/* make a copy we can chew on */
uniqueName = new char[strlen(pathName) + kMaxExtra +1];
strcpy(uniqueName, pathName);
fileName = strrchr(uniqueName, A2FileHFS::kFssep);
2014-11-18 05:13:13 +00:00
assert(fileName != NULL);
fileName++;
int nameLen = strlen(fileName);
int dotOffset=0, dotLen=0;
char dotBuf[kMaxExtensionLen+1];
/* ensure the result will be null-terminated */
memset(fileName + nameLen, 0, kMaxExtra+1);
/*
* If this has what looks like a filename extension, grab it. We want
* to preserve ".gif", ".c", etc., since the filetypes don't necessarily
* do everything we need.
*/
const char* cp = strrchr(fileName, '.');
2014-11-18 05:13:13 +00:00
if (cp != NULL) {
int tmpOffset = cp - fileName;
if (tmpOffset > 0 && nameLen - tmpOffset <= kMaxExtensionLen) {
LOGI(" HFS (keeping extension '%s')", cp);
assert(strlen(cp) <= kMaxExtensionLen);
strcpy(dotBuf, cp);
dotOffset = tmpOffset;
dotLen = nameLen - dotOffset;
}
}
int digits = 0;
int digitLen;
int copyOffset;
char digitBuf[kMaxExtra+1];
do {
if (digits == kMaxDigits)
return kDIErrFileExists;
digits++;
/* not the most efficient way to do this, but it'll do */
sprintf(digitBuf, "%d", digits);
digitLen = strlen(digitBuf);
if (nameLen + digitLen > A2FileHFS::kMaxFileName)
copyOffset = A2FileHFS::kMaxFileName - dotLen - digitLen;
else
copyOffset = nameLen - dotLen;
memcpy(fileName + copyOffset, digitBuf, digitLen);
if (dotLen != 0)
memcpy(fileName + copyOffset + digitLen, dotBuf, dotLen);
2014-11-18 05:13:13 +00:00
} while (GetFileByName(uniqueName+1) != NULL);
LOGI(" HFS converted to unique name: %s", uniqueName);
*pUniqueName = uniqueName;
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Create a new file or directory. Automatically creates the base path
* if necessary.
*
* NOTE: much of this was cloned out of the ProDOS code. We probably want
* a stronger set of utility functions in the parent class now that we have
* more than one hierarchical file system.
*/
DIError DiskFSHFS::CreateFile(const CreateParms* pParms, A2File** ppNewFile)
2007-03-27 17:47:10 +00:00
{
DIError dierr = kDIErrNone;
char typeStr[5], creatorStr[5];
2014-11-18 05:13:13 +00:00
char* normalizedPath = NULL;
char* basePath = NULL;
char* fileName = NULL;
char* fullPath = NULL;
A2FileHFS* pSubdir = NULL;
A2FileHFS* pNewFile = NULL;
hfsfile* pHfsFile = NULL;
const bool createUnique = (GetParameter(kParm_CreateUnique) != 0);
2014-11-18 05:13:13 +00:00
assert(fHfsVol != NULL);
if (fpImg->GetReadOnly())
return kDIErrAccessDenied;
2014-11-18 05:13:13 +00:00
assert(pParms != NULL);
assert(pParms->pathName != NULL);
assert(pParms->storageType == A2FileProDOS::kStorageSeedling ||
pParms->storageType == A2FileProDOS::kStorageExtended ||
pParms->storageType == A2FileProDOS::kStorageDirectory);
// kStorageVolumeDirHeader not allowed -- that's created by Format
LOGI(" HFS ---v--- CreateFile '%s'", pParms->pathName);
/*
* Normalize the pathname so that all components are HFS-safe
* and separated by ':'.
*
* This must not "sanitize" the path. We need to be working with the
* original characters, not the sanitized-for-display versions.
*/
2014-11-18 05:13:13 +00:00
assert(pParms->pathName != NULL);
dierr = DoNormalizePath(pParms->pathName, pParms->fssep,
&normalizedPath);
if (dierr != kDIErrNone)
goto bail;
2014-11-18 05:13:13 +00:00
assert(normalizedPath != NULL);
/*
* The normalized path lacks a leading ':', and might need to
* have some digits added to make the name unique.
*/
fullPath = new char[strlen(normalizedPath)+2];
fullPath[0] = A2FileHFS::kFssep;
strcpy(fullPath+1, normalizedPath);
delete[] normalizedPath;
2014-11-18 05:13:13 +00:00
normalizedPath = NULL;
/*
* Make the name unique within the current directory. This requires
* appending digits until the name doesn't match any others.
*/
if (createUnique &&
pParms->storageType != A2FileProDOS::kStorageDirectory)
{
char* uniquePath;
dierr = MakeFileNameUnique(fullPath, &uniquePath);
if (dierr != kDIErrNone)
goto bail;
2014-11-18 05:13:13 +00:00
if (uniquePath != NULL) {
delete[] fullPath;
fullPath = uniquePath;
}
} else {
/* can't make unique; check to see if it already exists */
hfsdirent dirEnt;
if (hfs_stat(fHfsVol, fullPath, &dirEnt) == 0) {
if (pParms->storageType == A2FileProDOS::kStorageDirectory)
dierr = kDIErrDirectoryExists;
else
dierr = kDIErrFileExists;
goto bail;
}
}
/*
* Split the base path and filename apart.
*/
char* cp;
cp = strrchr(fullPath, A2FileHFS::kFssep);
2014-11-18 05:13:13 +00:00
assert(cp != NULL);
if (cp == fullPath) {
2014-11-18 05:13:13 +00:00
assert(basePath == NULL);
fileName = new char[strlen(fullPath) +1];
strcpy(fileName, fullPath);
} else {
int dirNameLen = cp - fullPath;
fileName = new char[strlen(cp+1) +1];
strcpy(fileName, cp+1);
basePath = new char[dirNameLen+1];
strncpy(basePath, fullPath, dirNameLen);
basePath[dirNameLen] = '\0';
}
LOGI("SPLIT: '%s' '%s'", basePath, fileName);
2014-11-18 05:13:13 +00:00
assert(fileName != NULL);
/*
* Open the base path. If it doesn't exist, create it recursively.
*/
2014-11-18 05:13:13 +00:00
if (basePath != NULL) {
LOGI(" HFS Creating '%s' in '%s'", fileName, basePath);
/*
* Open the named subdir, creating it if it doesn't exist. We need
* to check basePath+1 because we're comparing against what's in our
* linear file list, and that doesn't include the leading ':'.
*/
pSubdir = (A2FileHFS*)GetFileByName(basePath+1, CompareMacFileNames);
2014-11-18 05:13:13 +00:00
if (pSubdir == NULL) {
LOGI(" HFS Creating subdir '%s'", basePath);
A2File* pNewSub;
CreateParms newDirParms;
newDirParms.pathName = basePath;
newDirParms.fssep = A2FileHFS::kFssep;
newDirParms.storageType = A2FileProDOS::kStorageDirectory;
newDirParms.fileType = 0;
newDirParms.auxType = 0;
newDirParms.access = 0;
2014-11-18 05:13:13 +00:00
newDirParms.createWhen = newDirParms.modWhen = time(NULL);
dierr = this->CreateFile(&newDirParms, &pNewSub);
if (dierr != kDIErrNone)
goto bail;
2014-11-18 05:13:13 +00:00
assert(pNewSub != NULL);
pSubdir = (A2FileHFS*) pNewSub;
}
/*
* And now the annoying part. We need to reconstruct basePath out
* of the filenames actually present, rather than relying on the
* argument passed in. That's because HFS is case-insensitive but
* case-preserving. It's not crucial for our inner workings, but the
* linear file list in the DiskFS should have accurate strings.
* (It'll work just fine, but the display might show the wrong values
* for parent directories until they reload the disk.)
*
* On the bright side, we know exactly how long the string needs
* to be, so we can just stomp on it in place. Assuming, of course,
* that the filename created matches up with what the filename
* normalizer came up with, which we can guarantee since (a) everybody
* uses the same normalizer and (b) the "uniqueify" stuff doesn't
* kick in for subdirs because we wouldn't be creating a new subdir
* if it didn't already exist.
*
* This is essentially the same as RegeneratePathName(), but that's
* meant for a situation where the filename already exists.
*/
A2FileHFS* pBaseDir = pSubdir;
int basePathLen = strlen(basePath);
while (!pBaseDir->IsVolumeDirectory()) {
const char* fixedName = pBaseDir->GetFileName();
int fixedLen = strlen(fixedName);
if (fixedLen > basePathLen) {
assert(false);
break;
}
assert(basePathLen == fixedLen ||
*(basePath + (basePathLen-fixedLen-1)) == kDIFssep);
memcpy(basePath + (basePathLen-fixedLen), fixedName, fixedLen);
basePathLen -= fixedLen+1;
pBaseDir = (A2FileHFS*) pBaseDir->GetParent();
2014-11-18 05:13:13 +00:00
assert(pBaseDir != NULL);
}
// check the math; we should be left with the leading ':'
if (pSubdir->IsVolumeDirectory())
assert(basePathLen == 1);
else
assert(basePathLen == 0);
} else {
/* open the volume directory */
LOGI(" HFS Creating '%s' in volume dir", fileName);
/* volume dir must be first in the list */
2014-11-18 05:13:13 +00:00
pSubdir = (A2FileHFS*) GetNextFile(NULL);
assert(pSubdir != NULL);
assert(pSubdir->IsVolumeDirectory());
}
2014-11-18 05:13:13 +00:00
if (pSubdir == NULL) {
LOGI(" HFS Unable to open subdir '%s'", basePath);
dierr = kDIErrFileNotFound;
goto bail;
}
/*
* Figure out file type.
*/
A2FileHFS::ConvertTypeToHFS(pParms->fileType, pParms->auxType,
typeStr, creatorStr);
/*
* Create the file or directory. Populate "dirEnt" with the details.
*/
hfsdirent dirEnt;
if (pParms->storageType == A2FileProDOS::kStorageDirectory) {
/* create the directory */
if (hfs_mkdir(fHfsVol, fullPath) != 0) {
LOGI(" HFS mkdir '%s' failed: %s", fullPath, hfs_error);
dierr = kDIErrGeneric;
goto bail;
}
if (hfs_stat(fHfsVol, fullPath, &dirEnt) != 0) {
LOGI(" HFS stat on new dir failed: %s", hfs_error);
dierr = kDIErrGeneric;
goto bail;
}
/* create date *might* be useful, but probably not worth adjusting */
} else {
/* create, and open, the file */
pHfsFile = hfs_create(fHfsVol, fullPath, typeStr, creatorStr);
2014-11-18 05:13:13 +00:00
if (pHfsFile == NULL) {
LOGI(" HFS create failed: %s", hfs_error);
dierr = kDIErrGeneric;
goto bail;
}
if (hfs_fstat(pHfsFile, &dirEnt) != 0) {
LOGI(" HFS fstat on new file failed: %s", hfs_error);
dierr = kDIErrGeneric;
goto bail;
}
/* set the attributes according to pParms, and update the file */
dirEnt.crdate = pParms->createWhen;
dirEnt.mddate = pParms->modWhen;
if (pParms->access & A2FileProDOS::kAccessInvisible)
dirEnt.fdflags |= HFS_FNDR_ISINVISIBLE;
else
dirEnt.fdflags &= ~HFS_FNDR_ISINVISIBLE;
if ((pParms->access & ~A2FileProDOS::kAccessInvisible) == kFileAccessLocked)
dirEnt.flags |= HFS_ISLOCKED;
else
dirEnt.flags &= ~HFS_ISLOCKED;
(void) hfs_fsetattr(pHfsFile, &dirEnt);
(void) hfs_close(pHfsFile);
2014-11-18 05:13:13 +00:00
pHfsFile = NULL;
}
/*
* Success!
*
* Create a new entry and set the structure fields.
*/
pNewFile = new A2FileHFS(this);
pNewFile->InitEntry(&dirEnt);
2014-11-18 05:13:13 +00:00
pNewFile->SetPathName(basePath == NULL ? "" : basePath, pNewFile->fFileName);
pNewFile->SetParent(pSubdir);
/*
* Because we're hierarchical, and we guarantee that the contents of
* subdirectories are grouped together, we must insert the file into an
* appropriate place in the list rather than just throwing it onto the
* end.
*
* The proper location for the new file in the linear list is in sorted
* order with the files in the current directory. We have to be careful
* here because libhfs is going to use Macintosh Roman sort ordering,
* which may be different from ASCII ordering. Worst case: we end up
* putting it in the wrong place and it jumps around when the disk image
* is reopened.
*
* All files in a subdir appear in the list after that subdir, but there
* might be intervening entries from deeper directories. So we have to
* chase through some or all of the file list to find the right place.
* Not great, but we don't have enough files or do adds often enough to
* make this worth optimizing.
*/
A2File* pLastSubdirFile;
A2File* pPrevFile;
A2File* pNextFile;
pPrevFile = pLastSubdirFile = pSubdir;
pNextFile = GetNextFile(pPrevFile);
2014-11-18 05:13:13 +00:00
while (pNextFile != NULL) {
if (pNextFile->GetParent() == pNewFile->GetParent()) {
/* in same subdir, compare names */
if (CompareMacFileNames(pNextFile->GetPathName(),
pNewFile->GetPathName()) > 0)
{
/* passed it; insert new after previous file */
pLastSubdirFile = pPrevFile;
LOGI(" HFS Found '%s' > cur(%s)", pNextFile->GetPathName(),
pNewFile->GetPathName());
break;
}
/* still too early; save in case it's last one in dir */
pLastSubdirFile = pNextFile;
}
pPrevFile = pNextFile;
pNextFile = GetNextFile(pNextFile);
}
/* insert us after last file we saw that was part of the same subdir */
LOGI(" HFS inserting '%s' after '%s'", pNewFile->GetPathName(),
pLastSubdirFile->GetPathName());
InsertFileInList(pNewFile, pLastSubdirFile);
//LOGI("LIST NOW:");
//DumpFileList();
*ppNewFile = pNewFile;
2014-11-18 05:13:13 +00:00
pNewFile = NULL;
2007-03-27 17:47:10 +00:00
bail:
delete pNewFile;
delete[] normalizedPath;
delete[] basePath;
delete[] fileName;
delete[] fullPath;
hfs_flush(fHfsVol);
LOGI(" HFS ---^--- CreateFile '%s' DONE", pParms->pathName);
return dierr;
2007-03-27 17:47:10 +00:00
}
/*
* Delete the named file.
*
* We need to use a different call for file vs. directory.
*/
DIError DiskFSHFS::DeleteFile(A2File* pGenericFile)
2007-03-27 17:47:10 +00:00
{
DIError dierr = kDIErrNone;
2014-11-18 05:13:13 +00:00
char* pathName = NULL;
if (fpImg->GetReadOnly())
return kDIErrAccessDenied;
if (!fDiskIsGood)
return kDIErrBadDiskImage;
if (pGenericFile->IsFileOpen())
return kDIErrFileOpen;
A2FileHFS* pFile = (A2FileHFS*) pGenericFile;
pathName = pFile->GetLibHFSPathName();
LOGI(" Deleting '%s'", pathName);
if (pFile->IsDirectory()) {
if (hfs_rmdir(fHfsVol, pathName) != 0) {
LOGI(" HFS rmdir failed '%s': '%s'", pathName, hfs_error);
dierr = kDIErrGeneric;
goto bail;
}
} else {
if (hfs_delete(fHfsVol, pathName) != 0) {
LOGI(" HFS delete failed '%s': '%s'", pathName, hfs_error);
dierr = kDIErrGeneric;
goto bail;
}
}
/*
* Remove the A2File* from the list.
*/
DeleteFileFromList(pFile);
2007-03-27 17:47:10 +00:00
bail:
hfs_flush(fHfsVol);
delete[] pathName;
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Rename a file.
*
* Pass in a pointer to the file and a string with the new filename (just
* the filename, not a pathname -- this function doesn't move files
* between directories). The new name must already be normalized.
*
* Renaming the magic volume directory "file" is not allowed.
*
* We don't try to keep AppleWorks aux type flags consistent (they're used
* to determine which characters are lower case on ProDOS disks). They'll
* get fixed up when we copy them to a ProDOS disk, which is the only way
* 8-bit AppleWorks can get at them.
*/
DIError DiskFSHFS::RenameFile(A2File* pGenericFile, const char* newName)
2007-03-27 17:47:10 +00:00
{
DIError dierr = kDIErrNone;
A2FileHFS* pFile = (A2FileHFS*) pGenericFile;
2014-11-18 05:13:13 +00:00
char* colonOldName = NULL;
char* colonNewName = NULL;
2014-11-18 05:13:13 +00:00
if (pFile == NULL || newName == NULL)
return kDIErrInvalidArg;
if (!IsValidFileName(newName))
return kDIErrInvalidArg;
if (pFile->IsVolumeDirectory())
return kDIErrInvalidArg;
if (fpImg->GetReadOnly())
return kDIErrAccessDenied;
if (!fDiskIsGood)
return kDIErrBadDiskImage;
char* lastColon;
colonOldName = pFile->GetLibHFSPathName(); // adds ':' to start of string
lastColon = strrchr(colonOldName, A2FileHFS::kFssep);
2014-11-18 05:13:13 +00:00
assert(lastColon != NULL);
if (lastColon == colonOldName) {
/* in root dir */
colonNewName = new char[1 + strlen(newName) +1];
colonNewName[0] = A2FileHFS::kFssep;
strcpy(colonNewName+1, newName);
} else {
/* prepend subdir */
int len = lastColon - colonOldName +1; // e.g. ":path1:path2:"
colonNewName = new char[len + strlen(newName) +1];
strncpy(colonNewName, colonOldName, len);
strcpy(colonNewName+len, newName);
}
LOGI(" HFS renaming '%s' to '%s'", colonOldName, colonNewName);
if (hfs_rename(fHfsVol, colonOldName, colonNewName) != 0) {
LOGI(" HFS rename('%s','%s') failed: %s",
colonOldName, colonNewName, hfs_error);
dierr = kDIErrGeneric;
goto bail;
}
/*
* Success! Update the file name.
*/
strcpy(pFile->fFileName, newName);
/*
* Now the fun part. If we simply renamed a file, we can just update the
* one entry. If we renamed a directory, life gets interesting because
* we store the full pathname in every A2FileHFS entry. (It's an
* efficiency win most of the time, but it's really annoying here.)
*
* HFS makes this especially unpleasant because it keeps the files
* arranged in sorted order. If we change a file's name, we may have to
* move it to a new position in the linear file list. If we don't, the
* list no longer reflects the order in which the files actually appear
* on the disk, and they'll shift around when we reload.
*
* There are two approaches: re-sort the list (awkward, since it's stored
* in a linked list -- we'd probably want to sort tags in a parallel
* structure), or find the affected block of files, find the new start
* position, and shift the entire range in one shot.
*
* This doesn't seem like something that anybody but me will ever care
* about, so I'm going to skip it for now.
*/
A2File* pCur;
if (pFile->IsDirectory()) {
/* do all files that come after us */
pCur = pFile;
2014-11-18 05:13:13 +00:00
while (pCur != NULL) {
RegeneratePathName((A2FileHFS*) pCur);
pCur = GetNextFile(pCur);
}
} else {
RegeneratePathName(pFile);
}
2007-03-27 17:47:10 +00:00
bail:
delete[] colonOldName;
delete[] colonNewName;
hfs_flush(fHfsVol);
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Regenerate fPathName for the specified file.
*
* Has no effect on the magic volume dir entry.
*
* This could be implemented more efficiently, but it's only used when
* renaming files, so there's not much point.
*
* [This was lifted straight out of the ProDOS sources. It should probably
* be moved into generic DiskFS.]
*/
DIError DiskFSHFS::RegeneratePathName(A2FileHFS* pFile)
2007-03-27 17:47:10 +00:00
{
A2FileHFS* pParent;
2014-11-18 05:13:13 +00:00
char* buf = NULL;
int len;
/* nothing to do here */
if (pFile->IsVolumeDirectory())
return kDIErrNone;
/* compute the length of the path name */
len = strlen(pFile->GetFileName());
pParent = (A2FileHFS*) pFile->GetParent();
while (!pParent->IsVolumeDirectory()) {
len++; // leave space for the ':'
len += strlen(pParent->GetFileName());
pParent = (A2FileHFS*) pParent->GetParent();
}
buf = new char[len+1];
2014-11-18 05:13:13 +00:00
if (buf == NULL)
return kDIErrMalloc;
/* generate the new path name */
int partLen;
partLen = strlen(pFile->GetFileName());
strcpy(buf + len - partLen, pFile->GetFileName());
len -= partLen;
pParent = (A2FileHFS*) pFile->GetParent();
while (!pParent->IsVolumeDirectory()) {
assert(len > 0);
buf[--len] = A2FileHFS::kFssep;
partLen = strlen(pParent->GetFileName());
strncpy(buf + len - partLen, pParent->GetFileName(), partLen);
len -= partLen;
assert(len >= 0);
pParent = (A2FileHFS*) pParent->GetParent();
}
LOGI("Replacing '%s' with '%s'", pFile->GetPathName(), buf);
pFile->SetPathName("", buf);
delete[] buf;
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Change the HFS volume name.
*
* This uses the same libhfs interface that we use for renaming files. The
* Mac convention is to *not* start the volume name with a colon. In fact,
* the libhfs convention is to *end* the volume names with a colon.
*/
DIError DiskFSHFS::RenameVolume(const char* newName)
2007-03-27 17:47:10 +00:00
{
DIError dierr = kDIErrNone;
A2FileHFS* pFile;
2014-11-18 05:13:13 +00:00
char* oldNameColon = NULL;
char* newNameColon = NULL;
if (!IsValidVolumeName(newName))
return kDIErrInvalidArg;
if (fpImg->GetReadOnly())
return kDIErrAccessDenied;
/* get file list entry for volume name */
2014-11-18 05:13:13 +00:00
pFile = (A2FileHFS*) GetNextFile(NULL);
assert(strcmp(pFile->GetFileName(), fVolumeName) == 0);
oldNameColon = new char[strlen(fVolumeName)+2];
strcpy(oldNameColon, fVolumeName);
strcat(oldNameColon, ":");
newNameColon = new char[strlen(newName)+2];
strcpy(newNameColon, newName);
strcat(newNameColon, ":");
if (hfs_rename(fHfsVol, oldNameColon, newNameColon) != 0) {
LOGI(" HFS rename '%s' -> '%s' failed: %s",
oldNameColon, newNameColon, hfs_error);
dierr = kDIErrGeneric;
goto bail;
}
/* update stuff */
strcpy(fVolumeName, newName);
SetVolumeID();
strcpy(pFile->fFileName, newName);
pFile->SetPathName("", newName);
2007-03-27 17:47:10 +00:00
bail:
delete[] oldNameColon;
delete[] newNameColon;
hfs_flush(fHfsVol);
return dierr;
2007-03-27 17:47:10 +00:00
}
/*
* Set file attributes.
*/
DIError DiskFSHFS::SetFileInfo(A2File* pGenericFile, long fileType, long auxType,
long accessFlags)
2007-03-27 17:47:10 +00:00
{
DIError dierr = kDIErrNone;
A2FileHFS* pFile = (A2FileHFS*) pGenericFile;
hfsdirent dirEnt;
char* colonPath;
if (fpImg->GetReadOnly())
return kDIErrAccessDenied;
2014-11-18 05:13:13 +00:00
if (pFile == NULL)
return kDIErrInvalidArg;
if (pFile->IsDirectory() || pFile->IsVolumeDirectory())
return kDIErrNone; // impossible; just ignore it
colonPath = pFile->GetLibHFSPathName();
if (hfs_stat(fHfsVol, colonPath, &dirEnt) != 0) {
LOGI(" HFS unable to stat '%s': %s", colonPath, hfs_error);
dierr = kDIErrGeneric;
goto bail;
}
A2FileHFS::ConvertTypeToHFS(fileType, auxType,
dirEnt.u.file.type, dirEnt.u.file.creator);
if (accessFlags & A2FileProDOS::kAccessInvisible)
dirEnt.fdflags |= HFS_FNDR_ISINVISIBLE;
else
dirEnt.fdflags &= ~HFS_FNDR_ISINVISIBLE;
if ((accessFlags & ~A2FileProDOS::kAccessInvisible) == kFileAccessLocked)
dirEnt.flags |= HFS_ISLOCKED;
else
dirEnt.flags &= ~HFS_ISLOCKED;
LOGD(" HFS setting '%s' to fdflags=0x%04x flags=0x%04x",
colonPath, dirEnt.fdflags, dirEnt.flags);
LOGD(" type=0x%08lx creator=0x%08lx", fileType, auxType);
if (hfs_setattr(fHfsVol, colonPath, &dirEnt) != 0) {
LOGW(" HFS setattr '%s' failed: %s", colonPath, hfs_error);
dierr = kDIErrGeneric;
goto bail;
}
/* update our local copy */
pFile->fType = fileType;
pFile->fCreator = auxType;
pFile->fAccess = accessFlags; // should actually base them on HFS vals
2007-03-27 17:47:10 +00:00
bail:
delete[] colonPath;
hfs_flush(fHfsVol);
return dierr;
2007-03-27 17:47:10 +00:00
}
#endif // !EXCISE_GPL_CODE
/*
* ===========================================================================
* A2FileHFS
2007-03-27 17:47:10 +00:00
* ===========================================================================
*/
/*
* Dump the contents of the A2File structure.
*/
void A2FileHFS::Dump(void) const
2007-03-27 17:47:10 +00:00
{
LOGI("A2FileHFS '%s'", fFileName);
2007-03-27 17:47:10 +00:00
}
/* convert hex to decimal */
inline int FromHex(char hexVal)
{
if (hexVal >= '0' && hexVal <= '9')
return hexVal - '0';
else if (hexVal >= 'a' && hexVal <= 'f')
return hexVal -'a' + 10;
else if (hexVal >= 'A' && hexVal <= 'F')
return hexVal - 'A' + 10;
else
return -1;
2007-03-27 17:47:10 +00:00
}
/*
* If this has a ProDOS filetype, convert it.
*
* This stuff is defined in Technical Note PT515, "Apple File Exchange Q&As".
* In theory we should convert type=BINA and type=TEXT regardless of the
* creator, but since those just go to generic text/binary types I don't
* think we need to handle it here (and I'm more comfortable leaving them
* with their Macintosh creators).
*
* In some respects, converting to ProDOS types is a bad idea, because we
* don't have a 1:1 mapping. If we copy a pdos/p\0\0\0 file we will store it
* as pdos/BINA instead. In practice, for the Apple II world they are
* equivalent, and CiderPress really doesn't need the "raw" file type. If
* it becomes annoying, we can add a DiskFSParameter to control it.
*/
long A2FileHFS::GetFileType(void) const
{
if (fCreator != kPdosType)
return fType;
if ((fType & 0xffff) == 0x2020) {
// 'XY ', where XY are hex digits for ProDOS file type
int digit1, digit2;
digit1 = FromHex((char) (fType >> 24));
digit2 = FromHex((char) (fType >> 16));
if (digit1 < 0 || digit2 < 0) {
LOGI(" Unexpected: pdos + %08lx", fType);
return 0x00;
}
return digit1 << 4 | digit2;
2007-03-27 17:47:10 +00:00
}
uint8_t flag = (uint8_t)(fType >> 24);
2007-03-27 17:47:10 +00:00
if (flag == 0x70) { // 'p'
/* type and aux embedded within */
return (fType >> 16) & 0xff;
} else {
/* type stored as a string */
if (fType == 0x42494e41) // 'BINA'
return 0x00; // NON
else if (fType == 0x54455854) // 'TEXT'
return 0x04;
else if (fType == 0x50535953) // 'PSYS'
return 0xff;
else if (fType == 0x50533136) // 'PS16'
return 0xb3;
else
return 0x00;
}
}
2007-03-27 17:47:10 +00:00
/*
* If this has a ProDOS aux type, convert it.
*/
long A2FileHFS::GetAuxType(void) const
{
if (fCreator != kPdosType)
return fCreator;
uint8_t flag = (uint8_t)(fType >> 24);
2007-03-27 17:47:10 +00:00
if (flag == 0x70) { // 'p'
/* type and aux embedded within */
return fType & 0xffff;
} else {
return 0x0000;
}
}
/*
* Set the full pathname to a combination of the base path and the
* current file's name.
*
2014-11-18 05:13:13 +00:00
* If we're in the volume directory, pass in "" for the base path (not NULL).
2007-03-27 17:47:10 +00:00
*/
void A2FileHFS::SetPathName(const char* basePath, const char* fileName)
2007-03-27 17:47:10 +00:00
{
2014-11-18 05:13:13 +00:00
assert(basePath != NULL && fileName != NULL);
if (fPathName != NULL)
delete[] fPathName;
2007-03-27 17:47:10 +00:00
// strip leading ':' (but treat ":" specially for volume dir entry)
if (basePath[0] == ':' && basePath[1] != '\0')
basePath++;
int baseLen = strlen(basePath);
fPathName = new char[baseLen + 1 + strlen(fileName)+1];
strcpy(fPathName, basePath);
if (baseLen != 0 &&
!(baseLen == 1 && basePath[0] == ':'))
{
*(fPathName + baseLen) = kFssep;
baseLen++;
}
strcpy(fPathName + baseLen, fileName);
2007-03-27 17:47:10 +00:00
}
#ifndef EXCISE_GPL_CODE
/*
* Return a copy of the pathname that libhfs will like.
*
* The caller must delete[] the return value.
*/
char* A2FileHFS::GetLibHFSPathName(void) const
2007-03-27 17:47:10 +00:00
{
char* nameBuf;
2007-03-27 17:47:10 +00:00
nameBuf = new char[strlen(fPathName)+2];
nameBuf[0] = kFssep;
strcpy(nameBuf+1, fPathName);
2007-03-27 17:47:10 +00:00
return nameBuf;
2007-03-27 17:47:10 +00:00
}
/*
* Convert numeric file/aux type to HFS strings. "pType" and "pCreator" must
* be able to hold 5 bytes each (4-byte type + nul).
*
* Follows the PT515 recommendations, mostly. The "PSYS" and "PS16"
* conversions discard the file's aux type and therefore are unsuitable,
* and the conversion of SRC throws away its identity.
*/
/*static*/ void A2FileHFS::ConvertTypeToHFS(long fileType, long auxType,
char* pType, char* pCreator)
2007-03-27 17:47:10 +00:00
{
if (fileType == 0x00 && auxType == 0x0000) {
strcpy(pCreator, "pdos");
strcpy(pType, "BINA");
} else if (fileType == 0x04 && auxType == 0x0000) {
strcpy(pCreator, "pdos");
strcpy(pType, "TEXT");
} else if (fileType >= 0 && fileType <= 0xff &&
auxType >= 0 && auxType <= 0xffff)
{
pType[0] = 'p';
pType[1] = (uint8_t) fileType;
pType[2] = (uint8_t) (auxType >> 8);
pType[3] = (uint8_t) auxType;
pType[4] = '\0';
pCreator[0] = 'p';
pCreator[1] = 'd';
pCreator[2] = 'o';
pCreator[3] = 's';
pCreator[4] = '\0';
} else {
pType[0] = (uint8_t)(fileType >> 24);
pType[1] = (uint8_t)(fileType >> 16);
pType[2] = (uint8_t)(fileType >> 8);
pType[3] = (uint8_t) fileType;
pType[4] = '\0';
pCreator[0] = (uint8_t)(auxType >> 24);
pCreator[1] = (uint8_t)(auxType >> 16);
pCreator[2] = (uint8_t)(auxType >> 8);
pCreator[3] = (uint8_t) auxType;
pCreator[4] = '\0';
}
2007-03-27 17:47:10 +00:00
}
/*
* Open a file through libhfs.
*
* libhfs wants filenames to begin with ':' unless they start with the
* name of the volume. This is the opposite of the convention followed
* by the rest of CiderPress (and most of the civilized world), so instead
* of storing the pathname that way we just tack it on here.
*/
DIError A2FileHFS::Open(A2FileDescr** ppOpenFile, bool readOnly,
bool rsrcFork /*=false*/)
2007-03-27 17:47:10 +00:00
{
DIError dierr = kDIErrNone;
2014-11-18 05:13:13 +00:00
A2FDHFS* pOpenFile = NULL;
hfsfile* pHfsFile;
2014-11-18 05:13:13 +00:00
char* nameBuf = NULL;
2014-11-18 05:13:13 +00:00
if (fpOpenFile != NULL)
return kDIErrAlreadyOpen;
//if (rsrcFork && fRsrcLength < 0)
// return kDIErrForkNotFound;
nameBuf = GetLibHFSPathName();
DiskFSHFS* pDiskFS = (DiskFSHFS*) GetDiskFS();
pHfsFile = hfs_open(pDiskFS->GetHfsVol(), nameBuf);
if (pHfsFile == NULL) {
LOGI(" HFS hfs_open(%s) failed: %s", nameBuf, hfs_error);
dierr = kDIErrGeneric; // better value might be in errno
goto bail;
}
hfs_setfork(pHfsFile, rsrcFork ? 1 : 0);
2007-03-27 17:47:10 +00:00
pOpenFile = new A2FDHFS(this, pHfsFile);
2007-03-27 17:47:10 +00:00
fpOpenFile = pOpenFile;
*ppOpenFile = pOpenFile;
2007-03-27 17:47:10 +00:00
bail:
delete[] nameBuf;
return dierr;
2007-03-27 17:47:10 +00:00
}
/*
* ===========================================================================
* A2FDHFS
2007-03-27 17:47:10 +00:00
* ===========================================================================
*/
/*
* Read a chunk of data from the fake file.
*/
DIError A2FDHFS::Read(void* buf, size_t len, size_t* pActual)
2007-03-27 17:47:10 +00:00
{
long result;
LOGI(" HFS reading %d bytes from '%s' (offset=%ld)",
len, fpFile->GetPathName(), hfs_seek(fHfsFile, 0, HFS_SEEK_CUR));
//A2FileHFS* pFile = (A2FileHFS*) fpFile;
result = hfs_read(fHfsFile, buf, len);
if (result < 0)
return kDIErrReadFailed;
2014-11-18 05:13:13 +00:00
if (pActual != NULL) {
*pActual = (size_t) result;
} else if (result != (long) len) {
// short read, can't report it, return error
return kDIErrDataUnderrun;
}
/*
* To do this right we need to break the hfs_read() into smaller
* pieces. However, it only really affects us for files that are
* getting reformatted, because that's the only time we grab the
* entire thing in one big piece.
*/
long offset = hfs_seek(fHfsFile, 0, HFS_SEEK_CUR);
if (!UpdateProgress(offset)) {
return kDIErrCancelled;
}
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Write data at the current offset.
*
* (In the current implementation, the entire file is always written in
* one piece. This function does work correctly with multiple smaller
* pieces though, because it lets libhfs do all the work.)
*/
DIError A2FDHFS::Write(const void* buf, size_t len, size_t* pActual)
2007-03-27 17:47:10 +00:00
{
long result;
2007-03-27 17:47:10 +00:00
LOGI(" HFS writing %d bytes to '%s' (offset=%ld)",
len, fpFile->GetPathName(), hfs_seek(fHfsFile, 0, HFS_SEEK_CUR));
2007-03-27 17:47:10 +00:00
fModified = true; // assume something gets changed
2007-03-27 17:47:10 +00:00
//A2FileHFS* pFile = (A2FileHFS*) fpFile;
2007-03-27 17:47:10 +00:00
result = hfs_write(fHfsFile, buf, len);
if (result < 0)
return kDIErrWriteFailed;
2007-03-27 17:47:10 +00:00
2014-11-18 05:13:13 +00:00
if (pActual != NULL) {
*pActual = (size_t) result;
} else if (result != (long) len) {
// short write, can't report it, return error
return kDIErrDataUnderrun;
}
2007-03-27 17:47:10 +00:00
/* to make this work right, we need to break hfs_write into pieces */
long offset = hfs_seek(fHfsFile, 0, HFS_SEEK_CUR);
if (!UpdateProgress(offset)) {
return kDIErrCancelled;
}
2007-03-27 17:47:10 +00:00
/*
* We don't hfs_flush here, because we don't expect the application to
* hold the file open, and we flush in Close().
*/
2007-03-27 17:47:10 +00:00
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Seek to a new offset.
*/
DIError A2FDHFS::Seek(di_off_t offset, DIWhence whence)
2007-03-27 17:47:10 +00:00
{
int hfsWhence;
unsigned long result;
switch (whence) {
case kSeekSet: hfsWhence = HFS_SEEK_SET; break;
case kSeekEnd: hfsWhence = HFS_SEEK_END; break;
case kSeekCur: hfsWhence = HFS_SEEK_CUR; break;
default:
assert(false);
return kDIErrInvalidArg;
}
result = hfs_seek(fHfsFile, (long) offset, hfsWhence);
if (result == (unsigned long) -1) {
DebugBreak();
return kDIErrGeneric;
}
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Return current offset.
*/
di_off_t A2FDHFS::Tell(void)
2007-03-27 17:47:10 +00:00
{
di_off_t offset;
2007-03-27 17:47:10 +00:00
/* get current position without moving pointer */
offset = hfs_seek(fHfsFile, 0, HFS_SEEK_CUR);
return offset;
2007-03-27 17:47:10 +00:00
}
/*
* Release file state, and tell our parent to destroy us.
*/
DIError A2FDHFS::Close(void)
2007-03-27 17:47:10 +00:00
{
hfsdirent dirEnt;
/*
* If the file was written to, update our info.
*/
if (fModified) {
if (hfs_fstat(fHfsFile, &dirEnt) == 0) {
A2FileHFS* pFile = (A2FileHFS*) fpFile;
pFile->fDataLength = dirEnt.u.file.dsize;
pFile->fRsrcLength = dirEnt.u.file.rsize;
if (pFile->fRsrcLength == 0)
pFile->fRsrcLength = -1;
LOGI(" HFS close set dataLen=%ld rsrcLen=%ld",
(long) pFile->fDataLength, (long) pFile->fRsrcLength);
} else {
LOGI(" HFS Close fstat failed: %s", hfs_error);
// close it anyway
}
}
hfs_close(fHfsFile);
2014-11-18 05:13:13 +00:00
fHfsFile = NULL;
/* flush changes */
if (fModified) {
DiskFSHFS* pDiskFS = (DiskFSHFS*) fpFile->GetDiskFS();
if (hfs_flush(pDiskFS->GetHfsVol()) != 0) {
LOGI("HEY: Close flush failed!");
DebugBreak();
}
}
fpFile->CloseDescr(this);
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Return the #of sectors/blocks in the file. Not supported, but since HFS
* doesn't support "sparse" files we can fake it.
*/
long A2FDHFS::GetSectorCount(void) const
2007-03-27 17:47:10 +00:00
{
A2FileHFS* pFile = (A2FileHFS*) fpFile;
return (long) ((pFile->fDataLength+255) / 256 +
(pFile->fRsrcLength+255) / 256);
2007-03-27 17:47:10 +00:00
}
long A2FDHFS::GetBlockCount(void) const
2007-03-27 17:47:10 +00:00
{
A2FileHFS* pFile = (A2FileHFS*) fpFile;
return (long) ((pFile->fDataLength+511) / 512 +
(pFile->fRsrcLength+511) / 512);
2007-03-27 17:47:10 +00:00
}
/*
* Return the Nth track/sector in this file. Not supported.
*/
DIError A2FDHFS::GetStorage(long sectorIdx, long* pTrack, long* pSector) const
2007-03-27 17:47:10 +00:00
{
return kDIErrNotSupported;
2007-03-27 17:47:10 +00:00
}
2007-03-27 17:47:10 +00:00
/*
* Return the Nth 512-byte block in this file. Not supported.
*/
DIError A2FDHFS::GetStorage(long blockIdx, long* pBlock) const
2007-03-27 17:47:10 +00:00
{
return kDIErrNotSupported;
2007-03-27 17:47:10 +00:00
}
#else // EXCISE_GPL_CODE -----------------------------------------------------
/*
* Get things rolling.
*
* Since we're assured that this is a valid disk, errors encountered from here
* on out must be handled somehow, possibly by claiming that the disk is
* completely full and has no files on it.
*/
DIError DiskFSHFS::Initialize(InitMode initMode)
2007-03-27 17:47:10 +00:00
{
DIError dierr = kDIErrNone;
2007-03-27 17:47:10 +00:00
dierr = LoadVolHeader();
if (dierr != kDIErrNone)
goto bail;
DumpVolHeader();
2007-03-27 17:47:10 +00:00
CreateFakeFile();
2007-03-27 17:47:10 +00:00
SetVolumeUsageMap();
2007-03-27 17:47:10 +00:00
bail:
return dierr;
2007-03-27 17:47:10 +00:00
}
/*
* Fill a buffer with some interesting stuff, and add it to the file list.
*/
void DiskFSHFS::CreateFakeFile(void)
2007-03-27 17:47:10 +00:00
{
A2FileHFS* pFile;
char buf[768]; // currently running about 475
static const char* kFormatMsg =
2007-03-27 17:47:10 +00:00
"The Macintosh HFS filesystem is not supported. CiderPress knows how to\r"
"recognize HFS volumes so that it can identify partitions on CFFA-formatted\r"
"CompactFlash cards and Apple II CD-ROMs, but the current version does not\r"
"know how to view or extract files.\r"
"\r"
"Some information about this HFS volume:\r"
"\r"
" Volume name : '%s'\r"
" Storage capacity : %ld blocks (%.2fMB)\r"
" Number of files : %ld\r"
" Number of folders : %ld\r"
" Last modified : %s\r"
"\r"
;
char dateBuf[32];
long capacity;
const char* timeStr;
capacity = (fAllocationBlockSize / kBlkSize) * fNumAllocationBlocks;
/* get the mod time, format it, and remove the trailing '\n' */
time_t when =
(time_t) (fModifiedDateTime - kDateTimeOffset - fLocalTimeOffset);
timeStr = ctime(&when);
2014-11-18 05:13:13 +00:00
if (timeStr == NULL) {
LOGI("Invalid date %ld (orig=%ld)", when, fModifiedDateTime);
strcpy(dateBuf, "<no date>");
} else
strncpy(dateBuf, timeStr, sizeof(dateBuf));
int len = strlen(dateBuf);
if (len > 0)
dateBuf[len-1] = '\0';
memset(buf, 0, sizeof(buf));
snprintf(buf, NELEM(buf), kFormatMsg,
fVolumeName,
capacity,
(double) capacity / 2048.0,
fNumFiles,
fNumDirectories,
dateBuf);
pFile = new A2FileHFS(this);
2007-03-27 17:47:10 +00:00
pFile->fIsDir = false;
pFile->fIsVolumeDir = false;
pFile->fType = 0;
pFile->fCreator = 0;
pFile->SetFakeFile(buf, strlen(buf));
strcpy(pFile->fFileName, "(not supported)");
2007-03-27 17:47:10 +00:00
pFile->SetPathName("", pFile->fFileName);
pFile->fDataLength = 0;
pFile->fRsrcLength = -1;
pFile->fCreateWhen = 0;
pFile->fModWhen = 0;
2007-03-27 17:47:10 +00:00
pFile->SetFakeFile(buf, strlen(buf));
2007-03-27 17:47:10 +00:00
AddFileToList(pFile);
2007-03-27 17:47:10 +00:00
}
/*
* We could do this, but there's not much point.
*/
DIError GetFreeSpaceCount(long* pTotalUnits, long* pFreeUnits,
int* pUnitSize) const
2007-03-27 17:47:10 +00:00
{
return kDIErrNotSupported;
2007-03-27 17:47:10 +00:00
}
/*
* Not a whole lot to do.
*/
DIError A2FileHFS::Open(A2FileDescr** ppOpenFile, bool readOnly,
bool rsrcFork /*=false*/)
2007-03-27 17:47:10 +00:00
{
2014-11-18 05:13:13 +00:00
A2FDHFS* pOpenFile = NULL;
2007-03-27 17:47:10 +00:00
2014-11-18 05:13:13 +00:00
if (fpOpenFile != NULL)
return kDIErrAlreadyOpen;
if (rsrcFork && fRsrcLength < 0)
return kDIErrForkNotFound;
assert(readOnly == true);
2007-03-27 17:47:10 +00:00
2014-11-18 05:13:13 +00:00
pOpenFile = new A2FDHFS(this, NULL);
2007-03-27 17:47:10 +00:00
fpOpenFile = pOpenFile;
*ppOpenFile = pOpenFile;
2007-03-27 17:47:10 +00:00
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* ===========================================================================
* A2FDHFS
2007-03-27 17:47:10 +00:00
* ===========================================================================
*/
/*
* Read a chunk of data from the fake file.
*/
DIError A2FDHFS::Read(void* buf, size_t len, size_t* pActual)
2007-03-27 17:47:10 +00:00
{
LOGD(" HFS reading %d bytes from '%s' (offset=%ld)",
len, fpFile->GetPathName(), (long) fOffset);
2007-03-27 17:47:10 +00:00
A2FileHFS* pFile = (A2FileHFS*) fpFile;
2007-03-27 17:47:10 +00:00
/* don't allow them to read past the end of the file */
if (fOffset + (long)len > pFile->fDataLength) {
2014-11-18 05:13:13 +00:00
if (pActual == NULL)
return kDIErrDataUnderrun;
len = (size_t) (pFile->fDataLength - fOffset);
}
2014-11-18 05:13:13 +00:00
if (pActual != NULL)
*pActual = len;
2007-03-27 17:47:10 +00:00
memcpy(buf, pFile->GetFakeFileBuf(), len);
2007-03-27 17:47:10 +00:00
fOffset += len;
2007-03-27 17:47:10 +00:00
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Write data at the current offset.
*/
DIError A2FDHFS::Write(const void* buf, size_t len, size_t* pActual)
2007-03-27 17:47:10 +00:00
{
return kDIErrNotSupported;
2007-03-27 17:47:10 +00:00
}
/*
* Seek to a new offset.
*/
DIError A2FDHFS::Seek(di_off_t offset, DIWhence whence)
2007-03-27 17:47:10 +00:00
{
di_off_t fileLen = ((A2FileHFS*) fpFile)->fDataLength;
switch (whence) {
case kSeekSet:
if (offset < 0 || offset > fileLen)
return kDIErrInvalidArg;
fOffset = offset;
break;
case kSeekEnd:
if (offset > 0 || offset < -fileLen)
return kDIErrInvalidArg;
fOffset = fileLen + offset;
break;
case kSeekCur:
if (offset < -fOffset ||
offset >= (fileLen - fOffset))
{
return kDIErrInvalidArg;
}
fOffset += offset;
break;
default:
assert(false);
return kDIErrInvalidArg;
}
assert(fOffset >= 0 && fOffset <= fileLen);
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Return current offset.
*/
di_off_t A2FDHFS::Tell(void)
2007-03-27 17:47:10 +00:00
{
return fOffset;
2007-03-27 17:47:10 +00:00
}
/*
* Release file state, and tell our parent to destroy us.
*/
DIError A2FDHFS::Close(void)
2007-03-27 17:47:10 +00:00
{
fpFile->CloseDescr(this);
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Return the #of sectors/blocks in the file.
*/
long A2FDHFS::GetSectorCount(void) const
2007-03-27 17:47:10 +00:00
{
A2FileHFS* pFile = (A2FileHFS*) fpFile;
return (long) ((pFile->fDataLength+255) / 256);
2007-03-27 17:47:10 +00:00
}
long A2FDHFS::GetBlockCount(void) const
2007-03-27 17:47:10 +00:00
{
A2FileHFS* pFile = (A2FileHFS*) fpFile;
return (long) ((pFile->fDataLength+511) / 512);
2007-03-27 17:47:10 +00:00
}
/*
* Return the Nth track/sector in this file.
*/
DIError A2FDHFS::GetStorage(long sectorIdx, long* pTrack, long* pSector) const
2007-03-27 17:47:10 +00:00
{
return kDIErrNotSupported;
2007-03-27 17:47:10 +00:00
}
2007-03-27 17:47:10 +00:00
/*
* Return the Nth 512-byte block in this file.
*/
DIError A2FDHFS::GetStorage(long blockIdx, long* pBlock) const
2007-03-27 17:47:10 +00:00
{
return kDIErrNotSupported;
2007-03-27 17:47:10 +00:00
}
#endif // EXCISE_GPL_CODE ---------------------------------------------------