stuffr-cli 0.22.0

The stuffr command line interface
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
//! Split archives (`fileio.c`): the names of the parts (`get_in_split_path`,
//! `get_out_split_path`), the questions zip asks about them
//! (`ask_for_split_read_path`, `ask_for_split_write_path`), and the half of
//! `bfwrite` that moves to the next split (`close_split`, `rename_split`).
//!
//! **A split being written** is a temporary (`ziXXXXXX`, in `-b`'s
//! directory or the archive's) until it is full; it is then renamed to
//! `name.z01`, `name.z02`, ... (`-s`), unless (`split_method` 1) it holds
//! the local header of the entry being written, which zip rewrites once
//! the entry is done: that split stays open until then. The last split is
//! the temporary the run publishes as `name.zip`. Only the newest
//! temporary is armed for zip's interrupt handler, as zip's handler removes
//! only `tempzip`.
//!
//! **Questions at the end of standard input (ruling Z5, deviation 18).**
//! zip reads its answers with `fgets` and does not look at what it
//! returns: at the end of standard input the buffer keeps whatever it held
//! (uninitialised at the first question), and `-FF` asks again for ever
//! (both references; gigabytes of prompts), as does Ubuntu's `-sp`. stuffr
//! never loops: the end of standard input at a question about a part to
//! read answers `q` (abort, as an explicit `q` does: measured on both
//! references), and at `-sp`'s question about where to write it answers
//! ENTER (the question has no way out but an interrupt; ENTER is what
//! Apple's build does there).

use std::cell::RefCell;
use std::io::{self, BufRead, Write};
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::atomic::AtomicU64;

use super::cli::ZE_ABORT;
use super::messages::{self as m, printf};
use super::sink::FileSink;
use super::zipfile::{Bf, Pend, ZOut, ZWrite};
use super::{Exit, Zr};
use crate::compat::host::{self, CleanupGuard, Host};

/// ziperr.h codes this file raises.
const ZE_EOF: i32 = 2;
const ZE_FORM: i32 = 3;
const ZE_BIG: i32 = 6;
const ZE_TEMP: i32 = 10;
const ZE_WRITE: i32 = 14;
pub(crate) const ZE_OK: i32 = 0;

/// Standard input as zip's prompts read it: the process's (`None`, locked
/// for each read, so `-c`/`-z`'s reads share its buffer), or a sandbox's
/// bytes, shared by every question of the run.
pub(crate) type Prompt = Option<Rc<RefCell<Box<dyn BufRead>>>>;

/// The run's prompt stream (`stdin`), made at the first question.
pub(crate) fn prompt(z: &mut Zr) -> Prompt {
    if let Some(p) = &z.prompt {
        return p.clone();
    }
    let p = match &mut z.host {
        Host::Real => None,
        h @ Host::Sandbox(_) => {
            let r: Box<dyn BufRead> = Box::new(h.stdin());
            Some(Rc::new(RefCell::new(r)))
        }
    };
    z.prompt = Some(p.clone());
    p
}

/// `fgets(buf, n, stdin)`: at most `n - 1` bytes, up to and with a
/// newline. `None` at the end with nothing read (zip's buffer would keep
/// what it held: ruling Z5 decides what that answers).
pub(crate) fn fgets_n(p: &Prompt, n: usize) -> Option<Vec<u8>> {
    match p {
        None => fgets_from(&mut io::stdin().lock(), n),
        Some(rc) => fgets_from(&mut **rc.borrow_mut(), n),
    }
}

fn fgets_from(r: &mut dyn BufRead, n: usize) -> Option<Vec<u8>> {
    let mut line = Vec::new();
    let max = n.saturating_sub(1);
    while line.len() < max {
        let buf = match r.fill_buf() {
            Ok(b) => b,
            Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
            Err(_) => break,
        };
        if buf.is_empty() {
            break;
        }
        let take = buf.len().min(max - line.len());
        if let Some(i) = buf[..take].iter().position(|&b| b == b'\n') {
            line.extend_from_slice(&buf[..=i]);
            r.consume(i + 1);
            return Some(line);
        }
        line.extend_from_slice(&buf[..take]);
        r.consume(take);
    }
    (!line.is_empty()).then_some(line)
}

