mirror of
https://github.com/cmclark00/retro-imager.git
synced 2025-05-18 16:05:21 +01:00
Advanced settings FAT modification: fix expanding directory
When no more directory entries are available in the existing cluster(s) of a directory on FAT32, we allocate an extra cluster. Make sure to zero out that new cluster, as disk checking utilities may not stop reading when reaching an end of directory marker but read the rest of cluster as well. So there must not be any garbage data from a previously deleted file in the sector there. Also add checks to prevent getting in an endless loop on encoutering circular "next cluster" references.
This commit is contained in:
parent
2694f3976a
commit
a403df4385
3 changed files with 24 additions and 4 deletions
|
@ -506,6 +506,8 @@ void DeviceWrapperFatPartition::updateDirEntry(struct dir_entry *dirEntry)
|
|||
struct dir_entry iterEntry;
|
||||
|
||||
openDir();
|
||||
quint64 oldOffset = _offset;
|
||||
|
||||
while (readDir(&iterEntry))
|
||||
{
|
||||
/* Look for existing entry with same short filename */
|
||||
|
@ -513,10 +515,12 @@ void DeviceWrapperFatPartition::updateDirEntry(struct dir_entry *dirEntry)
|
|||
&& memcmp(dirEntry->DIR_Name, iterEntry.DIR_Name, sizeof(iterEntry.DIR_Name)) == 0)
|
||||
{
|
||||
/* seek() back and write out new entry */
|
||||
_offset -= sizeof(*dirEntry);
|
||||
_offset = oldOffset;
|
||||
write((char *) dirEntry, sizeof(*dirEntry));
|
||||
return;
|
||||
}
|
||||
|
||||
oldOffset = _offset;
|
||||
}
|
||||
|
||||
throw std::runtime_error("Error locating existing directory entry");
|
||||
|
@ -524,6 +528,7 @@ void DeviceWrapperFatPartition::updateDirEntry(struct dir_entry *dirEntry)
|
|||
|
||||
void DeviceWrapperFatPartition::writeDirEntryAtCurrentPos(struct dir_entry *dirEntry)
|
||||
{
|
||||
//qDebug() << "Write new entry" << QByteArray((char *) dirEntry->DIR_Name, 11);
|
||||
write((char *) dirEntry, sizeof(*dirEntry));
|
||||
|
||||
if (_type == FAT32)
|
||||
|
@ -532,15 +537,23 @@ void DeviceWrapperFatPartition::writeDirEntryAtCurrentPos(struct dir_entry *dirE
|
|||
{
|
||||
/* We reached the end of the cluster, allocate/seek to next cluster */
|
||||
uint32_t nextCluster = getFAT(_fat32_currentRootDirCluster);
|
||||
/* FIXME: should we check for circular cluster references? */
|
||||
|
||||
if (nextCluster > 0xFFFFFF7)
|
||||
{
|
||||
nextCluster = allocateCluster(_fat32_currentRootDirCluster);
|
||||
}
|
||||
|
||||
if (_currentDirClusters.contains(nextCluster))
|
||||
throw std::runtime_error("Circular cluster references in FAT32 directory detected");
|
||||
_currentDirClusters.append(nextCluster);
|
||||
|
||||
_fat32_currentRootDirCluster = nextCluster;
|
||||
seekCluster(_fat32_currentRootDirCluster);
|
||||
|
||||
/* Zero out entire new cluster, as fsck.fat does not stop reading entries at end-of-directory marker */
|
||||
QByteArray zeroes(_bytesPerCluster, 0);
|
||||
write(zeroes.data(), zeroes.length() );
|
||||
seekCluster(_fat32_currentRootDirCluster);
|
||||
}
|
||||
}
|
||||
else if (pos() > (_fat16_firstRootDirSector+_fat16_rootDirSectors)*_bytesPerSector)
|
||||
|
@ -560,6 +573,10 @@ void DeviceWrapperFatPartition::openDir()
|
|||
{
|
||||
_fat32_currentRootDirCluster = _fat32_firstRootDirCluster;
|
||||
seekCluster(_fat32_currentRootDirCluster);
|
||||
/* Keep track of directory clusters we seeked to, to be able
|
||||
to detect circular references */
|
||||
_currentDirClusters.clear();
|
||||
_currentDirClusters.append(_fat32_currentRootDirCluster);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -581,11 +598,13 @@ bool DeviceWrapperFatPartition::readDir(struct dir_entry *result)
|
|||
{
|
||||
/* We reached the end of the cluster, seek to next cluster */
|
||||
uint32_t nextCluster = getFAT(_fat32_currentRootDirCluster);
|
||||
/* FIXME: should we check for circular cluster references? */
|
||||
|
||||
if (nextCluster > 0xFFFFFF7)
|
||||
throw std::runtime_error("Reached end of FAT32 root directory, but no end-of-directory marker found");
|
||||
|
||||
if (_currentDirClusters.contains(nextCluster))
|
||||
throw std::runtime_error("Circular cluster references in FAT32 directory detected");
|
||||
_currentDirClusters.append(nextCluster);
|
||||
_fat32_currentRootDirCluster = nextCluster;
|
||||
seekCluster(_fat32_currentRootDirCluster);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue