2020-01-24 08:10:00 +08:00
|
|
|
//===-- runtime/file.h ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Raw system I/O wrappers
|
|
|
|
|
|
|
|
#ifndef FORTRAN_RUNTIME_FILE_H_
|
|
|
|
#define FORTRAN_RUNTIME_FILE_H_
|
|
|
|
|
|
|
|
#include "io-error.h"
|
2021-09-02 07:00:53 +08:00
|
|
|
#include "flang/Runtime/memory.h"
|
2020-01-24 08:10:00 +08:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace Fortran::runtime::io {
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
enum class OpenStatus { Old, New, Scratch, Replace, Unknown };
|
|
|
|
enum class CloseStatus { Keep, Delete };
|
|
|
|
enum class Position { AsIs, Rewind, Append };
|
2020-07-18 02:24:29 +08:00
|
|
|
enum class Action { Read, Write, ReadWrite };
|
2020-02-05 08:55:45 +08:00
|
|
|
|
2020-01-24 08:10:00 +08:00
|
|
|
class OpenFile {
|
|
|
|
public:
|
2020-01-24 08:59:27 +08:00
|
|
|
using FileOffset = std::int64_t;
|
2020-01-24 08:10:00 +08:00
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
const char *path() const { return path_.get(); }
|
|
|
|
std::size_t pathLength() const { return pathLength_; }
|
2022-06-04 04:44:13 +08:00
|
|
|
void set_path(OwningPtr<char> &&, std::size_t bytes);
|
2020-01-24 08:59:27 +08:00
|
|
|
bool mayRead() const { return mayRead_; }
|
2020-02-05 08:55:45 +08:00
|
|
|
bool mayWrite() const { return mayWrite_; }
|
2020-07-18 02:24:29 +08:00
|
|
|
bool mayPosition() const { return mayPosition_; }
|
2020-02-05 08:55:45 +08:00
|
|
|
bool mayAsynchronous() const { return mayAsynchronous_; }
|
|
|
|
void set_mayAsynchronous(bool yes) { mayAsynchronous_ = yes; }
|
|
|
|
bool isTerminal() const { return isTerminal_; }
|
2022-01-29 07:34:28 +08:00
|
|
|
bool isWindowsTextFile() const { return isWindowsTextFile_; }
|
2020-07-04 01:56:08 +08:00
|
|
|
std::optional<FileOffset> knownSize() const { return knownSize_; }
|
2020-02-05 08:55:45 +08:00
|
|
|
|
2021-11-25 08:05:37 +08:00
|
|
|
bool IsConnected() const { return fd_ >= 0; }
|
2020-07-18 02:24:29 +08:00
|
|
|
void Open(OpenStatus, std::optional<Action>, Position, IoErrorHandler &);
|
2020-02-05 08:55:45 +08:00
|
|
|
void Predefine(int fd);
|
|
|
|
void Close(CloseStatus, IoErrorHandler &);
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-01-24 08:10:00 +08:00
|
|
|
// Reads data into memory; returns amount acquired. Synchronous.
|
|
|
|
// Partial reads (less than minBytes) signify end-of-file. If the
|
|
|
|
// buffer is larger than minBytes, and extra returned data will be
|
|
|
|
// preserved for future consumption, set maxBytes larger than minBytes
|
|
|
|
// to reduce system calls This routine handles EAGAIN/EWOULDBLOCK and EINTR.
|
2020-01-24 08:59:27 +08:00
|
|
|
std::size_t Read(FileOffset, char *, std::size_t minBytes,
|
|
|
|
std::size_t maxBytes, IoErrorHandler &);
|
2020-01-24 08:10:00 +08:00
|
|
|
|
|
|
|
// Writes data. Synchronous. Partial writes indicate program-handled
|
|
|
|
// error conditions.
|
2020-01-24 08:59:27 +08:00
|
|
|
std::size_t Write(FileOffset, const char *, std::size_t, IoErrorHandler &);
|
2020-01-24 08:10:00 +08:00
|
|
|
|
|
|
|
// Truncates the file
|
2020-01-24 08:59:27 +08:00
|
|
|
void Truncate(FileOffset, IoErrorHandler &);
|
2020-01-24 08:10:00 +08:00
|
|
|
|
|
|
|
// Asynchronous transfers
|
2020-01-24 08:59:27 +08:00
|
|
|
int ReadAsynchronously(FileOffset, char *, std::size_t, IoErrorHandler &);
|
|
|
|
int WriteAsynchronously(
|
|
|
|
FileOffset, const char *, std::size_t, IoErrorHandler &);
|
2020-01-24 08:10:00 +08:00
|
|
|
void Wait(int id, IoErrorHandler &);
|
|
|
|
void WaitAll(IoErrorHandler &);
|
|
|
|
|
2021-11-25 08:05:37 +08:00
|
|
|
// INQUIRE(POSITION=)
|
|
|
|
Position InquirePosition() const;
|
|
|
|
|
2020-01-24 08:10:00 +08:00
|
|
|
private:
|
|
|
|
struct Pending {
|
|
|
|
int id;
|
|
|
|
int ioStat{0};
|
|
|
|
OwningPtr<Pending> next;
|
|
|
|
};
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
void CheckOpen(const Terminator &);
|
2020-01-24 08:59:27 +08:00
|
|
|
bool Seek(FileOffset, IoErrorHandler &);
|
|
|
|
bool RawSeek(FileOffset);
|
2020-02-05 08:55:45 +08:00
|
|
|
bool RawSeekToEnd();
|
|
|
|
int PendingResult(const Terminator &, int);
|
2021-11-25 08:05:37 +08:00
|
|
|
void SetPosition(FileOffset pos) {
|
|
|
|
position_ = pos;
|
|
|
|
openPosition_.reset();
|
|
|
|
}
|
2021-12-01 08:21:11 +08:00
|
|
|
void CloseFd(IoErrorHandler &);
|
2020-01-24 08:10:00 +08:00
|
|
|
|
|
|
|
int fd_{-1};
|
|
|
|
OwningPtr<char> path_;
|
2020-01-24 08:59:27 +08:00
|
|
|
std::size_t pathLength_;
|
|
|
|
bool mayRead_{false};
|
|
|
|
bool mayWrite_{false};
|
|
|
|
bool mayPosition_{false};
|
2020-02-05 08:55:45 +08:00
|
|
|
bool mayAsynchronous_{false};
|
2021-11-25 08:05:37 +08:00
|
|
|
std::optional<Position> openPosition_; // from Open(); reset after positioning
|
2020-01-24 08:59:27 +08:00
|
|
|
FileOffset position_{0};
|
|
|
|
std::optional<FileOffset> knownSize_;
|
2020-02-05 08:55:45 +08:00
|
|
|
bool isTerminal_{false};
|
2022-01-29 07:34:28 +08:00
|
|
|
bool isWindowsTextFile_{false}; // expands LF to CR+LF on write
|
2020-02-05 08:55:45 +08:00
|
|
|
|
2020-01-24 08:10:00 +08:00
|
|
|
int nextId_;
|
|
|
|
OwningPtr<Pending> pending_;
|
|
|
|
};
|
2020-07-04 01:56:08 +08:00
|
|
|
|
|
|
|
bool IsATerminal(int fd);
|
2020-08-04 02:29:15 +08:00
|
|
|
bool IsExtant(const char *path);
|
|
|
|
bool MayRead(const char *path);
|
|
|
|
bool MayWrite(const char *path);
|
|
|
|
bool MayReadAndWrite(const char *path);
|
2022-05-29 23:21:59 +08:00
|
|
|
std::int64_t SizeInBytes(const char *path);
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::runtime::io
|
|
|
|
#endif // FORTRAN_RUNTIME_FILE_H_
|