nathanhi / pyfatfs

Python based FAT12/FAT16/FAT32 implementation with VFAT support
https://pypi.org/project/pyfatfs/
MIT License
29 stars 14 forks source link

Light-weight test for FAT validity of a given file object #42

Open mxmlnkn opened 2 weeks ago

mxmlnkn commented 2 weeks ago

As mentioned in #41, I am trying to use this project via FUSE. The current work in progress works quite fine already.

Bash script to test the work-in-progress state. ```bash # Would be nice to have this without sudo, but I don't want to create test cases with the same program being tested. head -c $(( 1024 * 1024 )) /dev/zero > 'folder-symlink.fat' mkfs.fat 'folder-symlink.fat' mkdir mounted sudo mount 'folder-symlink.fat' mounted cd mounted sudo mkdir -p foo/fighter echo bar | sudo tee > foo/fighter/ufo sudo umount mounted python3 -m pip install --user --force-reinstall \ 'git+https://github.com/mxmlnkn/ratarmount.git@fat#egginfo=ratarmountcore&subdirectory=core' \ 'git+https://github.com/mxmlnkn/ratarmount.git@fat#egginfo=ratarmount' ratarmount folder-symlink.fat mounted tree mounted # mounted/ # └── foo # └── fighter # └── ufo # # 2 directories, 1 file ```

However, for integration, I need a isFATImage function that can relatively quickly check a given bytes file object whether it is a FAT file / image. Normally, these check functions would only require checking the first 2-10 bytes, depending on the file format, but for FAT it is probably not all that easy. Currently, I call private functions of pyfatfs to achieve something like this:

fs = PyFat.PyFat()
fs._PyFat__set_fp(fileObject)
fs.is_read_only = True
try:
    fs.parse_header()
    return True
except pyfatfs.PyFATException:
    return False
finally:
    # Reset file object so that it does not get closed! Cannot be None because that is checked.
    fs._PyFat__fp = io.BytesIO()

It would be nice to have some officially supported way to do something like this. I am not sure whether _parse_fat, in addition to parse_header, should be included in such a check. It depends a bit on how much it checks.