[flang-commits] [flang] d8334c4 - [flang] Acquire file accessibility, size, positioning
peter klausler via flang-commits
flang-commits at lists.llvm.org
Mon Aug 3 16:43:12 PDT 2020
Author: peter klausler
Date: 2020-08-03T16:42:48-07:00
New Revision: d8334c43606a08dc13a69d0993dc7a52d5c0fe56
URL: https://github.com/llvm/llvm-project/commit/d8334c43606a08dc13a69d0993dc7a52d5c0fe56
DIFF: https://github.com/llvm/llvm-project/commit/d8334c43606a08dc13a69d0993dc7a52d5c0fe56.diff
LOG: [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
Added:
Modified:
flang/runtime/file.cpp
flang/runtime/file.h
Removed:
################################################################################
diff --git a/flang/runtime/file.cpp b/flang/runtime/file.cpp
index 341702df995b..6823b19e5791 100644
--- a/flang/runtime/file.cpp
+++ b/flang/runtime/file.cpp
@@ -18,6 +18,7 @@
#include <io.h>
#include <windows.h>
#else
+#include <sys/stat.h>
#include <unistd.h>
#endif
@@ -84,8 +85,7 @@ void OpenFile::Open(OpenStatus status, std::optional<Action> 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> 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 1d25a91558a4..7e7b27c4be2a 100644
--- a/flang/runtime/file.h
+++ b/flang/runtime/file.h
@@ -95,5 +95,9 @@ class OpenFile {
};
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_
More information about the flang-commits
mailing list