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::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}
189
190impl Keyed {
191    fn new(keying: Keying, path: Option<PathBuf>, record_len: usize, page: &'static CodePage) -> Self {
192        let alternates = match &keying {
193            Keying::Indexed { alternates, .. } => {
194                alternates.iter().map(|&(span, duplicates)| Alternate { span, duplicates, index: BTreeSet::new(), arrival: HashMap::new() }).collect()
195            }
196            _ => Vec::new(),
197        };
198        Self { keying, records: BTreeMap::new(), alternates, arrivals: 0, cursor: Cursor::First, last_read: None, dirty: true, path, record_len, page }
199    }
200
201    pub fn prime_key(&self, record: &[u8]) -> Option<Vec<u8>> {
202        match &self.keying {
203            Keying::Indexed { prime, .. } => Some(prime.of(record)),
204            _ => None,
205        }
206    }
207
208    pub fn highest_key(&self) -> Option<&Vec<u8>> {
209        self.records.keys().next_back()
210    }
211
212    pub fn record(&self, key: &[u8]) -> Option<&Vec<u8>> {
213        self.records.get(key)
214    }
215
216    fn key_len(&self, which: usize) -> usize {
217        match (&self.keying, which) {
218            (Keying::Indexed { prime, .. }, 0) => prime.len,
219            (Keying::Indexed { .. }, n) => self.alternates[n - 1].span.len,
220            _ => 8,
221        }
222    }
223
224    /// The record whose key `which` is `value`: for an alternate key, the first of any duplicates.
225    pub fn get(&self, which: usize, value: &[u8]) -> Option<Found> {
226        if which == 0 {
227            return self.records.get(value).map(|r| Found { key: value.to_vec(), record: r.clone(), duplicate: false });
228        }
229        let mut hits = self.alternates[which - 1].holders(value);
230        let key = hits.next()?.2.clone();
231        Some(Found { record: self.records[&key].clone(), key, duplicate: hits.next().is_some() })
232    }
233
234    /// Adds a record, or replaces the one with its key: Err with status 22 when it would take a
235    /// unique alternate key another record holds. Ok(true) when it shares an alternate key that
236    /// allows duplicates (status 02).
237    fn put(&mut self, key: Vec<u8>, record: Vec<u8>) -> Result<bool, FileStatus> {
238        let mut shared = false;
239        for alt in &self.alternates {
240            let value = alt.span.of(&record);
241            let others = alt.holders(&value).any(|(_, _, k)| *k != key);
242            if others && !alt.duplicates {
243                return Err(FileStatus::DuplicateKey);
244            }
245            shared |= others;
246        }
247        let old = self.records.get(&key).cloned();
248        let mut arrivals = self.arrivals;
249        for alt in &mut self.alternates {
250            let now = alt.span.of(&record);
251            if let Some(old) = &old {
252                let was = alt.span.of(old);
253                if was == now {
254                    continue;
255                }
256                let seq = alt.arrival[&key];
257                alt.index.remove(&(was, seq, key.clone()));
258            }
259            alt.index.insert((now, arrivals, key.clone()));
260            alt.arrival.insert(key.clone(), arrivals);
261            arrivals += 1;
262        }
263        self.arrivals = arrivals;
264        self.records.insert(key, record);
265        self.dirty = true;
266        Ok(shared)
267    }
268
269    /// Adds a record: Err with status 22 when its prime key, or a unique alternate key, is already
270    /// there; Ok(true) when it shares an alternate key that allows duplicates (status 02).
271    pub fn insert(&mut self, key: Vec<u8>, record: Vec<u8>) -> Result<bool, FileStatus> {
272        if self.records.contains_key(&key) {
273            return Err(FileStatus::DuplicateKey);
274        }
275        self.put(key, record)
276    }
277
278    /// Replaces a record: Err with status 23 when none has its key, 22 as for [`Keyed::insert`].
279    pub fn replace(&mut self, key: Vec<u8>, record: Vec<u8>) -> Result<bool, FileStatus> {
280        if !self.records.contains_key(&key) {
281            return Err(FileStatus::NotFound);
282        }
283        self.put(key, record)
284    }
285
286    pub fn remove(&mut self, key: &[u8]) -> Option<Vec<u8>> {
287        let record = self.records.remove(key)?;
288        for alt in &mut self.alternates {
289            if let Some(seq) = alt.arrival.remove(key) {
290                alt.index.remove(&(alt.span.of(&record), seq, key.to_vec()));
291            }
292        }
293        self.dirty = true;
294        Some(record)
295    }
296
297    /// START: positions at the first record whose key `which`, compared over the length of `value`
298    /// (a longer `value` is cut to the key's length), is `wanted` to `value`, or equal when
299    /// `or_equal`. False, and no position, when there is none.
300    pub fn start(&mut self, which: usize, wanted: Ordering, or_equal: bool, value: &[u8]) -> bool {
301        let value = &value[..value.len().min(self.key_len(which))];
302        let fits = |k: &[u8]| {
303            let o = k[..value.len().min(k.len())].cmp(value);
304            o == wanted || (or_equal && o == Ordering::Equal)
305        };
306        let from = (value.to_vec(), 0, Vec::new());
307        let found = match which {
308            0 => self.records.range(from.0..).map(|(k, _)| k).find(|k| fits(k)).map(|k| (k.clone(), 0, Vec::new())),
309            n => self.alternates[n - 1].index.range(from..).find(|(k, ..)| fits(k)).cloned(),
310        };
311        self.last_read = None;
312        match found {
313            Some(at) => {
314                self.cursor = Cursor::At { which, at, inclusive: true };
315                true
316            }
317            None => {
318                self.lose_position();
319                false
320            }
321        }
322    }
323
324    /// After a READ that found nothing: READ NEXT has no valid next record.
325    pub fn lose_position(&mut self) {
326        self.cursor = Cursor::Undefined;
327        self.last_read = None;
328    }
329
330    /// Makes the record just read by key `which` the one READ NEXT goes on from.
331    pub fn read_at(&mut self, which: usize, key: &[u8]) {
332        let at = match which {
333            0 => (key.to_vec(), 0, Vec::new()),
334            n => {
335                let alt = &self.alternates[n - 1];
336                (alt.span.of(&self.records[key]), alt.arrival[key], key.to_vec())
337            }
338        };
339        self.cursor = Cursor::At { which, at, inclusive: false };
340        self.last_read = Some(key.to_vec());
341    }
342
343    /// The first record by prime key after `from` (at it too when `inclusive`), or before it when
344    /// `backward`: what a CICS browse reads, holding its own position rather than the file's.
345    pub fn seek(&self, from: &[u8], inclusive: bool, backward: bool) -> Option<(Vec<u8>, Vec<u8>)> {
346        let edge = if inclusive { Bound::Included(from.to_vec()) } else { Bound::Excluded(from.to_vec()) };
347        let mut hits = if backward { self.records.range((Bound::Unbounded, edge)) } else { self.records.range((edge, Bound::Unbounded)) };
348        let hit = if backward { hits.next_back() } else { hits.next() };
349        hit.map(|(k, r)| (k.clone(), r.clone()))
350    }
351
352    /// [`Keyed::read_next`] with the status as text, for SORT's reader.
353    pub fn step(&mut self, backward: bool) -> Result<Option<Found>, &'static str> {
354        self.read_next(backward).map_err(FileStatus::as_str)
355    }
356
357    /// READ NEXT, or READ PREVIOUS when `backward`: None at the end, Err(46) with no position.
358    pub fn read_next(&mut self, backward: bool) -> Result<Option<Found>, FileStatus> {
359        let (which, bound) = match &self.cursor {
360            Cursor::Undefined => return Err(FileStatus::NoNextRecord),
361            Cursor::First => (0, Bound::Unbounded),
362            Cursor::At { which, at, inclusive: true } => (*which, Bound::Included(at.clone())),
363            Cursor::At { which, at, inclusive: false } => (*which, Bound::Excluded(at.clone())),
364        };
365        let range = if backward { (Bound::Unbounded, bound) } else { (bound, Bound::Unbounded) };
366        let hit = if which == 0 {
367            let mut keys = self.records.range((range.0.map(|(k, ..)| k), range.1.map(|(k, ..)| k))).map(|(k, _)| k);
368            let key = if backward { keys.next_back() } else { keys.next() };
369            key.map(|k| (k.clone(), false))
370        } else {
371            let mut entries = self.alternates[which - 1].index.range(range);
372            let entry = if backward { entries.next_back() } else { entries.next() };
373            entry.map(|(value, _, key)| {
374                let after = if backward { entries.next_back() } else { entries.next() };
375                (key.clone(), after.is_some_and(|(v, ..)| v == value))
376            })
377        };
378        let Some((key, duplicate)) = hit else {
379            self.lose_position();
380            return Ok(None);
381        };
382        self.read_at(which, &key);
383        Ok(Some(Found { record: self.records[&key].clone(), key, duplicate }))
384    }
385
386    fn inbound(&self, raw: Vec<u8>, format: Format) -> Vec<u8> {
387        let mut record = match format {
388            Format::Text => self.page.encode_lossy(&String::from_utf8_lossy(&raw)),
389            _ => raw,
390        };
391        if format != Format::Variable {
392            record.resize(self.record_len.max(record.len()), ebcdic::SPACE);
393        }
394        record
395    }
396
397    fn outbound(&self, record: &[u8], format: Format) -> Vec<u8> {
398        match format {
399            Format::Text => format!("{}\n", self.page.decode(record).trim_end()).into_bytes(),
400            Format::Fixed => {
401                let mut r = record.to_vec();
402                r.resize(self.record_len, ebcdic::SPACE);
403                r
404            }
405            Format::Variable => record.to_vec(),
406        }
407    }
408
409    fn save(&self, format: Format) -> io::Result<()> {
410        let Some(path) = self.path.as_ref().filter(|_| self.dirty) else { return Ok(()) };
411        let mut out = Open { mode: OpenMode::Output, format, handle: Handle::Writer(BufWriter::new(File::create(path)?)), head: Head::Start, page: None };
412        let empty = match format {
413            Format::Fixed => vec![0; self.record_len],
414            Format::Variable => Vec::new(),
415            Format::Text => b"\n".to_vec(),
416        };
417        let mut next = 1;
418        for (key, record) in &self.records {
419            if self.keying == Keying::Relative {
420                for _ in next..number_of(key) {
421                    out.write(&empty)?;
422                }
423                next = number_of(key) + 1;
424            }
425            out.write(&self.outbound(record, format))?;
426        }
427        out.close()
428    }
429}
430
431pub struct Open {
432    pub mode: OpenMode,
433    pub format: Format,
434    handle: Handle,
435    head: Head,
436    /// The logical page of a LINAGE file opened OUTPUT or EXTEND.
437    pub page: Option<crate::linage::Page>,
438}
439
440/// A movement of the paper, as a text DD shows it: line feeds, or a form feed.
441#[derive(Clone, Copy, Debug, PartialEq, Eq)]
442pub enum Move {
443    Lines(u64),
444    Page,
445}
446
447/// Where a text DD's print position is: before its first line, on a line not yet ended (and
448/// whether anything shows on it), or at the start of a line.
449#[derive(Clone, Copy, Debug, PartialEq, Eq)]
450enum Head {
451    Start,
452    OnLine(bool),
453    Fresh,
454}
455
456/// What a READ found.
457pub enum Record {
458    Data(Vec<u8>),
459    /// A record whose length did not match: the bytes read, for FILE STATUS 04.
460    WrongLength(Vec<u8>),
461    End,
462}
463
464/// Opens a file held in memory. With no DD (an OPTIONAL file) or no file at the DD's path, it
465/// starts empty; OUTPUT always starts empty. `record_len` is the longest record.
466pub fn open_keyed(dd: Option<&Dd>, mode: OpenMode, format: Format, keying: Keying, record_len: usize, page: &'static CodePage) -> io::Result<Open> {
467    let path = dd.filter(|_| mode != OpenMode::Input).map(|d| d.path.clone());
468    let mut keyed = Keyed::new(keying, path, record_len, page);
469    if let Some(dd) = dd.filter(|d| mode != OpenMode::Output && d.path.exists()) {
470        let mut reader = open(dd, OpenMode::Input, format)?;
471        let mut number = 0u64;
472        loop {
473            let raw = match reader.read(record_len)? {
474                Record::End => break,
475                Record::Data(r) | Record::WrongLength(r) => r,
476            };
477            number += 1;
478            if keyed.keying == Keying::Relative && raw.iter().all(|&b| b == 0) {
479                continue;
480            }
481            let record = keyed.inbound(raw, format);
482            let key = keyed.prime_key(&record).unwrap_or_else(|| record_number(number));
483            keyed.insert(key, record).map_err(|_| io::Error::new(io::ErrorKind::InvalidData, format!("{}: two records with one key", dd.path.display())))?;
484        }
485        keyed.dirty = false;
486    }
487    Ok(Open { mode, format, handle: Handle::Keyed(Box::new(keyed)), head: Head::Start, page: None })
488}
489
490pub fn open(dd: &Dd, mode: OpenMode, format: Format) -> io::Result<Open> {
491    let handle = match mode {
492        OpenMode::Input => Handle::Reader(BufReader::new(File::open(&dd.path)?)),
493        OpenMode::Output if !dd.append => Handle::Writer(BufWriter::new(File::create(&dd.path)?)),
494        OpenMode::Output | OpenMode::Extend => Handle::Writer(BufWriter::new(OpenOptions::new().append(true).create(true).open(&dd.path)?)),
495        OpenMode::InputOutput => return Err(io::Error::new(io::ErrorKind::Unsupported, "OPEN I-O of a line-sequential file")),
496    };
497    Ok(Open { mode, format, handle, head: Head::Start, page: None })
498}
499
500/// An OPTIONAL input file with no DD: every READ is at end.
501pub fn absent() -> Open {
502    Open { mode: OpenMode::Input, format: Format::Fixed, handle: Handle::Empty, head: Head::Start, page: None }
503}
504
505impl Open {
506    pub fn keyed(&mut self) -> Option<&mut Keyed> {
507        match &mut self.handle {
508            Handle::Keyed(k) => Some(k),
509            _ => None,
510        }
511    }
512
513    pub fn is_keyed(&self) -> bool {
514        matches!(self.handle, Handle::Keyed(_))
515    }
516
517    pub fn read(&mut self, fixed_len: usize) -> io::Result<Record> {
518        let reader = match &mut self.handle {
519            Handle::Reader(r) => r,
520            Handle::Empty => return Ok(Record::End),
521            Handle::Writer(_) | Handle::Keyed(_) => return Err(io::Error::other("a sequential READ of a file not open for it")),
522        };
523        match self.format {
524            Format::Fixed => {
525                let mut buf = Vec::with_capacity(fixed_len);
526                reader.by_ref().take(fixed_len as u64).read_to_end(&mut buf)?;
527                Ok(match buf.len() {
528                    0 => Record::End,
529                    n if n == fixed_len => Record::Data(buf),
530                    _ => Record::WrongLength(buf),
531                })
532            }
533            Format::Variable => {
534                let mut rdw = [0u8; 4];
535                match reader.read_exact(&mut rdw) {
536                    Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => return Ok(Record::End),
537                    other => other?,
538                }
539                let len = u16::from_be_bytes([rdw[0], rdw[1]]) as usize;
540                if len < 4 {
541                    return Err(io::Error::new(io::ErrorKind::InvalidData, format!("an RDW of length {len}")));
542                }
543                let mut buf = vec![0u8; len - 4];
544                reader.read_exact(&mut buf)?;
545                Ok(Record::Data(buf))
546            }
547            Format::Text => {
548                let mut line = Vec::new();
549                if reader.read_until(b'\n', &mut line)? == 0 {
550                    return Ok(Record::End);
551                }
552                while matches!(line.last(), Some(b'\n' | b'\r')) {
553                    line.pop();
554                }
555                Ok(Record::Data(line))
556            }
557        }
558    }
559
560    pub fn write(&mut self, bytes: &[u8]) -> io::Result<()> {
561        let Handle::Writer(w) = &mut self.handle else {
562            return Err(io::Error::other("WRITE to a file not opened for output"));
563        };
564        match self.format {
565            Format::Fixed | Format::Text => w.write_all(bytes),
566            Format::Variable => {
567                let len = u16::try_from(bytes.len() + 4).map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "a record over 32,760 bytes"))?;
568                w.write_all(&len.to_be_bytes())?;
569                w.write_all(&[0, 0])?;
570                w.write_all(bytes)
571            }
572        }
573    }
574
575    /// Writes a line to a text DD where a printer would put it: after `before`, over a line not
576    /// yet ended when nothing moved the paper (after a carriage return, unless one of the two is
577    /// blank), then `after`. The first line of the DD takes one line of `before` as its own start,
578    /// as a printer at the top of a form prints a single-spaced first line on line 1.
579    pub fn print(&mut self, before: Option<Move>, line: &str, after: Option<Move>) -> io::Result<()> {
580        if let Some(m) = before {
581            self.feed(m)?;
582        }
583        let Handle::Writer(w) = &mut self.handle else {
584            return Err(io::Error::other("WRITE to a file not opened for output"));
585        };
586        let shown = matches!(self.head, Head::OnLine(true));
587        if shown && !line.is_empty() {
588            w.write_all(b"\r")?;
589        }
590        w.write_all(line.as_bytes())?;
591        self.head = Head::OnLine(shown || !line.is_empty());
592        match after {
593            Some(m) => self.feed(m),
594            None => Ok(()),
595        }
596    }
597
598    fn feed(&mut self, m: Move) -> io::Result<()> {
599        let Handle::Writer(w) = &mut self.handle else {
600            return Err(io::Error::other("WRITE to a file not opened for output"));
601        };
602        match (m, self.head) {
603            (Move::Lines(0), _) => return Ok(()),
604            (Move::Lines(n), head) => {
605                for _ in 0..n - u64::from(head == Head::Start) {
606                    w.write_all(b"\n")?;
607                }
608            }
609            (Move::Page, Head::OnLine(_)) => w.write_all(b"\n\x0c")?,
610            (Move::Page, _) => w.write_all(b"\x0c")?,
611        }
612        self.head = Head::Fresh;
613        Ok(())
614    }
615
616    pub fn close(self) -> io::Result<()> {
617        match self.handle {
618            Handle::Writer(mut w) => {
619                if matches!(self.head, Head::OnLine(_)) {
620                    w.write_all(b"\n")?;
621                }
622                w.flush()
623            }
624            Handle::Keyed(k) => k.save(self.format),
625            _ => Ok(()),
626        }
627    }
628}
629
630#[cfg(test)]
631mod tests {
632    use super::*;
633
634    #[test]
635    fn dd_specs() {
636        let dds = Dds::new(&["IN=/tmp/a.txt:text".into(), "out=/tmp/b.dat".into()], false).unwrap();
637        assert_eq!(dds.get("IN"), Some(Dd { path: "/tmp/a.txt".into(), format: Some(Format::Text), append: false }));
638        assert_eq!(dds.get("OUT").unwrap().format, None);
639        assert!(dds.get("NONE").is_none());
640        assert!(Dds::new(&["NOEQUALS".into()], false).is_err());
641    }
642
643    #[test]
644    fn mod_suffix_follows_the_format_or_stands_alone() {
645        let dd = |value: &str| dd_value(value).unwrap();
646        assert_eq!(dd("a.dat:mod"), Dd { path: "a.dat".into(), format: None, append: true });
647        assert_eq!(dd("a.dat:text:mod"), Dd { path: "a.dat".into(), format: Some(Format::Text), append: true });
648        assert_eq!(dd("A.DAT:TEXT:MOD"), Dd { path: "A.DAT".into(), format: Some(Format::Text), append: true });
649        assert_eq!(dd("a.dat:text"), Dd { path: "a.dat".into(), format: Some(Format::Text), append: false });
650        assert_eq!(dd("c:\\a.dat"), Dd { path: "c:\\a.dat".into(), format: None, append: false });
651        assert!(dd_value(":mod").is_err());
652    }
653
654    #[test]
655    fn open_output_on_a_mod_dd_writes_after_the_records_there() {
656        let path = std::env::temp_dir().join(format!("ironwork-mod-{}", std::process::id()));
657        let write = |append: bool, records: &[&[u8]]| {
658            let mut out = open(&Dd { path: path.clone(), format: None, append }, OpenMode::Output, Format::Fixed).unwrap();
659            for record in records {
660                out.write(record).unwrap();
661            }
662            out.close().unwrap();
663        };
664        write(false, &[b"AA", b"BB"]);
665        write(true, &[b"CC"]);
666        assert_eq!(std::fs::read(&path).unwrap(), b"AABBCC");
667        write(false, &[b"DD"]);
668        assert_eq!(std::fs::read(&path).unwrap(), b"DD");
669        std::fs::remove_file(&path).unwrap();
670    }
671
672    #[test]
673    fn variable_records_round_trip_through_rdws() {
674        let path = std::env::temp_dir().join(format!("ironwork-vb-{}", std::process::id()));
675        let dd = Dd { path: path.clone(), format: None, append: false };
676        let mut out = open(&dd, OpenMode::Output, Format::Variable).unwrap();
677        out.write(b"AB").unwrap();
678        out.write(b"CDEF").unwrap();
679        out.close().unwrap();
680        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']);
681        let mut input = open(&dd, OpenMode::Input, Format::Variable).unwrap();
682        assert!(matches!(input.read(0).unwrap(), Record::Data(d) if d == b"AB"));
683        assert!(matches!(input.read(0).unwrap(), Record::Data(d) if d == b"CDEF"));
684        assert!(matches!(input.read(0).unwrap(), Record::End));
685    }
686}