/// An answer as zip reads it: up to the first newline, then up to the
/// first NUL (`strlen`).
fn answer(mut l: Vec<u8>) -> Vec<u8> {
    if let Some(i) = l.iter().position(|&b| b == b'\n') {
        l.truncate(i);
    }
    super::readzip::cstr(&l).to_vec()
}

/// `base` without its last three bytes (`zip`), then `zNN`: the name of
/// split `num` (1-based). `None` past 99999.
fn split_name(base: &[u8], num: u64) -> Option<Vec<u8>> {
    if num > 99999 {
        return None;
    }
    let mut p = base[..base.len().saturating_sub(3)].to_vec();
    p.extend_from_slice(format!("z{num:02}").as_bytes());
    Some(p)
}

/// fileio.c:get_in_split_path: the path of disk `disk` (0-based) of the
/// archive whose last part is `base`: `base` itself for the last disk
/// (`total_disks`), else `.z01`, `.z02`, ...
// port: fileio.c:get_in_split_path
pub(crate) fn get_in_split_path(z: &mut Zr, base: &[u8], disk: u64) -> Result<Vec<u8>, Exit> {
    let num = disk.wrapping_add(1);
    if num == z.total_disks {
        return Ok(base.to_vec());
    }
    split_name(base, num).ok_or_else(|| z.ziperr(ZE_BIG, m::MORE_THAN_99999.as_bytes()))
}

/// fileio.c:get_out_split_path: `.z01`, `.z02`, ... for disk `disk`.
// port: fileio.c:get_out_split_path
fn get_out_split_path(base: &[u8], disk: u64) -> Option<Vec<u8>> {
    split_name(base, disk.wrapping_add(1))
}

/// The directory part of `p`: up to its last `/`, `\` or `:`, with it;
/// `None` when it has none.
fn dir_of(p: &[u8], seps: &[u8]) -> Option<usize> {
    p.iter().rposition(|b| seps.contains(b)).map(|i| i + 1)
}

