xref: /llvm-project-15.0.7/flang/runtime/file.h (revision fbdcfcd4)
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 
43   bool IsOpen() const { return fd_ >= 0; }
44   void Open(OpenStatus, Position, IoErrorHandler &);
45   void Predefine(int fd);
46   void Close(CloseStatus, IoErrorHandler &);
47 
48   // Reads data into memory; returns amount acquired.  Synchronous.
49   // Partial reads (less than minBytes) signify end-of-file.  If the
50   // buffer is larger than minBytes, and extra returned data will be
51   // preserved for future consumption, set maxBytes larger than minBytes
52   // to reduce system calls  This routine handles EAGAIN/EWOULDBLOCK and EINTR.
53   std::size_t Read(FileOffset, char *, std::size_t minBytes,
54       std::size_t maxBytes, IoErrorHandler &);
55 
56   // Writes data.  Synchronous.  Partial writes indicate program-handled
57   // error conditions.
58   std::size_t Write(FileOffset, const char *, std::size_t, IoErrorHandler &);
59 
60   // Truncates the file
61   void Truncate(FileOffset, IoErrorHandler &);
62 
63   // Asynchronous transfers
64   int ReadAsynchronously(FileOffset, char *, std::size_t, IoErrorHandler &);
65   int WriteAsynchronously(
66       FileOffset, const char *, std::size_t, IoErrorHandler &);
67   void Wait(int id, IoErrorHandler &);
68   void WaitAll(IoErrorHandler &);
69 
70 private:
71   struct Pending {
72     int id;
73     int ioStat{0};
74     OwningPtr<Pending> next;
75   };
76 
77   void CheckOpen(const Terminator &);
78   bool Seek(FileOffset, IoErrorHandler &);
79   bool RawSeek(FileOffset);
80   bool RawSeekToEnd();
81   int PendingResult(const Terminator &, int);
82 
83   int fd_{-1};
84   OwningPtr<char> path_;
85   std::size_t pathLength_;
86   bool mayRead_{false};
87   bool mayWrite_{false};
88   bool mayPosition_{false};
89   bool mayAsynchronous_{false};
90   FileOffset position_{0};
91   std::optional<FileOffset> knownSize_;
92   bool isTerminal_{false};
93 
94   int nextId_;
95   OwningPtr<Pending> pending_;
96 };
97 } // namespace Fortran::runtime::io
98 #endif // FORTRAN_RUNTIME_FILE_H_
99