natevw / fatfs

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

How to format/initialize? #21

Closed julien-f closed 8 years ago

julien-f commented 8 years ago

I need to create a FAT partition in a buffer so I created this driver but I get invalid volume signature.

I suppose it's because the buffer is initially not formatted, is there any way to initialize it properly?

natevw commented 8 years ago

Sounds like fun! Actually initializing a FAT filesystem is not implemented in this library, and may even be beyond its scope.

If you don't mind calling out to the shell, and can constrain yourself to certain OSes, take a look at the make_sample.sh file in this repository, which sets up a file-based partition on OS X and the equivalent on Linux would likely be even simpler. Basically, you could do something like that in a temp file and then read that whole file into your buffer.

That said, it shouldn't be too hard to set up a buffer so that fatfs is happy with it. Take a look at the boot16/boot32 structures this library uses, have a skim through the FAT documentation/tutorial links at the top of that file — or even, if your buffers will always be the same size, just use a command line tool to generate it once, then "hardcode" the first 512 bytes into your program. That's really the only "formatting" needed, that first 512 block which then should be prepended to the amount of remaining space it claims to have. Make sense?

julien-f commented 8 years ago

Here is what I eventually did but we still get some errors randomly such as no space left on device, maybe I did something wrong.

natevw commented 8 years ago

Offhand your structure values seem reasonable; have you tried mounting the same contents as a volume against a "real" (Linux or OS X built-in) FAT implementation?

If you're making heavy use of this it's certainly possible you've run into some other bugs or caveats. 10MB could fill up pretty fast with lots of little files. Lemme know if you're suspecting the volume header itself and I could take a closer look sometime, otherwise it might be worth taking each subsequent error on its own.

julien-f commented 8 years ago

Offhand your structure values seem reasonable; have you tried mounting the same contents as a volume against a "real" (Linux or OS X built-in) FAT implementation?

Nope, good idea, I'll keep you posted :)

If you're making heavy use of this it's certainly possible you've run into some other bugs or caveats. 10MB could fill up pretty fast with lots of little files.

We are only writing a few files, using at most a megabyte.

julien-f commented 8 years ago

Initializing the buffer with null bytes seems to resolve the issue.

natevw commented 8 years ago

Ah, of course! You have the volume "header" but if the allocation table itself is filled with random bytes that would cause some problems. Makes sense in retrospect ;-)

julien-f commented 8 years ago

Yep, thank you for your time :)