/// fileio.c:ask_for_split_read_path: the part `current_disk` of the
/// archive being read could not be opened; ask where it is. `ZE_OK` once
/// it can be opened (`z.o.in_path` may have changed), `ZE_ABORT` (`q`),
/// `ZE_FORM` (`s`, under `-F`/`-FF`: skip it), `ZE_EOF` (`e`, `-FF`: no
/// more parts). The end of standard input answers `q` (ruling Z5).
// port: fileio.c:ask_for_split_read_path
pub(crate) fn ask_for_split_read_path(z: &mut Zr, current_disk: u64) -> Result<i32, Exit> {
    let fix = z.o.fix;
    let maxpath = super::flavour::fnmax(z.flavour) + 4010;
    let p = prompt(z);
    let mut in_path = z.o.in_path.clone().unwrap_or_default();
    let mut split_path = get_in_split_path(z, &in_path, current_disk)?;
    // the name of the archive, without its directory
    let archive_name = match dir_of(&in_path, b"/\\:") {
        Some(i) => in_path[i..].to_vec(),
        None => in_path.clone(),
    };
    let mut is_readable = false;
    let mut s = Vec::new();
    printf(&mut s, m::COULD_NOT_FIND, &[&split_path]);
    z.mesg_write(&s);
    loop {
        let mut q = m::HIT_C.as_bytes().to_vec();
        if is_readable {
            q.extend_from_slice(m::HIT_Q.as_bytes());
            q.extend_from_slice(m::HIT_ENTER_CONTINUE.as_bytes());
        } else if fix == 1 {
            q.extend_from_slice(m::HIT_S.as_bytes());
            q.extend_from_slice(m::HIT_Q.as_bytes());
            q.extend_from_slice(m::HIT_ENTER_AGAIN.as_bytes());
        } else if fix == 2 {
            q.extend_from_slice(m::HIT_S.as_bytes());
            q.extend_from_slice(m::HIT_Q.as_bytes());
            q.extend_from_slice(m::HIT_E.as_bytes());
            q.extend_from_slice(m::HIT_Z.as_bytes());
            q.extend_from_slice(m::HIT_ENTER_AGAIN.as_bytes());
        } else {
            q.extend_from_slice(m::HIT_Q.as_bytes());
            q.extend_from_slice(m::HIT_ENTER_AGAIN.as_bytes());
        }
        z.mesg_write(&q);
        // ruling Z5: the end of standard input answers q
        let Some(l) = fgets_n(&p, maxpath) else {
            return Ok(ZE_ABORT);
        };
        let mut buf = answer(l);
        let first = buf.first().map(u8::to_ascii_uppercase);
        if first == Some(b'Q') {
            return Ok(ZE_ABORT);
        } else if (fix == 1 || fix == 2) && first == Some(b'S') {
            z.skip_this_disk = z.current_in_disk + 1;
            return Ok(ZE_FORM);
        } else if first == Some(b'C') {
            z.mesg_write(m::ENTER_SPLIT_PATH.as_bytes());
            let Some(l) = fgets_n(&p, maxpath) else {
                return Ok(ZE_ABORT);
            };
            is_readable = false;
            buf = answer(l);
            if buf.is_empty() {
                // ENTER: the same path again (removable media changed)
                buf = split_path.clone();
            }
        } else if fix == 2 && first == Some(b'E') {
            return Ok(ZE_EOF);
        } else if fix == 2 && first == Some(b'Z') {
            z.total_disks = current_disk + 1;
            split_path = get_in_split_path(z, &in_path, current_disk)?;
            buf = split_path.clone();
            buf.truncate(maxpath);
        }
        if !buf.is_empty() {
            // a new place: its directory, then the archive's name
            if buf == b"." {
                buf.clear();
            }
            match dir_of(&buf, b"/\\:") {
                None => in_path = archive_name.clone(),
                Some(i) => {
                    buf.truncate(i);
                    // only up to a `/` this time
                    let d = dir_of(&buf, b"/").unwrap_or(0);
                    buf.truncate(d);
                    in_path = buf.clone();
                    in_path.extend_from_slice(&archive_name);
                }
            }
            z.o.in_path = Some(in_path.clone());
            split_path = get_in_split_path(z, &in_path, current_disk)?;
            if !readable(z, &split_path) {
                let mut s = Vec::new();
                printf(&mut s, m::COULD_NOT_FIND_OR_OPEN, &[&split_path]);
                z.mesg_write(&s);
                continue;
            }
            is_readable = true;
            let mut s = Vec::new();
            printf(&mut s, m::FOUND_SPLIT, &[&split_path]);
            z.mesg_write(&s);
        } else {
            if !readable(z, &split_path) {
                let mut s = Vec::new();
                printf(&mut s, m::COULD_NOT_FIND_OR_OPEN, &[&split_path]);
                z.mesg_write(&s);
                continue;
            }
            let mut s = Vec::new();
            printf(&mut s, m::FOUND_SPLIT_NL, &[&split_path]);
            z.mesg_write(&s);
            return Ok(ZE_OK);
        }
    }
}

/// `fopen(path, "r")` succeeds (through `Host::track`, nothing read).
fn readable(z: &Zr, path: &[u8]) -> bool {
    super::readzip::CFile::open(z, path).is_ok()
}

// ---- writing splits ----

/// Where the split writer makes its files: a sandbox's directory and
/// output budget, or the process's.
pub(crate) struct SplitEnv {
    pub(crate) dir: Option<PathBuf>,
    pub(crate) budget: Option<Arc<AtomicU64>>,
}

impl SplitEnv {
    pub(crate) fn of(h: &Host) -> SplitEnv {
        match h {
            Host::Real => SplitEnv {
                dir: None,
                budget: None,
            },
            Host::Sandbox(s) => SplitEnv {
                dir: Some(s.dir.clone()),
                budget: Some(s.budget.clone()),
            },
        }
    }

    pub(crate) fn path(&self, b: &[u8]) -> PathBuf {
        match &self.dir {
            None => host::path(b),
            Some(d) => d.join(host::path(b)),
        }
    }
}

