Skip to main content

ironwork_rt/
files.rs

1//! Files. ASSIGN names a DD, and only the operator maps a DD to a host file, as JCL does, so a
2//! program reaches no file it was not given. A binary DD holds z/OS records byte for byte:
3//! fixed-length records back to back, or variable-length records each behind a 4-byte RDW. A text
4//! DD holds UTF-8 lines, converted through the program's code page, and placed as a printer would
5//! place them ([`Open::print`]). Sequential files stream;
6//! indexed and relative files are held in memory (see [`Keyed`]).
7
8use crate::vocab::OpenMode;
9use std::cmp::Ordering;
10use std::collections::{BTreeMap, BTreeSet, HashMap};
11use std::fs::{File, OpenOptions};
12use std::io::{self, BufRead, BufReader, BufWriter, Read, Write};
13use std::ops::Bound;
14use std::path::{Path, PathBuf};
15use zarch::ebcdic::{self, CodePage};
16
17pub use crate::abend::FileStatus;
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub enum Format {
21    Fixed,
22    Variable,
23    Text,
24}
25
26impl Format {
27    pub fn from_keyword(word: &str) -> Option<Format> {
28        match word.to_ascii_lowercase().as_str() {
29            "text" => Some(Format::Text),
30            "fixed" | "f" => Some(Format::Fixed),
31            "variable" | "v" => Some(Format::Variable),
32            _ => None,
33        }
34    }
35}
36
37#[derive(Clone, Debug, PartialEq, Eq)]
38pub struct Dd {
39    pub path: PathBuf,
40    pub format: Option<Format>,
41    /// DISP=MOD: OPEN OUTPUT keeps the records already there and writes after them.
42    pub append: bool,
43}
44
45/// The DDs a run may use: given explicitly, or read from `DD_<NAME>` in the environment.
46#[derive(Clone, Debug, Default)]
47pub struct Dds {
48    given: HashMap<String, Dd>,
49    environment: bool,
50}
51
52fn dd_value(value: &str) -> Result<Dd, String> {
53    let (rest, append) = match value.rsplit_once(':') {
54        Some((rest, word)) if word.eq_ignore_ascii_case("mod") => (rest, true),
55        _ => (value, false),
56    };
57    let (path, format) = rest.rsplit_once(':').and_then(|(p, word)| Some((p, Some(Format::from_keyword(word)?)))).unwrap_or((rest, None));
58    if path.is_empty() {
59        return Err(format!("DD {value}: no path"));
60    }
61    Ok(Dd { path: PathBuf::from(path), format, append })
62}
63
64impl Dds {
65    /// DDs from `NAME=path[:text|:fixed|:variable][:mod]` specs, and from the environment when `environment`.
66    pub fn new(specs: &[String], environment: bool) -> Result<Self, String> {
67        let mut given = HashMap::new();
68        for spec in specs {
69            let (name, value) = spec.split_once('=').ok_or_else(|| format!("--dd {spec}: expected NAME=path"))?;
70            given.insert(name.to_ascii_uppercase(), dd_value(value)?);
71        }
72        Ok(Self { given, environment })
73    }
74
75    pub fn get(&self, name: &str) -> Option<Dd> {
76        if let Some(dd) = self.given.get(name) {
77            return Some(dd.clone());
78        }
79        if !self.environment {
80            return None;
81        }
82        std::env::var(format!("DD_{name}")).ok().and_then(|v| dd_value(&v).ok())
83    }
84}
85
86enum Handle {
87    Reader(BufReader<File>),
88    Writer(BufWriter<File>),
89    Keyed(Box<Keyed>),
90    Empty,
91}
92
93/// Where a key lies in a record.
94#[derive(Clone, Copy, Debug, PartialEq, Eq)]
95pub struct KeySpan {
96    pub offset: usize,
97    pub len: usize,
98}
99
100impl KeySpan {
101    pub fn of(&self, record: &[u8]) -> Vec<u8> {
102        let mut key: Vec<u8> = record.iter().skip(self.offset).take(self.len).copied().collect();
103        key.resize(self.len, ebcdic::SPACE);
104        key
105    }
106}
107
108/// How the records of a file held in memory are keyed. Record numbers and positions are held as
109/// eight big-endian bytes, so that they sort.
110#[derive(Clone, Debug, PartialEq, Eq)]
111pub enum Keying {
112    /// A sequential file opened I-O, by position.
113    Position,
114    /// A relative file, by record number. In the DD an empty slot is a record of no bytes, or of
115    /// zero bytes only.
116    Relative,
117    /// An indexed file: the prime key, then each alternate key and whether it allows duplicates.
118    Indexed { prime: KeySpan, alternates: Vec<(KeySpan, bool)> },
119}
120
121/// The highest relative record number: past it WRITE is a boundary violation (status 24). It
122/// stands in for the space a cluster is defined with, so that a stray record number cannot make
123/// CLOSE write millions of empty slots.
124pub const MAX_RELATIVE: u64 = 16_777_215;
125
126pub fn record_number(n: u64) -> Vec<u8> {
127    n.to_be_bytes().to_vec()
128}
129
130pub fn number_of(key: &[u8]) -> u64 {
131    u64::from_be_bytes(key.try_into().unwrap_or([0; 8]))
132}
133
134/// An alternate index entry: alternate key, arrival number, prime key. Duplicates come back in
135/// the order they arrived, as VSAM keeps them; records loaded from a DD arrive in prime-key order,
136/// as BLDINDEX builds an alternate index.
137type Entry = (Vec<u8>, u64, Vec<u8>);
138
139struct Alternate {
140    span: KeySpan,
141    duplicates: bool,
142    index: BTreeSet<Entry>,
143    arrival: HashMap<Vec<u8>, u64>,
144}
145
146impl Alternate {
147    fn holders<'a>(&'a self, value: &'a [u8]) -> impl Iterator<Item = &'a Entry> {
148        self.index.range((value.to_vec(), 0, Vec::new())..).take_while(move |(v, ..)| v == value)
149    }
150}
151
152/// The file position indicator.
153#[derive(Clone, Debug)]
154enum Cursor {
155    /// After OPEN: READ NEXT reads the first record by the prime key.
156    First,
157    /// A key of reference (0 the prime key, then each alternate), a place in its index, and
158    /// whether the record there is itself next (after START) or was the last read.
159    At { which: usize, at: Entry, inclusive: bool },
160    /// After an AT END condition or a START that found nothing: READ NEXT fails with status 46.
161    Undefined,
162}
163
164/// A record READ found.
165pub struct Found {
166    pub key: Vec<u8>,
167    pub record: Vec<u8>,
168    /// The next record by the alternate key of reference has the same key (status 02).
169    pub duplicate: bool,
170}
171
172/// An indexed or relative file, or a sequential file opened I-O, held in memory from OPEN to
173/// CLOSE. The DD holds the records in key (or record-number) order, as an IDCAMS REPRO unload of a
174/// KSDS or RRDS does, and CLOSE writes them back that way when they changed.
175pub struct Keyed {
176    pub keying: Keying,
177    records: BTreeMap<Vec<u8>, Vec<u8>>,
178    alternates: Vec<Alternate>,
179    arrivals: u64,
180    cursor: Cursor,
181    /// The key of the record the last statement on the file, a successful READ, returned: what a
182    /// sequential REWRITE or DELETE acts on.
183    pub last_read: Option<Vec<u8>>,
184    dirty: bool,
185    path: Option<PathBuf>,
186    record_len: usize,
187    page: &'static CodePage,
188    /// The open mark ([`open_mark`]) CLOSE removes.
189    mark: Option<PathBuf>,
190}
191
192impl Keyed {
193    fn new(keying: Keying, path: Option<PathBuf>, record_len: usize, page: &'static CodePage) -> Self {
194        let alternates = match &keying {
195            Keying::Indexed { alternates, .. } => {
196                alternates.iter().map(|&(span, duplicates)| Alternate { span, duplicates, index: BTreeSet::new(), arrival: HashMap::new() }).collect()
197            }
198            _ => Vec::new(),
199        };
200        Self { keying, records: BTreeMap::new(), alternates, arrivals: 0, cursor: Cursor::First, last_read: None, dirty: true, path, record_len, page, mark: None }
201    }
202
203    pub fn prime_key(&self, record: &[u8]) -> Option<Vec<u8>> {
204        match &self.keying {
205            Keying::Indexed { prime, .. } => Some(prime.of(record)),
206            _ => None,
207        }
208    }
209
210    pub fn highest_key(&self) -> Option<&Vec<u8>> {
211        self.records.keys().next_back()
212    }
213
214    pub fn record(&self, key: &[u8]) -> Option<&Vec<u8>> {
215        self.records.get(key)
216    }
217
218    fn key_len(&self, which: usize) -> usize {
219        match (&self.keying, which) {
220            (Keying::Indexed { prime, .. }, 0) => prime.len,
221            (Keying::Indexed { .. }, n) => self.alternates[n - 1].span.len,
222            _ => 8,
223        }
224    }
225
226    /// The record whose key `which` is `value`: for an alternate key, the first of any duplicates.
227    pub fn get(&self, which: usize, value: &[u8]) -> Option<Found> {
228        if which == 0 {
229            return self.records.get(value).map(|r| Found { key: value.to_vec(), record: r.clone(), duplicate: false });
230        }
231        let mut hits = self.alternates[which - 1].holders(value);
232        let key = hits.next()?.2.clone();
233        Some(Found { record: self.records[&key].clone(), key, duplicate: hits.next().is_some() })
234    }
235
236    /// Adds a record, or replaces the one with its key: Err with status 22 when it would take a
237    /// unique alternate key another record holds. Ok(true) when it shares an alternate key that
238    /// allows duplicates (status 02).
239    fn put(&mut self, key: Vec<u8>, record: Vec<u8>) -> Result<bool, FileStatus> {
240        let mut shared = false;
241        for alt in &self.alternates {
242            let value = alt.span.of(&record);
243            let others = alt.holders(&value).any(|(_, _, k)| *k != key);
244            if others && !alt.duplicates {
245                return Err(FileStatus::DuplicateKey);
246            }
247            shared |= others;
248        }
249        let old = self.records.get(&key).cloned();
250        let mut arrivals = self.arrivals;
251        for alt in &mut self.alternates {
252            let now = alt.span.of(&record);
253            if let Some(old) = &old {
254                let was = alt.span.of(old);
255                if was == now {
256                    continue;
257                }
258                let seq = alt.arrival[&key];
259                alt.index.remove(&(was, seq, key.clone()));
260            }
261            alt.index.insert((now, arrivals, key.clone()));
262            alt.arrival.insert(key.clone(), arrivals);
263            arrivals += 1;
264        }
265        self.arrivals = arrivals;
266        self.records.insert(key, record);
267        self.dirty = true;
268        Ok(shared)
269    }
270
271    /// Adds a record: Err with status 22 when its prime key, or a unique alternate key, is already
272    /// there; Ok(true) when it shares an alternate key that allows duplicates (status 02).
273    pub fn insert(&mut self, key: Vec<u8>, record: Vec<u8>) -> Result<bool, FileStatus> {
274        if self.records.contains_key(&key) {
275            return Err(FileStatus::DuplicateKey);
276        }
277        self.put(key, record)
278    }
279
280    /// Replaces a record: Err with status 23 when none has its key, 22 as for [`Keyed::insert`].
281    pub fn replace(&mut self, key: Vec<u8>, record: Vec<u8>) -> Result<bool, FileStatus> {
282        if !self.records.contains_key(&key) {
283            return Err(FileStatus::NotFound);
284        }
285        self.put(key, record)
286    }
287
288    pub fn remove(&mut self, key: &[u8]) -> Option<Vec<u8>> {
289        let record = self.records.remove(key)?;
290        for alt in &mut self.alternates {
291            if let Some(seq) = alt.arrival.remove(key) {
292                alt.index.remove(&(alt.span.of(&record), seq, key.to_vec()));
293            }
294        }
295        self.dirty = true;
296        Some(record)
297    }
298
299    /// START: positions at the first record whose key `which`, compared over the length of `value`
300    /// (a longer `value` is cut to the key's length), is `wanted` to `value`, or equal when
301    /// `or_equal`. False, and no position, when there is none.
302    pub fn start(&mut self, which: usize, wanted: Ordering, or_equal: bool, value: &[u8]) -> bool {
303        let value = &value[..value.len().min(self.key_len(which))];
304        let fits = |k: &[u8]| {
305            let o = k[..value.len().min(k.len())].cmp(value);
306            o == wanted || (or_equal && o == Ordering::Equal)
307        };
308        let from = (value.to_vec(), 0, Vec::new());
309        let found = match which {
310            0 => self.records.range(from.0..).map(|(k, _)| k).find(|k| fits(k)).map(|k| (k.clone(), 0, Vec::new())),
311            n => self.alternates[n - 1].index.range(from..).find(|(k, ..)| fits(k)).cloned(),
312        };
313        self.last_read = None;
314        match found {
315            Some(at) => {
316                self.cursor = Cursor::At { which, at, inclusive: true };
317                true
318            }
319            None => {
320                self.lose_position();
321                false
322            }
323        }
324    }
325
326    /// After a READ that found nothing: READ NEXT has no valid next record.
327    pub fn lose_position(&mut self) {
328        self.cursor = Cursor::Undefined;
329        self.last_read = None;
330    }
331
332    /// Makes the record just read by key `which` the one READ NEXT goes on from.
333    pub fn read_at(&mut self, which: usize, key: &[u8]) {
334        let at = match which {
335            0 => (key.to_vec(), 0, Vec::new()),
336            n => {
337                let alt = &self.alternates[n - 1];
338                (alt.span.of(&self.records[key]), alt.arrival[key], key.to_vec())
339            }
340        };
341        self.cursor = Cursor::At { which, at, inclusive: false };
342        self.last_read = Some(key.to_vec());
343    }
344
345    /// The first record by prime key after `from` (at it too when `inclusive`), or before it when
346    /// `backward`: what a CICS browse reads, holding its own position rather than the file's.
347    pub fn seek(&self, from: &[u8], inclusive: bool, backward: bool) -> Option<(Vec<u8>, Vec<u8>)> {
348        let edge = if inclusive { Bound::Included(from.to_vec()) } else { Bound::Excluded(from.to_vec()) };
349        let mut hits = if backward { self.records.range((Bound::Unbounded, edge)) } else { self.records.range((edge, Bound::Unbounded)) };
350        let hit = if backward { hits.next_back() } else { hits.next() };
351        hit.map(|(k, r)| (k.clone(), r.clone()))
352    }
353
354    /// [`Keyed::read_next`] with the status as text, for SORT's reader.
355    pub fn step(&mut self, backward: bool) -> Result<Option<Found>, &'static str> {
356        self.read_next(backward).map_err(FileStatus::as_str)
357    }
358
359    /// READ NEXT, or READ PREVIOUS when `backward`: None at the end, Err(46) with no position.
360    pub fn read_next(&mut self, backward: bool) -> Result<Option<Found>, FileStatus> {
361        let (which, bound) = match &self.cursor {
362            Cursor::Undefined => return Err(FileStatus::NoNextRecord),
363            Cursor::First => (0, Bound::Unbounded),
364            Cursor::At { which, at, inclusive: true } => (*which, Bound::Included(at.clone())),
365            Cursor::At { which, at, inclusive: false } => (*which, Bound::Excluded(at.clone())),
366        };
367        let range = if backward { (Bound::Unbounded, bound) } else { (bound, Bound::Unbounded) };
368        let hit = if which == 0 {
369            let mut keys = self.records.range((range.0.map(|(k, ..)| k), range.1.map(|(k, ..)| k))).map(|(k, _)| k);
370            let key = if backward { keys.next_back() } else { keys.next() };
371            key.map(|k| (k.clone(), false))
372        } else {
373            let mut entries = self.alternates[which - 1].index.range(range);
374            let entry = if backward { entries.next_back() } else { entries.next() };
375            entry.map(|(value, _, key)| {
376                let after = if backward { entries.next_back() } else { entries.next() };
377                (key.clone(), after.is_some_and(|(v, ..)| v == value))
378            })
379        };
380        let Some((key, duplicate)) = hit else {
381            self.lose_position();
382            return Ok(None);
383        };
384        self.read_at(which, &key);
385        Ok(Some(Found { record: self.records[&key].clone(), key, duplicate }))
386    }
387
388    fn inbound(&self, raw: Vec<u8>, format: Format) -> Vec<u8> {
389        let mut record = match format {
390            Format::Text => self.page.encode_lossy(&String::from_utf8_lossy(&raw)),
391            _ => raw,
392        };
393        if format != Format::Variable {
394            record.resize(self.record_len.max(record.len()), ebcdic::SPACE);
395        }
396        record
397    }
398
399    fn outbound(&self, record: &[u8], format: Format) -> Vec<u8> {
400        match format {
401            Format::Text => format!("{}\n", self.page.decode(record).trim_end()).into_bytes(),
402            Format::Fixed => {
403                let mut r = record.to_vec();
404                r.resize(self.record_len, ebcdic::SPACE);
405                r
406            }
407            Format::Variable => record.to_vec(),
408        }
409    }
410
411    fn save(&self, format: Format) -> io::Result<()> {
412        let Some(path) = self.path.as_ref().filter(|_| self.dirty) else { return Ok(()) };
413        let mut out = Open { mode: OpenMode::Output, format, handle: Handle::Writer(BufWriter::new(File::create(path)?)), head: Head::Start, page: None };
414        let empty = match format {
415            Format::Fixed => vec![0; self.record_len],
416            Format::Variable => Vec::new(),
417            Format::Text => b"\n".to_vec(),
418        };
419        let mut next = 1;
420        for (key, record) in &self.records {
421            if self.keying == Keying::Relative {
422                for _ in next..number_of(key) {
423                    out.write(&empty)?;
424                }
425                next = number_of(key) + 1;
426            }
427            out.write(&self.outbound(record, format))?;
428        }
429        out.close()
430    }
431}
432
433/// The file beside a VSAM data set that marks it open for output, as the catalog's open-for-output
434/// indicator does: an OPEN OUTPUT, I-O or EXTEND makes it and the CLOSE after it removes it, so a
435/// data set a run left open keeps it for the next OPEN to verify
436/// ([`numeric::assumptions::VSAM_DATA_SET_LEFT_OPEN`]).
437pub fn open_mark(data_set: &Path) -> PathBuf {
438    let mut name = data_set.as_os_str().to_owned();
439    name.push(".open-for-output");
440    PathBuf::from(name)
441}
442
443/// Whether a run left the VSAM data set at `data_set` open for output.
444pub fn left_open(data_set: &Path) -> bool {
445    data_set.is_file() && open_mark(data_set).is_file()
446}
447
448/// Removes the data set's open mark, if it has one.
449pub fn clear_open_mark(data_set: &Path) -> io::Result<()> {
450    remove_mark(&open_mark(data_set))
451}
452
453fn remove_mark(mark: &Path) -> io::Result<()> {
454    match std::fs::remove_file(mark) {
455        Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(()),
456        other => other,
457    }
458}
459
460pub struct Open {
461    pub mode: OpenMode,
462    pub format: Format,
463    handle: Handle,
464    head: Head,
465    /// The logical page of a LINAGE file opened OUTPUT or EXTEND.
466    pub page: Option<crate::linage::Page>,
467}
468
469/// A movement of the paper, as a text DD shows it: line feeds, or a form feed.
470#[derive(Clone, Copy, Debug, PartialEq, Eq)]
471pub enum Move {
472    Lines(u64),
473    Page,
474}
475
476/// Where a text DD's print position is: before its first line, on a line not yet ended (and
477/// whether anything shows on it), or at the start of a line.
478#[derive(Clone, Copy, Debug, PartialEq, Eq)]
479enum Head {
480    Start,
481    OnLine(bool),
482    Fresh,
483}
484
485/// What a READ found.
486pub enum Record {
487    Data(Vec<u8>),
488    /// A record whose length did not match: the bytes read, for FILE STATUS 04.
489    WrongLength(Vec<u8>),
490    End,
491}
492
493/// Opens a file held in memory. With no DD (an OPTIONAL file) or no file at the DD's path, it
494/// starts empty; OUTPUT always starts empty. `record_len` is the longest record.
495pub fn open_keyed(dd: Option<&Dd>, mode: OpenMode, format: Format, keying: Keying, record_len: usize, page: &'static CodePage) -> io::Result<Open> {
496    let path = dd.filter(|_| mode != OpenMode::Input).map(|d| d.path.clone());
497    let mut keyed = Keyed::new(keying, path, record_len, page);
498    if let Some(dd) = dd.filter(|d| mode != OpenMode::Output && d.path.exists()) {
499        let mut reader = open(dd, OpenMode::Input, format)?;
500        let mut number = 0u64;
501        loop {
502            let raw = match reader.read(record_len)? {
503                Record::End => break,
504                Record::Data(r) | Record::WrongLength(r) => r,
505            };
506            number += 1;
507            if keyed.keying == Keying::Relative && raw.iter().all(|&b| b == 0) {
508                continue;
509            }
510            let record = keyed.inbound(raw, format);
511            let key = keyed.prime_key(&record).unwrap_or_else(|| record_number(number));
512            keyed.insert(key, record).map_err(|_| io::Error::new(io::ErrorKind::InvalidData, format!("{}: two records with one key", dd.path.display())))?;
513        }
514        keyed.dirty = false;
515    }
516    Ok(Open { mode, format, handle: Handle::Keyed(Box::new(keyed)), head: Head::Start, page: None })
517}
518
519pub fn open(dd: &Dd, mode: OpenMode, format: Format) -> io::Result<Open> {
520    let handle = match mode {
521        // nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- a DD names the file the job assigned
522        OpenMode::Input => Handle::Reader(BufReader::new(File::open(&dd.path)?)),
523        // nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- a DD names the file the job assigned
524        OpenMode::Output if !dd.append => Handle::Writer(BufWriter::new(File::create(&dd.path)?)),
525        OpenMode::Output | OpenMode::Extend => Handle::Writer(BufWriter::new(OpenOptions::new().append(true).create(true).open(&dd.path)?)),
526        OpenMode::InputOutput => return Err(io::Error::new(io::ErrorKind::Unsupported, "OPEN I-O of a line-sequential file")),
527    };
528    Ok(Open { mode, format, handle, head: Head::Start, page: None })
529}
530
531/// An OPTIONAL input file with no DD: every READ is at end.
532pub fn absent() -> Open {
533    Open { mode: OpenMode::Input, format: Format::Fixed, handle: Handle::Empty, head: Head::Start, page: None }
534}
535
536impl Open {
537    pub fn keyed(&mut self) -> Option<&mut Keyed> {
538        match &mut self.handle {
539            Handle::Keyed(k) => Some(k),
540            _ => None,
541        }
542    }
543
544    pub fn is_keyed(&self) -> bool {
545        matches!(self.handle, Handle::Keyed(_))
546    }
547
548    /// Marks the VSAM data set this file holds open for output until CLOSE ([`open_mark`]).
549    pub fn mark_open(&mut self, data_set: &Path) -> io::Result<()> {
550        let Handle::Keyed(k) = &mut self.handle else { return Ok(()) };
551        let mark = open_mark(data_set);
552        std::fs::write(&mark, b"")?;
553        k.mark = Some(mark);
554        Ok(())
555    }
556
557    pub fn read(&mut self, fixed_len: usize) -> io::Result<Record> {
558        let reader = match &mut self.handle {
559            Handle::Reader(r) => r,
560            Handle::Empty => return Ok(Record::End),
561            Handle::Writer(_) | Handle::Keyed(_) => return Err(io::Error::other("a sequential READ of a file not open for it")),
562        };
563        match self.format {
564            Format::Fixed => {
565                let mut buf = Vec::with_capacity(fixed_len);
566                reader.by_ref().take(fixed_len as u64).read_to_end(&mut buf)?;
567                Ok(match buf.len() {
568                    0 => Record::End,
569                    n if n == fixed_len => Record::Data(buf),
570                    _ => Record::WrongLength(buf),
571                })
572            }
573            Format::Variable => {
574                let mut rdw = [0u8; 4];
575                match reader.read_exact(&mut rdw) {
576                    Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => return Ok(Record::End),
577                    other => other?,
578                }
579                let len = u16::from_be_bytes([rdw[0], rdw[1]]) as usize;
580                if len < 4 {
581                    return Err(io::Error::new(io::ErrorKind::InvalidData, format!("an RDW of length {len}")));
582                }
583                let mut buf = vec![0u8; len - 4];
584                reader.read_exact(&mut buf)?;
585                Ok(Record::Data(buf))
586            }
587            Format::Text => {
588                let mut line = Vec::new();
589                if reader.read_until(b'\n', &mut line)? == 0 {
590                    return Ok(Record::End);
591                }
592                while matches!(line.last(), Some(b'\n' | b'\r')) {
593                    line.pop();
594                }
595                Ok(Record::Data(line))
596            }
597        }
598    }
599
600    pub fn write(&mut self, bytes: &[u8]) -> io::Result<()> {
601        let Handle::Writer(w) = &mut self.handle else {
602            return Err(io::Error::other("WRITE to a file not opened for output"));
603        };
604        match self.format {
605            Format::Fixed | Format::Text => w.write_all(bytes),
606            Format::Variable => {
607                let len = u16::try_from(bytes.len() + 4).map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "a record over 32,760 bytes"))?;
608                w.write_all(&len.to_be_bytes())?;
609                w.write_all(&[0, 0])?;
610                w.write_all(bytes)
611            }
612        }
613    }
614
615    /// Writes a line to a text DD where a printer would put it: after `before`, over a line not
616    /// yet ended when nothing moved the paper (after a carriage return, unless one of the two is
617    /// blank), then `after`. The first line of the DD takes one line of `before` as its own start,
618    /// as a printer at the top of a form prints a single-spaced first line on line 1.
619    pub fn print(&mut self, before: Option<Move>, line: &str, after: Option<Move>) -> io::Result<()> {
620        if let Some(m) = before {
621            self.feed(m)?;
622        }
623        let Handle::Writer(w) = &mut self.handle else {
624            return Err(io::Error::other("WRITE to a file not opened for output"));
625        };
626        let shown = matches!(self.head, Head::OnLine(true));
627        if shown && !line.is_empty() {
628            w.write_all(b"\r")?;
629        }
630        w.write_all(line.as_bytes())?;
631        self.head = Head::OnLine(shown || !line.is_empty());
632        match after {
633            Some(m) => self.feed(m),
634            None => Ok(()),
635        }
636    }
637
638    fn feed(&mut self, m: Move) -> io::Result<()> {
639        let Handle::Writer(w) = &mut self.handle else {
640            return Err(io::Error::other("WRITE to a file not opened for output"));
641        };
642        match (m, self.head) {
643            (Move::Lines(0), _) => return Ok(()),
644            (Move::Lines(n), head) => {
645                for _ in 0..n - u64::from(head == Head::Start) {
646                    w.write_all(b"\n")?;
647                }
648            }
649            (Move::Page, Head::OnLine(_)) => w.write_all(b"\n\x0c")?,
650            (Move::Page, _) => w.write_all(b"\x0c")?,
651        }
652        self.head = Head::Fresh;
653        Ok(())
654    }
655
656    pub fn close(self) -> io::Result<()> {
657        match self.handle {
658            Handle::Writer(mut w) => {
659                if matches!(self.head, Head::OnLine(_)) {
660                    w.write_all(b"\n")?;
661                }
662                w.flush()
663            }
664            Handle::Keyed(k) => {
665                k.save(self.format)?;
666                k.mark.as_deref().map_or(Ok(()), remove_mark)
667            }
668            _ => Ok(()),
669        }
670    }
671
672    /// Ends the file as an abend that Language Environment is not told of leaves it: its records are
673    /// written as CLOSE writes them, and a VSAM data set keeps its open mark
674    /// ([`numeric::assumptions::TRAP_OFF_LEAVES_FILES_OPEN`]).
675    pub fn abandon(mut self) -> io::Result<()> {
676        if let Handle::Keyed(k) = &mut self.handle {
677            k.mark = None;
678        }
679        self.close()
680    }
681}
682
683#[cfg(test)]
684mod tests {
685    use super::*;
686
687    #[test]
688    fn dd_specs() {
689        let dds = Dds::new(&["IN=/tmp/a.txt:text".into(), "out=/tmp/b.dat".into()], false).unwrap();
690        assert_eq!(dds.get("IN"), Some(Dd { path: "/tmp/a.txt".into(), format: Some(Format::Text), append: false }));
691        assert_eq!(dds.get("OUT").unwrap().format, None);
692        assert!(dds.get("NONE").is_none());
693        assert!(Dds::new(&["NOEQUALS".into()], false).is_err());
694    }
695
696    #[test]
697    fn mod_suffix_follows_the_format_or_stands_alone() {
698        let dd = |value: &str| dd_value(value).unwrap();
699        assert_eq!(dd("a.dat:mod"), Dd { path: "a.dat".into(), format: None, append: true });
700        assert_eq!(dd("a.dat:text:mod"), Dd { path: "a.dat".into(), format: Some(Format::Text), append: true });
701        assert_eq!(dd("A.DAT:TEXT:MOD"), Dd { path: "A.DAT".into(), format: Some(Format::Text), append: true });
702        assert_eq!(dd("a.dat:text"), Dd { path: "a.dat".into(), format: Some(Format::Text), append: false });
703        assert_eq!(dd("c:\\a.dat"), Dd { path: "c:\\a.dat".into(), format: None, append: false });
704        assert!(dd_value(":mod").is_err());
705    }
706
707    #[test]
708    fn open_output_on_a_mod_dd_writes_after_the_records_there() {
709        let path = std::env::temp_dir().join(format!("ironwork-mod-{}", std::process::id()));
710        let write = |append: bool, records: &[&[u8]]| {
711            let mut out = open(&Dd { path: path.clone(), format: None, append }, OpenMode::Output, Format::Fixed).unwrap();
712            for record in records {
713                out.write(record).unwrap();
714            }
715            out.close().unwrap();
716        };
717        write(false, &[b"AA", b"BB"]);
718        write(true, &[b"CC"]);
719        assert_eq!(std::fs::read(&path).unwrap(), b"AABBCC");
720        write(false, &[b"DD"]);
721        assert_eq!(std::fs::read(&path).unwrap(), b"DD");
722        std::fs::remove_file(&path).unwrap();
723    }
724
725    #[test]
726    fn variable_records_round_trip_through_rdws() {
727        let path = std::env::temp_dir().join(format!("ironwork-vb-{}", std::process::id()));
728        let dd = Dd { path: path.clone(), format: None, append: false };
729        let mut out = open(&dd, OpenMode::Output, Format::Variable).unwrap();
730        out.write(b"AB").unwrap();
731        out.write(b"CDEF").unwrap();
732        out.close().unwrap();
733        assert_eq!(std::fs::read(&path).unwrap(), [0, 6, 0, 0, b'A', b'B', 0, 8, 0, 0, b'C', b'D', b'E', b'F']);
734        let mut input = open(&dd, OpenMode::Input, Format::Variable).unwrap();
735        assert!(matches!(input.read(0).unwrap(), Record::Data(d) if d == b"AB"));
736        assert!(matches!(input.read(0).unwrap(), Record::Data(d) if d == b"CDEF"));
737        assert!(matches!(input.read(0).unwrap(), Record::End));
738    }
739}