From d8334c43606a08dc13a69d0993dc7a52d5c0fe56 Mon Sep 17 00:00:00 2001 From: peter klausler Date: Mon, 3 Aug 2020 11:29:15 -0700 Subject: [PATCH] [flang] Acquire file accessibility, size, positioning Extend the raw file wrapper to get accessibility, positioning, and size information. This is needed for INQUIRE (to follow). Differential Revision: https://reviews.llvm.org/D85160 --- flang/runtime/file.cpp | 21 +++++++++++++++++++-- flang/runtime/file.h | 4 ++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/flang/runtime/file.cpp b/flang/runtime/file.cpp index 341702d..6823b19 100644 --- a/flang/runtime/file.cpp +++ b/flang/runtime/file.cpp @@ -18,6 +18,7 @@ #include #include #else +#include #include #endif @@ -84,8 +85,7 @@ void OpenFile::Open(OpenStatus status, std::optional action, fd_ = openfile_mkstemp(handler); } else { if (!path_.get()) { - handler.SignalError( - "FILE= is required unless STATUS='OLD' and unit is connected"); + handler.SignalError("FILE= is required"); return; } int flags{0}; @@ -134,8 +134,18 @@ void OpenFile::Open(OpenStatus status, std::optional action, mayWrite_ = *action != Action::Read; if (status == OpenStatus::Old || status == OpenStatus::Unknown) { knownSize_.reset(); +#ifndef _WIN32 + struct stat buf; + if (::fstat(fd_, &buf) == 0) { + mayPosition_ = S_ISREG(buf.st_mode); + knownSize_ = buf.st_size; + } +#else // TODO: _WIN32 + mayPosition_ = true; +#endif } else { knownSize_ = 0; + mayPosition_ = true; } } @@ -385,4 +395,11 @@ int OpenFile::PendingResult(const Terminator &terminator, int iostat) { } bool IsATerminal(int fd) { return ::isatty(fd); } + +bool IsExtant(const char *path) { return ::access(path, F_OK) == 0; } +bool MayRead(const char *path) { return ::access(path, R_OK) == 0; } +bool MayWrite(const char *path) { return ::access(path, W_OK) == 0; } +bool MayReadAndWrite(const char *path) { + return ::access(path, R_OK | W_OK) == 0; +} } // namespace Fortran::runtime::io diff --git a/flang/runtime/file.h b/flang/runtime/file.h index 1d25a91..7e7b27c 100644 --- a/flang/runtime/file.h +++ b/flang/runtime/file.h @@ -95,5 +95,9 @@ private: }; bool IsATerminal(int fd); +bool IsExtant(const char *path); +bool MayRead(const char *path); +bool MayWrite(const char *path); +bool MayReadAndWrite(const char *path); } // namespace Fortran::runtime::io #endif // FORTRAN_RUNTIME_FILE_H_ -- 2.7.4