/// A split kept open for its local header's rewrite (`current_local_file`,
/// `current_local_tempname`).
pub(crate) struct LocalSplit {
    pub(crate) w: Box<dyn ZWrite>,
    pub(crate) tempname: Vec<u8>,
}

/// The splits being written (`split_method`, `split_size`, the split
/// options, `out_path`, `tempzip`).
pub(crate) struct SplitW {
    /// 1: `-s` (local headers rewritten); 2: `-sp` (descriptors, a question
    /// before each split).
    pub(crate) method: i8,
    pub(crate) bell: bool,
    pub(crate) noisy_splits: bool,
    /// `out_path`, which `-sp`'s answers change.
    pub(crate) out_path: Vec<u8>,
    /// The directory new temporaries go to (`-b`'s, or the archive's).
    pub(crate) temp_dir: Vec<u8>,
    pub(crate) env: SplitEnv,
    /// `tempzip`: the current split's temporary name, and its guard.
    pub(crate) tempzip: Vec<u8>,
    pub(crate) guard: Option<CleanupGuard>,
    /// The current temporary is removed when the run fails (`destroy`).
    pub(crate) published: bool,
    /// `current_local_file` when it is not the current split.
    pub(crate) local: Option<LocalSplit>,
    /// Splits zip lost track of (a local header split never rewritten, as
    /// with `-fd`): left under their temporary names, as zip leaves them.
    pub(crate) lost: Vec<LocalSplit>,
    /// `zip_attributes`: the mode each split is given once renamed.
    pub(crate) zip_attributes: u32,
    pub(crate) prompt: Prompt,
    pub(crate) fnmax: usize,
    /// `mesg` (standard output: splits never go to standard output), for
    /// the questions written before their answers are read.
    pub(crate) mesg: Option<std::io::BufWriter<Box<dyn Write + Send>>>,
    /// `noisy`, for `zipmessage`'s text written early.
    pub(crate) noisy: bool,
    /// The run's `mesg_line_started` (`Zr::line_cell`).
    pub(crate) line: Rc<std::cell::Cell<bool>>,
}

impl Drop for SplitW {
    fn drop(&mut self) {
        // ziperr: destroy(tempzip); the local split's file is closed only
        self.guard = None;
        if !self.published {
            let _ = std::fs::remove_file(self.env.path(&self.tempzip));
        }
    }
}

/// A failure inside `bfwrite`'s split half, carried in an `io::Error` to
/// whoever ends the run (`zipup::write_failed`): `ZIPERR(code, text)`, the
/// `zip I/O error:` line's system answer for the codes that print one, and
/// zip's warning before it.
#[derive(Debug)]
pub(crate) struct SplitFail {
    pub(crate) code: i32,
    pub(crate) text: Vec<u8>,
    pub(crate) os: Option<io::Error>,
    pub(crate) warn: Option<(Vec<u8>, Vec<u8>)>,
}

impl std::fmt::Display for SplitFail {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "split failure {}", self.code)
    }
}

impl std::error::Error for SplitFail {}

fn split_fail(code: i32, text: &[u8], os: Option<io::Error>) -> io::Error {
    io::Error::other(SplitFail {
        code,
        text: text.to_vec(),
        os,
        warn: None,
    })
}

/// The run's end for a [`SplitFail`] carried by `err`, if it carries one.
pub(crate) fn split_fail_exit(z: &mut Zr, err: &io::Error) -> Option<Exit> {
    let f = err.get_ref()?.downcast_ref::<SplitFail>()?;
    if let Some((a, b)) = &f.warn {
        z.zipwarn(a, b);
    }
    Some(match &f.os {
        Some(e) if super::zipup::perr(f.code) => z.ziperr_os(f.code, &f.text, e),
        _ => z.ziperr(f.code, &f.text),
    })
}

impl SplitW {
    /// A new local header goes to the current split: a local split still
    /// open is lost track of (zip overwrites `current_local_file`).
    pub(crate) fn local_header_starts(&mut self) {
        if let Some(l) = self.local.take() {
            self.lost.push(l);
        }
    }

