1 //===-- runtime/unit.cpp --------------------------------------------------===//
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 "unit.h"
10 #include "io-error.h"
11 #include "lock.h"
12 #include "unit-map.h"
13 #include <cstdio>
14 #include <limits>
15 #include <utility>
16 
17 namespace Fortran::runtime::io {
18 
19 // The per-unit data structures are created on demand so that Fortran I/O
20 // should work without a Fortran main program.
21 static Lock unitMapLock;
22 static UnitMap *unitMap{nullptr};
23 static ExternalFileUnit *defaultInput{nullptr}; // unit 5
24 static ExternalFileUnit *defaultOutput{nullptr}; // unit 6
25 static ExternalFileUnit *errorOutput{nullptr}; // unit 0 extension
26 
27 void FlushOutputOnCrash(const Terminator &terminator) {
28   if (!defaultOutput && !errorOutput) {
29     return;
30   }
31   IoErrorHandler handler{terminator};
32   handler.HasIoStat(); // prevent nested crash if flush has error
33   CriticalSection critical{unitMapLock};
34   if (defaultOutput) {
35     defaultOutput->FlushOutput(handler);
36   }
37   if (errorOutput) {
38     errorOutput->FlushOutput(handler);
39   }
40 }
41 
42 ExternalFileUnit *ExternalFileUnit::LookUp(int unit) {
43   return GetUnitMap().LookUp(unit);
44 }
45 
46 ExternalFileUnit &ExternalFileUnit::LookUpOrCrash(
47     int unit, const Terminator &terminator) {
48   ExternalFileUnit *file{LookUp(unit)};
49   if (!file) {
50     terminator.Crash("%d is not an open I/O unit number", unit);
51   }
52   return *file;
53 }
54 
55 ExternalFileUnit &ExternalFileUnit::LookUpOrCreate(
56     int unit, const Terminator &terminator, bool &wasExtant) {
57   return GetUnitMap().LookUpOrCreate(unit, terminator, wasExtant);
58 }
59 
60 ExternalFileUnit &ExternalFileUnit::LookUpOrCreateAnonymous(int unit,
61     Direction dir, std::optional<bool> isUnformatted,
62     const Terminator &terminator) {
63   bool exists{false};
64   ExternalFileUnit &result{
65       GetUnitMap().LookUpOrCreate(unit, terminator, exists)};
66   if (!exists) {
67     IoErrorHandler handler{terminator};
68     result.OpenAnonymousUnit(
69         dir == Direction::Input ? OpenStatus::Unknown : OpenStatus::Replace,
70         Action::ReadWrite, Position::Rewind, Convert::Native, handler);
71     result.isUnformatted = isUnformatted;
72   }
73   return result;
74 }
75 
76 ExternalFileUnit *ExternalFileUnit::LookUp(const char *path) {
77   return GetUnitMap().LookUp(path);
78 }
79 
80 ExternalFileUnit &ExternalFileUnit::CreateNew(
81     int unit, const Terminator &terminator) {
82   bool wasExtant{false};
83   ExternalFileUnit &result{
84       GetUnitMap().LookUpOrCreate(unit, terminator, wasExtant)};
85   RUNTIME_CHECK(terminator, !wasExtant);
86   return result;
87 }
88 
89 ExternalFileUnit *ExternalFileUnit::LookUpForClose(int unit) {
90   return GetUnitMap().LookUpForClose(unit);
91 }
92 
93 ExternalFileUnit &ExternalFileUnit::NewUnit(
94     const Terminator &terminator, bool forChildIo) {
95   ExternalFileUnit &unit{GetUnitMap().NewUnit(terminator)};
96   unit.createdForInternalChildIo_ = forChildIo;
97   return unit;
98 }
99 
100 void ExternalFileUnit::OpenUnit(std::optional<OpenStatus> status,
101     std::optional<Action> action, Position position, OwningPtr<char> &&newPath,
102     std::size_t newPathLength, Convert convert, IoErrorHandler &handler) {
103   if (executionEnvironment.conversion != Convert::Unknown) {
104     convert = executionEnvironment.conversion;
105   }
106   swapEndianness_ = convert == Convert::Swap ||
107       (convert == Convert::LittleEndian && !isHostLittleEndian) ||
108       (convert == Convert::BigEndian && isHostLittleEndian);
109   if (IsConnected()) {
110     bool isSamePath{newPath.get() && path() && pathLength() == newPathLength &&
111         std::memcmp(path(), newPath.get(), newPathLength) == 0};
112     if (status && *status != OpenStatus::Old && isSamePath) {
113       handler.SignalError("OPEN statement for connected unit may not have "
114                           "explicit STATUS= other than 'OLD'");
115       return;
116     }
117     if (!newPath.get() || isSamePath) {
118       // OPEN of existing unit, STATUS='OLD' or unspecified, not new FILE=
119       newPath.reset();
120       return;
121     }
122     // Otherwise, OPEN on open unit with new FILE= implies CLOSE
123     DoImpliedEndfile(handler);
124     FlushOutput(handler);
125     Close(CloseStatus::Keep, handler);
126   }
127   set_path(std::move(newPath), newPathLength);
128   Open(status.value_or(OpenStatus::Unknown), action, position, handler);
129   auto totalBytes{knownSize()};
130   if (access == Access::Direct) {
131     if (!openRecl) {
132       handler.SignalError(IostatOpenBadRecl,
133           "OPEN(UNIT=%d,ACCESS='DIRECT'): record length is not known",
134           unitNumber());
135     } else if (*openRecl <= 0) {
136       handler.SignalError(IostatOpenBadRecl,
137           "OPEN(UNIT=%d,ACCESS='DIRECT',RECL=%jd): record length is invalid",
138           unitNumber(), static_cast<std::intmax_t>(*openRecl));
139     } else if (totalBytes && (*totalBytes % *openRecl != 0)) {
140       handler.SignalError(IostatOpenBadAppend,
141           "OPEN(UNIT=%d,ACCESS='DIRECT',RECL=%jd): record length is not an "
142           "even divisor of the file size %jd",
143           unitNumber(), static_cast<std::intmax_t>(*openRecl),
144           static_cast<std::intmax_t>(*totalBytes));
145     }
146     recordLength = openRecl;
147   }
148   endfileRecordNumber.reset();
149   currentRecordNumber = 1;
150   if (totalBytes && access == Access::Direct && openRecl.value_or(0) > 0) {
151     endfileRecordNumber = 1 + (*totalBytes / *openRecl);
152   }
153   if (position == Position::Append && access != Access::Stream) {
154     if (!endfileRecordNumber) {
155       // Fake it so that we can backspace relative from the end
156       endfileRecordNumber = std::numeric_limits<std::int64_t>::max() - 2;
157     }
158     currentRecordNumber = *endfileRecordNumber;
159   }
160 }
161 
162 void ExternalFileUnit::OpenAnonymousUnit(std::optional<OpenStatus> status,
163     std::optional<Action> action, Position position, Convert convert,
164     IoErrorHandler &handler) {
165   // I/O to an unconnected unit reads/creates a local file, e.g. fort.7
166   std::size_t pathMaxLen{32};
167   auto path{SizedNew<char>{handler}(pathMaxLen)};
168   std::snprintf(path.get(), pathMaxLen, "fort.%d", unitNumber_);
169   OpenUnit(status, action, position, std::move(path), std::strlen(path.get()),
170       convert, handler);
171 }
172 
173 void ExternalFileUnit::CloseUnit(CloseStatus status, IoErrorHandler &handler) {
174   DoImpliedEndfile(handler);
175   FlushOutput(handler);
176   Close(status, handler);
177 }
178 
179 void ExternalFileUnit::DestroyClosed() {
180   GetUnitMap().DestroyClosed(*this); // destroys *this
181 }
182 
183 Iostat ExternalFileUnit::SetDirection(Direction direction) {
184   if (direction == Direction::Input) {
185     if (mayRead()) {
186       direction_ = Direction::Input;
187       return IostatOk;
188     } else {
189       return IostatReadFromWriteOnly;
190     }
191   } else {
192     if (mayWrite()) {
193       direction_ = Direction::Output;
194       return IostatOk;
195     } else {
196       return IostatWriteToReadOnly;
197     }
198   }
199 }
200 
201 UnitMap &ExternalFileUnit::GetUnitMap() {
202   if (unitMap) {
203     return *unitMap;
204   }
205   CriticalSection critical{unitMapLock};
206   if (unitMap) {
207     return *unitMap;
208   }
209   Terminator terminator{__FILE__, __LINE__};
210   IoErrorHandler handler{terminator};
211   UnitMap *newUnitMap{New<UnitMap>{terminator}().release()};
212 
213   bool wasExtant{false};
214   ExternalFileUnit &out{newUnitMap->LookUpOrCreate(6, terminator, wasExtant)};
215   RUNTIME_CHECK(terminator, !wasExtant);
216   out.Predefine(1);
217   handler.SignalError(out.SetDirection(Direction::Output));
218   out.isUnformatted = false;
219   defaultOutput = &out;
220 
221   ExternalFileUnit &in{newUnitMap->LookUpOrCreate(5, terminator, wasExtant)};
222   RUNTIME_CHECK(terminator, !wasExtant);
223   in.Predefine(0);
224   handler.SignalError(in.SetDirection(Direction::Input));
225   in.isUnformatted = false;
226   defaultInput = &in;
227 
228   ExternalFileUnit &error{newUnitMap->LookUpOrCreate(0, terminator, wasExtant)};
229   RUNTIME_CHECK(terminator, !wasExtant);
230   error.Predefine(2);
231   handler.SignalError(error.SetDirection(Direction::Output));
232   error.isUnformatted = false;
233   errorOutput = &error;
234 
235   unitMap = newUnitMap;
236   return *unitMap;
237 }
238 
239 void ExternalFileUnit::CloseAll(IoErrorHandler &handler) {
240   CriticalSection critical{unitMapLock};
241   if (unitMap) {
242     unitMap->CloseAll(handler);
243     FreeMemoryAndNullify(unitMap);
244   }
245   defaultOutput = nullptr;
246   defaultInput = nullptr;
247   errorOutput = nullptr;
248 }
249 
250 void ExternalFileUnit::FlushAll(IoErrorHandler &handler) {
251   CriticalSection critical{unitMapLock};
252   if (unitMap) {
253     unitMap->FlushAll(handler);
254   }
255 }
256 
257 static void SwapEndianness(
258     char *data, std::size_t bytes, std::size_t elementBytes) {
259   if (elementBytes > 1) {
260     auto half{elementBytes >> 1};
261     for (std::size_t j{0}; j + elementBytes <= bytes; j += elementBytes) {
262       for (std::size_t k{0}; k < half; ++k) {
263         std::swap(data[j + k], data[j + elementBytes - 1 - k]);
264       }
265     }
266   }
267 }
268 
269 bool ExternalFileUnit::Emit(const char *data, std::size_t bytes,
270     std::size_t elementBytes, IoErrorHandler &handler) {
271   auto furthestAfter{std::max(furthestPositionInRecord,
272       positionInRecord + static_cast<std::int64_t>(bytes))};
273   if (openRecl) {
274     // Check for fixed-length record overrun, but allow for
275     // sequential record termination.
276     int extra{0};
277     int header{0};
278     if (access == Access::Sequential) {
279       if (isUnformatted.value_or(false)) {
280         // record header + footer
281         header = static_cast<int>(sizeof(std::uint32_t));
282         extra = 2 * header;
283       } else {
284 #ifdef _WIN32
285         if (!isWindowsTextFile()) {
286           ++extra; // carriage return (CR)
287         }
288 #endif
289         ++extra; // newline (LF)
290       }
291     }
292     if (furthestAfter > extra + *openRecl) {
293       handler.SignalError(IostatRecordWriteOverrun,
294           "Attempt to write %zd bytes to position %jd in a fixed-size record "
295           "of %jd bytes",
296           bytes, static_cast<std::intmax_t>(positionInRecord - header),
297           static_cast<std::intmax_t>(*openRecl));
298       return false;
299     }
300   }
301   if (recordLength) {
302     // It is possible for recordLength to have a value now for a
303     // variable-length output record if the previous operation
304     // was a BACKSPACE or non advancing input statement.
305     recordLength.reset();
306     beganReadingRecord_ = false;
307   }
308   if (IsAfterEndfile()) {
309     handler.SignalError(IostatWriteAfterEndfile);
310     return false;
311   }
312   CheckDirectAccess(handler);
313   WriteFrame(frameOffsetInFile_, recordOffsetInFrame_ + furthestAfter, handler);
314   if (positionInRecord > furthestPositionInRecord) {
315     std::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord, ' ',
316         positionInRecord - furthestPositionInRecord);
317   }
318   char *to{Frame() + recordOffsetInFrame_ + positionInRecord};
319   std::memcpy(to, data, bytes);
320   if (swapEndianness_) {
321     SwapEndianness(to, bytes, elementBytes);
322   }
323   positionInRecord += bytes;
324   furthestPositionInRecord = furthestAfter;
325   return true;
326 }
327 
328 bool ExternalFileUnit::Receive(char *data, std::size_t bytes,
329     std::size_t elementBytes, IoErrorHandler &handler) {
330   RUNTIME_CHECK(handler, direction_ == Direction::Input);
331   auto furthestAfter{std::max(furthestPositionInRecord,
332       positionInRecord + static_cast<std::int64_t>(bytes))};
333   if (furthestAfter > recordLength.value_or(furthestAfter)) {
334     handler.SignalError(IostatRecordReadOverrun,
335         "Attempt to read %zd bytes at position %jd in a record of %jd bytes",
336         bytes, static_cast<std::intmax_t>(positionInRecord),
337         static_cast<std::intmax_t>(*recordLength));
338     return false;
339   }
340   auto need{recordOffsetInFrame_ + furthestAfter};
341   auto got{ReadFrame(frameOffsetInFile_, need, handler)};
342   if (got >= need) {
343     std::memcpy(data, Frame() + recordOffsetInFrame_ + positionInRecord, bytes);
344     if (swapEndianness_) {
345       SwapEndianness(data, bytes, elementBytes);
346     }
347     positionInRecord += bytes;
348     furthestPositionInRecord = furthestAfter;
349     return true;
350   } else {
351     handler.SignalEnd();
352     if (access == Access::Sequential) {
353       endfileRecordNumber = currentRecordNumber;
354     }
355     return false;
356   }
357 }
358 
359 std::size_t ExternalFileUnit::GetNextInputBytes(
360     const char *&p, IoErrorHandler &handler) {
361   RUNTIME_CHECK(handler, direction_ == Direction::Input);
362   std::size_t length{1};
363   if (auto recl{EffectiveRecordLength()}) {
364     if (positionInRecord < *recl) {
365       length = *recl - positionInRecord;
366     } else {
367       p = nullptr;
368       return 0;
369     }
370   }
371   p = FrameNextInput(handler, length);
372   return p ? length : 0;
373 }
374 
375 const char *ExternalFileUnit::FrameNextInput(
376     IoErrorHandler &handler, std::size_t bytes) {
377   RUNTIME_CHECK(handler, isUnformatted.has_value() && !*isUnformatted);
378   if (static_cast<std::int64_t>(positionInRecord + bytes) <=
379       recordLength.value_or(positionInRecord + bytes)) {
380     auto at{recordOffsetInFrame_ + positionInRecord};
381     auto need{static_cast<std::size_t>(at + bytes)};
382     auto got{ReadFrame(frameOffsetInFile_, need, handler)};
383     SetVariableFormattedRecordLength();
384     if (got >= need) {
385       return Frame() + at;
386     }
387     handler.SignalEnd();
388     if (access == Access::Sequential) {
389       endfileRecordNumber = currentRecordNumber;
390     }
391   }
392   return nullptr;
393 }
394 
395 bool ExternalFileUnit::SetVariableFormattedRecordLength() {
396   if (recordLength || access == Access::Direct) {
397     return true;
398   } else if (FrameLength() > recordOffsetInFrame_) {
399     const char *record{Frame() + recordOffsetInFrame_};
400     std::size_t bytes{FrameLength() - recordOffsetInFrame_};
401     if (const char *nl{
402             reinterpret_cast<const char *>(std::memchr(record, '\n', bytes))}) {
403       recordLength = nl - record;
404       if (*recordLength > 0 && record[*recordLength - 1] == '\r') {
405         --*recordLength;
406       }
407       return true;
408     }
409   }
410   return false;
411 }
412 
413 bool ExternalFileUnit::BeginReadingRecord(IoErrorHandler &handler) {
414   RUNTIME_CHECK(handler, direction_ == Direction::Input);
415   if (!beganReadingRecord_) {
416     beganReadingRecord_ = true;
417     if (access == Access::Direct) {
418       CheckDirectAccess(handler);
419       auto need{static_cast<std::size_t>(recordOffsetInFrame_ + *openRecl)};
420       auto got{ReadFrame(frameOffsetInFile_, need, handler)};
421       if (got >= need) {
422         recordLength = openRecl;
423       } else {
424         recordLength.reset();
425         handler.SignalEnd();
426       }
427     } else {
428       recordLength.reset();
429       if (IsAtEOF()) {
430         handler.SignalEnd();
431       } else {
432         RUNTIME_CHECK(handler, isUnformatted.has_value());
433         if (*isUnformatted) {
434           if (access == Access::Sequential) {
435             BeginSequentialVariableUnformattedInputRecord(handler);
436           }
437         } else { // formatted sequential or stream
438           BeginVariableFormattedInputRecord(handler);
439         }
440       }
441     }
442   }
443   RUNTIME_CHECK(handler,
444       recordLength.has_value() || !IsRecordFile() || handler.InError());
445   return !handler.InError();
446 }
447 
448 void ExternalFileUnit::FinishReadingRecord(IoErrorHandler &handler) {
449   RUNTIME_CHECK(handler, direction_ == Direction::Input && beganReadingRecord_);
450   beganReadingRecord_ = false;
451   if (handler.InError() && handler.GetIoStat() != IostatEor) {
452     // Avoid bogus crashes in END/ERR circumstances; but
453     // still increment the current record number so that
454     // an attempted read of an endfile record, followed by
455     // a BACKSPACE, will still be at EOF.
456     ++currentRecordNumber;
457   } else if (IsRecordFile()) {
458     RUNTIME_CHECK(handler, recordLength.has_value());
459     recordOffsetInFrame_ += *recordLength;
460     if (access != Access::Direct) {
461       RUNTIME_CHECK(handler, isUnformatted.has_value());
462       recordLength.reset();
463       if (isUnformatted.value_or(false)) {
464         // Retain footer in frame for more efficient BACKSPACE
465         frameOffsetInFile_ += recordOffsetInFrame_;
466         recordOffsetInFrame_ = sizeof(std::uint32_t);
467       } else { // formatted
468         if (FrameLength() > recordOffsetInFrame_ &&
469             Frame()[recordOffsetInFrame_] == '\r') {
470           ++recordOffsetInFrame_;
471         }
472         if (FrameLength() > recordOffsetInFrame_ &&
473             Frame()[recordOffsetInFrame_] == '\n') {
474           ++recordOffsetInFrame_;
475         }
476         if (!pinnedFrame || mayPosition()) {
477           frameOffsetInFile_ += recordOffsetInFrame_;
478           recordOffsetInFrame_ = 0;
479         }
480       }
481     }
482     ++currentRecordNumber;
483   } else { // unformatted stream
484     furthestPositionInRecord =
485         std::max(furthestPositionInRecord, positionInRecord);
486     frameOffsetInFile_ += recordOffsetInFrame_ + furthestPositionInRecord;
487   }
488   BeginRecord();
489 }
490 
491 bool ExternalFileUnit::AdvanceRecord(IoErrorHandler &handler) {
492   if (direction_ == Direction::Input) {
493     FinishReadingRecord(handler);
494     return BeginReadingRecord(handler);
495   } else { // Direction::Output
496     bool ok{true};
497     RUNTIME_CHECK(handler, isUnformatted.has_value());
498     positionInRecord = furthestPositionInRecord;
499     if (access == Access::Direct) {
500       if (furthestPositionInRecord <
501           openRecl.value_or(furthestPositionInRecord)) {
502         // Pad remainder of fixed length record
503         WriteFrame(
504             frameOffsetInFile_, recordOffsetInFrame_ + *openRecl, handler);
505         std::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord,
506             isUnformatted.value_or(false) ? 0 : ' ',
507             *openRecl - furthestPositionInRecord);
508         furthestPositionInRecord = *openRecl;
509       }
510     } else if (*isUnformatted) {
511       if (access == Access::Sequential) {
512         // Append the length of a sequential unformatted variable-length record
513         // as its footer, then overwrite the reserved first four bytes of the
514         // record with its length as its header.  These four bytes were skipped
515         // over in BeginUnformattedIO<Output>().
516         // TODO: Break very large records up into subrecords with negative
517         // headers &/or footers
518         std::uint32_t length;
519         length = furthestPositionInRecord - sizeof length;
520         ok = ok &&
521             Emit(reinterpret_cast<const char *>(&length), sizeof length,
522                 sizeof length, handler);
523         positionInRecord = 0;
524         ok = ok &&
525             Emit(reinterpret_cast<const char *>(&length), sizeof length,
526                 sizeof length, handler);
527       } else {
528         // Unformatted stream: nothing to do
529       }
530     } else {
531       // Terminate formatted variable length record
532       const char *lineEnding{"\n"};
533       std::size_t lineEndingBytes{1};
534 #ifdef _WIN32
535       if (!isWindowsTextFile()) {
536         lineEnding = "\r\n";
537         lineEndingBytes = 2;
538       }
539 #endif
540       ok = ok && Emit(lineEnding, lineEndingBytes, 1, handler);
541     }
542     if (IsAfterEndfile()) {
543       return false;
544     }
545     CommitWrites();
546     ++currentRecordNumber;
547     if (access != Access::Direct) {
548       impliedEndfile_ = IsRecordFile();
549       if (IsAtEOF()) {
550         endfileRecordNumber.reset();
551       }
552     }
553     return ok;
554   }
555 }
556 
557 void ExternalFileUnit::BackspaceRecord(IoErrorHandler &handler) {
558   if (access == Access::Direct || !IsRecordFile()) {
559     handler.SignalError(IostatBackspaceNonSequential,
560         "BACKSPACE(UNIT=%d) on direct-access file or unformatted stream",
561         unitNumber());
562   } else {
563     if (IsAfterEndfile()) {
564       // BACKSPACE after explicit ENDFILE
565       currentRecordNumber = *endfileRecordNumber;
566     } else {
567       DoImpliedEndfile(handler);
568       if (frameOffsetInFile_ + recordOffsetInFrame_ > 0) {
569         --currentRecordNumber;
570         if (openRecl && access == Access::Direct) {
571           BackspaceFixedRecord(handler);
572         } else {
573           RUNTIME_CHECK(handler, isUnformatted.has_value());
574           if (isUnformatted.value_or(false)) {
575             BackspaceVariableUnformattedRecord(handler);
576           } else {
577             BackspaceVariableFormattedRecord(handler);
578           }
579         }
580       }
581     }
582     BeginRecord();
583   }
584 }
585 
586 void ExternalFileUnit::FlushOutput(IoErrorHandler &handler) {
587   if (!mayPosition()) {
588     auto frameAt{FrameAt()};
589     if (frameOffsetInFile_ >= frameAt &&
590         frameOffsetInFile_ <
591             static_cast<std::int64_t>(frameAt + FrameLength())) {
592       // A Flush() that's about to happen to a non-positionable file
593       // needs to advance frameOffsetInFile_ to prevent attempts at
594       // impossible seeks
595       CommitWrites();
596     }
597   }
598   Flush(handler);
599 }
600 
601 void ExternalFileUnit::FlushIfTerminal(IoErrorHandler &handler) {
602   if (isTerminal()) {
603     FlushOutput(handler);
604   }
605 }
606 
607 void ExternalFileUnit::Endfile(IoErrorHandler &handler) {
608   if (access == Access::Direct) {
609     handler.SignalError(IostatEndfileDirect,
610         "ENDFILE(UNIT=%d) on direct-access file", unitNumber());
611   } else if (!mayWrite()) {
612     handler.SignalError(IostatEndfileUnwritable,
613         "ENDFILE(UNIT=%d) on read-only file", unitNumber());
614   } else if (IsAfterEndfile()) {
615     // ENDFILE after ENDFILE
616   } else {
617     DoEndfile(handler);
618     if (access == Access::Sequential) {
619       // Explicit ENDFILE leaves position *after* the endfile record
620       RUNTIME_CHECK(handler, endfileRecordNumber.has_value());
621       currentRecordNumber = *endfileRecordNumber + 1;
622     }
623   }
624 }
625 
626 void ExternalFileUnit::Rewind(IoErrorHandler &handler) {
627   if (access == Access::Direct) {
628     handler.SignalError(IostatRewindNonSequential,
629         "REWIND(UNIT=%d) on non-sequential file", unitNumber());
630   } else {
631     SetPosition(0, handler);
632     currentRecordNumber = 1;
633   }
634 }
635 
636 void ExternalFileUnit::SetPosition(std::int64_t pos, IoErrorHandler &handler) {
637   DoImpliedEndfile(handler);
638   frameOffsetInFile_ = pos;
639   recordOffsetInFrame_ = 0;
640   if (access == Access::Direct) {
641     directAccessRecWasSet_ = true;
642   }
643   BeginRecord();
644 }
645 
646 void ExternalFileUnit::EndIoStatement() {
647   io_.reset();
648   u_.emplace<std::monostate>();
649   lock_.Drop();
650 }
651 
652 void ExternalFileUnit::BeginSequentialVariableUnformattedInputRecord(
653     IoErrorHandler &handler) {
654   std::int32_t header{0}, footer{0};
655   std::size_t need{recordOffsetInFrame_ + sizeof header};
656   std::size_t got{ReadFrame(frameOffsetInFile_, need, handler)};
657   // Try to emit informative errors to help debug corrupted files.
658   const char *error{nullptr};
659   if (got < need) {
660     if (got == recordOffsetInFrame_) {
661       handler.SignalEnd();
662     } else {
663       error = "Unformatted variable-length sequential file input failed at "
664               "record #%jd (file offset %jd): truncated record header";
665     }
666   } else {
667     std::memcpy(&header, Frame() + recordOffsetInFrame_, sizeof header);
668     recordLength = sizeof header + header; // does not include footer
669     need = recordOffsetInFrame_ + *recordLength + sizeof footer;
670     got = ReadFrame(frameOffsetInFile_, need, handler);
671     if (got < need) {
672       error = "Unformatted variable-length sequential file input failed at "
673               "record #%jd (file offset %jd): hit EOF reading record with "
674               "length %jd bytes";
675     } else {
676       std::memcpy(&footer, Frame() + recordOffsetInFrame_ + *recordLength,
677           sizeof footer);
678       if (footer != header) {
679         error = "Unformatted variable-length sequential file input failed at "
680                 "record #%jd (file offset %jd): record header has length %jd "
681                 "that does not match record footer (%jd)";
682       }
683     }
684   }
685   if (error) {
686     handler.SignalError(error, static_cast<std::intmax_t>(currentRecordNumber),
687         static_cast<std::intmax_t>(frameOffsetInFile_),
688         static_cast<std::intmax_t>(header), static_cast<std::intmax_t>(footer));
689     // TODO: error recovery
690   }
691   positionInRecord = sizeof header;
692 }
693 
694 void ExternalFileUnit::BeginVariableFormattedInputRecord(
695     IoErrorHandler &handler) {
696   if (this == defaultInput) {
697     if (defaultOutput) {
698       defaultOutput->FlushOutput(handler);
699     }
700     if (errorOutput) {
701       errorOutput->FlushOutput(handler);
702     }
703   }
704   std::size_t length{0};
705   do {
706     std::size_t need{length + 1};
707     length =
708         ReadFrame(frameOffsetInFile_, recordOffsetInFrame_ + need, handler) -
709         recordOffsetInFrame_;
710     if (length < need) {
711       if (length > 0) {
712         // final record w/o \n
713         recordLength = length;
714       } else {
715         handler.SignalEnd();
716       }
717       break;
718     }
719   } while (!SetVariableFormattedRecordLength());
720 }
721 
722 void ExternalFileUnit::BackspaceFixedRecord(IoErrorHandler &handler) {
723   RUNTIME_CHECK(handler, openRecl.has_value());
724   if (frameOffsetInFile_ < *openRecl) {
725     handler.SignalError(IostatBackspaceAtFirstRecord);
726   } else {
727     frameOffsetInFile_ -= *openRecl;
728   }
729 }
730 
731 void ExternalFileUnit::BackspaceVariableUnformattedRecord(
732     IoErrorHandler &handler) {
733   std::int32_t header{0}, footer{0};
734   auto headerBytes{static_cast<std::int64_t>(sizeof header)};
735   frameOffsetInFile_ += recordOffsetInFrame_;
736   recordOffsetInFrame_ = 0;
737   if (frameOffsetInFile_ <= headerBytes) {
738     handler.SignalError(IostatBackspaceAtFirstRecord);
739     return;
740   }
741   // Error conditions here cause crashes, not file format errors, because the
742   // validity of the file structure before the current record will have been
743   // checked informatively in NextSequentialVariableUnformattedInputRecord().
744   std::size_t got{
745       ReadFrame(frameOffsetInFile_ - headerBytes, headerBytes, handler)};
746   if (static_cast<std::int64_t>(got) < headerBytes) {
747     handler.SignalError(IostatShortRead);
748     return;
749   }
750   std::memcpy(&footer, Frame(), sizeof footer);
751   recordLength = footer;
752   if (frameOffsetInFile_ < *recordLength + 2 * headerBytes) {
753     handler.SignalError(IostatBadUnformattedRecord);
754     return;
755   }
756   frameOffsetInFile_ -= *recordLength + 2 * headerBytes;
757   if (frameOffsetInFile_ >= headerBytes) {
758     frameOffsetInFile_ -= headerBytes;
759     recordOffsetInFrame_ = headerBytes;
760   }
761   auto need{static_cast<std::size_t>(
762       recordOffsetInFrame_ + sizeof header + *recordLength)};
763   got = ReadFrame(frameOffsetInFile_, need, handler);
764   if (got < need) {
765     handler.SignalError(IostatShortRead);
766     return;
767   }
768   std::memcpy(&header, Frame() + recordOffsetInFrame_, sizeof header);
769   if (header != *recordLength) {
770     handler.SignalError(IostatBadUnformattedRecord);
771     return;
772   }
773 }
774 
775 // There's no portable memrchr(), unfortunately, and strrchr() would
776 // fail on a record with a NUL, so we have to do it the hard way.
777 static const char *FindLastNewline(const char *str, std::size_t length) {
778   for (const char *p{str + length}; p-- > str;) {
779     if (*p == '\n') {
780       return p;
781     }
782   }
783   return nullptr;
784 }
785 
786 void ExternalFileUnit::BackspaceVariableFormattedRecord(
787     IoErrorHandler &handler) {
788   // File offset of previous record's newline
789   auto prevNL{
790       frameOffsetInFile_ + static_cast<std::int64_t>(recordOffsetInFrame_) - 1};
791   if (prevNL < 0) {
792     handler.SignalError(IostatBackspaceAtFirstRecord);
793     return;
794   }
795   while (true) {
796     if (frameOffsetInFile_ < prevNL) {
797       if (const char *p{
798               FindLastNewline(Frame(), prevNL - 1 - frameOffsetInFile_)}) {
799         recordOffsetInFrame_ = p - Frame() + 1;
800         recordLength = prevNL - (frameOffsetInFile_ + recordOffsetInFrame_);
801         break;
802       }
803     }
804     if (frameOffsetInFile_ == 0) {
805       recordOffsetInFrame_ = 0;
806       recordLength = prevNL;
807       break;
808     }
809     frameOffsetInFile_ -= std::min<std::int64_t>(frameOffsetInFile_, 1024);
810     auto need{static_cast<std::size_t>(prevNL + 1 - frameOffsetInFile_)};
811     auto got{ReadFrame(frameOffsetInFile_, need, handler)};
812     if (got < need) {
813       handler.SignalError(IostatShortRead);
814       return;
815     }
816   }
817   if (Frame()[recordOffsetInFrame_ + *recordLength] != '\n') {
818     handler.SignalError(IostatMissingTerminator);
819     return;
820   }
821   if (*recordLength > 0 &&
822       Frame()[recordOffsetInFrame_ + *recordLength - 1] == '\r') {
823     --*recordLength;
824   }
825 }
826 
827 void ExternalFileUnit::DoImpliedEndfile(IoErrorHandler &handler) {
828   if (impliedEndfile_) {
829     impliedEndfile_ = false;
830     if (access != Access::Direct && IsRecordFile() && mayPosition()) {
831       DoEndfile(handler);
832     }
833   }
834 }
835 
836 void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
837   if (access == Access::Sequential) {
838     endfileRecordNumber = currentRecordNumber;
839   }
840   FlushOutput(handler);
841   Truncate(frameOffsetInFile_ + recordOffsetInFrame_, handler);
842   BeginRecord();
843   impliedEndfile_ = false;
844 }
845 
846 void ExternalFileUnit::CommitWrites() {
847   frameOffsetInFile_ +=
848       recordOffsetInFrame_ + recordLength.value_or(furthestPositionInRecord);
849   recordOffsetInFrame_ = 0;
850   BeginRecord();
851 }
852 
853 bool ExternalFileUnit::CheckDirectAccess(IoErrorHandler &handler) {
854   if (access == Access::Direct) {
855     RUNTIME_CHECK(handler, openRecl);
856     if (!directAccessRecWasSet_) {
857       handler.SignalError(
858           "No REC= was specified for a data transfer with ACCESS='DIRECT'");
859       return false;
860     }
861   }
862   return true;
863 }
864 
865 ChildIo &ExternalFileUnit::PushChildIo(IoStatementState &parent) {
866   OwningPtr<ChildIo> current{std::move(child_)};
867   Terminator &terminator{parent.GetIoErrorHandler()};
868   OwningPtr<ChildIo> next{New<ChildIo>{terminator}(parent, std::move(current))};
869   child_.reset(next.release());
870   return *child_;
871 }
872 
873 void ExternalFileUnit::PopChildIo(ChildIo &child) {
874   if (child_.get() != &child) {
875     child.parent().GetIoErrorHandler().Crash(
876         "ChildIo being popped is not top of stack");
877   }
878   child_.reset(child.AcquirePrevious().release()); // deletes top child
879 }
880 
881 void ChildIo::EndIoStatement() {
882   io_.reset();
883   u_.emplace<std::monostate>();
884 }
885 
886 Iostat ChildIo::CheckFormattingAndDirection(
887     bool unformatted, Direction direction) {
888   bool parentIsInput{!parent_.get_if<IoDirectionState<Direction::Output>>()};
889   bool parentIsFormatted{parentIsInput
890           ? parent_.get_if<FormattedIoStatementState<Direction::Input>>() !=
891               nullptr
892           : parent_.get_if<FormattedIoStatementState<Direction::Output>>() !=
893               nullptr};
894   bool parentIsUnformatted{!parentIsFormatted};
895   if (unformatted != parentIsUnformatted) {
896     return unformatted ? IostatUnformattedChildOnFormattedParent
897                        : IostatFormattedChildOnUnformattedParent;
898   } else if (parentIsInput != (direction == Direction::Input)) {
899     return parentIsInput ? IostatChildOutputToInputParent
900                          : IostatChildInputFromOutputParent;
901   } else {
902     return IostatOk;
903   }
904 }
905 
906 } // namespace Fortran::runtime::io
907