PascalFileEntry now implicitly writes the entry back to disk with every "set" method. Should close #144.

This commit is contained in:
Rob Greene
2025-07-28 11:36:08 -05:00
parent 3353d971ca
commit 7109d42ad4
3 changed files with 38 additions and 25 deletions
@@ -59,12 +59,35 @@ public class PascalFileEntry implements FileEntry {
/**
* Constructor for PascalFileEntry.
*/
public PascalFileEntry(byte[] fileEntry, PascalFormatDisk disk) {
public PascalFileEntry(byte[] fileEntry, int index, PascalFormatDisk disk) {
super();
this.fileEntry = fileEntry;
this.index = index;
this.disk = disk;
}
/**
* Write the fileEntry data to the disk image.
*/
protected void writeFileEntry() {
if (this.deleted) {
return; // prevents trying to save a deleted file (not a reality in Pascal filesystem)
}
List<PascalFileEntry> dir = disk.getDirectory();
if (this.index < dir.size()) {
dir.set(this.index, this);
}
else if (this.index == dir.size()) {
dir.add(this.index, this);
}
else {
throw new RuntimeException(textBundle.format("PascalFormatDisk.UnexpectedDirectoryIndex", this.index, dir.size()));
}
// Since every "set" triggers a write, the file count tends to get whacked; this makes it sane.
dir.get(0).setFileCount(dir.size()-1);
disk.putDirectory(dir);
}
/**
* Get the block number of the file's 1st block.
*/
@@ -77,6 +100,7 @@ public class PascalFileEntry implements FileEntry {
*/
public void setFirstBlock(int first) {
AppleUtil.setWordValue(fileEntry, 0, first);
writeFileEntry();
}
/**
@@ -91,6 +115,7 @@ public class PascalFileEntry implements FileEntry {
*/
public void setLastBlock(int last) {
AppleUtil.setWordValue(fileEntry, 2, last);
writeFileEntry();
}
/**
@@ -105,6 +130,7 @@ public class PascalFileEntry implements FileEntry {
*/
public void setFilename(String filename) {
AppleUtil.setPascalString(fileEntry, 6, filename.toUpperCase(), 15);
writeFileEntry();
}
/**
@@ -147,6 +173,7 @@ public class PascalFileEntry implements FileEntry {
} else {
AppleUtil.setWordValue(fileEntry, 4, 0);
}
writeFileEntry();
}
/**
@@ -175,6 +202,7 @@ public class PascalFileEntry implements FileEntry {
*/
public void setBytesUsedInLastBlock(int value) {
AppleUtil.setWordValue(fileEntry, 22, value);
writeFileEntry();
}
/**
@@ -235,7 +263,7 @@ public class PascalFileEntry implements FileEntry {
if (index != 0) {
dir.remove(index);
PascalFileEntry volEntry = (PascalFileEntry) dir.get(0);
volEntry.setFileCount(count - 2); // inlcudes the volume entry
volEntry.setFileCount(count - 2); // includes the volume entry
dir.set(0, volEntry);
disk.putDirectory(dir);
deleted = true;
@@ -254,6 +282,7 @@ public class PascalFileEntry implements FileEntry {
*/
public void setModificationDate(Date date) {
AppleUtil.setPascalDate(fileEntry, 24, date);
writeFileEntry();
}
/**
@@ -375,7 +404,6 @@ public class PascalFileEntry implements FileEntry {
* Set file data for this file entry. Because the directory entry may
* have been changed, use this.index to determine which entry to update.
* author John B. Matthews.
* @see #setEntryIndex
* @see PascalFormatDisk#createFile
*/
public void setFileData(byte[] data) throws DiskFullException {
@@ -448,11 +476,7 @@ public class PascalFileEntry implements FileEntry {
}
}
// update this directory entry
if (this.index > 0) {
List<PascalFileEntry> dir = disk.getDirectory();
dir.set(this.index, this);
disk.putDirectory(dir);
}
writeFileEntry();
}
/**
@@ -522,16 +546,6 @@ public class PascalFileEntry implements FileEntry {
return false;
}
/**
* Remember the index of a newly created file entry.
* Required to update the entry after setFileData,
* which may change any or all of the new entry's fields.
* author John B. Matthews
*/
public void setEntryIndex(int index) {
this.index = index;
}
/**
* Set the file count in a volume entry.
* Use only on the volume entry: dir.get(0).
@@ -152,7 +152,8 @@ public class PascalFormatDisk extends FormattedDisk {
for (int i=0; i<count; i++) {
byte[] entry = new byte[entrySize];
System.arraycopy(directory, offset, entry, 0, entry.length);
list.add(new PascalFileEntry(entry, this));
// we skipped the "volume" entry, so the actual index value is +1:
list.add(new PascalFileEntry(entry, i+1, this));
offset+= entrySize;
}
return list;
@@ -169,7 +170,7 @@ public class PascalFormatDisk extends FormattedDisk {
for (int i = 0; i <= count; i++) {
byte[] entry = new byte[ENTRY_SIZE];
System.arraycopy(directory, offset, entry, 0, entry.length);
list.add(new PascalFileEntry(entry, this));
list.add(new PascalFileEntry(entry, i,this));
offset += ENTRY_SIZE;
}
return list;
@@ -219,7 +220,7 @@ public class PascalFormatDisk extends FormattedDisk {
volEntry.setFileCount(count);
dir.set(0, volEntry);
// add new entry to list
dir.add(index, new PascalFileEntry(new byte[ENTRY_SIZE], this));
dir.add(index, new PascalFileEntry(new byte[ENTRY_SIZE], index, this));
PascalFileEntry entry = (PascalFileEntry) dir.get(index);
// fill in plausible values; will rely index, first and last
first = ((PascalFileEntry) dir.get(index - 1)).getLastBlock();
@@ -229,10 +230,7 @@ public class PascalFormatDisk extends FormattedDisk {
entry.setFilename("x"); //$NON-NLS-1$
entry.setBytesUsedInLastBlock(512);
entry.setModificationDate(new Date());
entry.setEntryIndex(index);
dir.set(index, entry);
// write it back to disk
putDirectory(dir);
// Note that each "set" does an implicit write
return entry;
} else {
throw new DiskFullException(
@@ -135,6 +135,7 @@ PascalFormatDisk.LastBlock=Last Block
PascalFormatDisk.VolumeDate=Volume Date
PascalFormatDisk.IncorrectFileEntryError=Must have a Pascal file entry\!
PascalFormatDisk.DiskFull=Disk full.
PascalFormatDisk.UnexpectedDirectoryIndex=Pascal file entry out of expected directory bounds. Index {0} expected to be in range 0..{1}+1.
# PascalFileEntry
PascalFileEntry.UnknownFiletype=unknown ({0})