Skip to main content

zxc/
file.rs

1// SPDX-License-Identifier: BSD-3-Clause
2/*
3 * ZXC - High-performance lossless compression
4 *
5 * Copyright (c) Bertrand Lebonnois and contributors.
6 */
7
8//! File-based multi-threaded streaming API.
9
10use std::fs::{self, File, OpenOptions};
11use std::io;
12use std::path::{Path, PathBuf};
13use std::sync::atomic::{AtomicU32, Ordering};
14
15#[cfg(unix)]
16use std::os::unix::io::AsRawFd;
17
18use crate::ctx::{RawCctx, RawDctx, thread_param};
19use crate::error::error_from_code;
20use crate::{Error, Level};
21
22/// Destination of a file pipeline. A regular or new output is written to a
23/// sibling temporary file renamed over it on success, so a failed call leaves
24/// the previous content intact. It is written in place when that is
25/// impossible: a device or FIFO, or a directory that refuses the temporary
26/// file.
27struct Output {
28    /// Taken on commit, so the file is closed before the rename.
29    file: Option<File>,
30    /// Temporary path and its rename target; `None` once in place.
31    pending: Option<(PathBuf, PathBuf)>,
32}
33
34static TMP_SEQ: AtomicU32 = AtomicU32::new(0);
35
36/// The file `path` ends up naming, which need not exist: canonicalize() fails on
37/// a link to a missing target.
38fn resolve_output(path: &Path) -> PathBuf {
39    if let Ok(resolved) = fs::canonicalize(path) {
40        return resolved;
41    }
42    let mut path = path.to_path_buf();
43    // A loop ends here too, and fails when opened.
44    for _ in 0..40 {
45        let Ok(target) = fs::read_link(&path) else {
46            break;
47        };
48        path = path.parent().unwrap_or(Path::new("")).join(target);
49    }
50    path
51}
52
53#[cfg(unix)]
54fn same_file(a: &File, b: &File) -> io::Result<bool> {
55    use std::os::unix::fs::MetadataExt;
56    let (a, b) = (a.metadata()?, b.metadata()?);
57    Ok((a.dev(), a.ino()) == (b.dev(), b.ino()))
58}
59
60#[cfg(windows)]
61fn same_file(a: &File, b: &File) -> io::Result<bool> {
62    use std::os::windows::io::AsRawHandle;
63    use windows_sys::Win32::Storage::FileSystem::{
64        BY_HANDLE_FILE_INFORMATION, GetFileInformationByHandle,
65    };
66    fn id(f: &File) -> io::Result<(u32, u32, u32)> {
67        let mut info: BY_HANDLE_FILE_INFORMATION = unsafe { std::mem::zeroed() };
68        if unsafe { GetFileInformationByHandle(f.as_raw_handle() as _, &mut info) } == 0 {
69            return Err(io::Error::last_os_error());
70        }
71        Ok((
72            info.dwVolumeSerialNumber,
73            info.nFileIndexHigh,
74            info.nFileIndexLow,
75        ))
76    }
77    Ok(id(a)? == id(b)?)
78}
79
80impl Output {
81    /// `input` is the open source, which a pipeline must not overwrite.
82    fn create(path: &Path, input: &File) -> io::Result<Self> {
83        // Replace a link's target, not the link.
84        let path = resolve_output(path);
85        // Opening the existing output fails where File::create would, tells a
86        // device or FIFO apart, gives the mode the replacement keeps, and is
87        // the handle to fall back on.
88        let mut existing = match OpenOptions::new().write(true).open(&path) {
89            Ok(file) => {
90                let meta = file.metadata()?;
91                if !meta.is_file() {
92                    return Ok(Self {
93                        file: Some(file),
94                        pending: None,
95                    });
96                }
97                if same_file(&file, input)? {
98                    return Err(io::Error::new(
99                        io::ErrorKind::InvalidInput,
100                        "input and output are the same file",
101                    ));
102                }
103                Some((file, meta.permissions()))
104            }
105            Err(e) if e.kind() == io::ErrorKind::NotFound => None,
106            Err(e) => return Err(e),
107        };
108        let (file, tmp) = loop {
109            // Not derived from the output's name, which may leave no room for a suffix.
110            let tmp = path.parent().unwrap_or(Path::new("")).join(format!(
111                ".zxc-{}-{}.tmp",
112                std::process::id(),
113                TMP_SEQ.fetch_add(1, Ordering::Relaxed)
114            ));
115            match OpenOptions::new().write(true).create_new(true).open(&tmp) {
116                Ok(file) => break (file, tmp),
117                Err(e) if e.kind() == io::ErrorKind::AlreadyExists => continue,
118                Err(e) if e.kind() == io::ErrorKind::PermissionDenied => match existing.take() {
119                    // The directory refuses a sibling: truncate the output in place.
120                    Some((file, _)) => {
121                        file.set_len(0)?;
122                        return Ok(Self {
123                            file: Some(file),
124                            pending: None,
125                        });
126                    }
127                    None => return Err(e),
128                },
129                Err(e) => return Err(e),
130            }
131        };
132        let out = Self {
133            file: Some(file),
134            pending: Some((tmp, path)),
135        };
136        if let Some((_, mode)) = existing {
137            out.file().set_permissions(mode)?; // drop removes the temporary file
138        }
139        Ok(out)
140    }
141
142    fn file(&self) -> &File {
143        self.file.as_ref().expect("output is open")
144    }
145
146    /// Closes the output and, for a temporary file, renames it into place.
147    fn commit(mut self) -> io::Result<()> {
148        self.file = None;
149        match self.pending.take() {
150            Some((tmp, target)) => fs::rename(&tmp, target).inspect_err(|_| {
151                let _ = fs::remove_file(&tmp);
152            }),
153            None => Ok(()),
154        }
155    }
156}
157
158impl Drop for Output {
159    /// Removes the temporary file of an uncommitted output.
160    fn drop(&mut self) {
161        self.file = None;
162        if let Some((tmp, _)) = self.pending.take() {
163            let _ = fs::remove_file(tmp);
164        }
165    }
166}
167
168/// Options for streaming compression operations.
169#[derive(Debug, Clone)]
170pub struct StreamCompressOptions {
171    /// Compression level (default: `Level::Default`)
172    pub level: Level,
173    /// Number of worker threads (default: `None` = auto-detect CPU cores).
174    /// At most the library maximum (512); more is [`Error::BadParam`].
175    pub threads: Option<usize>,
176    /// Write checksums for data integrity (default: `false`)
177    pub checksum: bool,
178    /// Enable seek table for random-access decompression (default: `false`)
179    pub seekable: bool,
180}
181
182impl Default for StreamCompressOptions {
183    fn default() -> Self {
184        Self {
185            level: Level::Default,
186            threads: None,
187            checksum: false,
188            seekable: false,
189        }
190    }
191}
192
193impl StreamCompressOptions {
194    /// Create options with the specified compression level.
195    pub fn with_level(level: Level) -> Self {
196        Self {
197            level,
198            ..Default::default()
199        }
200    }
201
202    /// Set the number of worker threads.
203    pub fn threads(mut self, n: usize) -> Self {
204        self.threads = Some(n);
205        self
206    }
207
208    /// Writes checksums for data integrity.
209    pub fn with_checksum(mut self) -> Self {
210        self.checksum = true;
211        self
212    }
213
214    /// Writes no checksums, which is the default.
215    #[deprecated(note = "checksums are off by default: drop this call")]
216    pub fn without_checksum(mut self) -> Self {
217        self.checksum = false;
218        self
219    }
220
221    /// Enable seek table for random-access decompression.
222    pub fn with_seekable(mut self) -> Self {
223        self.seekable = true;
224        self
225    }
226}
227
228/// Options for streaming decompression operations.
229#[derive(Debug, Clone)]
230pub struct StreamDecompressOptions {
231    /// Number of worker threads (default: `None` = auto-detect CPU cores).
232    /// At most the library maximum (512); more is [`Error::BadParam`].
233    pub threads: Option<usize>,
234    /// Verify checksums when the archive carries them (default: `true`)
235    pub verify_checksum: bool,
236}
237
238impl Default for StreamDecompressOptions {
239    fn default() -> Self {
240        Self {
241            threads: None,
242            verify_checksum: true,
243        }
244    }
245}
246
247impl StreamDecompressOptions {
248    /// Set the number of worker threads.
249    pub fn threads(mut self, n: usize) -> Self {
250        self.threads = Some(n);
251        self
252    }
253
254    /// Skip checksum verification.
255    pub fn skip_checksum(mut self) -> Self {
256        self.verify_checksum = false;
257        self
258    }
259}
260
261/// Errors specific to the streaming file API.
262#[derive(Debug, thiserror::Error)]
263#[non_exhaustive]
264pub enum StreamError {
265    /// I/O error during file operations
266    #[error("I/O error: {0}")]
267    Io(#[from] io::Error),
268
269    /// Error from buffer operations
270    #[error("buffer error: {0}")]
271    BufferError(#[from] Error),
272
273    /// Invalid compressed file
274    #[error("invalid compressed file")]
275    InvalidFile,
276}
277
278/// Result type for streaming operations.
279pub type StreamResult<T> = std::result::Result<T, StreamError>;
280
281/// Closes the output FILE*, reporting a failure: close() can fail on its own
282/// (e.g. deferred write errors on NFS), after the library already flushed.
283unsafe fn close_output(f: *mut libc::FILE) -> io::Result<()> {
284    if unsafe { libc::fclose(f) } == 0 {
285        return Ok(());
286    }
287    Err(close_error())
288}
289
290#[cfg(unix)]
291fn close_error() -> io::Error {
292    let err = io::Error::last_os_error();
293    if err.raw_os_error() == Some(0) {
294        return io::Error::other("failed to close output file");
295    }
296    err
297}
298
299/// fclose() reports through the CRT errno, not GetLastError(), so
300/// last_os_error() would return an unrelated Win32 value. CRT errno values are
301/// not Win32 codes either, so they cannot go through from_raw_os_error().
302#[cfg(windows)]
303fn close_error() -> io::Error {
304    unsafe extern "C" {
305        fn _errno() -> *mut libc::c_int;
306    }
307    let code = unsafe { *_errno() };
308    if code == 0 {
309        return io::Error::other("failed to close output file");
310    }
311    let msg = unsafe { std::ffi::CStr::from_ptr(libc::strerror(code)) };
312    io::Error::other(format!(
313        "failed to close output file: {}",
314        msg.to_string_lossy()
315    ))
316}
317
318/// Convert a Rust File to a C FILE* for read operations.
319///
320/// This function duplicates the file descriptor before passing it to fdopen,
321/// so the returned FILE* owns its own fd and must be closed with fclose().
322#[cfg(unix)]
323unsafe fn file_to_c_file_read(file: &File) -> *mut libc::FILE {
324    let fd = file.as_raw_fd();
325    // Duplicate the fd so C FILE* has its own ownership
326    let dup_fd = unsafe { libc::dup(fd) };
327    if dup_fd < 0 {
328        return std::ptr::null_mut();
329    }
330
331    let file_ptr = unsafe { libc::fdopen(dup_fd, c"rb".as_ptr()) };
332    if file_ptr.is_null() {
333        // fdopen failed, close the duplicated fd to avoid leak
334        unsafe {
335            libc::close(dup_fd);
336        }
337    }
338    file_ptr
339}
340
341/// Convert a Rust File to a C FILE* for write operations.
342///
343/// This function duplicates the file descriptor before passing it to fdopen,
344/// so the returned FILE* owns its own fd and must be closed with fclose().
345#[cfg(unix)]
346unsafe fn file_to_c_file_write(file: &File) -> *mut libc::FILE {
347    let fd = file.as_raw_fd();
348    // Duplicate the fd so C FILE* has its own ownership
349    let dup_fd = unsafe { libc::dup(fd) };
350    if dup_fd < 0 {
351        return std::ptr::null_mut();
352    }
353
354    let file_ptr = unsafe { libc::fdopen(dup_fd, c"wb".as_ptr()) };
355    if file_ptr.is_null() {
356        // fdopen failed, close the duplicated fd to avoid leak
357        unsafe {
358            libc::close(dup_fd);
359        }
360    }
361    file_ptr
362}
363
364/// Convert a Rust File to a C FILE* for read operations (Windows).
365///
366/// This function duplicates the file handle before passing it to the C runtime,
367/// so the returned FILE* owns its own handle and must be closed with fclose().
368#[cfg(windows)]
369unsafe fn file_to_c_file_read(file: &File) -> *mut libc::FILE {
370    use std::os::windows::io::AsRawHandle;
371
372    let handle = file.as_raw_handle();
373
374    // Duplicate the handle so C FILE* has its own ownership
375    let mut dup_handle: *mut std::ffi::c_void = std::ptr::null_mut();
376    let result = unsafe {
377        windows_sys::Win32::Foundation::DuplicateHandle(
378            windows_sys::Win32::System::Threading::GetCurrentProcess(),
379            handle as *mut std::ffi::c_void,
380            windows_sys::Win32::System::Threading::GetCurrentProcess(),
381            &mut dup_handle,
382            0,
383            0,
384            windows_sys::Win32::Foundation::DUPLICATE_SAME_ACCESS,
385        )
386    };
387
388    if result == 0 {
389        return std::ptr::null_mut();
390    }
391
392    let fd = unsafe { libc::open_osfhandle(dup_handle as libc::intptr_t, libc::O_RDONLY) };
393    if fd < 0 {
394        // open_osfhandle failed, close the duplicated handle to avoid leak
395        unsafe {
396            windows_sys::Win32::Foundation::CloseHandle(dup_handle);
397        }
398        return std::ptr::null_mut();
399    }
400
401    let file_ptr = unsafe { libc::fdopen(fd, c"rb".as_ptr()) };
402    if file_ptr.is_null() {
403        // fdopen failed, close the fd (which will close the handle)
404        unsafe {
405            libc::close(fd);
406        }
407    }
408    file_ptr
409}
410
411/// Convert a Rust File to a C FILE* for write operations (Windows).
412///
413/// This function duplicates the file handle before passing it to the C runtime,
414/// so the returned FILE* owns its own handle and must be closed with fclose().
415#[cfg(windows)]
416unsafe fn file_to_c_file_write(file: &File) -> *mut libc::FILE {
417    use std::os::windows::io::AsRawHandle;
418
419    let handle = file.as_raw_handle();
420
421    // Duplicate the handle so C FILE* has its own ownership
422    let mut dup_handle: *mut std::ffi::c_void = std::ptr::null_mut();
423    let result = unsafe {
424        windows_sys::Win32::Foundation::DuplicateHandle(
425            windows_sys::Win32::System::Threading::GetCurrentProcess(),
426            handle as *mut std::ffi::c_void,
427            windows_sys::Win32::System::Threading::GetCurrentProcess(),
428            &mut dup_handle,
429            0,
430            0,
431            windows_sys::Win32::Foundation::DUPLICATE_SAME_ACCESS,
432        )
433    };
434
435    if result == 0 {
436        return std::ptr::null_mut();
437    }
438
439    let fd = unsafe { libc::open_osfhandle(dup_handle as libc::intptr_t, libc::O_WRONLY) };
440    if fd < 0 {
441        // open_osfhandle failed, close the duplicated handle to avoid leak
442        unsafe {
443            windows_sys::Win32::Foundation::CloseHandle(dup_handle);
444        }
445        return std::ptr::null_mut();
446    }
447
448    let file_ptr = unsafe { libc::fdopen(fd, c"wb".as_ptr()) };
449    if file_ptr.is_null() {
450        // fdopen failed, close the fd (which will close the handle)
451        unsafe {
452            libc::close(fd);
453        }
454    }
455    file_ptr
456}
457
458/// Compresses a file using multi-threaded streaming.
459///
460/// This is the recommended method for compressing large files, as it:
461/// - Processes data in chunks without loading the entire file into memory
462/// - Uses multiple CPU cores for parallel compression
463/// - Provides better throughput for files larger than a few MB
464///
465/// # Arguments
466///
467/// * `input` - Path to the input file
468/// * `output` - Path to the output file
469/// * `level` - Compression level
470/// * `threads` - Number of threads (`None` = auto-detect CPU cores)
471/// * `checksum` - Optional checksum for data integrity (`None` = disabled for maximum performance)
472///
473/// # Example
474///
475/// ```rust,no_run
476/// use zxc::{compress_file, Level};
477///
478/// // Maximum performance (no checksum, auto threads)
479/// let bytes = compress_file("input.bin", "output.zxc", Level::Default, None, None)?;
480///
481/// // With data integrity verification
482/// let bytes = compress_file("input.bin", "output.zxc", Level::Default, None, Some(true))?;
483///
484/// // Custom configuration
485/// let bytes = compress_file("input.bin", "output.zxc", Level::Compact, Some(4), Some(true))?;
486/// # Ok::<(), zxc::StreamError>(())
487/// ```
488pub fn compress_file<P: AsRef<Path>>(
489    input: P,
490    output: P,
491    level: Level,
492    threads: Option<usize>,
493    checksum: Option<bool>,
494) -> StreamResult<u64> {
495    compress_file_with_options(
496        input,
497        output,
498        &StreamCompressOptions {
499            level,
500            threads,
501            checksum: checksum.unwrap_or(false),
502            seekable: false,
503        },
504    )
505}
506
507/// Compresses a file using multi-threaded streaming, honouring `opts`.
508///
509/// Unlike [`compress_file`], this exposes every [`StreamCompressOptions`]
510/// field — including `seekable`, which appends a seek table for
511/// random-access decompression via [`crate::seekable::Seekable`].
512///
513/// An output that is the input file (links included) is refused with
514/// [`io::ErrorKind::InvalidInput`].
515///
516/// # Example
517///
518/// ```rust,no_run
519/// use zxc::{compress_file_with_options, Level, StreamCompressOptions};
520///
521/// let opts = StreamCompressOptions::with_level(Level::Compact)
522///     .threads(4)
523///     .with_seekable();
524/// let bytes = compress_file_with_options("input.bin", "output.zxc", &opts)?;
525/// # Ok::<(), zxc::StreamError>(())
526/// ```
527pub fn compress_file_with_options<P: AsRef<Path>>(
528    input: P,
529    output: P,
530    opts: &StreamCompressOptions,
531) -> StreamResult<u64> {
532    // The pipeline reads its parameters from a configured context; validate
533    // them before touching the files.
534    let mut cfg = RawCctx::new()?;
535    cfg.set_params(
536        opts.level,
537        opts.checksum,
538        opts.seekable,
539        thread_param(opts.threads),
540    )?;
541
542    let f_in = File::open(input)?;
543    let f_out = Output::create(output.as_ref(), &f_in)?;
544
545    unsafe {
546        let c_in = file_to_c_file_read(&f_in);
547        let c_out = file_to_c_file_write(f_out.file());
548
549        // Check for errors and cleanup on failure
550        if c_in.is_null() {
551            if !c_out.is_null() {
552                libc::fclose(c_out);
553            }
554            return Err(StreamError::Io(io::Error::last_os_error()));
555        }
556        if c_out.is_null() {
557            libc::fclose(c_in);
558            return Err(StreamError::Io(io::Error::last_os_error()));
559        }
560
561        let result = zxc_sys::zxc_stream_compress(c_in, c_out, cfg.as_ptr());
562
563        // Always close C FILE handles (they own duplicated fds)
564        let closed = close_output(c_out);
565        libc::fclose(c_in);
566
567        if result < 0 {
568            return Err(StreamError::BufferError(error_from_code(result)));
569        }
570        closed?;
571        f_out.commit()?;
572        Ok(result as u64)
573    }
574}
575
576/// Decompresses a file using multi-threaded streaming.
577///
578/// # Example
579///
580/// ```rust,no_run
581/// use zxc::decompress_file;
582///
583/// // Decompress with auto-detected thread count
584/// let bytes = decompress_file("compressed.zxc", "output.bin", None)?;
585/// println!("Decompressed {} bytes", bytes);
586/// # Ok::<(), zxc::StreamError>(())
587/// ```
588pub fn decompress_file<P: AsRef<Path>>(
589    input: P,
590    output: P,
591    threads: Option<usize>,
592) -> StreamResult<u64> {
593    decompress_file_with_options(
594        input,
595        output,
596        &StreamDecompressOptions {
597            threads,
598            verify_checksum: true,
599        },
600    )
601}
602
603/// Decompresses a file using multi-threaded streaming, honouring `opts`.
604///
605/// Unlike [`decompress_file`], this exposes every
606/// [`StreamDecompressOptions`] field — including `verify_checksum`, which
607/// can be disabled to skip integrity verification.
608///
609/// An output that is the input file (links included) is refused with
610/// [`io::ErrorKind::InvalidInput`].
611///
612/// # Example
613///
614/// ```rust,no_run
615/// use zxc::{decompress_file_with_options, StreamDecompressOptions};
616///
617/// let opts = StreamDecompressOptions::default().skip_checksum();
618/// let bytes = decompress_file_with_options("compressed.zxc", "output.bin", &opts)?;
619/// # Ok::<(), zxc::StreamError>(())
620/// ```
621pub fn decompress_file_with_options<P: AsRef<Path>>(
622    input: P,
623    output: P,
624    opts: &StreamDecompressOptions,
625) -> StreamResult<u64> {
626    let mut cfg = RawDctx::new()?;
627    cfg.set_params(opts.verify_checksum, thread_param(opts.threads))?;
628
629    let f_in = File::open(input)?;
630    let f_out = Output::create(output.as_ref(), &f_in)?;
631
632    unsafe {
633        let c_in = file_to_c_file_read(&f_in);
634        let c_out = file_to_c_file_write(f_out.file());
635
636        // Check for errors and cleanup on failure
637        if c_in.is_null() {
638            if !c_out.is_null() {
639                libc::fclose(c_out);
640            }
641            return Err(StreamError::Io(io::Error::last_os_error()));
642        }
643        if c_out.is_null() {
644            libc::fclose(c_in);
645            return Err(StreamError::Io(io::Error::last_os_error()));
646        }
647
648        let result = zxc_sys::zxc_stream_decompress(c_in, c_out, cfg.as_ptr());
649
650        // Always close C FILE handles (they own duplicated fds)
651        let closed = close_output(c_out);
652        libc::fclose(c_in);
653
654        if result < 0 {
655            return Err(StreamError::BufferError(error_from_code(result)));
656        }
657        closed?;
658        f_out.commit()?;
659        Ok(result as u64)
660    }
661}
662
663/// Returns the decompressed size stored in a compressed file.
664///
665/// This reads the file footer without performing decompression,
666/// useful for pre-allocating buffers or showing progress.
667///
668/// # Example
669///
670/// ```rust,no_run
671/// use zxc::file_decompressed_size;
672///
673/// let size = file_decompressed_size("compressed.zxc")?;
674/// println!("Original size: {} bytes", size);
675/// # Ok::<(), zxc::StreamError>(())
676/// ```
677pub fn file_decompressed_size<P: AsRef<Path>>(path: P) -> StreamResult<u64> {
678    let f = File::open(path)?;
679
680    unsafe {
681        let c_file = file_to_c_file_read(&f);
682
683        if c_file.is_null() {
684            return Err(StreamError::Io(io::Error::last_os_error()));
685        }
686
687        let result = zxc_sys::zxc_stream_get_decompressed_size(c_file);
688
689        // The FILE* owns a dup'd fd; it must be closed here or every call
690        // leaks one file descriptor.
691        libc::fclose(c_file);
692
693        if result < 0 {
694            Err(StreamError::InvalidFile)
695        } else {
696            Ok(result as u64)
697        }
698    }
699}
700
701#[cfg(test)]
702mod tests {
703    use crate::*;
704    use std::fs;
705    use std::io::Write;
706
707    fn temp_path(name: &str) -> String {
708        let dir_name = format!("zxc_test_{}", std::process::id());
709
710        // Try the system temp directory first
711        let mut path = std::env::temp_dir();
712        path.push(&dir_name);
713
714        // If we can't create the directory in the system temp, fall back
715        // to a subdirectory relative to the current working directory.
716        // This happens on some Windows CI runners where TEMP is missing or
717        // points to a path the process cannot access.
718        if fs::create_dir_all(&path).is_err() {
719            path = std::env::current_dir().expect("cannot determine current directory");
720            path.push(&dir_name);
721            fs::create_dir_all(&path).expect("failed to create temp directory in current dir");
722        }
723
724        path.push(name);
725        path.to_string_lossy().into_owned()
726    }
727
728    #[test]
729    fn test_file_roundtrip() {
730        let input_path = temp_path("roundtrip_input.bin");
731        let compressed_path = temp_path("roundtrip_compressed.zxc");
732        let output_path = temp_path("roundtrip_output.bin");
733
734        // Create test data
735        let data: Vec<u8> = (0..64 * 1024) // 64 KB
736            .map(|i| ((i % 256) ^ ((i / 256) % 256)) as u8)
737            .collect();
738
739        // Write test file
740        {
741            let mut f = fs::File::create(&input_path).unwrap();
742            f.write_all(&data).unwrap();
743        }
744
745        // Compress
746        let compressed_size =
747            compress_file(&input_path, &compressed_path, Level::Default, None, None).unwrap();
748        assert!(compressed_size > 0);
749
750        // Decompress
751        let decompressed_size = decompress_file(&compressed_path, &output_path, None).unwrap();
752        assert_eq!(decompressed_size, data.len() as u64);
753
754        // Verify content
755        let result = fs::read(&output_path).unwrap();
756        assert_eq!(result, data);
757
758        // Cleanup
759        let _ = fs::remove_file(&input_path);
760        let _ = fs::remove_file(&compressed_path);
761        let _ = fs::remove_file(&output_path);
762    }
763
764    /// A writable output in a directory that refuses new files is written in
765    /// place.
766    #[cfg(unix)]
767    #[test]
768    fn test_file_in_place_fallback() {
769        use std::os::unix::fs::PermissionsExt;
770        if unsafe { libc::geteuid() } == 0 {
771            return; // root writes anywhere
772        }
773        let dir = std::path::PathBuf::from(temp_path("in_place_fallback"));
774        let locked = dir.join("locked");
775        let _ = fs::set_permissions(&locked, fs::Permissions::from_mode(0o755));
776        let _ = fs::remove_dir_all(&dir);
777        fs::create_dir_all(&locked).unwrap();
778        let (input, out) = (dir.join("in"), locked.join("out"));
779        let payload = b"in-place fallback";
780        fs::write(&input, payload).unwrap();
781        fs::write(&out, b"old").unwrap();
782        fs::set_permissions(&locked, fs::Permissions::from_mode(0o555)).unwrap();
783
784        let result = compress_file(&input, &out, Level::Default, None, None);
785        let arc = fs::read(&out).unwrap();
786        let left = fs::read_dir(&locked).unwrap().count();
787        fs::set_permissions(&locked, fs::Permissions::from_mode(0o755)).unwrap();
788        let _ = fs::remove_dir_all(&dir);
789
790        result.expect("compress_file into a read-only directory");
791        assert_eq!(decompress(&arc).unwrap(), payload);
792        assert_eq!(left, 1, "temporary file left behind");
793    }
794
795    /// A failed call, refused option or bad data, leaves an existing output
796    /// untouched and no temporary file behind; a successful one replaces it.
797    #[test]
798    fn test_file_failure_keeps_output() {
799        let dir = std::path::PathBuf::from(temp_path("failure_keeps_output"));
800        let _ = fs::remove_dir_all(&dir);
801        fs::create_dir_all(&dir).unwrap();
802        let (input, arc, corrupt, out) = (
803            dir.join("in"),
804            dir.join("arc"),
805            dir.join("corrupt"),
806            dir.join("out"),
807        );
808        let payload = b"file pipeline input ".repeat(500);
809        fs::write(&input, &payload).unwrap();
810        compress_file(&input, &arc, Level::Default, None, Some(true)).unwrap();
811        let mut bad = fs::read(&arc).unwrap();
812        let mid = bad.len() / 2;
813        bad[mid] ^= 0xFF;
814        fs::write(&corrupt, &bad).unwrap();
815        let keep = b"previous content must survive";
816        fs::write(&out, keep).unwrap();
817
818        assert!(compress_file(&input, &out, Level::Default, Some(1000), None).is_err());
819        assert!(decompress_file(&corrupt, &out, None).is_err());
820        assert_eq!(fs::read(&out).unwrap(), keep);
821        assert_eq!(
822            fs::read_dir(&dir).unwrap().count(),
823            4,
824            "temporary file left behind"
825        );
826
827        decompress_file(&arc, &out, None).unwrap();
828        assert_eq!(fs::read(&out).unwrap(), payload);
829        #[cfg(unix)]
830        {
831            use std::os::unix::fs::PermissionsExt;
832            fs::set_permissions(&out, fs::Permissions::from_mode(0o600)).unwrap();
833            decompress_file(&arc, &out, None).unwrap();
834            let mode = fs::metadata(&out).unwrap().permissions().mode();
835            assert_eq!(mode & 0o777, 0o600, "output mode not kept");
836            decompress_file(&arc, &std::path::PathBuf::from("/dev/null"), None).unwrap();
837        }
838        let _ = fs::remove_dir_all(&dir);
839    }
840
841    fn assert_same_file_refused(input: &std::path::Path, output: &std::path::Path, keep: &[u8]) {
842        for r in [
843            compress_file(input, output, Level::Default, Some(1), None),
844            decompress_file(input, output, Some(1)),
845        ] {
846            match r {
847                Err(StreamError::Io(e)) => {
848                    assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput)
849                }
850                other => panic!("same file not refused: {other:?}"),
851            }
852            assert_eq!(fs::read(input).unwrap(), keep);
853        }
854    }
855
856    #[test]
857    fn test_file_same_input_and_output_refused() {
858        let dir = std::path::PathBuf::from(temp_path("same_file"));
859        let _ = fs::remove_dir_all(&dir);
860        fs::create_dir_all(&dir).unwrap();
861        let (file, hard, other) = (dir.join("file"), dir.join("hard"), dir.join("other"));
862        let keep = b"precious payload";
863        fs::write(&file, keep).unwrap();
864        fs::hard_link(&file, &hard).unwrap();
865
866        assert_same_file_refused(&file, &file, keep);
867        assert_same_file_refused(&file, &hard, keep);
868        assert_eq!(
869            fs::read_dir(&dir).unwrap().count(),
870            2,
871            "temporary file left"
872        );
873
874        compress_file(&file, &other, Level::Default, Some(1), None).unwrap();
875        assert_eq!(decompress(&fs::read(&other).unwrap()).unwrap(), keep);
876        let _ = fs::remove_dir_all(&dir);
877    }
878
879    /// Here the output would be truncated in place, under the open input.
880    #[cfg(unix)]
881    #[test]
882    fn test_file_same_file_in_read_only_directory_refused() {
883        use std::os::unix::fs::{PermissionsExt, symlink};
884        if unsafe { libc::geteuid() } == 0 {
885            return; // root writes anywhere
886        }
887        let dir = std::path::PathBuf::from(temp_path("same_file_locked"));
888        let _ = fs::set_permissions(&dir, fs::Permissions::from_mode(0o755));
889        let _ = fs::remove_dir_all(&dir);
890        fs::create_dir_all(&dir).unwrap();
891        let (file, link) = (dir.join("file"), dir.join("link"));
892        let keep = b"precious payload";
893        fs::write(&file, keep).unwrap();
894        symlink(&file, &link).unwrap();
895        fs::set_permissions(&dir, fs::Permissions::from_mode(0o555)).unwrap();
896
897        let caught = std::panic::catch_unwind(|| {
898            assert_same_file_refused(&file, &file, keep);
899            assert_same_file_refused(&file, &link, keep);
900        });
901        fs::set_permissions(&dir, fs::Permissions::from_mode(0o755)).unwrap();
902        let _ = fs::remove_dir_all(&dir);
903        caught.unwrap();
904    }
905
906    #[cfg(unix)]
907    #[test]
908    fn test_file_dangling_link_output_keeps_the_link() {
909        use std::os::unix::fs::symlink;
910        let dir = std::path::PathBuf::from(temp_path("dangling_link"));
911        let _ = fs::remove_dir_all(&dir);
912        fs::create_dir_all(&dir).unwrap();
913        let input = dir.join("in");
914        let payload = b"through a dangling link";
915        fs::write(&input, payload).unwrap();
916        // link -> middle -> target, the first hop relative, the target missing.
917        let (link, middle, target) = (dir.join("link"), dir.join("middle"), dir.join("target"));
918        symlink("middle", &link).unwrap();
919        symlink(&target, &middle).unwrap();
920
921        compress_file(&input, &link, Level::Default, Some(1), None).unwrap();
922
923        assert!(
924            fs::symlink_metadata(&link)
925                .unwrap()
926                .file_type()
927                .is_symlink()
928        );
929        assert!(
930            fs::symlink_metadata(&middle)
931                .unwrap()
932                .file_type()
933                .is_symlink()
934        );
935        assert_eq!(decompress(&fs::read(&target).unwrap()).unwrap(), payload);
936
937        // A loop stays an error.
938        let looped = dir.join("loop");
939        symlink("loop", &looped).unwrap();
940        assert!(compress_file(&input, &looped, Level::Default, Some(1), None).is_err());
941        let _ = fs::remove_dir_all(&dir);
942    }
943
944    #[test]
945    fn test_file_checksum_builders() {
946        let dir = std::path::PathBuf::from(temp_path("checksum_builders"));
947        let _ = fs::remove_dir_all(&dir);
948        fs::create_dir_all(&dir).unwrap();
949        let (input, out) = (dir.join("in"), dir.join("out"));
950        fs::write(&input, b"builders").unwrap();
951        let has = |opts: &StreamCompressOptions| {
952            compress_file_with_options(&input, &out, opts).unwrap();
953            frame_info(&fs::read(&out).unwrap()).unwrap().has_checksum
954        };
955        let on = StreamCompressOptions::with_level(Level::Fast)
956            .threads(1)
957            .with_checksum();
958        assert!(has(&on));
959        #[allow(deprecated)]
960        let off = on.without_checksum();
961        assert!(!has(&off));
962        let _ = fs::remove_dir_all(&dir);
963    }
964
965    #[test]
966    fn test_file_output_name_at_the_length_limit() {
967        let dir = std::path::PathBuf::from(temp_path("long_name"));
968        let _ = fs::remove_dir_all(&dir);
969        fs::create_dir_all(&dir).unwrap();
970        let (input, arc) = (dir.join("in"), dir.join("arc"));
971        let payload = b"long output name";
972        fs::write(&input, payload).unwrap();
973        compress_file(&input, &arc, Level::Default, Some(1), None).unwrap();
974
975        // Legal on the file systems CI runs on, with no room for a suffix.
976        let output = dir.join("n".repeat(250));
977        for _new_then_existing in 0..2 {
978            compress_file(&input, &output, Level::Default, Some(1), None).unwrap();
979            assert_eq!(decompress(&fs::read(&output).unwrap()).unwrap(), payload);
980            decompress_file(&arc, &output, Some(1)).unwrap();
981            assert_eq!(fs::read(&output).unwrap(), payload);
982        }
983        assert_eq!(
984            fs::read_dir(&dir).unwrap().count(),
985            3,
986            "temporary file left"
987        );
988        let _ = fs::remove_dir_all(&dir);
989    }
990
991    #[test]
992    fn test_file_decompressed_size_query() {
993        let input_path = temp_path("size_input.bin");
994        let compressed_path = temp_path("size_compressed.zxc");
995
996        // Create test data
997        let data: Vec<u8> = (0..128 * 1024) // 128 KB
998            .map(|i| (i % 256) as u8)
999            .collect();
1000
1001        // Write and compress
1002        {
1003            let mut f = fs::File::create(&input_path).unwrap();
1004            f.write_all(&data).unwrap();
1005        }
1006        compress_file(&input_path, &compressed_path, Level::Default, None, None).unwrap();
1007
1008        // Query size
1009        let reported_size = file_decompressed_size(&compressed_path).unwrap();
1010        assert_eq!(reported_size, data.len() as u64);
1011
1012        // Cleanup
1013        let _ = fs::remove_file(&input_path);
1014        let _ = fs::remove_file(&compressed_path);
1015    }
1016
1017    #[test]
1018    fn test_file_all_levels() {
1019        let input_path = temp_path("levels_input.bin");
1020
1021        // Create test data
1022        let data: Vec<u8> = (0..32 * 1024) // 32 KB
1023            .map(|i| ((i % 256) ^ ((i / 256) % 256)) as u8)
1024            .collect();
1025
1026        {
1027            let mut f = fs::File::create(&input_path).unwrap();
1028            f.write_all(&data).unwrap();
1029        }
1030
1031        for level in Level::all() {
1032            let compressed_path = temp_path(&format!("levels_{:?}.zxc", level));
1033            let output_path = temp_path(&format!("levels_{:?}_out.bin", level));
1034
1035            // Compress with this level
1036            compress_file(&input_path, &compressed_path, *level, Some(2), None).unwrap();
1037
1038            // Decompress
1039            decompress_file(&compressed_path, &output_path, Some(2)).unwrap();
1040
1041            // Verify
1042            let result = fs::read(&output_path).unwrap();
1043            assert_eq!(result, data, "Data mismatch at level {:?}", level);
1044
1045            // Cleanup
1046            let _ = fs::remove_file(&compressed_path);
1047            let _ = fs::remove_file(&output_path);
1048        }
1049
1050        let _ = fs::remove_file(&input_path);
1051    }
1052
1053    #[test]
1054    fn test_file_multithreaded() {
1055        let input_path = temp_path("mt_input.bin");
1056        let compressed_path = temp_path("mt_compressed.zxc");
1057        let output_path = temp_path("mt_output.bin");
1058
1059        // Create larger test data (1 MB)
1060        let data: Vec<u8> = (0..1024 * 1024)
1061            .map(|i| ((i % 256) ^ ((i / 256) % 256)) as u8)
1062            .collect();
1063
1064        {
1065            let mut f = fs::File::create(&input_path).unwrap();
1066            f.write_all(&data).unwrap();
1067        }
1068
1069        // Test with different thread counts
1070        for threads in [1, 2, 4] {
1071            // Compress
1072            compress_file(
1073                &input_path,
1074                &compressed_path,
1075                Level::Default,
1076                Some(threads),
1077                None,
1078            )
1079            .unwrap();
1080
1081            // Decompress
1082            let size = decompress_file(&compressed_path, &output_path, Some(threads)).unwrap();
1083            assert_eq!(size, data.len() as u64);
1084
1085            // Verify
1086            let result = fs::read(&output_path).unwrap();
1087            assert_eq!(result, data, "Mismatch with {} threads", threads);
1088        }
1089
1090        // Cleanup
1091        let _ = fs::remove_file(&input_path);
1092        let _ = fs::remove_file(&compressed_path);
1093        let _ = fs::remove_file(&output_path);
1094    }
1095
1096    // A small output stays in the stdio buffer until the final flush, the only
1097    // write that reaches /dev/full (ENOSPC): it must still surface as an error.
1098    #[cfg(target_os = "linux")]
1099    #[test]
1100    fn test_file_write_failure_at_flush() {
1101        let input_path = temp_path("flush_input.bin");
1102        let compressed_path = temp_path("flush_compressed.zxc");
1103
1104        let data: Vec<u8> = (0..1000).map(|i| (i % 7) as u8).collect();
1105        fs::write(&input_path, &data).unwrap();
1106        compress_file(&input_path, &compressed_path, Level::Default, None, None).unwrap();
1107
1108        let r = compress_file(input_path.as_str(), "/dev/full", Level::Default, None, None);
1109        assert!(r.is_err(), "compress to /dev/full reported {r:?}");
1110        let r = decompress_file(compressed_path.as_str(), "/dev/full", None);
1111        assert!(r.is_err(), "decompress to /dev/full reported {r:?}");
1112
1113        let _ = fs::remove_file(&input_path);
1114        let _ = fs::remove_file(&compressed_path);
1115    }
1116}