    /// fileio.c:close_split: close `w` and rename it (`tempname`) to the
    /// name of split `disk`, with `-sv`'s message.
    // port: fileio.c:close_split
    fn close_split(
        &mut self,
        pending: &mut Vec<Pend>,
        disk: u64,
        mut w: Box<dyn ZWrite>,
        tempname: &[u8],
    ) -> io::Result<()> {
        let Some(split_path) = get_out_split_path(&self.out_path, disk) else {
            return Err(split_fail(ZE_BIG, m::MORE_THAN_99999.as_bytes(), None));
        };
        if self.noisy_splits {
            let mut l = m::CLOSING_SPLIT.as_bytes().to_vec();
            l.extend_from_slice(&split_path);
            pending.push(Pend::Message(l));
        }
        // fclose: a buffered write failing here is not looked at by zip
        let _ = w.flush();
        drop(w);
        // zip.c:rename_split
        let to = self.env.path(&split_path);
        if let Err((code, e)) = super::sink::replace(&to, &self.env.path(tempname)) {
            return Err(io::Error::other(SplitFail {
                code,
                text: m::WAS_REPLACING_SPLIT.as_bytes().to_vec(),
                os: Some(e),
                warn: Some((m::NEW_ZIP_LEFT_AS.as_bytes().to_vec(), tempname.to_vec())),
            }));
        }
        if self.zip_attributes != 0 {
            super::write::set_mode(&to, self.zip_attributes);
        }
        Ok(())
    }

    /// fileio.c:ask_for_split_write_path: before split `current_disk`
    /// (`-sp`), where to write it. The end of standard input answers ENTER
    /// (ruling Z5).
    // port: fileio.c:ask_for_split_write_path
    fn ask_for_split_write_path(&mut self, pending: &mut Vec<Pend>, current_disk: u64) {
        let num = (current_disk as u32).wrapping_add(1);
        // ruling Z24 (deviation 22): a name with no directory is a name in
        // the current directory (the C leaves the name empty, and the next
        // split's name then indexes before its buffer)
        let (split_dir, mut split_name) = match dir_of(&self.out_path, b"/\\:") {
            Some(i) => (self.out_path[..i].to_vec(), self.out_path[i..].to_vec()),
            None => (
                m::CURRENT_DIRECTORY.as_bytes().to_vec(),
                self.out_path.clone(),
            ),
        };
        pending.push(Pend::NlIfLine);
        let mut s = Vec::new();
        printf(
            &mut s,
            m::OPENING_DISK,
            &[num.to_string().as_bytes(), &split_dir],
        );
        pending.push(Pend::Raw(s));
        loop {
            pending.push(Pend::Raw(m::SPLIT_PATH_PROMPT.as_bytes().to_vec()));
            // fprintf, fflush, then fgets: the question is on the terminal
            // (and -sb's bell has rung) before the answer is read
            self.flush_pending(pending);
            // ruling Z5: the end of standard input answers ENTER
            let Some(l) = fgets_n(&self.prompt, self.fnmax) else {
                break;
            };
            let mut buf = answer(l);
            if buf.is_empty() {
                break;
            }
            if buf == b"." {
                buf.clear();
            }
            match dir_of(&buf, b"/\\:") {
                None => {
                    buf = m::CURRENT_DIRECTORY.as_bytes().to_vec();
                    self.out_path = split_name.clone();
                }
                Some(i) => {
                    buf.truncate(i);
                    let d = dir_of(&buf, b"/").unwrap_or(0);
                    buf.truncate(d);
                    let mut o = buf.clone();
                    o.extend_from_slice(&split_name);
                    self.out_path = o;
                }
            }
            let mut s = Vec::new();
            printf(&mut s, m::WRITING_TO, &[&buf]);
            pending.push(Pend::Raw(s));
            // the name again (ruling Z24: with no `/`, the whole name)
            split_name = match dir_of(&self.out_path, b"/") {
                Some(i) => self.out_path[i..].to_vec(),
                None => self.out_path.clone(),
            };
        }
    }

