1 //===-- runtime/file.cpp ----------------------------------------*- 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 #include "file.h"
10 #include "magic-numbers.h"
11 #include "memory.h"
12 #include <algorithm>
13 #include <cerrno>
14 #include <cstring>
15 #include <fcntl.h>
16 #include <stdlib.h>
17 #include <sys/stat.h>
18 #ifdef _WIN32
19 #define NOMINMAX
20 #include <io.h>
21 #include <windows.h>
22 #else
23 #include <unistd.h>
24 #endif
25 
26 namespace Fortran::runtime::io {
27 
28 void OpenFile::set_path(OwningPtr<char> &&path, std::size_t bytes) {
29   path_ = std::move(path);
30   pathLength_ = bytes;
31 }
32 
33 static int openfile_mkstemp(IoErrorHandler &handler) {
34 #ifdef _WIN32
35   const unsigned int uUnique{0};
36   // GetTempFileNameA needs a directory name < MAX_PATH-14 characters in length.
37   // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettempfilenamea
38   char tempDirName[MAX_PATH - 14];
39   char tempFileName[MAX_PATH];
40   unsigned long nBufferLength{sizeof(tempDirName)};
41   nBufferLength = ::GetTempPathA(nBufferLength, tempDirName);
42   if (nBufferLength > sizeof(tempDirName) || nBufferLength == 0) {
43     return -1;
44   }
45   if (::GetTempFileNameA(tempDirName, "Fortran", uUnique, tempFileName) == 0) {
46     return -1;
47   }
48   int fd{::_open(tempFileName, _O_CREAT | _O_TEMPORARY, _S_IREAD | _S_IWRITE)};
49 #else
50   char path[]{"/tmp/Fortran-Scratch-XXXXXX"};
51   int fd{::mkstemp(path)};
52 #endif
53   if (fd < 0) {
54     handler.SignalErrno();
55   }
56 #ifndef _WIN32
57   ::unlink(path);
58 #endif
59   return fd;
60 }
61 
62 void OpenFile::Open(OpenStatus status, std::optional<Action> action,
63     Position position, IoErrorHandler &handler) {
64   if (fd_ >= 0 &&
65       (status == OpenStatus::Old || status == OpenStatus::Unknown)) {
66     return;
67   }
68   if (fd_ >= 0) {
69     if (fd_ <= 2) {
70       // don't actually close a standard file descriptor, we might need it
71     } else {
72       if (::close(fd_) != 0) {
73         handler.SignalErrno();
74       }
75     }
76     fd_ = -1;
77   }
78   if (status == OpenStatus::Scratch) {
79     if (path_.get()) {
80       handler.SignalError("FILE= must not appear with STATUS='SCRATCH'");
81       path_.reset();
82     }
83     if (!action) {
84       action = Action::ReadWrite;
85     }
86     fd_ = openfile_mkstemp(handler);
87   } else {
88     if (!path_.get()) {
89       handler.SignalError("FILE= is required");
90       return;
91     }
92     int flags{0};
93     if (status != OpenStatus::Old) {
94       flags |= O_CREAT;
95     }
96     if (status == OpenStatus::New) {
97       flags |= O_EXCL;
98     } else if (status == OpenStatus::Replace) {
99       flags |= O_TRUNC;
100     }
101     if (!action) {
102       // Try to open read/write, back off to read-only on failure
103       fd_ = ::open(path_.get(), flags | O_RDWR, 0600);
104       if (fd_ >= 0) {
105         action = Action::ReadWrite;
106       } else {
107         action = Action::Read;
108       }
109     }
110     if (fd_ < 0) {
111       switch (*action) {
112       case Action::Read:
113         flags |= O_RDONLY;
114         break;
115       case Action::Write:
116         flags |= O_WRONLY;
117         break;
118       case Action::ReadWrite:
119         flags |= O_RDWR;
120         break;
121       }
122       fd_ = ::open(path_.get(), flags, 0600);
123       if (fd_ < 0) {
124         handler.SignalErrno();
125       }
126     }
127   }
128   RUNTIME_CHECK(handler, action.has_value());
129   pending_.reset();
130   if (position == Position::Append && !RawSeekToEnd()) {
131     handler.SignalErrno();
132   }
133   isTerminal_ = ::isatty(fd_) == 1;
134   mayRead_ = *action != Action::Write;
135   mayWrite_ = *action != Action::Read;
136   if (status == OpenStatus::Old || status == OpenStatus::Unknown) {
137     knownSize_.reset();
138 #ifndef _WIN32
139     struct stat buf;
140     if (::fstat(fd_, &buf) == 0) {
141       mayPosition_ = S_ISREG(buf.st_mode);
142       knownSize_ = buf.st_size;
143     }
144 #else // TODO: _WIN32
145     mayPosition_ = true;
146 #endif
147   } else {
148     knownSize_ = 0;
149     mayPosition_ = true;
150   }
151 }
152 
153 void OpenFile::Predefine(int fd) {
154   fd_ = fd;
155   path_.reset();
156   pathLength_ = 0;
157   position_ = 0;
158   knownSize_.reset();
159   nextId_ = 0;
160   pending_.reset();
161   mayRead_ = fd == 0;
162   mayWrite_ = fd != 0;
163   mayPosition_ = false;
164 }
165 
166 void OpenFile::Close(CloseStatus status, IoErrorHandler &handler) {
167   CheckOpen(handler);
168   pending_.reset();
169   knownSize_.reset();
170   switch (status) {
171   case CloseStatus::Keep:
172     break;
173   case CloseStatus::Delete:
174     if (path_.get()) {
175       ::unlink(path_.get());
176     }
177     break;
178   }
179   path_.reset();
180   if (fd_ >= 0) {
181     if (::close(fd_) != 0) {
182       handler.SignalErrno();
183     }
184     fd_ = -1;
185   }
186 }
187 
188 std::size_t OpenFile::Read(FileOffset at, char *buffer, std::size_t minBytes,
189     std::size_t maxBytes, IoErrorHandler &handler) {
190   if (maxBytes == 0) {
191     return 0;
192   }
193   CheckOpen(handler);
194   if (!Seek(at, handler)) {
195     return 0;
196   }
197   minBytes = std::min(minBytes, maxBytes);
198   std::size_t got{0};
199   while (got < minBytes) {
200     auto chunk{::read(fd_, buffer + got, maxBytes - got)};
201     if (chunk == 0) {
202       break;
203     } else if (chunk < 0) {
204       auto err{errno};
205       if (err != EAGAIN && err != EWOULDBLOCK && err != EINTR) {
206         handler.SignalError(err);
207         break;
208       }
209     } else {
210       position_ += chunk;
211       got += chunk;
212     }
213   }
214   return got;
215 }
216 
217 std::size_t OpenFile::Write(FileOffset at, const char *buffer,
218     std::size_t bytes, IoErrorHandler &handler) {
219   if (bytes == 0) {
220     return 0;
221   }
222   CheckOpen(handler);
223   if (!Seek(at, handler)) {
224     return 0;
225   }
226   std::size_t put{0};
227   while (put < bytes) {
228     auto chunk{::write(fd_, buffer + put, bytes - put)};
229     if (chunk >= 0) {
230       position_ += chunk;
231       put += chunk;
232     } else {
233       auto err{errno};
234       if (err != EAGAIN && err != EWOULDBLOCK && err != EINTR) {
235         handler.SignalError(err);
236         break;
237       }
238     }
239   }
240   if (knownSize_ && position_ > *knownSize_) {
241     knownSize_ = position_;
242   }
243   return put;
244 }
245 
246 inline static int openfile_ftruncate(int fd, OpenFile::FileOffset at) {
247 #ifdef _WIN32
248   return !::_chsize(fd, at);
249 #else
250   return ::ftruncate(fd, at);
251 #endif
252 }
253 
254 void OpenFile::Truncate(FileOffset at, IoErrorHandler &handler) {
255   CheckOpen(handler);
256   if (!knownSize_ || *knownSize_ != at) {
257     if (openfile_ftruncate(fd_, at) != 0) {
258       handler.SignalErrno();
259     }
260     knownSize_ = at;
261   }
262 }
263 
264 // The operation is performed immediately; the results are saved
265 // to be claimed by a later WAIT statement.
266 // TODO: True asynchronicity
267 int OpenFile::ReadAsynchronously(
268     FileOffset at, char *buffer, std::size_t bytes, IoErrorHandler &handler) {
269   CheckOpen(handler);
270   int iostat{0};
271   for (std::size_t got{0}; got < bytes;) {
272 #if _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L
273     auto chunk{::pread(fd_, buffer + got, bytes - got, at)};
274 #else
275     auto chunk{Seek(at, handler) ? ::read(fd_, buffer + got, bytes - got) : -1};
276 #endif
277     if (chunk == 0) {
278       iostat = FORTRAN_RUNTIME_IOSTAT_END;
279       break;
280     }
281     if (chunk < 0) {
282       auto err{errno};
283       if (err != EAGAIN && err != EWOULDBLOCK && err != EINTR) {
284         iostat = err;
285         break;
286       }
287     } else {
288       at += chunk;
289       got += chunk;
290     }
291   }
292   return PendingResult(handler, iostat);
293 }
294 
295 // TODO: True asynchronicity
296 int OpenFile::WriteAsynchronously(FileOffset at, const char *buffer,
297     std::size_t bytes, IoErrorHandler &handler) {
298   CheckOpen(handler);
299   int iostat{0};
300   for (std::size_t put{0}; put < bytes;) {
301 #if _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L
302     auto chunk{::pwrite(fd_, buffer + put, bytes - put, at)};
303 #else
304     auto chunk{
305         Seek(at, handler) ? ::write(fd_, buffer + put, bytes - put) : -1};
306 #endif
307     if (chunk >= 0) {
308       at += chunk;
309       put += chunk;
310     } else {
311       auto err{errno};
312       if (err != EAGAIN && err != EWOULDBLOCK && err != EINTR) {
313         iostat = err;
314         break;
315       }
316     }
317   }
318   return PendingResult(handler, iostat);
319 }
320 
321 void OpenFile::Wait(int id, IoErrorHandler &handler) {
322   std::optional<int> ioStat;
323   Pending *prev{nullptr};
324   for (Pending *p{pending_.get()}; p; p = (prev = p)->next.get()) {
325     if (p->id == id) {
326       ioStat = p->ioStat;
327       if (prev) {
328         prev->next.reset(p->next.release());
329       } else {
330         pending_.reset(p->next.release());
331       }
332       break;
333     }
334   }
335   if (ioStat) {
336     handler.SignalError(*ioStat);
337   }
338 }
339 
340 void OpenFile::WaitAll(IoErrorHandler &handler) {
341   while (true) {
342     int ioStat;
343     if (pending_) {
344       ioStat = pending_->ioStat;
345       pending_.reset(pending_->next.release());
346     } else {
347       return;
348     }
349     handler.SignalError(ioStat);
350   }
351 }
352 
353 void OpenFile::CheckOpen(const Terminator &terminator) {
354   RUNTIME_CHECK(terminator, fd_ >= 0);
355 }
356 
357 bool OpenFile::Seek(FileOffset at, IoErrorHandler &handler) {
358   if (at == position_) {
359     return true;
360   } else if (RawSeek(at)) {
361     position_ = at;
362     return true;
363   } else {
364     handler.SignalErrno();
365     return false;
366   }
367 }
368 
369 bool OpenFile::RawSeek(FileOffset at) {
370 #ifdef _LARGEFILE64_SOURCE
371   return ::lseek64(fd_, at, SEEK_SET) == at;
372 #else
373   return ::lseek(fd_, at, SEEK_SET) == at;
374 #endif
375 }
376 
377 bool OpenFile::RawSeekToEnd() {
378 #ifdef _LARGEFILE64_SOURCE
379   std::int64_t at{::lseek64(fd_, 0, SEEK_END)};
380 #else
381   std::int64_t at{::lseek(fd_, 0, SEEK_END)};
382 #endif
383   if (at >= 0) {
384     knownSize_ = at;
385     return true;
386   } else {
387     return false;
388   }
389 }
390 
391 int OpenFile::PendingResult(const Terminator &terminator, int iostat) {
392   int id{nextId_++};
393   pending_ = New<Pending>{terminator}(id, iostat, std::move(pending_));
394   return id;
395 }
396 
397 bool IsATerminal(int fd) { return ::isatty(fd); }
398 
399 #ifdef WIN32
400 // Access flags are normally defined in unistd.h, which unavailable under
401 // Windows. Instead, define the flags as documented at
402 // https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/access-waccess
403 #define F_OK 00
404 #define W_OK 02
405 #define R_OK 04
406 #endif
407 
408 bool IsExtant(const char *path) { return ::access(path, F_OK) == 0; }
409 bool MayRead(const char *path) { return ::access(path, R_OK) == 0; }
410 bool MayWrite(const char *path) { return ::access(path, W_OK) == 0; }
411 bool MayReadAndWrite(const char *path) {
412   return ::access(path, R_OK | W_OK) == 0;
413 }
414 } // namespace Fortran::runtime::io
415