natevw / fatfs

Standalone FAT16/FAT32 filesystem implementation in JavaScript
47 stars 13 forks source link

Input/output error when writing a file #18

Closed jviotti closed 9 years ago

jviotti commented 9 years ago

I'm having a weird Input/output error when writing a file to a partition (reading the same file works fine).

The error is being thrown here: https://github.com/natevw/fatfs/blob/master/chains.js#L122, (d === "free" there) however my lack of in depth knowledge prevents me from understanding the reason of the error.

The project is the following: https://github.com/resin-io/resin-config-inject, which reads/writes a json file from/to a FAT partition in an *.img file.

The project includes a test script that triggers the error at https://github.com/resin-io/resin-config-inject/blob/master/tests/scripts/inject.coffee.

I've uploaded an image example to Dropbox (https://dl.dropboxusercontent.com/u/18129309/raspberry-pi2.img) so anyone can reproduce the error as following:

$ git clone https://github.com/resin-io/resin-config-inject
$ cd resin-config-inject
$ coffee tests/scripts/inject.coffee path/to/raspberry-pi2.img "4:1"

4:1 means the first logical partition of the fourth extended partition, which happens to be my FAT partition. Notice I double checked I was using 'fatfs' in the correct partition, and that the read operation works fine.

What could be causing this error?

natevw commented 9 years ago

Not quite sure what's going on, but wanted to say thanks for the very detailed report and let you know I am able to trigger the issue using your steps.

I'm a tad rusty here too, but so far it appears that the "linked list" of allocated clusters for a file may be pointing into a free cluster at some point along its chain — or at least it is appearing that way to the code. AFAIK, these chains should always lead to either another reserved cluster or an EOF marker. Why this is happening, instead, I need to look more thoroughly into. It could possibly be a corrupt filesystem, or perhaps more likely a bug or missing handling for some situation in this library.

jviotti commented 9 years ago

@natevw We were able to solve the issue, which wasn't a problem with fatfs but with our FAT partition in the image file.

We were forcing FAT32 but the partition size was less than 32MB (~4MB), thus didn't comply with the FAT specification. We increased the partition size to 16MB and formatted as FAT16, which worked as expected.

Closing the issue, and thank you very much for the support!