First pass at detecting ROM file size and not loading ROMs that are too large.

Note that this only works in UNIX for now; still TODO for Windows and ZIP files.
For the TODO, the current code still loads ROMs, but just doesn't ignore large files.
This commit is contained in:
Stephen Anthony 2022-06-05 20:58:03 -02:30
parent 7e03cfac4d
commit cec3b76fb9
5 changed files with 43 additions and 3 deletions

View File

@ -328,6 +328,12 @@ bool FilesystemNode::rename(const string& newfile)
return (_realNode && _realNode->exists()) ? _realNode->rename(newfile) : false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
size_t FilesystemNode::getSize() const
{
return (_realNode && _realNode->exists()) ? _realNode->getSize() : 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
size_t FilesystemNode::read(ByteBuffer& buffer, size_t size) const
{

View File

@ -243,6 +243,13 @@ class FilesystemNode
*/
bool rename(const string& newfile);
/**
* Get the size of the current node path.
*
* @return Size (in bytes) of the current node path.
*/
size_t getSize() const;
/**
* Read data (binary format) into the given buffer.
*
@ -438,6 +445,13 @@ class AbstractFSNode
*/
virtual bool rename(const string& newfile) = 0;
/**
* Get the size of the current node path.
*
* @return Size (in bytes) of the current node path.
*/
virtual size_t getSize() const { return 0; }
/**
* Read data (binary format) into the given buffer.
*

View File

@ -787,10 +787,22 @@ ByteBuffer OSystem::openROM(const FilesystemNode& rom, size_t& size,
if(!isValidROM && showErrorMessage)
throw runtime_error("Unrecognized ROM file type");
// Next check for a proper file size:
// Streaming ROMs read only a portion of the file
// TODO: Otherwise, cap the size to the maximum Cart size
// Next check for a proper file size
// Streaming ROMs read only a portion of the file
// Otherwise the size to read is 0 (meaning read the entire file)
const size_t sizeToRead = CartDetector::isProbablyMVC(rom);
const bool isStreaming = sizeToRead > 0;
// Make sure we only read up to the maximum supported cart size
const bool isValidSize = isValidROM && (isStreaming ||
rom.getSize() <= Cartridge::maxSize());
if(!isValidSize)
{
if(showErrorMessage)
throw runtime_error("ROM file too large");
else
return nullptr;
}
// Now we can try to open the file
ByteBuffer image;

View File

@ -180,6 +180,13 @@ bool FilesystemNodePOSIX::getChildren(AbstractFSList& myList, ListMode mode) con
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
size_t FilesystemNodePOSIX::getSize() const
{
struct stat st;
return (stat(_path.c_str(), &st) == 0) ? st.st_size : 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool FilesystemNodePOSIX::makeDir()
{

View File

@ -73,6 +73,7 @@ class FilesystemNodePOSIX : public AbstractFSNode
bool makeDir() override;
bool rename(const string& newfile) override;
size_t getSize() const override;
bool getChildren(AbstractFSList& list, ListMode mode) const override;
AbstractFSNodePtr getParent() const override;