    /// Write what `bfwrite` has queued for `mesg` now, flushed, as the C
    /// writes it before it reads an answer: `zipmessage`'s text (its log
    /// copy stays queued for the caller), `fprintf`s, `-dg`'s dots, keeping
    /// `mesg_line_started` in the run's shared cell.
    pub(crate) fn flush_pending(&mut self, pending: &mut Vec<Pend>) {
        let Some(w) = self.mesg.as_mut() else {
            return;
        };
        let mut keep = Vec::new();
        for p in pending.drain(..) {
            match p {
                Pend::Dots(d) => {
                    let _ = w.write_all(&d);
                    if d.contains(&b'.') {
                        self.line.set(true);
                    }
                }
                Pend::Raw(b) => {
                    let _ = w.write_all(&b);
                }
                Pend::NlIfLine => {
                    if self.line.get() {
                        let _ = w.write_all(b"\n");
                    }
                }
                Pend::Message(b) => {
                    if self.noisy {
                        if self.line.get() {
                            let _ = w.write_all(b"\n");
                        }
                        let _ = w.write_all(&b);
                        let _ = w.write_all(b"\n");
                        self.line.set(false);
                    }
                    keep.push(Pend::LogOnly(b));
                }
                Pend::LogOnly(b) => keep.push(Pend::LogOnly(b)),
            }
        }
        let _ = w.flush();
        *pending = keep;
    }

    /// `mkstemp` of the next split's temporary, armed in place of the
    /// previous one.
    fn open_temp(&mut self) -> io::Result<Box<dyn ZWrite>> {
        // only the newest temporary is zip's `tempzip`
        self.guard = None;
        let env = &self.env;
        let (name, r) =
            super::sink::mkstemp_with(env.dir.is_none(), &|b: &[u8]| env.path(b), &self.temp_dir);
        self.tempzip = name;
        match r {
            Err(e) => Err(split_fail(ZE_TEMP, &self.tempzip.clone(), Some(e))),
            Ok((f, guard)) => {
                self.guard = Some(guard);
                Ok(Box::new(FileSink::new(f, self.env.budget.clone())))
            }
        }
    }
}

#[cfg(test)]
thread_local! {
    /// Tests: the write of what did not fit, into the new split, fails
    /// (`fwrite` short: the disk full).
    pub(crate) static FAIL_SPLIT_REST: std::cell::Cell<bool> =
        const { std::cell::Cell::new(false) };
}

impl ZOut {
    /// fileio.c:bfwrite, the split half: `rest` did not fit in the current
    /// split (or its write failed: `err`); close the split (or keep it for
    /// its local header), open the next and write `rest` there.
    // port: fileio.c:bfwrite
    pub(crate) fn next_split(
        &mut self,
        rest: &[u8],
        mode: Bf,
        err: Option<io::Error>,
    ) -> io::Result<()> {
        let header = mode != Bf::Data;
        let mut sp = self.split.take().expect("splitting");
        let r = (|| -> io::Result<()> {
            self.bytes_prev_splits += self.w.offset();
            if let Some(e) = err {
                if sp.method == 1 {
                    // writing all splits to the same place: bad
                    return Err(split_fail(
                        ZE_WRITE,
                        m::COULD_NOT_WRITE_SPLIT.as_bytes(),
                        Some(e),
                    ));
                }
                // a split must be at least 64K, except the last
                if self.w.offset() < 64 * 0x400 {
                    return Err(split_fail(
                        ZE_WRITE,
                        m::NOT_ENOUGH_SPACE_SPLIT.as_bytes(),
                        Some(e),
                    ));
                }
            }
            let old = std::mem::replace(&mut self.w, Box::new(super::sink::Closed));
            let oldname = sp.tempzip.clone();
            if sp.method == 1 && self.current_local_disk == self.current_disk {
                // keep this split open so its local header can be updated
                sp.local = Some(LocalSplit {
                    w: old,
                    tempname: oldname,
                });
            } else {
                sp.close_split(&mut self.pending, self.current_disk, old, &oldname)?;
                sp.published = true;
            }
            self.cd_entries_this_disk = 0;
            self.current_disk += 1;
            if sp.method == 2 && sp.bell {
                // bell when pausing to ask for the next split
                self.pending.push(Pend::Raw(b"\x07".to_vec()));
            }
            if sp.method == 2 {
                sp.ask_for_split_write_path(&mut self.pending, self.current_disk);
            }
            self.w = sp.open_temp()?;
            sp.published = false;
            #[cfg(test)]
            let r = if FAIL_SPLIT_REST.with(std::cell::Cell::get) {
                Err(io::Error::from(io::ErrorKind::StorageFull))
            } else {
                self.w.write_all(rest)
            };
            #[cfg(not(test))]
            let r = self.w.write_all(rest);
            let n = if r.is_ok() { rest.len() } else { 0 };
            if !header {
                self.bytes_this_entry += n as u64;
            }
            if let Err(e) = r {
                // the buffer is bigger than a split, or the disk full. Ruling
                // Z23 (deviation 21): -sp's "let the user choose another
                // disk" retry is not made (it would open another temporary
                // for ever on a full disk): both methods end here
                if sp.method == 2 {
                    self.pending.push(Pend::Message({
                        let mut l = Vec::new();
                        printf(
                            &mut l,
                            m::ZIP_WARNING,
                            &[m::NOT_ENOUGH_ROOM.as_bytes(), b""],
                        );
                        l.pop();
                        l
                    }));
                }
                return Err(split_fail(ZE_WRITE, m::NOT_ENOUGH_ROOM.as_bytes(), Some(e)));
            }
            if header {
                if sp.method == 1 && self.current_local_disk != self.current_disk {
                    // the header did not fit in the last split: that one
                    // can be closed now
                    if let Some(l) = sp.local.take() {
                        sp.close_split(
                            &mut self.pending,
                            self.current_local_disk,
                            l.w,
                            &l.tempname,
                        )?;
                    }
                }
                self.current_local_offset = 0;
                self.current_local_disk = self.current_disk;
            }
            Ok(())
        })();
        self.split = Some(sp);
        r
    }

