FLANG

file.h
1 //===-- runtime/file.h ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // Raw system I/O wrappers
10
11 #ifndef FORTRAN_RUNTIME_FILE_H_
12 #define FORTRAN_RUNTIME_FILE_H_
13
14 #include "io-error.h"
15 #include "memory.h"
16 #include <cinttypes>
17 #include <optional>
18
19 namespace Fortran::runtime::io {
20
21 enum class OpenStatus { Old, New, Scratch, Replace, Unknown };
22 enum class CloseStatus { Keep, Delete };
23 enum class Position { AsIs, Rewind, Append };
24
25 class OpenFile {
26 public:
27 using FileOffset = std::int64_t;
28
29 const char *path() const { return path_.get(); }
30 void set_path(OwningPtr<char> &&, std::size_t bytes);
31 std::size_t pathLength() const { return pathLength_; }
32 bool mayRead() const { return mayRead_; }
33 void set_mayRead(bool yes) { mayRead_ = yes; }
34 bool mayWrite() const { return mayWrite_; }
35 void set_mayWrite(bool yes) { mayWrite_ = yes; }
36 bool mayAsynchronous() const { return mayAsynchronous_; }
37 void set_mayAsynchronous(bool yes) { mayAsynchronous_ = yes; }
38 bool mayPosition() const { return mayPosition_; }
39 void set_mayPosition(bool yes) { mayPosition_ = yes; }
40 FileOffset position() const { return position_; }
41 bool isTerminal() const { return isTerminal_; }
42 std::optional<FileOffset> knownSize() const { return knownSize_; }
43
44 bool IsOpen() const { return fd_ >= 0; }
45 void Open(OpenStatus, Position, IoErrorHandler &);
46 void Predefine(int fd);
47 void Close(CloseStatus, IoErrorHandler &);
48
49 // Reads data into memory; returns amount acquired. Synchronous.
50 // Partial reads (less than minBytes) signify end-of-file. If the
51 // buffer is larger than minBytes, and extra returned data will be
52 // preserved for future consumption, set maxBytes larger than minBytes
53 // to reduce system calls This routine handles EAGAIN/EWOULDBLOCK and EINTR.
54 std::size_t Read(FileOffset, char *, std::size_t minBytes,
55 std::size_t maxBytes, IoErrorHandler &);
56
57 // Writes data. Synchronous. Partial writes indicate program-handled
58 // error conditions.
59 std::size_t Write(FileOffset, const char *, std::size_t, IoErrorHandler &);
60
61 // Truncates the file
62 void Truncate(FileOffset, IoErrorHandler &);
63
64 // Asynchronous transfers
65 int ReadAsynchronously(FileOffset, char *, std::size_t, IoErrorHandler &);
66 int WriteAsynchronously(
67 FileOffset, const char *, std::size_t, IoErrorHandler &);
68 void Wait(int id, IoErrorHandler &);
69 void WaitAll(IoErrorHandler &);
70
71 private:
72 struct Pending {
73 int id;
74 int ioStat{0};
75 OwningPtr<Pending> next;
76 };
77
78 void CheckOpen(const Terminator &);
79 bool Seek(FileOffset, IoErrorHandler &);
80 bool RawSeek(FileOffset);
81 bool RawSeekToEnd();
82 int PendingResult(const Terminator &, int);
83
84 int fd_{-1};
85 OwningPtr<char> path_;
86 std::size_t pathLength_;
87 bool mayRead_{false};
88 bool mayWrite_{false};
89 bool mayPosition_{false};
90 bool mayAsynchronous_{false};
91 FileOffset position_{0};
92 std::optional<FileOffset> knownSize_;
93 bool isTerminal_{false};
94
95 int nextId_;
96 OwningPtr<Pending> pending_;
97 };
98
99 bool IsATerminal(int fd);
100 } // namespace Fortran::runtime::io
101 #endif // FORTRAN_RUNTIME_FILE_H_
Fortran::runtime::io::OpenFile
Definition: file.h:25
Fortran::runtime::Terminator
Definition: terminator.h:21
Fortran::runtime::io::IoErrorHandler
Definition: io-error.h:26