andreasgal/j2me.js — pull request #713 by mykmelez (2014-12-11 01:25:46+00:00)
Title: WIP: normalize data about files in directory

The fs module currently tracks files in dirs via a *files* array in each directory record, but that array could get out-of-sync with the keys in the store, which represent the paths of existing files. And code could check one or the other inconsistently.

For example, *exists* currently just checks whether or not the file path is a key in the store, while *createInternal* refuses to create the file if it's listed in its directory's *files* array. So if a file doesn't have a key, but it's listed in the directory's *files* array, then *exists* will return `false`, but nevertheless *createInternal* will refuse to create it because it thinks it already exists.

We should prevent this possibility by removing *files* and generating the list of files in a directory from the file records in the store. This change does that in an expensive way, by iterating keys. A better way might be to add a *parent* property to each record and create an index on that property. We could do that here or merge this first and then do that in a followup.

Either way, we should remove the *files* arrays from all the directory records at some point (which we can also do either here or in a followup).

Marking this WIP because I haven't tested it with my favorite midlet yet, even though it passes all the tests.