    /// zipfile.c:putlocal's rewrite under `-s`: to the split with the local
    /// header (the current one, or the one kept open for it), which is then
    /// closed if it is not the current one.
    pub(crate) fn rewrite_in_local_split(&mut self, b: &[u8]) -> io::Result<()> {
        let mut sp = self.split.take().expect("splitting");
        let r = (|| -> io::Result<()> {
            match sp.local.as_mut() {
                Some(l) => l.w.write_here(b)?,
                None => self.w.write_here(b)?,
            }
            if self.current_local_disk != self.current_disk
                && let Some(l) = sp.local.take()
            {
                sp.close_split(&mut self.pending, self.current_local_disk, l.w, &l.tempname)?;
            }
            Ok(())
        })();
        self.split = Some(sp);
        r
    }

    /// zipup.c's seek of `current_local_file` to the local header, when it
    /// is not the current split.
    pub(crate) fn seek_local_split(&mut self, at: u64) -> io::Result<()> {
        match self.split.as_mut() {
            Some(s) if s.method == 1 => match s.local.as_mut() {
                Some(l) => l.w.seek_to(at),
                None => Ok(()),
            },
            _ => Ok(()),
        }
    }

    /// zipfile.c:zipcopy's end under `-s`: the split with the local header
    /// can be closed once the entry is copied.
    pub(crate) fn close_local_split(&mut self) -> io::Result<()> {
        let Some(mut sp) = self.split.take() else {
            return Ok(());
        };
        let r = if sp.method == 1 && self.current_local_disk != self.current_disk {
            match sp.local.take() {
                Some(l) => {
                    sp.close_split(&mut self.pending, self.current_local_disk, l.w, &l.tempname)
                }
                None => Ok(()),
            }
        } else {
            Ok(())
        };
        self.split = Some(sp);
        r
    }
}

/// zip.c:main's `ZIPERR(ZE_ABORT, ...)` and friends for the split prompts:
/// the code an answer gave, as `scanzipf_*` returns it.
pub(crate) fn is_abort(code: i32) -> bool {
    code == ZE_ABORT
}

/// The codes an answer can give besides `ZE_OK`.
pub(crate) const ANSWER_EOF: i32 = ZE_EOF;
pub(crate) const ANSWER_SKIP: i32 = ZE_FORM;

#[cfg(test)]
mod tests;