This is an archive of the discontinued LLVM Phabricator instance.

[Support] Re-work the Flags parameter of the FileSystem open APIs
Needs ReviewPublic

Authored by zturner on Jun 6 2018, 4:41 PM.

Details

Reviewers
rnk
Summary

The original motivation for this came from the desire to add a couple more different OpenFlags. It became clear that such a monolithic and overloaded enumeration was a source of complexity and subtle gotchas. For example, certain flags could not be specified together, or would be ignored in some situations but not other. It also became difficult to reason about the subtle differences in each combination of flags. Finally, I wanted some types of flags to be available even on read operations, but only a couple of the flags actually made sense on that API, while the rest were mostly for writing.

To address this, I split the enumeration into three enumerations. These are:

CreationDisposition - Controls the behavior of the API based on whether or not the file you are trying to open already exists. It can fail if the file exists, fail if it doesn't exist, truncate, etc. This takes the place of various uses of O_Excl, but also opens up new possibilities. For example, previously we always specified O_CREAT, which means we had no way to force a file to already exist when opening it.

FileAccess - Controls whether or not to open the file for read or write. While this enumeration isn't used in any publicly exported function, I plan to do this in a followup. Currently it is used interally as a way to re-use code in the private implementation, with all calls to all openFileForXXX functions delegating to a single master function. However, I hope to expose this master function in a follow-up patch.

OpenFlags - What used to be a large monoloithic enumeration is now just 3 values. Append, Text, and Delete. Any combination of these values can be used together, unlike before.

Another advantage of doing things this way is that it becomes very easy to write exhaustive test coverage. I've provided tests for every new creation disposition as well as the Append open flag, none of which was thoroughly tested before.

Tested on Linux and Windows.

Diff Detail

Event Timeline

zturner created this revision.Jun 6 2018, 4:41 PM
rnk added inline comments.Jun 7 2018, 1:21 PM
llvm/lib/Support/Unix/Path.inc
742

TIL about O_EXCL.