nathanhi / pyfatfs

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

Opening file for write without append fails to truncate it, old data left at EOF #25

Closed dcoshea closed 2 years ago

dcoshea commented 2 years ago

With pyfatfs checked out from the bugfix/dentry_ordering branch as at 96ae6bc, with an MS-DOS 5 floppy image (probably the filesystem contents don't matter though), if I create a file and then overwrite it with less data, the old content remains at the end of the file:

>>> fs.writetext("/testfile", "1234567890")
>>> fs.writetext("/testfile", "abcd")
>>> fs.readtext("/testfile")
'abcd567890'

This test passes with fs.memoryfs.MemoryFS.

I assume that any case where a file is opened for write but not for append can trigger this bug.

I suspect that pyfatfs.FatIO.FatIO.__init__() needs to call truncate() when self.mode.truncate is True like fs.memoryfs.MemoryFile.__init__() does.

nathanhi commented 2 years ago

Fixed on master, will be part of the upcoming bugfix release.

dcoshea commented 2 years ago

Thanks! I verified that the issue I was having has gone away in v1.0.5 and I was able to revert my workaround of removing the file before calling writetext().