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.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Support for the VolumeUsage sub-class in DiskFS.
|
|
|
|
*/
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "DiskImgPriv.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize structures for a block-structured disk.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskFS::VolumeUsage::Create(long numBlocks)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
if (numBlocks <= 0 || numBlocks > 32*1024*1024) // 16GB
|
|
|
|
return kDIErrInvalidArg;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
fByBlocks = true;
|
|
|
|
fNumSectors = -1;
|
|
|
|
fTotalChunks = numBlocks;
|
|
|
|
fListSize = numBlocks;
|
|
|
|
fList = new unsigned char[fListSize];
|
2014-11-18 05:13:13 +00:00
|
|
|
if (fList == NULL)
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrMalloc;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
memset(fList, 0, fListSize);
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize structures for a track/sector-structured disk.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskFS::VolumeUsage::Create(long numTracks, long numSectors)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
long count = numTracks * numSectors;
|
|
|
|
if (numTracks <= 0 || count <= 0 || count > 32*1024*1024)
|
|
|
|
return kDIErrInvalidArg;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
fByBlocks = false;
|
|
|
|
fNumSectors = numSectors;
|
|
|
|
fTotalChunks = count;
|
|
|
|
fListSize = count;
|
|
|
|
fList = new unsigned char[fListSize];
|
2014-11-18 05:13:13 +00:00
|
|
|
if (fList == NULL)
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrMalloc;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
memset(fList, 0, fListSize);
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the state of a particular chunk.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskFS::VolumeUsage::GetChunkState(long block, ChunkState* pState) const
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
if (!fByBlocks)
|
|
|
|
return kDIErrInvalidArg;
|
|
|
|
return GetChunkStateIdx(block, pState);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
2014-11-24 20:56:19 +00:00
|
|
|
|
|
|
|
DIError DiskFS::VolumeUsage::GetChunkState(long track, long sector,
|
2014-11-04 00:26:53 +00:00
|
|
|
ChunkState* pState) const
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
if (fByBlocks)
|
|
|
|
return kDIErrInvalidArg;
|
|
|
|
if (track < 0 || sector < 0 || sector >= fNumSectors)
|
|
|
|
return kDIErrInvalidArg;
|
|
|
|
return GetChunkStateIdx(track * fNumSectors + sector, pState);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
2014-11-24 20:56:19 +00:00
|
|
|
|
|
|
|
DIError DiskFS::VolumeUsage::GetChunkStateIdx(int idx, ChunkState* pState) const
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-18 05:13:13 +00:00
|
|
|
if (fList == NULL || idx < 0 || idx >= fListSize) {
|
2014-11-04 00:26:53 +00:00
|
|
|
assert(false);
|
|
|
|
return kDIErrInvalidArg;
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
unsigned char val = fList[idx];
|
|
|
|
pState->isUsed = (val & kChunkUsedFlag) != 0;
|
|
|
|
pState->isMarkedUsed = (val & kChunkMarkedUsedFlag) != 0;
|
|
|
|
pState->purpose = (ChunkPurpose)(val & kChunkPurposeMask);
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the state of a particular chunk.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskFS::VolumeUsage::SetChunkState(long block, const ChunkState* pState)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
if (!fByBlocks)
|
|
|
|
return kDIErrInvalidArg;
|
|
|
|
return SetChunkStateIdx(block, pState);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
2014-11-24 20:56:19 +00:00
|
|
|
|
|
|
|
DIError DiskFS::VolumeUsage::SetChunkState(long track, long sector,
|
2014-11-04 00:26:53 +00:00
|
|
|
const ChunkState* pState)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
if (fByBlocks)
|
|
|
|
return kDIErrInvalidArg;
|
|
|
|
if (track < 0 || sector < 0 || sector >= fNumSectors)
|
|
|
|
return kDIErrInvalidArg;
|
|
|
|
return SetChunkStateIdx(track * fNumSectors + sector, pState);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
2014-11-24 20:56:19 +00:00
|
|
|
|
|
|
|
DIError DiskFS::VolumeUsage::SetChunkStateIdx(int idx, const ChunkState* pState)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-18 05:13:13 +00:00
|
|
|
if (fList == NULL || idx < 0 || idx >= fListSize) {
|
2014-11-04 00:26:53 +00:00
|
|
|
assert(false);
|
|
|
|
return kDIErrInvalidArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char val = 0;
|
|
|
|
if (pState->isUsed) {
|
|
|
|
if ((pState->purpose & ~kChunkPurposeMask) != 0) {
|
|
|
|
assert(false);
|
|
|
|
return kDIErrInvalidArg;
|
|
|
|
}
|
|
|
|
val |= kChunkUsedFlag;
|
|
|
|
val |= (int)pState->purpose;
|
|
|
|
}
|
|
|
|
if (pState->isMarkedUsed)
|
|
|
|
val |= kChunkMarkedUsedFlag;
|
|
|
|
|
|
|
|
fList[idx] = val;
|
|
|
|
|
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Count up the #of free chunks.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
long DiskFS::VolumeUsage::GetActualFreeChunks(void) const
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
ChunkState cstate; // could probably do this bitwise...
|
|
|
|
int freeCount = 0;
|
|
|
|
int funkyCount = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < fTotalChunks; i++) {
|
|
|
|
if (GetChunkStateIdx(i, &cstate) != kDIErrNone) {
|
|
|
|
assert(false);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cstate.isUsed && !cstate.isMarkedUsed)
|
|
|
|
freeCount++;
|
|
|
|
|
|
|
|
if ((!cstate.isUsed && cstate.isMarkedUsed) ||
|
|
|
|
(cstate.isUsed && !cstate.isMarkedUsed) ||
|
|
|
|
(cstate.isUsed && cstate.purpose == kChunkPurposeConflict))
|
|
|
|
{
|
|
|
|
funkyCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" VU total=%ld free=%d funky=%d",
|
2014-11-04 00:26:53 +00:00
|
|
|
fTotalChunks, freeCount, funkyCount);
|
|
|
|
|
|
|
|
return freeCount;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a ChunkState into a single, hopefully meaningful, character.
|
|
|
|
*
|
|
|
|
* Possible states:
|
2014-11-04 00:26:53 +00:00
|
|
|
* '.' - !inuse, !marked (free space)
|
|
|
|
* 'X' - !inuse, marked (could be embedded volume)
|
|
|
|
* '!' - inuse, !marked (danger!)
|
|
|
|
* '#' - inuse, marked, used by more than one thing
|
|
|
|
* 'S' - inuse, marked, used by system (directories, volume bit map)
|
|
|
|
* 'I' - inuse, marked, used by file structure (index block)
|
|
|
|
* 'F' - inuse, marked, used by file
|
2007-03-27 17:47:10 +00:00
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
char DiskFS::VolumeUsage::StateToChar(ChunkState* pState) const
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
if (!pState->isUsed && !pState->isMarkedUsed)
|
|
|
|
return '.';
|
|
|
|
if (!pState->isUsed && pState->isMarkedUsed)
|
|
|
|
return 'X';
|
|
|
|
if (pState->isUsed && !pState->isMarkedUsed)
|
|
|
|
return '!';
|
|
|
|
assert(pState->isUsed && pState->isMarkedUsed);
|
|
|
|
if (pState->purpose == kChunkPurposeUnknown)
|
|
|
|
return '?';
|
|
|
|
if (pState->purpose == kChunkPurposeConflict)
|
|
|
|
return '#';
|
|
|
|
if (pState->purpose == kChunkPurposeSystem)
|
|
|
|
return 'S';
|
|
|
|
if (pState->purpose == kChunkPurposeVolumeDir)
|
|
|
|
return 'V';
|
|
|
|
if (pState->purpose == kChunkPurposeSubdir)
|
|
|
|
return 'D';
|
|
|
|
if (pState->purpose == kChunkPurposeUserData)
|
|
|
|
return 'F';
|
|
|
|
if (pState->purpose == kChunkPurposeFileStruct)
|
|
|
|
return 'I';
|
|
|
|
if (pState->purpose == kChunkPurposeEmbedded)
|
|
|
|
return 'E';
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
return '?';
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump the list.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
void DiskFS::VolumeUsage::Dump(void) const
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
|
|
|
#define kMapInit "--------------------------------"
|
2014-11-18 05:13:13 +00:00
|
|
|
if (fList == NULL) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" VU asked to dump empty list?");
|
2014-11-04 00:26:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" VU VolumeUsage dump (%ld free chunks):",
|
2014-11-04 00:26:53 +00:00
|
|
|
GetActualFreeChunks());
|
|
|
|
|
|
|
|
if (fByBlocks) {
|
|
|
|
ChunkState cstate;
|
|
|
|
char freemap[32+1] = kMapInit;
|
|
|
|
int block;
|
|
|
|
const int kEntriesPerLine = 32; // use 20 to match Copy][+
|
|
|
|
|
|
|
|
for (block = 0; block < fTotalChunks; block++) {
|
|
|
|
if (GetChunkState(block, &cstate) != kDIErrNone) {
|
|
|
|
assert(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
freemap[block % kEntriesPerLine] = StateToChar(&cstate);
|
|
|
|
if ((block % kEntriesPerLine) == kEntriesPerLine-1) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" 0x%04x: %s", block-(kEntriesPerLine-1), freemap);
|
2014-11-04 00:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((block % kEntriesPerLine) != 0) {
|
|
|
|
memset(freemap + (block % kEntriesPerLine), '-',
|
|
|
|
kEntriesPerLine - (block % kEntriesPerLine));
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" 0x%04x: %s", block-(kEntriesPerLine-1), freemap);
|
2014-11-04 00:26:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ChunkState cstate;
|
|
|
|
char freemap[32+1] = kMapInit;
|
|
|
|
long numTracks = fTotalChunks / fNumSectors;
|
|
|
|
int track, sector;
|
|
|
|
|
|
|
|
if (fNumSectors > 32) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" VU too many sectors (%ld)", fNumSectors);
|
2014-11-04 00:26:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" map 0123456789abcdef");
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
for (track = 0; track < numTracks; track++) {
|
|
|
|
for (sector = 0; sector < fNumSectors; sector++) {
|
|
|
|
if (GetChunkState(track, sector, &cstate) != kDIErrNone) {
|
|
|
|
assert(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
freemap[sector] = StateToChar(&cstate);
|
|
|
|
}
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" %2d: %s", track, freemap);
|
2014-11-04 00:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|