[flang][runtime] OPEN write-only files

If a file being opened with no ACTION= is write-only then cope with
it rather than defaulting prematurely to treating it as read-only.

Differential Revision: https://reviews.llvm.org/D127015
This commit is contained in:
Peter Klausler 2022-05-29 08:36:57 -07:00
parent cc3bd43533
commit e5a4f730da
1 changed files with 8 additions and 2 deletions

View File

@ -97,12 +97,18 @@ void OpenFile::Open(OpenStatus status, std::optional<Action> action,
flags |= O_TRUNC;
}
if (!action) {
// Try to open read/write, back off to read-only on failure
// Try to open read/write, back off to read-only or even write-only
// on failure
fd_ = ::open(path_.get(), flags | O_RDWR, 0600);
if (fd_ >= 0) {
action = Action::ReadWrite;
} else {
action = Action::Read;
fd_ = ::open(path_.get(), flags | O_RDONLY, 0600);
if (fd_ >= 0) {
action = Action::Read;
} else {
action = Action::Write;
}
}
}
if (fd_ < 0) {