1use 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
22struct Output {
28 file: Option<File>,
30 pending: Option<(PathBuf, PathBuf)>,
32}
33
34static TMP_SEQ: AtomicU32 = AtomicU32::new(0);
35
36fn 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 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 fn create(path: &Path, input: &File) -> io::Result<Self> {
83 let path = resolve_output(path);
85 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 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 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)?; }
139 Ok(out)
140 }
141
142 fn file(&self) -> &File {
143 self.file.as_ref().expect("output is open")
144 }
145
146 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 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#[derive(Debug, Clone)]
170pub struct StreamCompressOptions {
171 pub level: Level,
173 pub threads: Option<usize>,
176 pub checksum: bool,
178 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 pub fn with_level(level: Level) -> Self {
196 Self {
197 level,
198 ..Default::default()
199 }
200 }
201
202 pub fn threads(mut self, n: usize) -> Self {
204 self.threads = Some(n);
205 self
206 }
207
208 pub fn with_checksum(mut self) -> Self {
210 self.checksum = true;
211 self
212 }
213
214 #[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 pub fn with_seekable(mut self) -> Self {
223 self.seekable = true;
224 self
225 }
226}
227
228#[derive(Debug, Clone)]
230pub struct StreamDecompressOptions {
231 pub threads: Option<usize>,
234 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 pub fn threads(mut self, n: usize) -> Self {
250 self.threads = Some(n);
251 self
252 }
253
254 pub fn skip_checksum(mut self) -> Self {
256 self.verify_checksum = false;
257 self
258 }
259}
260
261#[derive(Debug, thiserror::Error)]
263#[non_exhaustive]
264pub enum StreamError {
265 #[error("I/O error: {0}")]
267 Io(#[from] io::Error),
268
269 #[error("buffer error: {0}")]
271 BufferError(#[from] Error),
272
273 #[error("invalid compressed file")]
275 InvalidFile,
276}
277
278pub type StreamResult<T> = std::result::Result<T, StreamError>;
280
281unsafe 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#[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#[cfg(unix)]
323unsafe fn file_to_c_file_read(file: &File) -> *mut libc::FILE {
324 let fd = file.as_raw_fd();
325 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 unsafe {
335 libc::close(dup_fd);
336 }
337 }
338 file_ptr
339}
340
341#[cfg(unix)]
346unsafe fn file_to_c_file_write(file: &File) -> *mut libc::FILE {
347 let fd = file.as_raw_fd();
348 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 unsafe {
358 libc::close(dup_fd);
359 }
360 }
361 file_ptr
362}
363
364#[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 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 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 unsafe {
405 libc::close(fd);
406 }
407 }
408 file_ptr
409}
410
411#[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 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 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 unsafe {
452 libc::close(fd);
453 }
454 }
455 file_ptr
456}
457
458pub 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
507pub fn compress_file_with_options<P: AsRef<Path>>(
528 input: P,
529 output: P,
530 opts: &StreamCompressOptions,
531) -> StreamResult<u64> {
532 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 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 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
576pub 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
603pub 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 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 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
663pub 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 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 let mut path = std::env::temp_dir();
712 path.push(&dir_name);
713
714 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 let data: Vec<u8> = (0..64 * 1024) .map(|i| ((i % 256) ^ ((i / 256) % 256)) as u8)
737 .collect();
738
739 {
741 let mut f = fs::File::create(&input_path).unwrap();
742 f.write_all(&data).unwrap();
743 }
744
745 let compressed_size =
747 compress_file(&input_path, &compressed_path, Level::Default, None, None).unwrap();
748 assert!(compressed_size > 0);
749
750 let decompressed_size = decompress_file(&compressed_path, &output_path, None).unwrap();
752 assert_eq!(decompressed_size, data.len() as u64);
753
754 let result = fs::read(&output_path).unwrap();
756 assert_eq!(result, data);
757
758 let _ = fs::remove_file(&input_path);
760 let _ = fs::remove_file(&compressed_path);
761 let _ = fs::remove_file(&output_path);
762 }
763
764 #[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; }
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 #[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 #[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; }
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 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 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 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 let data: Vec<u8> = (0..128 * 1024) .map(|i| (i % 256) as u8)
999 .collect();
1000
1001 {
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 let reported_size = file_decompressed_size(&compressed_path).unwrap();
1010 assert_eq!(reported_size, data.len() as u64);
1011
1012 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 let data: Vec<u8> = (0..32 * 1024) .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_file(&input_path, &compressed_path, *level, Some(2), None).unwrap();
1037
1038 decompress_file(&compressed_path, &output_path, Some(2)).unwrap();
1040
1041 let result = fs::read(&output_path).unwrap();
1043 assert_eq!(result, data, "Data mismatch at level {:?}", level);
1044
1045 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 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 for threads in [1, 2, 4] {
1071 compress_file(
1073 &input_path,
1074 &compressed_path,
1075 Level::Default,
1076 Some(threads),
1077 None,
1078 )
1079 .unwrap();
1080
1081 let size = decompress_file(&compressed_path, &output_path, Some(threads)).unwrap();
1083 assert_eq!(size, data.len() as u64);
1084
1085 let result = fs::read(&output_path).unwrap();
1087 assert_eq!(result, data, "Mismatch with {} threads", threads);
1088 }
1089
1090 let _ = fs::remove_file(&input_path);
1092 let _ = fs::remove_file(&compressed_path);
1093 let _ = fs::remove_file(&output_path);
1094 }
1095
1096 #[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}