1 //===-- runtime/file.h ------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Raw system I/O wrappers
11 #ifndef FORTRAN_RUNTIME_FILE_H_
12 #define FORTRAN_RUNTIME_FILE_H_
15 #include "flang/Runtime/memory.h"
19 namespace Fortran::runtime::io
{
21 enum class OpenStatus
{ Old
, New
, Scratch
, Replace
, Unknown
};
22 enum class CloseStatus
{ Keep
, Delete
};
23 enum class Position
{ AsIs
, Rewind
, Append
};
24 enum class Action
{ Read
, Write
, ReadWrite
};
28 using FileOffset
= std::int64_t;
30 const char *path() const { return path_
.get(); }
31 std::size_t pathLength() const { return pathLength_
; }
32 void set_path(OwningPtr
<char> &&, std::size_t bytes
);
33 bool mayRead() const { return mayRead_
; }
34 bool mayWrite() const { return mayWrite_
; }
35 bool mayPosition() const { return mayPosition_
; }
36 bool mayAsynchronous() const { return mayAsynchronous_
; }
37 void set_mayAsynchronous(bool yes
) { mayAsynchronous_
= yes
; }
38 bool isTerminal() const { return isTerminal_
; }
39 bool isWindowsTextFile() const { return isWindowsTextFile_
; }
40 std::optional
<FileOffset
> knownSize() const { return knownSize_
; }
42 bool IsConnected() const { return fd_
>= 0; }
43 void Open(OpenStatus
, std::optional
<Action
>, Position
, IoErrorHandler
&);
44 void Predefine(int fd
);
45 void Close(CloseStatus
, IoErrorHandler
&);
47 // Reads data into memory; returns amount acquired. Synchronous.
48 // Partial reads (less than minBytes) signify end-of-file. If the
49 // buffer is larger than minBytes, and extra returned data will be
50 // preserved for future consumption, set maxBytes larger than minBytes
51 // to reduce system calls This routine handles EAGAIN/EWOULDBLOCK and EINTR.
52 std::size_t Read(FileOffset
, char *, std::size_t minBytes
,
53 std::size_t maxBytes
, IoErrorHandler
&);
55 // Writes data. Synchronous. Partial writes indicate program-handled
57 std::size_t Write(FileOffset
, const char *, std::size_t, IoErrorHandler
&);
60 void Truncate(FileOffset
, IoErrorHandler
&);
62 // Asynchronous transfers
63 int ReadAsynchronously(FileOffset
, char *, std::size_t, IoErrorHandler
&);
64 int WriteAsynchronously(
65 FileOffset
, const char *, std::size_t, IoErrorHandler
&);
66 void Wait(int id
, IoErrorHandler
&);
67 void WaitAll(IoErrorHandler
&);
70 Position
InquirePosition() const;
76 OwningPtr
<Pending
> next
;
79 void CheckOpen(const Terminator
&);
80 bool Seek(FileOffset
, IoErrorHandler
&);
81 bool RawSeek(FileOffset
);
83 int PendingResult(const Terminator
&, int);
84 void SetPosition(FileOffset pos
) {
86 openPosition_
.reset();
88 void CloseFd(IoErrorHandler
&);
91 OwningPtr
<char> path_
;
92 std::size_t pathLength_
;
94 bool mayWrite_
{false};
95 bool mayPosition_
{false};
96 bool mayAsynchronous_
{false};
97 std::optional
<Position
> openPosition_
; // from Open(); reset after positioning
98 FileOffset position_
{0};
99 std::optional
<FileOffset
> knownSize_
;
100 bool isTerminal_
{false};
101 bool isWindowsTextFile_
{false}; // expands LF to CR+LF on write
104 OwningPtr
<Pending
> pending_
;
107 bool IsATerminal(int fd
);
108 bool IsExtant(const char *path
);
109 bool MayRead(const char *path
);
110 bool MayWrite(const char *path
);
111 bool MayReadAndWrite(const char *path
);
112 std::int64_t SizeInBytes(const char *path
);
113 } // namespace Fortran::runtime::io
114 #endif // FORTRAN_RUNTIME_FILE_H_