Ensuring a repeatable directory ordering in linux

Solution 1:

I know it's not the answer you're looking for, but I believe the correct solution is to avoid depending on the ordering of files in a directory. Maybe it's always consistent across all HFS+ filesystems, and maybe you could find a way to make it consistent in ext4 or some other filesystem as well, but it will cost you more trouble in the long run than it will save. Someone else using your application will run into a nasty surprise when they don't realize that it's compatible only with some types of filesystems and not others. The order may change if a filesystem is restored from backup. You'll likely run into compatibility problems because the HFS+ consistent order and the ext4 consistent order might not be the same.

Just read all of the directory entries and sort the list lexicographically before using it. Just like ls does.

You mention files a.rb and b.rb, but if we're talking about programming language source files, shouldn't each file already be responsible for ensuring that it imports all its dependencies?

Solution 2:

The POSIX call in Linux readdir() doesn't guarantee any consistent ordering. If you want ordered results, the application that is handling files is responsible for ordering how they are presented to calling functions.

https://stackoverflow.com/questions/8977441/does-readdir-guarantee-an-order

Now, since you said this was your customer's code and you can't fix it, you could possibly alter the linked libraries that are used to provide a consistent readdir() call. That would take some work and be worth its own question. For a quick reference to that, see http://www.ibm.com/developerworks/linux/library/l-glibc/index.html.

Altering this could spawn some other entire series of issues that I may not be able to foresee. You are strongly cautioned, but it may be a solution if your customer cannot be properly educated.

Solution 3:

Educate your customer that there is an inherent order dependency that should be explicitly stated. Offer to help the customer express the dependency in such a way that a compile works on all systems and have the customer adopt the changed flow that captures the compilation order dependency.

If the customer wants to be able to compile on other machines then it would be churlish of them to think that it comes for free.