Skip to content

Windows shell replacement in Rust: taskbar, file manager, NTFS search, task manager, disk cleaner. One installer exe.

Rust

rust-explorer: own ZIP engine, zip opens like a folder, Rename in file menu; PowerShell replaced by Win32 in Settings opener, shell restart, wallpaper

Lemxya058a1ceea5b
diff --git a/common/src/lib.rs b/common/src/lib.rs
index f7f1f71..1eb226a 100644
--- a/common/src/lib.rs
+++ b/common/src/lib.rs
@@ -17,6 +17,16 @@ pub mod reg;
pub mod settings;
pub mod style;
+/// "Show in folder": opens `path`'s folder with the file selected in the installed rust-explorer, else in Windows Explorer.
+pub fn reveal(path: &str) {
+ use std::{os::windows::process::CommandExt, process::Command};
+ let rx = std::env::var_os("ProgramFiles").map(|pf| PathBuf::from(pf).join(r"rust-explorer\rust-explorer.exe")).filter(|p| p.is_file());
+ let _ = match rx {
+ Some(rx) => Command::new(rx).raw_arg(format!("/select,\"{path}\"")).spawn(),
+ None => Command::new("explorer.exe").raw_arg(format!("/select,\"{path}\"")).spawn(),
+ };
+}
+
static LANG: AtomicU8 = AtomicU8::new(0); // 0 = not read yet, 1 = English, 2 = Russian
fn file() -> Option<PathBuf> {
diff --git a/common/src/style.rs b/common/src/style.rs
index 7fd9243..f42fe46 100644
--- a/common/src/style.rs
+++ b/common/src/style.rs
@@ -113,6 +113,31 @@ pub fn set_autohide(on: bool) {
}
}
+fn left_file() -> Option<PathBuf> {
+ std::env::var_os("ProgramData").map(|p| PathBuf::from(p).join("WinRust").join("align-left.txt"))
+}
+
+static LEFT: AtomicU8 = AtomicU8::new(0);
+static LEFT_AT: AtomicU64 = AtomicU64::new(0);
+
+/// Start and the pinned apps sit at the bar's left end, like Windows 10 (off unless the file says 1). Re-read once a second.
+pub fn align_left() -> bool {
+ if now_ms().saturating_sub(LEFT_AT.load(Relaxed)) > 1000 {
+ LEFT.store(left_file().and_then(|f| std::fs::read_to_string(f).ok()).is_some_and(|s| s.trim() == "1") as u8, Relaxed);
+ LEFT_AT.store(now_ms(), Relaxed);
+ }
+ LEFT.load(Relaxed) == 1
+}
+
+pub fn set_align_left(on: bool) {
+ LEFT.store(on as u8, Relaxed);
+ LEFT_AT.store(now_ms(), Relaxed);
+ if let Some(f) = left_file() {
+ let _ = std::fs::create_dir_all(f.parent().unwrap());
+ let _ = std::fs::write(f, if on { "1" } else { "0" });
+ }
+}
+
/// Which screen edge the topbar sits on.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Side {
diff --git a/plugins/rtaskmgr-win32/src/app.rs b/plugins/rtaskmgr-win32/src/app.rs
index 9722646..3a21295 100644
--- a/plugins/rtaskmgr-win32/src/app.rs
+++ b/plugins/rtaskmgr-win32/src/app.rs
@@ -1054,10 +1054,7 @@ impl App {
let path = pids.iter().find_map(|&p| d.procs.iter().find(|x| x.pid == p).and_then(|x| x.exe.clone()));
drop(d);
match path {
- Some(p) => {
- use std::os::windows::process::CommandExt;
- let _ = std::process::Command::new("explorer").raw_arg(format!("/select,\"{}\"", p.replace("\\..\\", "\\"))).spawn();
- }
+ Some(p) => winrust_common::reveal(&p.replace("\\..\\", "\\")),
None => self.toast(t!("Путь к файлу недоступен (системный или защищённый процесс)", "File path unavailable (system or protected process)")),
}
}
@@ -1114,10 +1111,7 @@ impl App {
Act::StartEnable => true,
Act::StartDisable => false,
Act::OpenDir => {
- if let Some(p) = &row.icon_path {
- use std::os::windows::process::CommandExt;
- let _ = std::process::Command::new("explorer").raw_arg(format!("/select,\"{p}\"")).spawn();
- }
+ if let Some(p) = &row.icon_path { winrust_common::reveal(p); }
return;
}
_ => return,
diff --git a/plugins/rtaskmgr-win32/src/gpu.rs b/plugins/rtaskmgr-win32/src/gpu.rs
index 82c043b..b4609d8 100644
--- a/plugins/rtaskmgr-win32/src/gpu.rs
+++ b/plugins/rtaskmgr-win32/src/gpu.rs
@@ -172,7 +172,9 @@ pub fn adapters() -> Vec<Adapter> {
let name = String::from_utf16_lossy(&w);
let u64_at = |o: usize| u64::from_le_bytes(d[o..o + 8].try_into().unwrap());
let flags = u32::from_le_bytes([d[304], d[305], d[306], d[307]]);
- if flags & 2 == 0 && !name.to_lowercase().contains("basic render") {
+ // shortcut: one card can show up under several LUIDs (virtual displays); identical twin cards get merged too
+ let dup = out.iter().any(|a: &Adapter| a.name == name && a.dedicated == u64_at(272) && a.shared == u64_at(288));
+ if flags & 2 == 0 && !dup && !name.to_lowercase().contains("basic render") {
out.push(Adapter {
location: locs.iter().find(|l| l.0.eq_ignore_ascii_case(&name)).map(|l| l.1.clone()).unwrap_or_default(),
dx: directx_level(ad),
diff --git a/plugins/rust-explorer/Cargo.toml b/plugins/rust-explorer/Cargo.toml
index fd0029a..478e30e 100644
--- a/plugins/rust-explorer/Cargo.toml
+++ b/plugins/rust-explorer/Cargo.toml
@@ -6,15 +6,16 @@ license = "MIT OR Apache-2.0"
[dependencies]
winrust-common = { path = "../../common" }
-windows = { version = "0.62.2", features = ["Win32_Foundation", "Win32_Graphics_Gdi", "Win32_System_Com_StructuredStorage", "Win32_System_DataExchange", "Win32_System_Memory", "Win32_System_Ole", "Win32_System_SystemServices", "Win32_UI_Shell", "Win32_UI_Shell_Common", "Win32_UI_WindowsAndMessaging", "Win32_System_Com"] }
+windows = { version = "0.62.2", features = ["Win32_Foundation", "Win32_Graphics_Gdi", "Win32_System_Com_StructuredStorage", "Win32_System_DataExchange", "Win32_System_Memory", "Win32_System_Ole", "Win32_System_SystemServices", "Win32_UI_Shell", "Win32_UI_Shell_Common", "Win32_UI_Controls", "Win32_System_Variant", "Win32_UI_WindowsAndMessaging", "Win32_System_Com"] }
windows-core = "0.62"
windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_Graphics_Gdi", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_Ioctl", "Win32_System_LibraryLoader", "Win32_System_Registry", "Win32_System_Time", "Win32_System_SystemInformation", "Win32_Graphics_Dwm", "Win32_Graphics_GdiPlus", "Win32_UI_Controls", "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging"] }
-zip = { version = "9.8.1", default-features = false, features = ["deflate"] }
sevenz-rust2 = { version = "0.25.0", default-features = false, features = ["util", "ppmd", "bzip2", "deflate"] }
[build-dependencies]
embed-resource = "3"
[dev-dependencies]
+# the tests check our ZIP against this independent implementation, both ways
+zip = { version = "9.8.1", default-features = false, features = ["deflate"] }
# the tests make a real .7z to unpack (the exe itself only unpacks)
sevenz-rust2 = { version = "0.25.0", default-features = false, features = ["util", "compress"] }
diff --git a/plugins/rust-explorer/src/jobs.rs b/plugins/rust-explorer/src/jobs.rs
index 66272a1..1d08560 100644
--- a/plugins/rust-explorer/src/jobs.rs
+++ b/plugins/rust-explorer/src/jobs.rs
@@ -1,5 +1,5 @@
//! Background file jobs, faster than the shell's: copying (several files at once on SSDs, unbuffered I/O for big files),
-//! ZIP packing (files compressed on all cores) and unpacking ZIP / 7z. Progress shows in the status line (`status_text`),
+//! ZIP packing (files compressed on all cores, by our own deflate) and unpacking ZIP / 7z. Progress shows in the status line (`status_text`),
//! the end comes to the window as WM_JOB.
use std::{
fs,
@@ -17,12 +17,17 @@ pub struct Progress {
pub total_files: AtomicU64,
pub bytes: AtomicU64,
pub total_bytes: AtomicU64,
+ cancel: AtomicBool, // Esc: packing and unpacking stop at the next step
started: Instant,
}
impl Progress {
fn new(what: String) -> Arc<Progress> {
- Arc::new(Progress { what, files: 0.into(), total_files: 0.into(), bytes: 0.into(), total_bytes: 0.into(), started: Instant::now() })
+ Arc::new(Progress { what, files: 0.into(), total_files: 0.into(), bytes: 0.into(), total_bytes: 0.into(), cancel: false.into(), started: Instant::now() })
+ }
+
+ fn cancelled(&self) -> bool {
+ self.cancel.load(Relaxed)
}
}
@@ -48,6 +53,13 @@ pub fn busy() -> bool {
RUNNING.load(Relaxed)
}
+/// Asks the running pack / unpack to stop (a copy ignores it). The half-made archive or folder is removed.
+pub fn cancel() {
+ if let Some(p) = JOB.lock().unwrap().as_ref() {
+ p.cancel.store(true, Relaxed);
+ }
+}
+
/// Runs `f` on a worker thread; its error (or None) is posted to `h` as WM_JOB (wp = Box<Option<String>>).
fn spawn(h: isize, what: String, f: impl FnOnce(&Progress) -> Result<(), String> + Send + 'static) -> bool {
if RUNNING.swap(true, Relaxed) {
@@ -228,7 +240,7 @@ const PAR_MAX: u64 = 64 << 20;
pub fn zip(h: isize, from: Vec<PathBuf>, dest: PathBuf) -> bool {
let what = format!("Packing {}", dest.file_name().unwrap_or_default().to_string_lossy());
- spawn(h, what, move |p| zip_now(&from, &dest, p).inspect_err(|_| _ = fs::remove_file(&dest)))
+ spawn(h, what, move |p| zip_now(&from, &dest, p).inspect_err(|_| _ = fs::remove_file(&dest)).or_else(|e| if p.cancelled() { Ok(()) } else { Err(e) }))
}
/// Archive name of an entry: forward slashes, as ZIP wants.
@@ -237,7 +249,7 @@ fn zip_name(rel: &Path) -> String {
}
fn zip_now(from: &[PathBuf], dest: &Path, p: &Progress) -> Result<(), String> {
- use zip::{CompressionMethod, ZipWriter, write::{PreparedZipFile, SimpleFileOptions}};
+ use crate::zipx::{self, Prepared, Writer};
let mut entries = vec![];
for src in from {
let name = src.file_name().ok_or_else(|| format!("{}: no name", src.display()))?;
@@ -246,26 +258,25 @@ fn zip_now(from: &[PathBuf], dest: &Path, p: &Progress) -> Result<(), String> {
entries.retain(|e| e.0 != dest); // packing the folder the archive is written into
p.total_files.store(entries.iter().filter(|e| e.2.is_some()).count() as u64, Relaxed);
p.total_bytes.store(entries.iter().filter_map(|e| e.2).sum(), Relaxed);
- let opts = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated).large_file(true);
let file = fs::File::create(dest).map_err(|e| format!("{}: {e}", dest.display()))?;
- let mut w = ZipWriter::new(std::io::BufWriter::with_capacity(1 << 20, file));
+ let mut w = Writer::new(std::io::BufWriter::with_capacity(1 << 20, file));
let err = |e: &dyn std::fmt::Display, path: &Path| format!("{}: {e}", path.display());
for (src, rel, size) in &entries {
if size.is_none() {
- w.add_directory(format!("{}/", zip_name(rel)), opts).map_err(|e| err(&e, src))?;
+ w.add_dir(&zip_name(rel), zipx::dos_time(src)).map_err(|e| err(&e, src))?;
}
}
let small: Vec<&Entry> = entries.iter().filter(|e| e.2.is_some_and(|s| s <= PAR_MAX)).collect();
let threads = std::thread::available_parallelism().map_or(4, |n| n.get());
let next = AtomicU64::new(0);
- let (tx, rx) = std::sync::mpsc::sync_channel::<Result<PreparedZipFile, String>>(threads * 2); // bounded: memory stays small
+ let (tx, rx) = std::sync::mpsc::sync_channel::<Result<Prepared, String>>(threads * 2); // bounded: memory stays small
let written: Result<(), String> = std::thread::scope(|s| {
for _ in 0..threads {
let tx = tx.clone();
let (small, next) = (&small, &next);
s.spawn(move || {
while let Some(e) = small.get(next.fetch_add(1, Relaxed) as usize) {
- let r = PreparedZipFile::from_path(zip_name(&e.1), &e.0, opts).map_err(|x| format!("{}: {x}", e.0.display()));
+ let r = zipx::prepare(zip_name(&e.1), &e.0).map_err(|x| format!("{}: {x}", e.0.display()));
if tx.send(r).is_err() {
break; // the writer gave up
}
@@ -274,20 +285,20 @@ fn zip_now(from: &[PathBuf], dest: &Path, p: &Progress) -> Result<(), String> {
}
drop(tx);
for r in rx {
+ if p.cancelled() {
+ return Err("Cancelled".to_string());
+ }
let f = r?;
let n = f.uncompressed_size();
- w.add_prepared_file(f).map_err(|e| e.to_string())?;
+ w.add_prepared(f).map_err(|e| e.to_string())?;
p.bytes.fetch_add(n, Relaxed);
p.files.fetch_add(1, Relaxed);
}
Ok(())
});
written?;
- for (src, rel, size) in entries.iter().filter(|e| e.2.is_some_and(|s| s > PAR_MAX)) {
- w.start_file(zip_name(rel), opts).map_err(|e| err(&e, src))?;
- let mut f = fs::File::open(src).map_err(|e| err(&e, src))?;
- std::io::copy(&mut f, &mut w).map_err(|e| err(&e, src))?;
- p.bytes.fetch_add(size.unwrap(), Relaxed);
+ for (src, rel, _) in entries.iter().filter(|e| e.2.is_some_and(|s| s > PAR_MAX)) {
+ w.add_stream(zip_name(rel), src, |n| { p.bytes.fetch_add(n, Relaxed); !p.cancelled() }).map_err(|e| err(&e, src))?;
p.files.fetch_add(1, Relaxed);
}
w.finish().map_err(|e| err(&e, dest))?;
@@ -306,23 +317,30 @@ pub fn extract(h: isize, archives: Vec<PathBuf>, dir: PathBuf) -> bool {
for a in &archives {
let stem = a.file_stem().unwrap_or_default().to_string_lossy().into_owned();
let out = free_name(&dir, &stem, true);
- extract_one(a, &out).map_err(|e| format!("{}: {e}", a.display()))?;
+ let r = extract_with(a, &out, p);
+ if r.is_err() && p.cancelled() {
+ let _ = fs::remove_dir_all(&out); // the folder is the one just made for this archive
+ return Ok(());
+ }
+ r.map_err(|e| format!("{}: {e}", a.display()))?;
p.files.fetch_add(1, Relaxed);
}
Ok(())
})
}
-/// Entry names that would land outside `out` are refused by both libraries (zip-slip).
-pub fn extract_one(archive: &Path, out: &Path) -> Result<(), String> {
+/// Entry names that would land outside `out` are refused (zip-slip).
+#[cfg(test)]
+fn extract_one(archive: &Path, out: &Path) -> Result<(), String> {
+ extract_with(archive, out, &Progress::new(String::new()))
+}
+
+fn extract_with(archive: &Path, out: &Path, p: &Progress) -> Result<(), String> {
let is7z = archive.extension().is_some_and(|e| e.eq_ignore_ascii_case("7z"));
if is7z {
return sevenz_rust2::decompress_file(archive, out).map_err(|e| e.to_string());
}
- let f = fs::File::open(archive).map_err(|e| e.to_string())?;
- let mut z = zip::ZipArchive::new(std::io::BufReader::new(f)).map_err(|e| e.to_string())?;
- fs::create_dir_all(out).map_err(|e| e.to_string())?;
- z.extract(out).map_err(|e| e.to_string())
+ crate::zipx::extract(archive, out, |t| _ = p.total_bytes.fetch_add(t, Relaxed), |n| { p.bytes.fetch_add(n, Relaxed); !p.cancelled() })
}
#[cfg(test)]
@@ -388,6 +406,41 @@ mod tests {
let _ = fs::remove_dir_all(&d);
}
+ /// Our ZIP against an independent implementation, both ways, and against Windows' own tar.
+ #[test]
+ fn zip_agrees_with_other_implementations() {
+ use std::io::{Read, Write};
+ let d = tmp("xzip");
+ tree(&d);
+ let z = d.join("ours.zip");
+ zip_now(&[d.join("src")], &z, &Progress::new(String::new())).unwrap();
+ let mut a = zip::ZipArchive::new(fs::File::open(&z).unwrap()).unwrap();
+ let mut two = vec![];
+ a.by_name("src/a/two.bin").unwrap().read_to_end(&mut two).unwrap(); // the crate checks the CRC itself
+ assert_eq!(two, vec![7u8; 300_000]);
+ assert!(a.by_name("src/a/b/").is_ok() && a.by_name("src/one.txt").is_ok());
+ let t = std::process::Command::new("tar").arg("-xf").arg(&z).arg("-C").arg(&d).current_dir(&d).status();
+ if let Ok(t) = t {
+ assert!(t.success(), "tar could not read our zip");
+ assert_eq!(fs::read(d.join("src/a/two.bin")).unwrap().len(), 300_000);
+ }
+ // theirs, with deflate and with stored entries
+ let theirs = d.join("theirs.zip");
+ let mut w = zip::ZipWriter::new(fs::File::create(&theirs).unwrap());
+ let (def, sto) = (zip::write::SimpleFileOptions::default(), zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored));
+ w.start_file("a/x.txt", def).unwrap();
+ w.write_all(&b"hello hello hello ".repeat(5000)).unwrap();
+ w.start_file("raw.bin", sto).unwrap();
+ w.write_all(&[1, 2, 3]).unwrap();
+ w.add_directory("empty/", def).unwrap();
+ w.finish().unwrap();
+ extract_one(&theirs, &d.join("theirs")).unwrap();
+ assert_eq!(fs::read(d.join("theirs/a/x.txt")).unwrap().len(), 90_000);
+ assert_eq!(fs::read(d.join("theirs/raw.bin")).unwrap(), [1, 2, 3]);
+ assert!(d.join("theirs/empty").is_dir());
+ let _ = fs::remove_dir_all(&d);
+ }
+
#[test]
fn unpacks_7z() {
let d = tmp("7z");
@@ -406,6 +459,55 @@ mod tests {
let _ = fs::remove_dir_all(&d);
}
+ /// By hand, on real data: `set RX_LIVE=<folder>` then `cargo test -p rust-explorer --release live_zip -- --ignored --nocapture`.
+ /// Packs the folder with our engine, unpacks it with Windows' tar, PowerShell's Expand-Archive and our own, and compares every file.
+ #[test]
+ #[ignore]
+ fn live_zip() {
+ let Some(src) = std::env::var_os("RX_LIVE").map(PathBuf::from) else { return };
+ let d = tmp("live");
+ let z = d.join("live.zip");
+ let t = Instant::now();
+ zip_now(&[src.clone()], &z, &Progress::new(String::new())).unwrap();
+ let mut files = vec![];
+ walk(&src, PathBuf::new(), &mut files).unwrap();
+ let raw: u64 = files.iter().filter_map(|e| e.2).sum();
+ println!("LIVE packed {} files, {} -> {} bytes in {:?}", files.iter().filter(|e| e.2.is_some()).count(), raw, fs::metadata(&z).unwrap().len(), t.elapsed());
+ let name = src.file_name().unwrap();
+ let (a, b, c) = (d.join("tar"), d.join("ps"), d.join("ours"));
+ for o in [&a, &b] {
+ fs::create_dir_all(o).unwrap();
+ }
+ let t = Instant::now();
+ assert!(std::process::Command::new("tar").arg("-xf").arg(&z).arg("-C").arg(&a).status().unwrap().success());
+ println!("LIVE tar unpacked in {:?}", t.elapsed());
+ let t = Instant::now();
+ let ps = std::process::Command::new("powershell").args(["-NoProfile", "-Command", &format!("Expand-Archive -LiteralPath '{}' -DestinationPath '{}' -Force", z.display(), b.display())]).status().unwrap();
+ println!("LIVE Expand-Archive {} in {:?}", ps.success(), t.elapsed());
+ assert!(ps.success());
+ let t = Instant::now();
+ extract_one(&z, &c).unwrap();
+ println!("LIVE ours unpacked in {:?}", t.elapsed());
+ let mut bad = 0;
+ for (path, rel, size) in &files {
+ if size.is_none() {
+ continue;
+ }
+ let want = fs::read(path).unwrap();
+ for root in [&a, &b, &c] {
+ let got = fs::read(root.join(name).join(rel.strip_prefix(name).unwrap_or(rel)));
+ let got = got.or_else(|_| fs::read(root.join(rel)));
+ if got.as_deref().ok() != Some(&want[..]) {
+ bad += 1;
+ println!("LIVE MISMATCH {} in {}", rel.display(), root.display());
+ }
+ }
+ }
+ println!("LIVE compared {} files x 3 unpackers, {bad} mismatches", files.iter().filter(|e| e.2.is_some()).count());
+ let _ = fs::remove_dir_all(&d);
+ assert_eq!(bad, 0);
+ }
+
/// Old path (IFileOperation, Explorer's engine) against ours on the same data. By hand:
/// `set RX_BENCH=<empty dir on the disk to test>` and `cargo test -p rust-explorer --release bench -- --ignored --nocapture`
#[test]
diff --git a/plugins/rust-explorer/src/main.rs b/plugins/rust-explorer/src/main.rs
index c2df009..fba1d56 100644
--- a/plugins/rust-explorer/src/main.rs
+++ b/plugins/rust-explorer/src/main.rs
@@ -20,7 +20,9 @@
use std::{cell::RefCell, env, fs, os::windows::fs::MetadataExt, path::{Path, PathBuf}, ptr, sync::atomic::{AtomicBool, AtomicUsize, Ordering::Relaxed}};
use windows_sys::Win32::{Foundation::*, Graphics::Gdi::{BLACK_BRUSH, GetStockObject, InvalidateRect}, System::LibraryLoader::GetModuleHandleW, UI::{Controls::*, Input::KeyboardAndMouse::{GetKeyState, SetFocus, VK_CONTROL}, Shell::*, WindowsAndMessaging::*}};
+mod zipx; // our own ZIP engine (deflate, inflate, container)
mod jobs; // our own copier, ZIP packer, ZIP/7z unpacker (background, progress in the status line)
+mod shellwin; // registers the window as a shell window: "Show in folder" from other apps selects the file
mod ui; // the look: palette, painted tabs / toolbar / address pill / status line, dark theme
static DESKTOP: AtomicBool = AtomicBool::new(false);
@@ -135,6 +137,7 @@ fn properties(path: &PathBuf) {
/// New folder / new empty file with a free name ("New folder", "New folder (2)", ...).
fn new_entry(dir: &PathBuf, base: &str, folder: bool) {
+ if zipx::split_zip(dir).is_some() { return; }
let (stem, ext) = match base.rsplit_once('.') { Some((s, e)) if !folder => (s.to_string(), format!(".{e}")), _ => (base.to_string(), String::new()) };
let path = (1..).map(|n| dir.join(if n == 1 { base.to_string() } else { format!("{stem} ({n}){ext}") })).find(|p| !p.exists());
if let Some(p) = path { let _ = if folder { fs::create_dir(p) } else { fs::File::create(p).map(drop) }; }
@@ -144,6 +147,7 @@ fn new_entry(dir: &PathBuf, base: &str, folder: bool) {
/// Copies and moves to another drive run in our own copier (jobs.rs: parallel on SSDs, unbuffered for big files); moves on the
/// same drive (a rename), shell items, and folders that need elevation go through the shell.
fn paste(h: HWND, dir: &PathBuf) {
+ if zipx::split_zip(dir).is_some() { return; } // an archive is read-only here
let Some((paths, cut)) = sh::clip_get() else { return };
let fast = jobs::can_fast_copy(&paths, dir) && !(cut && paths.iter().all(|p| jobs::same_volume(p, dir))) && jobs::writable(dir);
if fast && !jobs::busy() {
@@ -157,17 +161,119 @@ fn paste(h: HWND, dir: &PathBuf) {
const MENU_ZIP: u32 = 0x9001;
const MENU_EXTRACT: u32 = 0x9002;
const MENU_PIN: u32 = 0x9003;
+const MENU_RENAME: u32 = 0x9004;
+
+/// True while the window shows the inside of a .zip: read-only, its rows are no files on disk.
+fn in_zip() -> bool { zipx::split_zip(&cur_dir()).is_some() }
+
+fn warn(h: HWND, text: &str) {
+ unsafe { MessageBoxW(h, w(text).as_ptr(), w("WinRust Explorer").as_ptr(), MB_ICONWARNING) };
+}
+
+/// (archive, archive paths) when every one of `paths` is something inside one and the same .zip.
+fn zip_names(paths: &[PathBuf]) -> Option<(PathBuf, Vec<String>)> {
+ let mut arc = None;
+ let mut names = vec![];
+ for p in paths {
+ let (a, inner) = zipx::split_zip(p).filter(|(_, i)| !i.is_empty())?;
+ if arc.get_or_insert_with(|| a.clone()) != &a { return None; }
+ names.push(inner);
+ }
+ Some((arc?, names))
+}
+
+/// Things from an archive as real files in a temp folder: the clipboard, drag and "open with" need files on disk. One path per name.
+/// Folders of earlier days are cleaned up here.
+fn materialize(arc: &Path, names: &[String]) -> Result<Vec<PathBuf>, String> {
+ static N: AtomicUsize = AtomicUsize::new(0);
+ let root = env::temp_dir().join("WinRust-zip");
+ for e in fs::read_dir(&root).into_iter().flatten().flatten() {
+ if e.metadata().and_then(|m| m.modified()).ok().and_then(|t| t.elapsed().ok()).is_some_and(|a| a > std::time::Duration::from_secs(86_400)) {
+ let _ = fs::remove_dir_all(e.path());
+ }
+ }
+ let out = root.join(format!("{}-{}", std::process::id(), N.fetch_add(1, Relaxed)));
+ zipx::extract_some(arc, names, &out)?;
+ Ok(names.iter().map(|n| out.join(n.rsplit('/').next().unwrap_or(n))).collect())
+}
+
+/// `paths` as files on disk: unchanged, or taken out of the archive they are in first.
+fn real_paths(paths: &[PathBuf]) -> Result<Vec<PathBuf>, String> {
+ match zip_names(paths) {
+ Some((arc, names)) => materialize(&arc, &names),
+ None => Ok(paths.to_vec()),
+ }
+}
+
+/// Enter / double click on a row.
+unsafe fn activate(h: HWND, p: PathBuf, is_dir: bool) {
+ // a file inside a zip: a real copy in the temp folder first
+ let p = match zip_names(std::slice::from_ref(&p)) {
+ Some((arc, names)) if !is_dir => match materialize(&arc, &names) {
+ Ok(mut v) => v.remove(0),
+ Err(e) => return warn(h, &e),
+ },
+ _ => p,
+ };
+ if is_dir && desk() { let _ = std::process::Command::new(env::current_exe().unwrap_or_default()).arg(p).spawn(); }
+ else if is_dir { load(h, p) }
+ else if p.extension().is_some_and(|e| e.eq_ignore_ascii_case("zip")) && p.is_file() { load(h, p) } // a zip opens like a folder; "Extract here" is in its menu
+ else if let Some(exe) = tool("photoview").filter(|_| is_pic(&p)) { let _ = std::process::Command::new(exe).arg(&p).spawn(); }
+ else { unsafe { ShellExecuteW(0 as _, ptr::null(), w(&p.to_string_lossy()).as_ptr(), ptr::null(), ptr::null(), SW_SHOWNORMAL) }; }
+}
+
+/// The menu for rows inside a zip. They are no shell items, so this is a small menu of our own.
+fn zip_menu(h: HWND, paths: &[PathBuf]) {
+ let id = unsafe {
+ let m = CreatePopupMenu();
+ for (i, t) in [(1usize, "Open"), (2, "Copy"), (3, "Extract here")] { AppendMenuW(m, MF_STRING, i, w(t).as_ptr()); }
+ let mut pt: POINT = std::mem::zeroed();
+ GetCursorPos(&mut pt);
+ let id = TrackPopupMenu(m, TPM_RETURNCMD | TPM_RIGHTBUTTON, pt.x, pt.y, 0, h, ptr::null());
+ DestroyMenu(m);
+ id
+ };
+ let Some((arc, names)) = zip_names(paths) else { return };
+ match id {
+ 1 => if let [p] = paths {
+ let is_dir = ST.with_borrow(|st| st.rows.iter().any(|r| r.dir && st.dir.join(&r.name) == *p));
+ unsafe { activate(h, p.clone(), is_dir) };
+ },
+ 2 => match materialize(&arc, &names) {
+ Ok(v) => { let _ = sh::clip_set(h, &v, false); }
+ Err(e) => warn(h, &e),
+ },
+ 3 => {
+ // next to the archive, in a new folder named after it; no progress yet (shortcut: fire and forget)
+ let (Some(parent), Some(stem)) = (arc.parent(), arc.file_stem()) else { return };
+ let out = jobs::free_name(parent, &stem.to_string_lossy(), true);
+ std::thread::spawn(move || _ = zipx::extract_some(&arc, &names, &out));
+ }
+ _ => {}
+ }
+}
/// The shell menu for the selection, with our "Compress to ZIP" / "Extract here" on top.
fn item_menu(h: HWND, dir: &Path, paths: &[PathBuf]) {
+ if zipx::split_zip(dir).is_some() { return zip_menu(h, paths); }
let mut extra = vec![(MENU_ZIP, "Compress to ZIP")];
if paths.iter().all(|p| p.is_file() && jobs::is_archive(p)) {
extra.insert(0, (MENU_EXTRACT, "Extract here"));
}
+ // the shell menu has no Rename here (it needs the shell's own folder view to answer it): the same edit box as F2
+ if paths.len() == 1 { extra.push((MENU_RENAME, "Rename")); }
if let [p] = paths { if p.is_dir() { extra.push((MENU_PIN, if ui::pins().contains(p) { "Unpin from Quick access" } else { "Pin to Quick access" })); } }
let picked = sh::show(h, paths, if jobs::busy() { &[] } else { &extra }).ok().flatten();
match picked {
Some(MENU_PIN) => ui::toggle_pin(&paths[0]),
+ Some(MENU_RENAME) => unsafe {
+ let (list, ..) = selected();
+ let i = put(list, LVM_GETNEXTITEM, usize::MAX, LVNI_SELECTED as isize);
+ if i >= 0 {
+ SetFocus(list);
+ put(list, LVM_EDITLABELW, i as usize, 0);
+ }
+ },
Some(MENU_ZIP) => {
let base = if paths.len() == 1 { paths[0].file_stem().unwrap_or_default().to_string_lossy().into_owned() } else { dir.file_name().map_or("Archive".into(), |n| n.to_string_lossy().into_owned()) };
let dest = jobs::free_name(dir, &format!("{base}.zip"), false);
@@ -178,6 +284,16 @@ fn item_menu(h: HWND, dir: &Path, paths: &[PathBuf]) {
}
}
+/// "Hide desktop icons" is a marker file, so it survives restarts.
+fn icons_marker() -> PathBuf { PathBuf::from(env::var_os("APPDATA").unwrap_or_default()).join(r"WinRust\desktop-icons-hidden.txt") }
+fn icons_hidden() -> bool { icons_marker().exists() }
+fn set_icons_hidden(hide: bool) {
+ let m = icons_marker();
+ if hide { if let Some(d) = m.parent() { let _ = fs::create_dir_all(d); } let _ = fs::write(&m, "1"); } else { let _ = fs::remove_file(&m); }
+ let list = ST.with_borrow(|st| st.list);
+ unsafe { ShowWindow(list, if hide { SW_HIDE } else { SW_SHOW }) };
+}
+
/// Right click on empty space: the folder's own menu.
#[allow(unsafe_op_in_unsafe_fn)]
unsafe fn bg_menu(h: HWND) {
@@ -185,12 +301,14 @@ unsafe fn bg_menu(h: HWND) {
for (id, t) in [(1usize, "New folder"), (2, "New text file"), (3, "Paste"), (4, "Refresh"), (5, "Properties")] { AppendMenuW(m, MF_STRING, id, w(t).as_ptr()); }
let disk = tool("wiztree");
if disk.is_some() { AppendMenuW(m, MF_STRING, 6, w("Disk usage (WinRust Disk)").as_ptr()); }
+ if desk() { AppendMenuW(m, MF_STRING, 7, w(if icons_hidden() { "Show desktop icons" } else { "Hide desktop icons" }).as_ptr()); }
let mut pt: POINT = std::mem::zeroed();
GetCursorPos(&mut pt);
let id = TrackPopupMenu(m, TPM_RETURNCMD | TPM_RIGHTBUTTON, pt.x, pt.y, 0, h, ptr::null());
DestroyMenu(m);
let dir = cur_dir();
match id {
+ 7 => set_icons_hidden(!icons_hidden()),
6 => { // wiztree takes the drive letter and asks for admin rights itself
let drive: String = dir.to_string_lossy().chars().take(2).collect();
if let Some(exe) = disk { let _ = std::process::Command::new(exe).arg(drive).spawn(); }
@@ -204,6 +322,49 @@ unsafe fn bg_menu(h: HWND) {
if id != 0 { reload(h); }
}
+/// Command line -> (folder to open, file to select there). Understands `/select,"C:\a\b.txt"` (what browsers and other
+/// apps pass for "show in folder"), a plain folder, or a plain file path (its folder, file selected).
+fn start_target() -> (PathBuf, Option<String>) {
+ let mut a = env::args().skip(1);
+ let mut p = a.next().unwrap_or_default();
+ if p.to_lowercase().starts_with("/select,") || p.to_lowercase().starts_with("--select") {
+ p = p.get(8..).unwrap_or("").trim_start_matches(',').to_string();
+ if p.is_empty() { p = a.next().unwrap_or_default(); }
+ }
+ let p = PathBuf::from(p.trim_matches('"'));
+ if p.as_os_str().is_empty() { return (PathBuf::from("C:\\"), None); }
+ match (p.is_file(), p.parent()) {
+ (true, Some(d)) => (d.to_path_buf(), p.file_name().map(|n| n.to_string_lossy().into_owned())),
+ _ => (p, None),
+ }
+}
+
+/// Another app asked the shell to show `p` in a folder window (SHOpenFolderAndSelectItems): open its folder here, select it, come to front.
+fn shell_select(h: isize, p: PathBuf) {
+ let h = h as HWND;
+ let (Some(dir), Some(name)) = (p.parent(), p.file_name()) else { return };
+ if !cur_dir().as_os_str().eq_ignore_ascii_case(dir.as_os_str()) { load(h, dir.to_path_buf()); }
+ select_row(&name.to_string_lossy());
+ unsafe {
+ if IsIconic(h) != 0 { ShowWindow(h, SW_RESTORE); }
+ SetForegroundWindow(h);
+ }
+}
+
+/// Selects and scrolls to the row called `name` in the current folder.
+fn select_row(name: &str) {
+ let (list, i) = ST.with_borrow(|st| (st.list, st.rows.iter().position(|r| r.name.eq_ignore_ascii_case(name))));
+ let Some(i) = i else { return };
+ unsafe {
+ let mut it: LVITEMW = std::mem::zeroed();
+ it.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
+ put(list, LVM_SETITEMSTATE, usize::MAX, &it as *const _ as isize); // the shell selects it alone
+ it.state = LVIS_SELECTED | LVIS_FOCUSED;
+ put(list, LVM_SETITEMSTATE, i, &it as *const _ as isize);
+ put(list, LVM_ENSUREVISIBLE, i, 0);
+ }
+}
+
/// Back / forward along the visited-folder trail.
fn nav(h: HWND, back: bool) {
let to = ST.with_borrow_mut(|st| if back { st.hist.back() } else { st.hist.forward() });
@@ -258,6 +419,13 @@ fn label(dir: &PathBuf) -> String {
/// Rows of `dir`, folders first. Desktop mode merges the public desktop (absolute names) and hides hidden files.
/// None = `dir` itself is unreadable.
fn rows_of(dir: &PathBuf) -> Option<Vec<Row>> {
+ if !desk() {
+ if let Some((arc, inner)) = zipx::split_zip(dir) { // inside a .zip: its entries are the rows
+ let mut rows: Vec<Row> = zipx::list_dir(&arc, &inner).ok()?.into_iter().map(|i| Row { name: i.name, dir: i.dir, size: i.size, time: zipx::dos_to_filetime(i.time), icon: -1, thumb: -2 }).collect();
+ sort_rows(&mut rows, SORT_COL.load(Relaxed), SORT_ASC.load(Relaxed));
+ return Some(rows);
+ }
+ }
let mut dirs = vec![dir.clone()];
if desk() { dirs.extend(sh::desktop_dirs().into_iter().skip(1)); }
let mut rows = vec![];
@@ -286,6 +454,7 @@ fn load(h: HWND, dir: PathBuf) {
(st.list, st.tab, st.edit, st.active, st.rows.len())
});
unsafe { SetWindowTextW(edit, w(&dir.to_string_lossy()).as_ptr()) };
+ shellwin::navigated(&dir);
ui::set_search(None); // a folder again: the pill shows its path
tab_text(tab, active, &label(&dir), false);
ui::nav_dirty(); // the pane highlights where we are
@@ -638,12 +807,41 @@ mod sh {
/// The real shell context menu for `paths`, so 7-Zip/Git/etc. entries show up. `extra` = our own items on top
/// (ids above the shell's range); returns the one picked, None when the shell handled the pick or nothing was picked.
+ /// The name Windows shows for its own "Send to > Compressed (zipped) folder": the stem of the .ZFSendToTarget file in SendTo.
+ pub(crate) fn system_zip_name() -> Option<String> {
+ let dir = PathBuf::from(std::env::var_os("APPDATA")?).join(r"Microsoft\Windows\SendTo");
+ let f = std::fs::read_dir(dir).ok()?.flatten().map(|e| e.path()).find(|p| p.extension().is_some_and(|x| x.eq_ignore_ascii_case("ZFSendToTarget")))?;
+ f.file_stem().map(|s| s.to_string_lossy().into_owned())
+ }
+
+ /// Takes Windows' own zip entry out of the menu: our "Compress to ZIP" is the one. (Menus of other zip tools, 7-Zip etc., stay.)
+ unsafe fn hide_system_zip(cm: &IContextMenu, menu: HMENU) {
+ use windows::Win32::UI::WindowsAndMessaging::{DeleteMenu, GetMenuItemCount, GetMenuStringW, GetSubMenu, MF_BYPOSITION};
+ let Some(name) = system_zip_name() else { return };
+ // WM_INITMENUPOPUP: the Send to submenu fills itself only when it is opened
+ if let Ok(c2) = cm.cast::<IContextMenu2>() { let _ = unsafe { c2.HandleMenuMsg(0x0117, WPARAM(menu.0 as usize), LPARAM(0)) }; }
+ unsafe fn strip(m: HMENU, name: &str) { unsafe {
+ for i in (0..GetMenuItemCount(Some(m)).max(0)).rev() {
+ let mut b = [0u16; 256];
+ let n = GetMenuStringW(m, i as u32, Some(&mut b), MF_BYPOSITION);
+ if String::from_utf16_lossy(&b[..n.max(0) as usize]).replace('&', "").eq_ignore_ascii_case(name) {
+ let _ = DeleteMenu(m, i as u32, MF_BYPOSITION);
+ continue;
+ }
+ let sub = GetSubMenu(m, i);
+ if !sub.is_invalid() { strip(sub, name); }
+ }
+ } }
+ unsafe { strip(menu, &name) };
+ }
+
pub fn show(h: *mut c_void, paths: &[PathBuf], extra: &[(u32, &str)]) -> Result<Option<u32>> {
unsafe {
let hwnd = HWND(h);
let cm: IContextMenu = ui(hwnd, paths)?;
let menu = CreatePopupMenu()?;
cm.QueryContextMenu(menu, 0, 1, 0x7FFF, CMF_NORMAL).ok()?;
+ hide_system_zip(&cm, menu);
if !extra.is_empty() {
let _ = InsertMenuW(menu, 0, MF_BYPOSITION | MF_SEPARATOR, 0, PCWSTR::null());
for (id, text) in extra.iter().rev() {
@@ -678,6 +876,31 @@ mod sh {
}
}
+ #[cfg(test)]
+ pub fn menu_dump(paths: &[PathBuf]) -> Vec<String> {
+ use windows::Win32::UI::WindowsAndMessaging::{GetMenuItemCount, GetMenuItemID, GetMenuStringW, GetSubMenu, MF_BYPOSITION};
+ fn walk(m: HMENU, depth: usize, out: &mut Vec<String>) {
+ unsafe {
+ for i in 0..GetMenuItemCount(Some(m)).max(0) as u32 {
+ let mut b = [0u16; 256];
+ let n = GetMenuStringW(m, i, Some(&mut b), MF_BYPOSITION);
+ out.push(format!("{}{} (id {})", " ".repeat(depth), String::from_utf16_lossy(&b[..n.max(0) as usize]), GetMenuItemID(m, i as i32)));
+ let sub = GetSubMenu(m, i as i32);
+ if !sub.is_invalid() { walk(sub, depth + 1, out); }
+ }
+ }
+ }
+ let mut out = vec![];
+ unsafe {
+ let cm: IContextMenu = ui(HWND::default(), paths).unwrap();
+ let menu = CreatePopupMenu().unwrap();
+ cm.QueryContextMenu(menu, 0, 1, 0x7FFF, CMF_NORMAL).ok().unwrap();
+ hide_system_zip(&cm, menu);
+ walk(menu, 0, &mut out);
+ }
+ out
+ }
+
/// Starts an OLE drag of `paths` (blocks until dropped).
pub fn drag(h: *mut c_void, paths: &[PathBuf]) -> Result<()> {
unsafe {
@@ -710,8 +933,8 @@ mod sh {
fn effect(k: MODIFIERKEYS_FLAGS) -> DROPEFFECT { if k.0 & 8 != 0 { DROPEFFECT_COPY } else { DROPEFFECT_MOVE } } // 8 = MK_CONTROL
impl IDropTarget_Impl for DropT_Impl {
- fn DragEnter(&self, _o: Ref<'_, IDataObject>, k: MODIFIERKEYS_FLAGS, _pt: &POINTL, e: *mut DROPEFFECT) -> Result<()> { unsafe { *e = effect(k) }; Ok(()) }
- fn DragOver(&self, k: MODIFIERKEYS_FLAGS, _pt: &POINTL, e: *mut DROPEFFECT) -> Result<()> { unsafe { *e = effect(k) }; Ok(()) }
+ fn DragEnter(&self, _o: Ref<'_, IDataObject>, k: MODIFIERKEYS_FLAGS, _pt: &POINTL, e: *mut DROPEFFECT) -> Result<()> { unsafe { *e = if super::in_zip() { DROPEFFECT_NONE } else { effect(k) } }; Ok(()) }
+ fn DragOver(&self, k: MODIFIERKEYS_FLAGS, _pt: &POINTL, e: *mut DROPEFFECT) -> Result<()> { unsafe { *e = if super::in_zip() { DROPEFFECT_NONE } else { effect(k) } }; Ok(()) }
fn DragLeave(&self) -> Result<()> { Ok(()) }
fn Drop(&self, o: Ref<'_, IDataObject>, k: MODIFIERKEYS_FLAGS, pt: &POINTL, e: *mut DROPEFFECT) -> Result<()> {
if super::desk::dragging() {
@@ -720,6 +943,10 @@ mod sh {
unsafe { *e = DROPEFFECT_NONE };
return Ok(());
}
+ if super::in_zip() { // nothing can be dropped into an archive here
+ unsafe { *e = DROPEFFECT_NONE };
+ return Ok(());
+ }
let paths = hdrop_paths(o.ok()?)?;
let to = super::cur_dir();
// like Explorer: same drive moves, other drive copies, Ctrl forces copy
@@ -1063,11 +1290,14 @@ unsafe extern "system" fn proc(h: HWND, m: u32, wp: WPARAM, lp: LPARAM) -> LRESU
ST.with_borrow_mut(|st| { st.list = list; st.tab = tab; st.edit = edit; st.tabs = vec![PathBuf::new()]; });
if !desk() { ui::place_edit(tab); }
let _ = sh::register_drop(list as _, h as isize);
+ if desk() && icons_hidden() { ShowWindow(list, SW_HIDE); }
// live wallpaper: the list is see-through (colour key), so over empty desktop OLE finds the wallpaper window under it,
// and walks up from there to the desktop window: it must accept the drop too
if desk() { let _ = sh::register_drop(h as _, h as isize); }
- let start = if desk() { sh::desktop_dirs().into_iter().next().unwrap_or_default() } else { env::args().nth(1).map(PathBuf::from).unwrap_or_else(|| PathBuf::from("C:\\")) };
+ let (start, pick) = if desk() { (sh::desktop_dirs().into_iter().next().unwrap_or_default(), None) } else { start_target() };
load(h, start);
+ if let Some(name) = pick { select_row(&name); }
+ if !desk() { shellwin::register(h as isize); shellwin::navigated(&cur_dir()); }
0
}
// the bar came, went or changed size (it sets the work area), or the screen changed: the icon list follows
@@ -1081,6 +1311,8 @@ unsafe extern "system" fn proc(h: HWND, m: u32, wp: WPARAM, lp: LPARAM) -> LRESU
}
0
}
+ // mouse buttons 4 / 5 (and keyboard media keys) arrive as APPCOMMAND_BROWSER_BACKWARD / _FORWARD
+ 0x0319 if !desk() && matches!((lp >> 16) & 0x7FF, 1 | 2) => { nav(h, (lp >> 16) & 0x7FF == 1); 1 }
WM_SIZE => {
let list = ST.with_borrow(|st| st.list);
let (cw, ch) = ((lp & 0xFFFF) as i32, ((lp >> 16) & 0xFFFF) as i32);
@@ -1154,12 +1386,7 @@ unsafe extern "system" fn proc(h: HWND, m: u32, wp: WPARAM, lp: LPARAM) -> LRESU
LVN_ITEMACTIVATE => {
let i = (*(lp as *const NMITEMACTIVATE)).iItem;
let hit = ST.with_borrow(|st| usize::try_from(i).ok().and_then(|i| st.rows.get(i)).map(|r| (st.dir.join(&r.name), r.dir)));
- if let Some((p, is_dir)) = hit {
- if is_dir && desk() { let _ = std::process::Command::new(env::current_exe().unwrap_or_default()).arg(p).spawn(); }
- else if is_dir { load(h, p) }
- else if let Some(exe) = tool("photoview").filter(|_| is_pic(&p)) { let _ = std::process::Command::new(exe).arg(&p).spawn(); }
- else { ShellExecuteW(0 as _, ptr::null(), w(&p.to_string_lossy()).as_ptr(), ptr::null(), ptr::null(), SW_SHOWNORMAL); }
- }
+ if let Some((p, is_dir)) = hit { activate(h, p, is_dir); }
}
NM_RCLICK if nm.hwndFrom == list => {
let (_, dir, paths) = selected();
@@ -1180,6 +1407,7 @@ unsafe extern "system" fn proc(h: HWND, m: u32, wp: WPARAM, lp: LPARAM) -> LRESU
}
LVN_BEGINDRAG => {
let (_, _, paths) = selected();
+ let paths = match real_paths(&paths) { Ok(p) => p, Err(e) => { warn(h, &e); return 0; } }; // out of a zip: real files first
drag_log(&format!("BEGINDRAG desk={} selected={}", desk(), paths.len()));
if !paths.is_empty() {
if desk() { let p = (*(lp as *const NMLISTVIEW)).ptAction; desk::drag_start((p.x, p.y)); }
@@ -1198,10 +1426,11 @@ unsafe extern "system" fn proc(h: HWND, m: u32, wp: WPARAM, lp: LPARAM) -> LRESU
let (list, dir, paths) = selected();
match vk {
0x08 => if let Some(p) = dir.parent() { load(h, p.to_path_buf()) },
- 0x2E if !paths.is_empty() => { let _ = sh::fileop(h, sh::Op::Delete, &paths, &dir); }
+ 0x1B if jobs::busy() => { jobs::cancel(); return 0; } // Esc stops packing / unpacking
+ 0x2E if !paths.is_empty() && !in_zip() => { let _ = sh::fileop(h, sh::Op::Delete, &paths, &dir); }
// Space on a picture: a quick look in photoview (pictures only: videos and text are not previewed yet)
0x20 if !desk() => if let (Some(p), Some(exe)) = (paths.first().filter(|p| is_pic(p)), tool("photoview")) { let _ = std::process::Command::new(exe).arg(p).spawn(); },
- 0x71 => {
+ 0x71 if !in_zip() => {
let i = put(list, LVM_GETNEXTITEM, usize::MAX, LVNI_SELECTED as isize);
if i >= 0 { put(list, LVM_EDITLABELW, i as usize, 0); }
}
@@ -1213,7 +1442,10 @@ unsafe extern "system" fn proc(h: HWND, m: u32, wp: WPARAM, lp: LPARAM) -> LRESU
return 0;
}
0x5D if !paths.is_empty() => item_menu(h, &dir, &paths),
- 0x43 | 0x58 if ctrl && !paths.is_empty() => { let _ = sh::clip_set(h, &paths, vk == 0x58); return 0; }
+ 0x43 | 0x58 if ctrl && !paths.is_empty() => { // from a zip: real files in the temp folder, and only a copy (nothing can be cut out of an archive)
+ match real_paths(&paths) { Ok(p) => { let _ = sh::clip_set(h, &p, vk == 0x58 && !in_zip()); } Err(e) => warn(h, &e) }
+ return 0;
+ }
0x41 if ctrl => { // Ctrl+A: select all (state mask LVIS_SELECTED, item -1 = every row)
let mut it: LVITEMW = std::mem::zeroed();
it.stateMask = LVIS_SELECTED;
@@ -1254,6 +1486,7 @@ unsafe extern "system" fn proc(h: HWND, m: u32, wp: WPARAM, lp: LPARAM) -> LRESU
}
WM_SETFOCUS => { SetFocus(ST.with_borrow(|st| st.list)); 0 }
WM_APP => { reload(h); 0 }
+ shellwin::WM_SHELL_SELECT => { shell_select(h as isize, *Box::from_raw(lp as *mut PathBuf)); 0 }
WM_JOB => {
let err = *Box::from_raw(wp as *mut Option<String>);
reload(h);
@@ -1290,7 +1523,7 @@ unsafe extern "system" fn proc(h: HWND, m: u32, wp: WPARAM, lp: LPARAM) -> LRESU
let text = String::from_utf16_lossy(&buf[..n]);
if let Some(q) = text.trim().strip_prefix('?') {
if !q.trim().is_empty() { search(h, q.trim()); SetFocus(list); return 0; } // keep the query in the bar
- } else if p.is_dir() { load(h, p); }
+ } else if p.is_dir() || zipx::split_zip(&p).is_some() { load(h, p); } // also a path inside a .zip
}
SetWindowTextW(edit, w(&cur_dir().to_string_lossy()).as_ptr()); // also undoes a bad path / Esc
SetFocus(list);
@@ -1298,7 +1531,7 @@ unsafe extern "system" fn proc(h: HWND, m: u32, wp: WPARAM, lp: LPARAM) -> LRESU
}
desk::SPAWN_WORKERW if desk() => { desk::spawn_workerw(h); 0 }
WM_TIMER if desk() && wp == 7 => { desk::check_live(ST.with_borrow(|st| st.list)); 0 }
- WM_DESTROY => { PostQuitMessage(0); 0 }
+ WM_DESTROY => { shellwin::revoke(); PostQuitMessage(0); 0 }
_ => DefWindowProcW(h, m, wp, lp),
}
}
@@ -1350,6 +1583,45 @@ fn main() {
#[cfg(test)]
mod tests {
+ #[test]
+ fn a_zip_opens_like_a_folder_and_gives_real_files() {
+ let d = std::env::temp_dir().join(format!("rx-zipview-{}", std::process::id()));
+ let _ = fs::remove_dir_all(&d);
+ fs::create_dir_all(&d).unwrap();
+ fs::write(d.join("f.txt"), "hello").unwrap();
+ let z = d.join("a.zip");
+ let mut w = zipx::Writer::new(fs::File::create(&z).unwrap());
+ w.add_prepared(zipx::prepare("inner/f.txt".into(), &d.join("f.txt")).unwrap()).unwrap();
+ w.add_prepared(zipx::prepare("top.txt".into(), &d.join("f.txt")).unwrap()).unwrap();
+ w.finish().unwrap();
+ let names = |dir: &PathBuf| rows_of(dir).unwrap().into_iter().map(|r| format!("{}{}", r.name, if r.dir { "/" } else { "" })).collect::<Vec<_>>();
+ assert_eq!(names(&z), ["inner/", "top.txt"]); // folders first
+ assert_eq!(names(&z.join("inner")), ["f.txt"]);
+ assert!(rows_of(&d.join("nothing.zip")).is_none());
+ let paths = [z.join("inner").join("f.txt"), z.join("top.txt")];
+ let real = real_paths(&paths).unwrap();
+ assert!(real.iter().all(|p| p.is_file() && fs::read_to_string(p).unwrap() == "hello"), "{real:?}");
+ assert_eq!(real_paths(&[d.join("f.txt")]).unwrap(), [d.join("f.txt")], "ordinary files stay as they are");
+ assert!(zip_names(&[z.join("top.txt"), d.join("f.txt")]).is_none(), "mixed selections are not archive selections");
+ let _ = fs::remove_dir_all(&d);
+ let _ = fs::remove_dir_all(real[0].parent().unwrap());
+ }
+
+ /// By hand (needs the real shell): `cargo test -p rust-explorer system_zip -- --ignored --nocapture`.
+ #[test]
+ #[ignore]
+ fn system_zip_is_not_in_the_menu() {
+ sh::init();
+ let f = std::env::temp_dir().join("rx-menu-dump.txt");
+ std::fs::write(&f, "x").unwrap();
+ let menu = sh::menu_dump(&[f.clone()]);
+ let _ = std::fs::remove_file(f);
+ println!("{}", menu.join("
+"));
+ assert!(menu.iter().any(|l| l.contains("Se&nd to") || l.contains("Send to") || l.contains("&Delete") || l.contains("Delete")), "the menu is there");
+ if let Some(n) = sh::system_zip_name() { assert!(!menu.iter().any(|l| l.trim().starts_with(&n)), "{n} should be gone"); }
+ }
+
use super::*;
fn r(name: &str, dir: bool, size: u64, time: u64) -> Row { Row { name: name.into(), dir, size, time, icon: -1, thumb: -2 } }
diff --git a/plugins/rust-explorer/src/shellwin.rs b/plugins/rust-explorer/src/shellwin.rs
new file mode 100644
index 0000000..348af33
--- /dev/null
+++ b/plugins/rust-explorer/src/shellwin.rs
@@ -0,0 +1,199 @@
+//! Makes the file manager findable as a shell window, so `SHOpenFolderAndSelectItems` ("Show in folder" in browsers, chat apps...)
+//! selects the file in it. Explorer finds the window in `IShellWindows` by folder, then asks it for `Document`, the folder-view
+//! service, and calls `IShellView::SelectItem`. Everything else the shell might ask for answers "not implemented".
+use std::{cell::RefCell, sync::Mutex, ffi::c_void, os::windows::ffi::OsStrExt, path::{Path, PathBuf}};
+use windows::{
+ core::*,
+ Win32::{
+ Foundation::*,
+ System::{Com::*, Ole::*, Variant::*},
+ UI::{Controls::*, Shell::Common::ITEMIDLIST, Shell::*, WindowsAndMessaging::*},
+ },
+};
+
+/// Posted to the window when the shell wants a file selected: lp = Box<PathBuf>. The shell calls us on an RPC thread, so the UI does the work.
+pub const WM_SHELL_SELECT: u32 = WM_APP + 6;
+
+struct Reg { sw: IShellWindows, cookie: i32 }
+thread_local! { static REG: RefCell<Option<Reg>> = const { RefCell::new(None) }; }
+static DIR: Mutex<String> = Mutex::new(String::new());
+
+fn nope<T>() -> Result<T> { Err(E_NOTIMPL.into()) }
+fn url() -> String { format!("file:///{}", DIR.lock().unwrap().replace('\\', "/")) }
+fn wide(s: &std::ffi::OsStr) -> Vec<u16> { s.encode_wide().chain([0]).collect() }
+
+/// Registers the window (call once, after the folder is shown, on the UI thread).
+pub fn register(hwnd: isize) {
+ let _ = (|| -> Result<()> {
+ unsafe {
+ let sw: IShellWindows = CoCreateInstance(&ShellWindows, None, CLSCTX_ALL)?;
+ let me: IWebBrowser2 = Browser { hwnd: hwnd as isize }.into();
+ let cookie = sw.Register(&me.cast::<IDispatch>()?, hwnd as i32, SWC_BROWSER)?;
+ REG.set(Some(Reg { sw, cookie }));
+ }
+ Ok(())
+ })();
+}
+
+/// The window now shows `dir`: the shell matches later requests against it.
+pub fn navigated(dir: &Path) {
+ *DIR.lock().unwrap() = dir.to_string_lossy().into_owned();
+ REG.with_borrow(|r| {
+ let Some(r) = r else { return };
+ let w = wide(dir.as_os_str());
+ unsafe {
+ let mut pidl = std::ptr::null_mut();
+ if SHParseDisplayName(PCWSTR(w.as_ptr()), None, &mut pidl, 0, None).is_err() { return; }
+ if let Ok(v) = InitVariantFromBuffer(pidl as *const c_void, ILGetSize(Some(pidl))) { let _ = r.sw.OnNavigate(r.cookie, &v); }
+ CoTaskMemFree(Some(pidl as *const c_void));
+ }
+ });
+}
+
+pub fn revoke() {
+ if let Some(r) = REG.take() { unsafe { let _ = r.sw.Revoke(r.cookie); } }
+}
+
+#[implement(IWebBrowser2)]
+struct Browser { hwnd: isize }
+impl IDispatch_Impl for Browser_Impl {
+ fn GetTypeInfoCount(&self) -> Result<u32> { Ok(0) }
+ fn GetTypeInfo(&self, _: u32, _: u32) -> Result<ITypeInfo> { nope() }
+ fn GetIDsOfNames(&self, _: *const GUID, _: *const PCWSTR, _: u32, _: u32, _: *mut i32) -> Result<()> { nope() }
+ fn Invoke(&self, _: i32, _: *const GUID, _: u32, _: DISPATCH_FLAGS, _: *const DISPPARAMS, _: *mut VARIANT, _: *mut EXCEPINFO, _: *mut u32) -> Result<()> { nope() }
+}
+
+/// What `Document` returns: only the folder-view service is answered.
+#[implement(IDispatch, IServiceProvider)]
+struct Doc { hwnd: isize }
+impl IDispatch_Impl for Doc_Impl {
+ fn GetTypeInfoCount(&self) -> Result<u32> { Ok(0) }
+ fn GetTypeInfo(&self, _: u32, _: u32) -> Result<ITypeInfo> { nope() }
+ fn GetIDsOfNames(&self, _: *const GUID, _: *const PCWSTR, _: u32, _: u32, _: *mut i32) -> Result<()> { nope() }
+ fn Invoke(&self, _: i32, _: *const GUID, _: u32, _: DISPATCH_FLAGS, _: *const DISPPARAMS, _: *mut VARIANT, _: *mut EXCEPINFO, _: *mut u32) -> Result<()> { nope() }
+}
+impl IServiceProvider_Impl for Doc_Impl {
+ fn QueryService(&self, _svc: *const GUID, riid: *const GUID, out: *mut *mut c_void) -> Result<()> {
+ let v: IShellView = View { hwnd: self.hwnd }.into();
+ unsafe { v.query(&*riid, out).ok() }
+ }
+}
+
+#[implement(IShellView)]
+struct View { hwnd: isize }
+impl IOleWindow_Impl for View_Impl {
+ fn GetWindow(&self) -> Result<HWND> { Ok(HWND(self.hwnd as *mut c_void)) }
+ fn ContextSensitiveHelp(&self, _: BOOL) -> Result<()> { Ok(()) }
+}
+impl IShellView_Impl for View_Impl {
+ fn TranslateAccelerator(&self, _: *const MSG) -> HRESULT { E_NOTIMPL }
+ fn EnableModeless(&self, _: BOOL) -> Result<()> { Ok(()) }
+ fn UIActivate(&self, _: u32) -> Result<()> { Ok(()) }
+ fn Refresh(&self) -> Result<()> { Ok(()) }
+ fn CreateViewWindow(&self, _: Ref<IShellView>, _: *const FOLDERSETTINGS, _: Ref<IShellBrowser>, _: *const RECT) -> Result<HWND> { nope() }
+ fn DestroyViewWindow(&self) -> Result<()> { Ok(()) }
+ fn GetCurrentInfo(&self) -> Result<FOLDERSETTINGS> { nope() }
+ fn AddPropertySheetPages(&self, _: u32, _: LPFNSVADDPROPSHEETPAGE, _: LPARAM) -> Result<()> { nope() }
+ fn SaveViewState(&self) -> Result<()> { Ok(()) }
+ fn SelectItem(&self, pidl: *const ITEMIDLIST, _flags: u32) -> Result<()> {
+ let path = item_path(pidl).ok_or(E_FAIL)?;
+ unsafe { PostMessageW(Some(HWND(self.hwnd as *mut c_void)), WM_SHELL_SELECT, WPARAM(0), LPARAM(Box::into_raw(Box::new(path)) as isize))? };
+ Ok(())
+ }
+ fn GetItemObject(&self, _: &_SVGIO, _: *const GUID, _: *mut *mut c_void) -> Result<()> { nope() }
+}
+
+/// The file system path of the item the shell sent: relative to the folder we showed (or absolute).
+fn item_path(pidl: *const ITEMIDLIST) -> Option<PathBuf> {
+ let name = |p: *const ITEMIDLIST| unsafe {
+ let s = SHGetNameFromIDList(p, SIGDN_FILESYSPATH).ok()?;
+ let r = s.to_string().ok();
+ CoTaskMemFree(Some(s.0 as *const c_void));
+ r.map(PathBuf::from)
+ };
+ let dir = DIR.lock().unwrap().clone();
+ let w = wide(std::ffi::OsStr::new(&dir));
+ let joined = unsafe {
+ let mut base = std::ptr::null_mut();
+ SHParseDisplayName(PCWSTR(w.as_ptr()), None, &mut base, 0, None).ok().and_then(|_| {
+ let abs = ILCombine(Some(base), Some(pidl));
+ let p = name(abs);
+ ILFree(Some(abs));
+ CoTaskMemFree(Some(base as *const c_void));
+ p
+ })
+ };
+ joined.filter(|p| p.parent().is_some_and(|d| d.as_os_str().eq_ignore_ascii_case(&dir))).or_else(|| name(pidl))
+}
+
+// the rest of the browser interface: nothing to offer
+impl IWebBrowser_Impl for Browser_Impl {
+ fn GoBack(&self) -> Result<()> { nope() }
+ fn GoForward(&self) -> Result<()> { nope() }
+ fn GoHome(&self) -> Result<()> { nope() }
+ fn GoSearch(&self) -> Result<()> { nope() }
+ fn Navigate(&self, _url: &BSTR, _flags: *const windows::Win32::System::Variant::VARIANT, _targetframename: *const windows::Win32::System::Variant::VARIANT, _postdata: *const windows::Win32::System::Variant::VARIANT, _headers: *const windows::Win32::System::Variant::VARIANT) -> Result<()> { nope() }
+ fn Refresh(&self) -> Result<()> { nope() }
+ fn Refresh2(&self, _level: *const windows::Win32::System::Variant::VARIANT) -> Result<()> { nope() }
+ fn Stop(&self) -> Result<()> { nope() }
+ fn Application(&self) -> Result<windows::Win32::System::Com::IDispatch> { nope() }
+ fn Parent(&self) -> Result<windows::Win32::System::Com::IDispatch> { nope() }
+ fn Container(&self) -> Result<windows::Win32::System::Com::IDispatch> { nope() }
+ fn Document(&self) -> Result<windows::Win32::System::Com::IDispatch> { Ok(Doc { hwnd: self.hwnd }.into()) }
+ fn TopLevelContainer(&self) -> Result<windows::Win32::Foundation::VARIANT_BOOL> { nope() }
+ fn Type(&self) -> Result<BSTR> { nope() }
+ fn Left(&self) -> Result<i32> { nope() }
+ fn SetLeft(&self, _left: i32) -> Result<()> { nope() }
+ fn Top(&self) -> Result<i32> { nope() }
+ fn SetTop(&self, _top: i32) -> Result<()> { nope() }
+ fn Width(&self) -> Result<i32> { nope() }
+ fn SetWidth(&self, _width: i32) -> Result<()> { nope() }
+ fn Height(&self) -> Result<i32> { nope() }
+ fn SetHeight(&self, _height: i32) -> Result<()> { nope() }
+ fn LocationName(&self) -> Result<BSTR> { Ok(BSTR::from(url())) }
+ fn LocationURL(&self) -> Result<BSTR> { Ok(BSTR::from(url())) }
+ fn Busy(&self) -> Result<windows::Win32::Foundation::VARIANT_BOOL> { nope() }
+}
+impl IWebBrowserApp_Impl for Browser_Impl {
+ fn Quit(&self) -> Result<()> { nope() }
+ fn ClientToWindow(&self, _pcx: *mut i32, _pcy: *mut i32) -> Result<()> { nope() }
+ fn PutProperty(&self, _property: &BSTR, _vtvalue: &windows::Win32::System::Variant::VARIANT) -> Result<()> { nope() }
+ fn GetProperty(&self, _property: &BSTR) -> Result<windows::Win32::System::Variant::VARIANT> { nope() }
+ fn Name(&self) -> Result<BSTR> { nope() }
+ fn HWND(&self) -> Result<windows::Win32::Foundation::SHANDLE_PTR> { Ok(SHANDLE_PTR(self.hwnd)) }
+ fn FullName(&self) -> Result<BSTR> { nope() }
+ fn Path(&self) -> Result<BSTR> { nope() }
+ fn Visible(&self) -> Result<windows::Win32::Foundation::VARIANT_BOOL> { Ok(VARIANT_TRUE) }
+ fn SetVisible(&self, _value: windows::Win32::Foundation::VARIANT_BOOL) -> Result<()> { nope() }
+ fn StatusBar(&self) -> Result<windows::Win32::Foundation::VARIANT_BOOL> { nope() }
+ fn SetStatusBar(&self, _value: windows::Win32::Foundation::VARIANT_BOOL) -> Result<()> { nope() }
+ fn StatusText(&self) -> Result<BSTR> { nope() }
+ fn SetStatusText(&self, _statustext: &BSTR) -> Result<()> { nope() }
+ fn ToolBar(&self) -> Result<i32> { nope() }
+ fn SetToolBar(&self, _value: i32) -> Result<()> { nope() }
+ fn MenuBar(&self) -> Result<windows::Win32::Foundation::VARIANT_BOOL> { nope() }
+ fn SetMenuBar(&self, _value: windows::Win32::Foundation::VARIANT_BOOL) -> Result<()> { nope() }
+ fn FullScreen(&self) -> Result<windows::Win32::Foundation::VARIANT_BOOL> { nope() }
+ fn SetFullScreen(&self, _bfullscreen: windows::Win32::Foundation::VARIANT_BOOL) -> Result<()> { nope() }
+}
+impl IWebBrowser2_Impl for Browser_Impl {
+ fn Navigate2(&self, _url: *const windows::Win32::System::Variant::VARIANT, _flags: *const windows::Win32::System::Variant::VARIANT, _targetframename: *const windows::Win32::System::Variant::VARIANT, _postdata: *const windows::Win32::System::Variant::VARIANT, _headers: *const windows::Win32::System::Variant::VARIANT) -> Result<()> { nope() }
+ fn QueryStatusWB(&self, _cmdid: windows::Win32::System::Ole::OLECMDID) -> Result<windows::Win32::System::Ole::OLECMDF> { nope() }
+ fn ExecWB(&self, _cmdid: windows::Win32::System::Ole::OLECMDID, _cmdexecopt: windows::Win32::System::Ole::OLECMDEXECOPT, _pvain: *const windows::Win32::System::Variant::VARIANT, _pvaout: *mut windows::Win32::System::Variant::VARIANT) -> Result<()> { nope() }
+ fn ShowBrowserBar(&self, _pvaclsid: *const windows::Win32::System::Variant::VARIANT, _pvarshow: *const windows::Win32::System::Variant::VARIANT, _pvarsize: *const windows::Win32::System::Variant::VARIANT) -> Result<()> { nope() }
+ fn ReadyState(&self) -> Result<windows::Win32::System::Ole::READYSTATE> { nope() }
+ fn Offline(&self) -> Result<windows::Win32::Foundation::VARIANT_BOOL> { nope() }
+ fn SetOffline(&self, _boffline: windows::Win32::Foundation::VARIANT_BOOL) -> Result<()> { nope() }
+ fn Silent(&self) -> Result<windows::Win32::Foundation::VARIANT_BOOL> { nope() }
+ fn SetSilent(&self, _bsilent: windows::Win32::Foundation::VARIANT_BOOL) -> Result<()> { nope() }
+ fn RegisterAsBrowser(&self) -> Result<windows::Win32::Foundation::VARIANT_BOOL> { nope() }
+ fn SetRegisterAsBrowser(&self, _bregister: windows::Win32::Foundation::VARIANT_BOOL) -> Result<()> { nope() }
+ fn RegisterAsDropTarget(&self) -> Result<windows::Win32::Foundation::VARIANT_BOOL> { nope() }
+ fn SetRegisterAsDropTarget(&self, _bregister: windows::Win32::Foundation::VARIANT_BOOL) -> Result<()> { nope() }
+ fn TheaterMode(&self) -> Result<windows::Win32::Foundation::VARIANT_BOOL> { nope() }
+ fn SetTheaterMode(&self, _bregister: windows::Win32::Foundation::VARIANT_BOOL) -> Result<()> { nope() }
+ fn AddressBar(&self) -> Result<windows::Win32::Foundation::VARIANT_BOOL> { nope() }
+ fn SetAddressBar(&self, _value: windows::Win32::Foundation::VARIANT_BOOL) -> Result<()> { nope() }
+ fn Resizable(&self) -> Result<windows::Win32::Foundation::VARIANT_BOOL> { nope() }
+ fn SetResizable(&self, _value: windows::Win32::Foundation::VARIANT_BOOL) -> Result<()> { nope() }
+}
diff --git a/plugins/rust-explorer/src/ui.rs b/plugins/rust-explorer/src/ui.rs
index 5a2d716..47e4a62 100644
--- a/plugins/rust-explorer/src/ui.rs
+++ b/plugins/rust-explorer/src/ui.rs
@@ -493,6 +493,7 @@ unsafe fn paint_chrome(hwnd: HWND) {
let bg = CreateSolidBrush(pal().bg);
FillRect(dc, &cr, bg);
DeleteObject(bg);
+ winrust_common::style::backdrop(dc, cr, sc(100) as f32 / 100.0);
SetBkMode(dc, TRANSPARENT as i32);
let l = layout(cr.right, labels.len());
let hot = HOT.get();
@@ -594,6 +595,7 @@ pub unsafe fn paint_status(h: HWND) {
let bg = CreateSolidBrush(pal().bg);
FillRect(dc, &strip, bg);
DeleteObject(bg);
+ winrust_common::style::backdrop(dc, strip, sc(100) as f32 / 100.0);
fill(dc, rc(0, strip.top, cr.right, strip.top + 1), pal().line, 0); // hairline above the status line
let snap = ST.with(|c| {
c.try_borrow().ok().map(|s| {
@@ -841,6 +843,7 @@ unsafe fn paint_nav(hwnd: HWND) {
let bg = CreateSolidBrush(pal().bg);
FillRect(dc, &cr, bg);
DeleteObject(bg);
+ winrust_common::style::backdrop(dc, cr, sc(100) as f32 / 100.0);
SetBkMode(dc, TRANSPARENT as i32);
let (f_txt, f_head, f_glyph, f_small) = (font(13, 400, ui_face()) as HFONT, font(12, 600, ui_face()) as HFONT, font(16, 400, winrust_common::style::icon_face()) as HFONT, font(11, 400, ui_face()) as HFONT);
let of = SelectObject(dc, f_txt);
diff --git a/plugins/rust-explorer/src/zipx.rs b/plugins/rust-explorer/src/zipx.rs
new file mode 100644
index 0000000..36c8b63
--- /dev/null
+++ b/plugins/rust-explorer/src/zipx.rs
@@ -0,0 +1,1573 @@
+//! Our own ZIP: CRC-32, inflate, deflate (LZ77 + dynamic Huffman) and the container with ZIP64. No crates.
+//! shortcuts: encrypted entries and methods other than stored / deflate are refused (upgrade when someone needs them); a sample
+//! with random-looking bytes is stored without trying to compress it, so a repeated random block would not be squeezed.
+use std::{
+ fs,
+ io::{self, BufWriter, Read, Seek, SeekFrom, Write},
+ path::{Component, Path, PathBuf},
+ time::{Duration, SystemTime, UNIX_EPOCH},
+};
+
+// ---------- CRC-32 ----------
+
+const fn crc_tables() -> [[u32; 256]; 8] {
+ let mut t = [[0u32; 256]; 8];
+ let mut i = 0;
+ while i < 256 {
+ let mut c = i as u32;
+ let mut k = 0;
+ while k < 8 {
+ c = if c & 1 != 0 { 0xEDB8_8320 ^ (c >> 1) } else { c >> 1 };
+ k += 1;
+ }
+ t[0][i] = c;
+ i += 1;
+ }
+ let mut i = 0;
+ while i < 256 {
+ let mut s = 1;
+ while s < 8 {
+ t[s][i] = (t[s - 1][i] >> 8) ^ t[0][(t[s - 1][i] & 0xFF) as usize];
+ s += 1;
+ }
+ i += 1;
+ }
+ t
+}
+static CRC: [[u32; 256]; 8] = crc_tables();
+
+/// CRC-32 of `data` continuing `crc` (start with 0).
+pub fn crc32(crc: u32, data: &[u8]) -> u32 {
+ let mut c = !crc;
+ let mut ch = data.chunks_exact(8);
+ for b in &mut ch {
+ let lo = u32::from_le_bytes([b[0], b[1], b[2], b[3]]) ^ c;
+ let hi = u32::from_le_bytes([b[4], b[5], b[6], b[7]]);
+ c = CRC[7][(lo & 0xFF) as usize]
+ ^ CRC[6][((lo >> 8) & 0xFF) as usize]
+ ^ CRC[5][((lo >> 16) & 0xFF) as usize]
+ ^ CRC[4][(lo >> 24) as usize]
+ ^ CRC[3][(hi & 0xFF) as usize]
+ ^ CRC[2][((hi >> 8) & 0xFF) as usize]
+ ^ CRC[1][((hi >> 16) & 0xFF) as usize]
+ ^ CRC[0][(hi >> 24) as usize];
+ }
+ for &b in ch.remainder() {
+ c = CRC[0][((c ^ b as u32) & 0xFF) as usize] ^ (c >> 8);
+ }
+ !c
+}
+
+// ---------- shared deflate tables ----------
+
+const LBASE: [u16; 29] = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258];
+const LEXT: [u8; 29] = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0];
+const DBASE: [u16; 30] = [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577];
+const DEXT: [u8; 30] = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13];
+const CL_ORDER: [usize; 19] = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
+
+const fn len_codes() -> [u8; 259] {
+ let mut t = [0u8; 259];
+ let mut c = 0;
+ while c < 29 {
+ let mut l = LBASE[c] as usize;
+ let end = if c == 28 { 259 } else { LBASE[c + 1] as usize };
+ while l < end {
+ t[l] = c as u8;
+ l += 1;
+ }
+ c += 1;
+ }
+ t[258] = 28;
+ t
+}
+static LEN_CODE: [u8; 259] = len_codes();
+
+fn dist_code(d: usize) -> usize {
+ let x = d - 1;
+ if x < 4 {
+ x
+ } else {
+ let l = (usize::BITS - 1 - x.leading_zeros()) as usize;
+ 2 * l + ((x >> (l - 1)) & 1)
+ }
+}
+
+fn rev_bits(mut c: u32, n: u32) -> u32 {
+ let mut r = 0;
+ for _ in 0..n {
+ r = (r << 1) | (c & 1);
+ c >>= 1;
+ }
+ r
+}
+
+// ---------- deflate ----------
+
+/// Code lengths (at most `limit`) of a canonical Huffman code for these frequencies; the code is complete.
+fn code_lengths(freq: &[u32], limit: u32) -> Vec<u8> {
+ let mut len = vec![0u8; freq.len()];
+ let used: Vec<usize> = (0..freq.len()).filter(|&i| freq[i] > 0).collect();
+ match used.len() {
+ 0 => return len,
+ 1 => {
+ len[used[0]] = 1;
+ return len;
+ }
+ _ => {}
+ }
+ // Huffman tree by a heap; the depth of a leaf is its length
+ let mut parent = vec![usize::MAX; freq.len() + used.len()];
+ let mut heap = std::collections::BinaryHeap::new();
+ for &i in &used {
+ heap.push(std::cmp::Reverse((freq[i] as u64, i)));
+ }
+ let mut next = freq.len();
+ while heap.len() > 1 {
+ let std::cmp::Reverse((fa, a)) = heap.pop().unwrap();
+ let std::cmp::Reverse((fb, b)) = heap.pop().unwrap();
+ parent[a] = next;
+ parent[b] = next;
+ heap.push(std::cmp::Reverse((fa + fb, next)));
+ next += 1;
+ }
+ for &i in &used {
+ let (mut d, mut n) = (0u32, i);
+ while parent[n] != usize::MAX {
+ n = parent[n];
+ d += 1;
+ }
+ len[i] = d.min(limit) as u8;
+ }
+ // the clamp can oversubscribe the code: lengthen the cheapest codes until it fits, then spend the slack on the commonest
+ let unit = |l: u8| 1u64 << (limit - l as u32);
+ let mut kraft: u64 = used.iter().map(|&i| unit(len[i])).sum();
+ let full = 1u64 << limit;
+ let mut by_freq = used.clone();
+ by_freq.sort_by_key(|&i| std::cmp::Reverse(freq[i]));
+ while kraft > full {
+ let i = *used.iter().filter(|&&i| (len[i] as u32) < limit).max_by_key(|&&i| (len[i], std::cmp::Reverse(freq[i]))).unwrap();
+ kraft -= unit(len[i]) / 2; // len -> len + 1 halves its share
+ len[i] += 1;
+ }
+ loop {
+ let mut changed = false;
+ for &i in &by_freq {
+ if kraft < full && len[i] > 1 && full - kraft >= unit(len[i]) {
+ kraft += unit(len[i]); // len -> len - 1 doubles its share: adds one more of the same
+ len[i] -= 1;
+ changed = true;
+ }
+ }
+ if kraft == full || !changed {
+ break;
+ }
+ }
+ len
+}
+
+/// Canonical codes for the lengths, already bit-reversed for the LSB-first stream.
+fn codes(len: &[u8]) -> Vec<u32> {
+ let mut count = [0u32; 16];
+ for &l in len {
+ count[l as usize] += 1;
+ }
+ count[0] = 0;
+ let (mut next, mut code) = ([0u32; 16], 0u32);
+ for b in 1..16 {
+ code = (code + count[b - 1]) << 1;
+ next[b] = code;
+ }
+ len.iter()
+ .map(|&l| {
+ if l == 0 {
+ return 0;
+ }
+ let c = next[l as usize];
+ next[l as usize] += 1;
+ rev_bits(c, l as u32)
+ })
+ .collect()
+}
+
+struct BitW {
+ acc: u64,
+ n: u32,
+}
+
+impl BitW {
+ fn put(&mut self, out: &mut Vec<u8>, v: u32, bits: u32) {
+ self.acc |= (v as u64) << self.n;
+ self.n += bits;
+ while self.n >= 8 {
+ out.push(self.acc as u8);
+ self.acc >>= 8;
+ self.n -= 8;
+ }
+ }
+ fn align(&mut self, out: &mut Vec<u8>) {
+ if self.n > 0 {
+ out.push(self.acc as u8);
+ self.acc = 0;
+ self.n = 0;
+ }
+ }
+}
+
+const WIN: usize = 32768;
+const HASH_BITS: u32 = 15;
+const MAX_CHAIN: u32 = 16;
+const LAZY_BELOW: u32 = 16;
+
+/// Deflate with a bit stream that continues across chunks; a match may reach back into the last 32 KB of the previous chunk.
+pub struct Deflater {
+ bw: BitW,
+ head: Vec<u32>,
+ prev: Vec<u32>,
+ next_ins: usize,
+ hist: Vec<u8>,
+}
+
+/// Bytes that look random (an already compressed file): not worth a match search. Order-0 entropy of 8 samples of 512 bytes.
+fn looks_random(d: &[u8]) -> bool {
+ if d.len() < 4096 {
+ return false;
+ }
+ let mut count = [0u32; 256];
+ let step = d.len() / 8;
+ for k in 0..8 {
+ for &b in &d[k * step..k * step + 512] {
+ count[b as usize] += 1;
+ }
+ }
+ let h: f64 = count.iter().filter(|&&c| c > 0).map(|&c| { let p = c as f64 / 4096.0; -p * p.log2() }).sum();
+ h > 7.8
+}
+
+impl Deflater {
+ pub fn new() -> Self {
+ Deflater { bw: BitW { acc: 0, n: 0 }, head: vec![u32::MAX; 1 << HASH_BITS], prev: vec![u32::MAX; WIN], next_ins: 0, hist: Vec::new() }
+ }
+
+ fn hash(d: &[u8], i: usize) -> usize {
+ let v = u32::from_le_bytes([d[i], d[i + 1], d[i + 2], d[i + 3]]);
+ (v.wrapping_mul(0x9E37_79B1) >> (32 - HASH_BITS)) as usize
+ }
+
+ fn ins(&mut self, d: &[u8], i: usize) {
+ if i >= self.next_ins && i + 4 <= d.len() {
+ let h = Self::hash(d, i);
+ self.prev[i & (WIN - 1)] = self.head[h];
+ self.head[h] = i as u32;
+ self.next_ins = i + 1;
+ }
+ }
+
+ fn search(&self, d: &[u8], i: usize) -> (u32, u32) {
+ if i + 4 > d.len() {
+ return (0, 0);
+ }
+ let max = (d.len() - i).min(258);
+ let (mut best, mut best_d) = (0usize, 0usize);
+ let mut cand = self.head[Self::hash(d, i)];
+ let mut chain = MAX_CHAIN;
+ while cand != u32::MAX && chain > 0 {
+ let c = cand as usize;
+ if c >= i || i - c > WIN {
+ break;
+ }
+ let mut l = 0;
+ while l + 8 <= max {
+ let x = u64::from_le_bytes(d[c + l..c + l + 8].try_into().unwrap()) ^ u64::from_le_bytes(d[i + l..i + l + 8].try_into().unwrap());
+ if x != 0 {
+ l += (x.trailing_zeros() / 8) as usize;
+ break;
+ }
+ l += 8;
+ }
+ while l < max && d[c + l] == d[i + l] {
+ l += 1;
+ }
+ if l > best {
+ best = l;
+ best_d = i - c;
+ if l == max {
+ break;
+ }
+ if l >= 128 {
+ chain >>= 2;
+ }
+ }
+ let nxt = self.prev[c & (WIN - 1)];
+ if nxt >= cand {
+ break;
+ }
+ cand = nxt;
+ chain -= 1;
+ }
+ if best < 4 && !(best == 3 && best_d <= 4096) { (0, 0) } else { (best as u32, best_d as u32) }
+ }
+
+ /// Appends the compressed form of `data` (not the end of the stream: call `finish` after the last chunk).
+ pub fn chunk(&mut self, data: &[u8], out: &mut Vec<u8>) {
+ if data.is_empty() {
+ return;
+ }
+ if looks_random(data) {
+ self.stored(data, out);
+ } else {
+ self.matched(data, out);
+ }
+ self.hist.extend_from_slice(data);
+ if self.hist.len() > WIN {
+ self.hist.drain(..self.hist.len() - WIN);
+ }
+ }
+
+ fn matched(&mut self, data: &[u8], out: &mut Vec<u8>) {
+ let h = self.hist.len();
+ let mut buf = Vec::with_capacity(h + data.len());
+ buf.extend_from_slice(&self.hist);
+ buf.extend_from_slice(data);
+ let d = &buf[..];
+ self.head.fill(u32::MAX);
+ self.next_ins = 0;
+ for j in 0..h {
+ self.ins(d, j);
+ }
+ let mut syms: Vec<u32> = Vec::with_capacity(data.len() / 2 + 16); // literal = byte, match = len << 16 | dist
+ let n = d.len();
+ let mut i = h;
+ while i < n {
+ let mut m = self.search(d, i);
+ self.ins(d, i);
+ if m.0 >= 3 {
+ while m.0 < LAZY_BELOW && i + 1 < n {
+ let m2 = self.search(d, i + 1);
+ if m2.0 <= m.0 {
+ break;
+ }
+ syms.push(d[i] as u32);
+ i += 1;
+ self.ins(d, i);
+ m = m2;
+ }
+ syms.push(m.0 << 16 | m.1);
+ // inside a long match only its start and end go into the hash: the middle is rarely found again and costs time
+ let end = i + m.0 as usize;
+ for j in (i + 1..end).filter(|&j| m.0 < 48 || j < i + 17 || j + 16 >= end) {
+ self.ins(d, j);
+ }
+ i = end;
+ } else {
+ syms.push(d[i] as u32);
+ i += 1;
+ }
+ }
+ self.block(data, &syms, out);
+ }
+
+ fn stored(&mut self, raw: &[u8], out: &mut Vec<u8>) {
+ for part in raw.chunks(65535) {
+ self.bw.put(out, 0, 3);
+ self.bw.align(out);
+ out.extend_from_slice(&(part.len() as u16).to_le_bytes());
+ out.extend_from_slice(&(!(part.len() as u16)).to_le_bytes());
+ out.extend_from_slice(part);
+ }
+ }
+
+ fn block(&mut self, raw: &[u8], syms: &[u32], out: &mut Vec<u8>) {
+ let (mut lf, mut df) = ([0u32; 286], [0u32; 30]);
+ lf[256] = 1;
+ for &s in syms {
+ if s >> 16 == 0 {
+ lf[s as usize] += 1;
+ } else {
+ lf[257 + LEN_CODE[(s >> 16) as usize] as usize] += 1;
+ df[dist_code((s & 0xFFFF) as usize)] += 1;
+ }
+ }
+ if df.iter().all(|&f| f == 0) {
+ df[0] = 1;
+ }
+ let ll = code_lengths(&lf, 15);
+ let dl = code_lengths(&df, 15);
+ let hlit = (257..=286).rev().find(|&n| ll[n - 1] != 0).unwrap_or(257);
+ let hdist = (1..=30).rev().find(|&n| dl[n - 1] != 0).unwrap_or(1);
+ // the lengths as run-length symbols (16 = repeat previous, 17 / 18 = zeros)
+ let all: Vec<u8> = ll[..hlit].iter().chain(&dl[..hdist]).copied().collect();
+ let mut rl: Vec<(u8, u8)> = vec![];
+ let mut k = 0;
+ while k < all.len() {
+ let v = all[k];
+ let mut run = 1;
+ while k + run < all.len() && all[k + run] == v {
+ run += 1;
+ }
+ k += run;
+ if v == 0 {
+ while run >= 3 {
+ let r = run.min(138);
+ if r >= 11 { rl.push((18, (r - 11) as u8)) } else { rl.push((17, (r - 3) as u8)) }
+ run -= r;
+ }
+ } else {
+ rl.push((v, 0));
+ run -= 1;
+ while run >= 3 {
+ let r = run.min(6);
+ rl.push((16, (r - 3) as u8));
+ run -= r;
+ }
+ }
+ for _ in 0..run {
+ rl.push((v, 0));
+ }
+ }
+ let mut cf = [0u32; 19];
+ for &(s, _) in &rl {
+ cf[s as usize] += 1;
+ }
+ let cl = code_lengths(&cf, 7);
+ let hclen = (4..=19).rev().find(|&n| cl[CL_ORDER[n - 1]] != 0).unwrap_or(4);
+ // dynamic against stored, in bits
+ let mut bits: u64 = 17 + 3 * hclen as u64;
+ bits += rl.iter().map(|&(s, _)| cl[s as usize] as u64 + [0, 0, 0, 2, 3, 7][if s >= 16 { s as usize - 13 } else { 0 }]).sum::<u64>();
+ bits += (0..286).map(|i| lf[i] as u64 * (ll[i] as u64 + if i >= 257 { LEXT[i - 257] as u64 } else { 0 })).sum::<u64>();
+ bits += (0..30).map(|i| df[i] as u64 * (dl[i] as u64 + DEXT[i] as u64)).sum::<u64>();
+ let stored_bits = raw.len() as u64 * 8 + 40 * (raw.len() as u64 / 65535 + 1);
+ if bits >= stored_bits {
+ self.stored(raw, out);
+ return;
+ }
+ let (lc, dc, cc) = (codes(&ll), codes(&dl), codes(&cl));
+ let bw = &mut self.bw;
+ bw.put(out, 0b100, 3); // not final (0), dynamic Huffman (type 2, LSB first)
+ bw.put(out, (hlit - 257) as u32, 5);
+ bw.put(out, (hdist - 1) as u32, 5);
+ bw.put(out, (hclen - 4) as u32, 4);
+ for &o in &CL_ORDER[..hclen] {
+ bw.put(out, cl[o] as u32, 3);
+ }
+ for &(s, x) in &rl {
+ bw.put(out, cc[s as usize], cl[s as usize] as u32);
+ match s {
+ 16 => bw.put(out, x as u32, 2),
+ 17 => bw.put(out, x as u32, 3),
+ 18 => bw.put(out, x as u32, 7),
+ _ => {}
+ }
+ }
+ for &s in syms {
+ if s >> 16 == 0 {
+ bw.put(out, lc[s as usize], ll[s as usize] as u32);
+ } else {
+ let (len, dist) = ((s >> 16) as usize, (s & 0xFFFF) as usize);
+ let c = LEN_CODE[len] as usize;
+ bw.put(out, lc[257 + c], ll[257 + c] as u32);
+ bw.put(out, (len - LBASE[c] as usize) as u32, LEXT[c] as u32);
+ let d = dist_code(dist);
+ bw.put(out, dc[d], dl[d] as u32);
+ bw.put(out, (dist - DBASE[d] as usize) as u32, DEXT[d] as u32);
+ }
+ }
+ bw.put(out, lc[256], ll[256] as u32);
+ }
+
+ /// Ends the stream: an empty final block, padded to a whole byte.
+ pub fn finish(&mut self, out: &mut Vec<u8>) {
+ self.bw.put(out, 0b011, 3); // final, fixed Huffman
+ self.bw.put(out, 0, 7); // end of block
+ self.bw.align(out);
+ }
+}
+
+/// The whole of `data` as one deflate stream.
+pub fn deflate(data: &[u8]) -> Vec<u8> {
+ let mut d = Deflater::new();
+ let mut out = Vec::with_capacity(data.len() / 2 + 64);
+ for c in data.chunks(CHUNK) {
+ d.chunk(c, &mut out);
+ }
+ d.finish(&mut out);
+ out
+}
+
+pub const CHUNK: usize = 1 << 20;
+const FLUSH_AT: usize = (1 << 19) + WIN;
+
+// ---------- inflate ----------
+
+struct Bits<R: Read> {
+ r: R,
+ buf: Box<[u8; 1 << 16]>,
+ pos: usize,
+ len: usize,
+ acc: u64,
+ n: u32,
+}
+
+fn eof() -> io::Error {
+ io::Error::new(io::ErrorKind::UnexpectedEof, "compressed data ends too early")
+}
+
+fn bad(m: &str) -> io::Error {
+ io::Error::new(io::ErrorKind::InvalidData, m.to_string())
+}
+
+impl<R: Read> Bits<R> {
+ fn new(r: R) -> Self {
+ Bits { r, buf: Box::new([0; 1 << 16]), pos: 0, len: 0, acc: 0, n: 0 }
+ }
+ fn fill_buf(&mut self) -> io::Result<()> {
+ if self.pos == self.len {
+ self.len = self.r.read(&mut self.buf[..])?;
+ self.pos = 0;
+ }
+ Ok(())
+ }
+ fn refill(&mut self) -> io::Result<()> {
+ while self.n <= 56 {
+ self.fill_buf()?;
+ if self.pos == self.len {
+ break; // the end: the missing bits read as zeros and `take` complains if they were needed
+ }
+ self.acc |= (self.buf[self.pos] as u64) << self.n;
+ self.pos += 1;
+ self.n += 8;
+ }
+ Ok(())
+ }
+ fn peek(&mut self, k: u32) -> io::Result<u32> {
+ if self.n < k {
+ self.refill()?;
+ }
+ Ok((self.acc & ((1u64 << k) - 1)) as u32)
+ }
+ fn take(&mut self, k: u32) -> io::Result<()> {
+ if self.n < k {
+ return Err(eof());
+ }
+ self.acc >>= k;
+ self.n -= k;
+ Ok(())
+ }
+ fn bits(&mut self, k: u32) -> io::Result<u32> {
+ let v = self.peek(k)?;
+ self.take(k)?;
+ Ok(v)
+ }
+ /// `n` whole bytes after aligning (stored block), appended to `out`.
+ fn bytes(&mut self, mut n: usize, out: &mut Vec<u8>) -> io::Result<()> {
+ while n > 0 && self.n >= 8 {
+ out.push(self.acc as u8);
+ self.acc >>= 8;
+ self.n -= 8;
+ n -= 1;
+ }
+ while n > 0 {
+ self.fill_buf()?;
+ if self.pos == self.len {
+ return Err(eof());
+ }
+ let k = n.min(self.len - self.pos);
+ out.extend_from_slice(&self.buf[self.pos..self.pos + k]);
+ self.pos += k;
+ n -= k;
+ }
+ Ok(())
+ }
+}
+
+/// Lookup by the next `max` bits (bit-reversed codes): `len << 9 | symbol`, 0 = no such code.
+struct Table {
+ max: u32,
+ t: Vec<u16>,
+}
+
+impl Table {
+ fn new(len: &[u8]) -> io::Result<Table> {
+ let max = len.iter().copied().max().unwrap_or(0) as u32;
+ let mut count = [0u32; 16];
+ for &l in len {
+ count[l as usize] += 1;
+ }
+ count[0] = 0;
+ let (mut next, mut code) = ([0u32; 16], 0u32);
+ for b in 1..16 {
+ code = (code + count[b - 1]) << 1;
+ next[b] = code;
+ if count[b] > 0 && code + count[b] > 1 << b {
+ return Err(bad("oversubscribed Huffman code"));
+ }
+ }
+ let mut t = vec![0u16; 1 << max];
+ for (s, &l) in len.iter().enumerate() {
+ if l == 0 {
+ continue;
+ }
+ let c = rev_bits(next[l as usize], l as u32) as usize;
+ next[l as usize] += 1;
+ for k in (c..t.len()).step_by(1 << l) {
+ t[k] = (l as u16) << 9 | s as u16;
+ }
+ }
+ Ok(Table { max, t })
+ }
+ fn sym<R: Read>(&self, b: &mut Bits<R>) -> io::Result<usize> {
+ let e = *self.t.get(b.peek(self.max)? as usize).ok_or_else(|| bad("bad Huffman code"))?;
+ if e == 0 {
+ return Err(bad("bad Huffman code"));
+ }
+ b.take((e >> 9) as u32)?;
+ Ok((e & 511) as usize)
+ }
+}
+
+/// Inflates `r` into `w`; returns the CRC-32 and the size of the output. `progress` gets the bytes written so far in each step:
+/// answering false stops with an Interrupted error.
+pub fn inflate<R: Read, W: Write>(r: R, w: &mut W, progress: &mut dyn FnMut(u64) -> bool) -> io::Result<(u32, u64)> {
+ let mut b = Bits::new(r);
+ let mut out: Vec<u8> = Vec::with_capacity(1 << 19);
+ let (mut crc, mut total) = (0u32, 0u64);
+ let mut flush = |out: &mut Vec<u8>, w: &mut W, all: bool| -> io::Result<()> {
+ let keep = if all { 0 } else { WIN };
+ if out.len() > keep {
+ let n = out.len() - keep;
+ crc = crc32(crc, &out[..n]);
+ total += n as u64;
+ w.write_all(&out[..n])?;
+ out.drain(..n);
+ if !progress(n as u64) {
+ return Err(io::Error::new(io::ErrorKind::Interrupted, "cancelled"));
+ }
+ }
+ Ok(())
+ };
+ let fixed = {
+ let mut l = [8u8; 288];
+ l[144..256].fill(9);
+ l[256..280].fill(7);
+ (Table::new(&l)?, Table::new(&[5u8; 30])?)
+ };
+ loop {
+ let last = b.bits(1)? == 1;
+ match b.bits(2)? {
+ 0 => {
+ b.take(b.n % 8)?;
+ let len = b.bits(16)? as usize;
+ if b.bits(16)? as usize != !len & 0xFFFF {
+ return Err(bad("stored block length"));
+ }
+ b.bytes(len, &mut out)?;
+ }
+ t @ (1 | 2) => {
+ let dynamic;
+ let (lt, dt) = if t == 1 {
+ (&fixed.0, &fixed.1)
+ } else {
+ let (hlit, hdist, hclen) = (b.bits(5)? as usize + 257, b.bits(5)? as usize + 1, b.bits(4)? as usize + 4);
+ if hlit > 286 || hdist > 30 {
+ return Err(bad("too many codes"));
+ }
+ let mut cl = [0u8; 19];
+ for &o in &CL_ORDER[..hclen] {
+ cl[o] = b.bits(3)? as u8;
+ }
+ let ct = Table::new(&cl)?;
+ let mut lens = vec![0u8; hlit + hdist];
+ let mut i = 0;
+ while i < lens.len() {
+ let s = ct.sym(&mut b)?;
+ let (v, rep) = match s {
+ 0..=15 => (s as u8, 1),
+ 16 => (*lens[..i].last().ok_or_else(|| bad("repeat with nothing before"))?, 3 + b.bits(2)? as usize),
+ 17 => (0, 3 + b.bits(3)? as usize),
+ _ => (0, 11 + b.bits(7)? as usize),
+ };
+ if i + rep > lens.len() {
+ return Err(bad("code lengths overrun"));
+ }
+ lens[i..i + rep].fill(v);
+ i += rep;
+ }
+ dynamic = (Table::new(&lens[..hlit])?, Table::new(&lens[hlit..])?);
+ (&dynamic.0, &dynamic.1)
+ };
+ loop {
+ let s = lt.sym(&mut b)?;
+ if s < 256 {
+ out.push(s as u8);
+ } else if s == 256 {
+ break;
+ } else {
+ let c = s - 257;
+ if c >= 29 {
+ return Err(bad("bad length code"));
+ }
+ let len = LBASE[c] as usize + b.bits(LEXT[c] as u32)? as usize;
+ let d = dt.sym(&mut b)?;
+ if d >= 30 {
+ return Err(bad("bad distance code"));
+ }
+ let dist = DBASE[d] as usize + b.bits(DEXT[d] as u32)? as usize;
+ if dist > out.len() {
+ return Err(bad("distance reaches before the start"));
+ }
+ let from = out.len() - dist;
+ if dist >= len {
+ out.extend_from_within(from..from + len);
+ } else {
+ for k in 0..len {
+ out.push(out[from + k]);
+ }
+ }
+ }
+ if out.len() >= FLUSH_AT {
+ flush(&mut out, w, false)?;
+ }
+ }
+ }
+ _ => return Err(bad("bad block type")),
+ }
+ if last {
+ break;
+ }
+ }
+ flush(&mut out, w, true)?;
+ Ok((crc, total))
+}
+
+// ---------- ZIP container: writing ----------
+
+const U32_MAX: u64 = 0xFFFF_FFFF;
+
+/// A file compressed in memory by a worker thread, ready to be written.
+pub struct Prepared {
+ name: String,
+ time: (u16, u16),
+ method: u16,
+ crc: u32,
+ usize: u64,
+ data: Vec<u8>,
+}
+
+impl Prepared {
+ pub fn uncompressed_size(&self) -> u64 {
+ self.usize
+ }
+}
+
+/// Reads and compresses one file (the caller keeps these small: it all sits in memory).
+pub fn prepare(name: String, path: &Path) -> io::Result<Prepared> {
+ let raw = fs::read(path)?;
+ let time = dos_time(path);
+ let crc = crc32(0, &raw);
+ let data = deflate(&raw);
+ if raw.is_empty() || data.len() >= raw.len() {
+ return Ok(Prepared { name, time, method: 0, crc, usize: raw.len() as u64, data: raw });
+ }
+ Ok(Prepared { name, time, method: 8, crc, usize: raw.len() as u64, data })
+}
+
+struct Cd {
+ name: String,
+ time: (u16, u16),
+ method: u16,
+ crc: u32,
+ csize: u64,
+ usize: u64,
+ offset: u64,
+ dir: bool,
+}
+
+pub struct Writer<W: Write> {
+ w: W,
+ pos: u64,
+ cd: Vec<Cd>,
+ limit: u64, // sizes and offsets from here on need ZIP64 fields
+}
+
+fn le16(v: &mut Vec<u8>, x: u16) {
+ v.extend_from_slice(&x.to_le_bytes());
+}
+fn le32(v: &mut Vec<u8>, x: u32) {
+ v.extend_from_slice(&x.to_le_bytes());
+}
+fn le64(v: &mut Vec<u8>, x: u64) {
+ v.extend_from_slice(&x.to_le_bytes());
+}
+
+impl<W: Write> Writer<W> {
+ pub fn new(w: W) -> Self {
+ Writer { w, pos: 0, cd: vec![], limit: U32_MAX }
+ }
+
+ /// Tests: ZIP64 fields from a small size on, to exercise them without gigabytes.
+ #[cfg(test)]
+ fn with_limit(mut self, limit: u64) -> Self {
+ self.limit = limit;
+ self
+ }
+
+ fn put(&mut self, b: &[u8]) -> io::Result<()> {
+ self.w.write_all(b)?;
+ self.pos += b.len() as u64;
+ Ok(())
+ }
+
+ pub fn add_dir(&mut self, name: &str, time: (u16, u16)) -> io::Result<()> {
+ let name = format!("{}/", name.trim_end_matches('/'));
+ let offset = self.pos;
+ let mut h = vec![];
+ local_header(&mut h, &name, time, 0, 0, 0, 0, false);
+ self.put(&h)?;
+ self.cd.push(Cd { name, time, method: 0, crc: 0, csize: 0, usize: 0, offset, dir: true });
+ Ok(())
+ }
+
+ pub fn add_prepared(&mut self, f: Prepared) -> io::Result<()> {
+ let offset = self.pos;
+ let mut h = vec![];
+ local_header(&mut h, &f.name, f.time, f.method, f.crc, f.data.len() as u32, f.usize as u32, false);
+ self.put(&h)?;
+ self.put(&f.data)?;
+ self.cd.push(Cd { name: f.name, time: f.time, method: f.method, crc: f.crc, csize: f.data.len() as u64, usize: f.usize, offset, dir: false });
+ Ok(())
+ }
+
+ /// A big file streamed from disk (sizes are unknown up front: ZIP64 data descriptor). A reader thread keeps the next chunk
+ /// coming while this one is compressed. `done` is told the bytes read in each step; false stops with an Interrupted error.
+ pub fn add_stream(&mut self, name: String, path: &Path, mut done: impl FnMut(u64) -> bool) -> io::Result<()> {
+ let time = dos_time(path);
+ let mut f = fs::File::open(path)?;
+ let offset = self.pos;
+ let mut h = vec![];
+ local_header(&mut h, &name, time, 8, 0, U32_MAX as u32, U32_MAX as u32, true);
+ self.put(&h)?;
+ let (mut d, mut out) = (Deflater::new(), Vec::new());
+ let (mut crc, mut total, mut csize) = (0u32, 0u64, 0u64);
+ let (tx, rx) = std::sync::mpsc::sync_channel::<io::Result<Vec<u8>>>(2);
+ std::thread::scope(|s| -> io::Result<()> {
+ s.spawn(move || loop {
+ let mut buf = vec![0u8; CHUNK];
+ let mut n = 0;
+ let r = loop {
+ if n == CHUNK {
+ break Ok(n);
+ }
+ match f.read(&mut buf[n..]) {
+ Ok(0) => break Ok(n),
+ Ok(k) => n += k,
+ Err(e) => break Err(e),
+ }
+ };
+ let last = !matches!(r, Ok(n) if n == CHUNK);
+ let msg = r.map(|n| { buf.truncate(n); buf });
+ if tx.send(msg).is_err() || last {
+ break; // the writer gave up, or the file ended
+ }
+ });
+ for r in rx {
+ let buf = r?;
+ if buf.is_empty() {
+ break;
+ }
+ crc = crc32(crc, &buf);
+ total += buf.len() as u64;
+ out.clear();
+ d.chunk(&buf, &mut out);
+ csize += out.len() as u64;
+ self.put(&out)?;
+ if !done(buf.len() as u64) {
+ return Err(io::Error::new(io::ErrorKind::Interrupted, "cancelled"));
+ }
+ }
+ Ok(())
+ })?;
+ out.clear();
+ d.finish(&mut out);
+ csize += out.len() as u64;
+ self.put(&out)?;
+ let mut dd = vec![];
+ le32(&mut dd, 0x0807_4b50);
+ le32(&mut dd, crc);
+ le64(&mut dd, csize);
+ le64(&mut dd, total);
+ self.put(&dd)?;
+ self.cd.push(Cd { name, time, method: 8, crc, csize, usize: total, offset, dir: false });
+ Ok(())
+ }
+
+ pub fn finish(mut self) -> io::Result<W> {
+ let start = self.pos;
+ let mut v = vec![];
+ for e in &self.cd {
+ let mut x = vec![]; // zip64 extra: only the fields that do not fit 32 bits, in this order
+ let (us, cs, of) = (e.usize >= self.limit, e.csize >= self.limit, e.offset >= self.limit);
+ if us {
+ le64(&mut x, e.usize);
+ }
+ if cs {
+ le64(&mut x, e.csize);
+ }
+ if of {
+ le64(&mut x, e.offset);
+ }
+ let big = |b: bool, v: u64| if b { U32_MAX as u32 } else { v as u32 };
+ le32(&mut v, 0x0201_4b50);
+ le16(&mut v, 45 | 10 << 8); // made by: 4.5, FAT
+ le16(&mut v, if x.is_empty() { 20 } else { 45 });
+ le16(&mut v, 0x0800); // UTF-8 names
+ le16(&mut v, e.method);
+ le16(&mut v, e.time.0);
+ le16(&mut v, e.time.1);
+ le32(&mut v, e.crc);
+ le32(&mut v, big(cs, e.csize));
+ le32(&mut v, big(us, e.usize));
+ le16(&mut v, e.name.len() as u16);
+ le16(&mut v, if x.is_empty() { 0 } else { x.len() as u16 + 4 });
+ le16(&mut v, 0);
+ le16(&mut v, 0);
+ le16(&mut v, 0);
+ le32(&mut v, if e.dir { 0x10 } else { 0x20 }); // DOS attributes: directory / archive
+ le32(&mut v, big(of, e.offset));
+ v.extend_from_slice(e.name.as_bytes());
+ if !x.is_empty() {
+ le16(&mut v, 1);
+ le16(&mut v, x.len() as u16);
+ v.extend_from_slice(&x);
+ }
+ }
+ let (n, size) = (self.cd.len() as u64, v.len() as u64);
+ let z64 = n >= 0xFFFF || start >= self.limit || size >= self.limit;
+ if z64 {
+ let z = start + size;
+ le32(&mut v, 0x0606_4b50); // zip64 end of central directory
+ le64(&mut v, 44);
+ le16(&mut v, 45);
+ le16(&mut v, 45);
+ le32(&mut v, 0);
+ le32(&mut v, 0);
+ le64(&mut v, n);
+ le64(&mut v, n);
+ le64(&mut v, size);
+ le64(&mut v, start);
+ le32(&mut v, 0x0706_4b50); // locator
+ le32(&mut v, 0);
+ le64(&mut v, z);
+ le32(&mut v, 1);
+ }
+ le32(&mut v, 0x0605_4b50);
+ le16(&mut v, 0);
+ le16(&mut v, 0);
+ let sat = |v: u64, m: u64| if z64 { m } else { v }; // with a ZIP64 directory the old fields hold the markers
+ le16(&mut v, sat(n, 0xFFFF) as u16);
+ le16(&mut v, sat(n, 0xFFFF) as u16);
+ le32(&mut v, sat(size, U32_MAX) as u32);
+ le32(&mut v, sat(start, U32_MAX) as u32);
+ le16(&mut v, 0);
+ self.put(&v)?;
+ self.w.flush()?;
+ Ok(self.w)
+ }
+}
+
+#[allow(clippy::too_many_arguments)]
+fn local_header(h: &mut Vec<u8>, name: &str, time: (u16, u16), method: u16, crc: u32, csize: u32, usize: u32, stream: bool) {
+ le32(h, 0x0403_4b50);
+ le16(h, if stream { 45 } else { 20 });
+ le16(h, 0x0800 | if stream { 8 } else { 0 });
+ le16(h, method);
+ le16(h, time.0);
+ le16(h, time.1);
+ le32(h, crc);
+ le32(h, csize);
+ le32(h, usize);
+ le16(h, name.len() as u16);
+ le16(h, if stream { 20 } else { 0 });
+ h.extend_from_slice(name.as_bytes());
+ if stream {
+ le16(h, 1); // zip64 extra: the real sizes follow in the data descriptor
+ le16(h, 16);
+ le64(h, 0);
+ le64(h, 0);
+ }
+}
+
+/// (DOS time, DOS date) of the file's modification, in local time.
+pub fn dos_time(p: &Path) -> (u16, u16) {
+ use windows_sys::Win32::{Foundation::{FILETIME, SYSTEMTIME}, Storage::FileSystem::FileTimeToLocalFileTime, System::Time::FileTimeToSystemTime};
+ let t = fs::metadata(p).and_then(|m| m.modified()).ok().and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok());
+ let Some(t) = t else { return (0, 0x21) };
+ let ft100 = t.as_secs() * 10_000_000 + (t.subsec_nanos() / 100) as u64 + 116_444_736_000_000_000;
+ let (ft, mut local, mut st) = (FILETIME { dwLowDateTime: ft100 as u32, dwHighDateTime: (ft100 >> 32) as u32 }, unsafe { std::mem::zeroed::<FILETIME>() }, unsafe { std::mem::zeroed::<SYSTEMTIME>() });
+ unsafe {
+ if FileTimeToLocalFileTime(&ft, &mut local) == 0 || FileTimeToSystemTime(&local, &mut st) == 0 || st.wYear < 1980 {
+ return (0, 0x21);
+ }
+ }
+ ((st.wHour << 11) | (st.wMinute << 5) | (st.wSecond / 2), ((st.wYear - 1980) << 9) | (st.wMonth << 5) | st.wDay)
+}
+
+/// The moment a (DOS time, DOS date) pair stands for (local time); None for "no time" (date 0 or the 1980-01-01 placeholder).
+fn from_dos(t: (u16, u16)) -> Option<SystemTime> {
+ use windows_sys::Win32::{Foundation::{FILETIME, SYSTEMTIME}, Storage::FileSystem::LocalFileTimeToFileTime, System::Time::SystemTimeToFileTime};
+ let (tm, dt) = t;
+ if dt == 0 || (dt == 0x21 && tm == 0) {
+ return None;
+ }
+ let st = SYSTEMTIME { wYear: (dt >> 9) + 1980, wMonth: (dt >> 5) & 15, wDayOfWeek: 0, wDay: dt & 31, wHour: tm >> 11, wMinute: (tm >> 5) & 63, wSecond: (tm & 31) * 2, wMilliseconds: 0 };
+ let (mut local, mut utc) = unsafe { (std::mem::zeroed::<FILETIME>(), std::mem::zeroed::<FILETIME>()) };
+ unsafe {
+ if SystemTimeToFileTime(&st, &mut local) == 0 || LocalFileTimeToFileTime(&local, &mut utc) == 0 {
+ return None;
+ }
+ }
+ let t100 = (utc.dwHighDateTime as u64) << 32 | utc.dwLowDateTime as u64;
+ t100.checked_sub(116_444_736_000_000_000).map(|x| UNIX_EPOCH + Duration::from_nanos(x * 100))
+}
+
+fn set_mtime(path: &Path, t: SystemTime) {
+ use std::os::windows::fs::OpenOptionsExt;
+ // FILE_FLAG_BACKUP_SEMANTICS lets a folder be opened too
+ if let Ok(f) = fs::OpenOptions::new().write(true).custom_flags(0x0200_0000).open(path) {
+ let _ = f.set_modified(t);
+ }
+}
+
+// ---------- ZIP container: reading ----------
+
+struct Entry {
+ name: String,
+ time: (u16, u16),
+ method: u16,
+ flags: u16,
+ crc: u32,
+ csize: u64,
+ usize: u64,
+ offset: u64,
+}
+
+fn u16_at(b: &[u8], i: usize) -> u16 {
+ u16::from_le_bytes([b[i], b[i + 1]])
+}
+fn u32_at(b: &[u8], i: usize) -> u32 {
+ u32::from_le_bytes(b[i..i + 4].try_into().unwrap())
+}
+fn u64_at(b: &[u8], i: usize) -> u64 {
+ u64::from_le_bytes(b[i..i + 8].try_into().unwrap())
+}
+
+fn read_at<F: Read + Seek>(f: &mut F, pos: u64, n: usize) -> io::Result<Vec<u8>> {
+ f.seek(SeekFrom::Start(pos))?;
+ let mut v = vec![0; n];
+ f.read_exact(&mut v)?;
+ Ok(v)
+}
+
+fn central_directory<F: Read + Seek>(f: &mut F) -> io::Result<Vec<Entry>> {
+ let end = f.seek(SeekFrom::End(0))?;
+ let tail_len = end.min(22 + 65535);
+ let tail = read_at(f, end - tail_len, tail_len as usize)?;
+ let at = (0..tail.len().saturating_sub(21)).rev().find(|&i| u32_at(&tail, i) == 0x0605_4b50).ok_or_else(|| bad("not a ZIP archive"))?;
+ let eocd = end - tail_len + at as u64;
+ let (mut count, mut size, mut start) = (u16_at(&tail, at + 10) as u64, u32_at(&tail, at + 12) as u64, u32_at(&tail, at + 16) as u64);
+ if (count == 0xFFFF || size == U32_MAX || start == U32_MAX) && eocd >= 20 {
+ let loc = read_at(f, eocd - 20, 20)?;
+ if u32_at(&loc, 0) == 0x0706_4b50 {
+ let z = read_at(f, u64_at(&loc, 8), 56)?;
+ if u32_at(&z, 0) != 0x0606_4b50 {
+ return Err(bad("broken ZIP64 directory"));
+ }
+ (count, size, start) = (u64_at(&z, 32), u64_at(&z, 40), u64_at(&z, 48));
+ }
+ }
+ if size > 1 << 30 {
+ return Err(bad("central directory is too big"));
+ }
+ let cd = read_at(f, start, size as usize)?;
+ let (mut v, mut i) = (vec![], 0usize);
+ for _ in 0..count {
+ if i + 46 > cd.len() || u32_at(&cd, i) != 0x0201_4b50 {
+ return Err(bad("broken central directory"));
+ }
+ let (flags, method) = (u16_at(&cd, i + 8), u16_at(&cd, i + 10));
+ let (nl, el, cl) = (u16_at(&cd, i + 28) as usize, u16_at(&cd, i + 30) as usize, u16_at(&cd, i + 32) as usize);
+ if i + 46 + nl + el + cl > cd.len() {
+ return Err(bad("broken central directory"));
+ }
+ let mut e = Entry {
+ name: decode_name(&cd[i + 46..i + 46 + nl], flags),
+ time: (u16_at(&cd, i + 12), u16_at(&cd, i + 14)),
+ method,
+ flags,
+ crc: u32_at(&cd, i + 16),
+ csize: u32_at(&cd, i + 20) as u64,
+ usize: u32_at(&cd, i + 24) as u64,
+ offset: u32_at(&cd, i + 42) as u64,
+ };
+ let mut x = &cd[i + 46 + nl..i + 46 + nl + el];
+ while x.len() >= 4 {
+ let (id, sz) = (u16_at(x, 0), u16_at(x, 2) as usize);
+ if x.len() < 4 + sz {
+ break;
+ }
+ if id == 1 {
+ let mut f = &x[4..4 + sz];
+ for field in [&mut e.usize, &mut e.csize, &mut e.offset] {
+ if *field == U32_MAX && f.len() >= 8 {
+ *field = u64_at(f, 0);
+ f = &f[8..];
+ }
+ }
+ }
+ x = &x[4 + sz..];
+ }
+ v.push(e);
+ i += 46 + nl + el + cl;
+ }
+ Ok(v)
+}
+
+const CP437_HIGH: &str = "ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■\u{a0}";
+
+/// UTF-8 when the flag says so or the bytes are valid UTF-8; otherwise the old DOS code page 437.
+fn decode_name(b: &[u8], flags: u16) -> String {
+ match std::str::from_utf8(b) {
+ Ok(s) => s.to_string(),
+ Err(_) if flags & 0x800 != 0 => String::from_utf8_lossy(b).into_owned(),
+ Err(_) => {
+ let high: Vec<char> = CP437_HIGH.chars().collect();
+ b.iter().map(|&c| if c < 0x80 { c as char } else { high[c as usize - 0x80] }).collect()
+ }
+ }
+}
+
+/// The path of an entry under `out`, or None for one that would land outside it (zip-slip) or has no name.
+fn safe_path(out: &Path, name: &str) -> Result<Option<PathBuf>, String> {
+ let mut p = out.to_path_buf();
+ let mut any = false;
+ for c in name.replace('\\', "/").split('/') {
+ match c {
+ "" | "." => continue,
+ ".." => return Err(format!("{name}: the name leads out of the folder")),
+ _ if Path::new(c).components().any(|k| !matches!(k, Component::Normal(_))) || c.contains(':') => return Err(format!("{name}: bad name")),
+ _ => {
+ p.push(c);
+ any = true;
+ }
+ }
+ }
+ Ok(any.then_some(p))
+}
+
+/// One row of an archive browsed like a folder.
+pub struct Item {
+ pub name: String,
+ pub dir: bool,
+ pub size: u64,
+ pub time: (u16, u16),
+}
+
+/// What is directly inside the folder `inner` ('/' separated, "" = the top) of the archive. Folders that exist only as a part
+/// of a file's path are listed too.
+pub fn list_dir(archive: &Path, inner: &str) -> Result<Vec<Item>, String> {
+ let mut f = fs::File::open(archive).map_err(|e| e.to_string())?;
+ let entries = central_directory(&mut f).map_err(|e| e.to_string())?;
+ let prefix = if inner.is_empty() { String::new() } else { format!("{}/", inner.trim_matches('/')) };
+ let mut m: std::collections::BTreeMap<String, Item> = Default::default();
+ for e in entries {
+ let name = e.name.replace('\\', "/");
+ let Some(rest) = name.trim_start_matches('/').strip_prefix(&prefix) else { continue };
+ let Some(first) = rest.split('/').next().filter(|f| !f.is_empty()) else { continue };
+ let deeper = rest.trim_end_matches('/').len() > first.len() || rest.ends_with('/');
+ let item = m.entry(first.to_string()).or_insert(Item { name: first.to_string(), dir: deeper, size: 0, time: (0, 0x21) });
+ if deeper {
+ item.dir = true;
+ if rest.trim_end_matches('/') == first { item.time = e.time; } // the folder's own entry
+ } else {
+ item.size = e.usize;
+ item.time = e.time;
+ }
+ }
+ Ok(m.into_values().collect())
+}
+
+/// `C:\a\b.zip\x\y` -> (`C:\a\b.zip`, "x/y"): a path that goes into a .zip FILE (so it is no real folder path). The
+/// archive itself gives ("", ...).
+pub fn split_zip(p: &Path) -> Option<(PathBuf, String)> {
+ let anc: Vec<&Path> = p.ancestors().collect();
+ let a = anc.iter().rev().find(|a| a.extension().is_some_and(|e| e.eq_ignore_ascii_case("zip")) && a.is_file())?;
+ let inner = p.strip_prefix(a).ok()?.components().map(|c| c.as_os_str().to_string_lossy().into_owned()).collect::<Vec<_>>().join("/");
+ Some((a.to_path_buf(), inner))
+}
+
+/// A DOS time as FILETIME ticks (what the file list sorts and shows); 0 for none.
+pub fn dos_to_filetime(t: (u16, u16)) -> u64 {
+ from_dos(t).and_then(|t| t.duration_since(UNIX_EPOCH).ok()).map_or(0, |d| d.as_secs() * 10_000_000 + 116_444_736_000_000_000)
+}
+
+/// Unpacks the archive into `out` (created) and gives files and folders their stored times. Stops at the first bad entry.
+/// `total` is told the unpacked size of everything up front; `step` the bytes written in each step (false = stop, "cancelled").
+pub fn extract(archive: &Path, out: &Path, total: impl FnOnce(u64), step: impl FnMut(u64) -> bool) -> Result<(), String> {
+ extract_where(archive, out, |n| Some(n.to_string()), total, step)
+}
+
+/// Only the entries under the given archive paths (files or folders, '/' separated): each lands in `out` under its own name,
+/// with the path above it cut off. Used to take a few things out of an archive that is browsed like a folder.
+pub fn extract_some(archive: &Path, names: &[String], out: &Path) -> Result<(), String> {
+ let pick = |n: &str| {
+ let n = n.replace('\\', "/");
+ let n = n.trim_start_matches('/');
+ names.iter().find_map(|sel| {
+ let sel = sel.trim_matches('/');
+ let cut = sel.rfind('/').map_or(0, |i| i + 1);
+ (n.trim_end_matches('/') == sel || n.starts_with(&format!("{sel}/"))).then(|| n[cut..].to_string())
+ })
+ };
+ extract_where(archive, out, pick, |_| {}, |_| true)
+}
+
+fn extract_where(archive: &Path, out: &Path, pick: impl Fn(&str) -> Option<String>, total: impl FnOnce(u64), mut step: impl FnMut(u64) -> bool) -> Result<(), String> {
+ let mut f = fs::File::open(archive).map_err(|e| e.to_string())?;
+ let entries = central_directory(&mut f).map_err(|e| e.to_string())?;
+ fs::create_dir_all(out).map_err(|e| e.to_string())?;
+ total(entries.iter().map(|e| e.usize).sum());
+ let mut dirs = vec![];
+ for e in entries {
+ let Some(rel) = pick(&e.name) else { continue };
+ let Some(path) = safe_path(out, &rel)? else { continue };
+ if e.name.ends_with('/') || e.name.ends_with('\\') {
+ fs::create_dir_all(&path).map_err(|x| format!("{}: {x}", e.name))?;
+ dirs.push((path, from_dos(e.time)));
+ continue;
+ }
+ if e.flags & 1 != 0 {
+ return Err(format!("{}: encrypted entries are not supported", e.name));
+ }
+ let ctx = |x: io::Error| if x.kind() == io::ErrorKind::Interrupted { "cancelled".to_string() } else { format!("{}: {x}", e.name) };
+ let lh = read_at(&mut f, e.offset, 30).map_err(ctx)?;
+ if u32_at(&lh, 0) != 0x0403_4b50 {
+ return Err(format!("{}: broken local header", e.name));
+ }
+ let data = e.offset + 30 + u16_at(&lh, 26) as u64 + u16_at(&lh, 28) as u64;
+ f.seek(SeekFrom::Start(data)).map_err(ctx)?;
+ if let Some(d) = path.parent() {
+ fs::create_dir_all(d).map_err(ctx)?;
+ }
+ let mut w = BufWriter::with_capacity(1 << 20, fs::File::create(&path).map_err(ctx)?);
+ let src = io::BufReader::with_capacity(1 << 16, (&mut f).take(e.csize));
+ let (crc, size) = match e.method {
+ 0 => {
+ let mut src = src;
+ let (mut crc, mut n, mut buf) = (0u32, 0u64, vec![0u8; 1 << 16]);
+ loop {
+ let k = src.read(&mut buf).map_err(ctx)?;
+ if k == 0 {
+ break;
+ }
+ crc = crc32(crc, &buf[..k]);
+ n += k as u64;
+ w.write_all(&buf[..k]).map_err(ctx)?;
+ if !step(k as u64) {
+ return Err("cancelled".into());
+ }
+ }
+ (crc, n)
+ }
+ 8 => inflate(src, &mut w, &mut step).map_err(ctx)?,
+ m => return Err(format!("{}: compression method {m} is not supported", e.name)),
+ };
+ let file = w.into_inner().map_err(|x| ctx(x.into_error()))?;
+ if let Some(t) = from_dos(e.time) {
+ let _ = file.set_modified(t);
+ }
+ drop(file);
+ if crc != e.crc || size != e.usize {
+ return Err(format!("{}: the data is damaged (checksum or size differs)", e.name));
+ }
+ }
+ for (p, t) in dirs.into_iter().rev().filter_map(|(p, t)| Some((p, t?))) {
+ set_mtime(&p, t); // last and deepest first: writing into a folder changes its time
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::io::Read;
+
+ fn sample(n: usize, seed: u32) -> Vec<u8> {
+ // text-like data with repeats, so matches and several symbol lengths all occur
+ let words = ["alpha", "beta", "gamma", "delta", "epsilon", " ", "\n", "0123456789", "zip"];
+ let (mut s, mut v) = (seed, Vec::new());
+ while v.len() < n {
+ s = s.wrapping_mul(1664525).wrapping_add(1013904223);
+ v.extend_from_slice(words[(s >> 24) as usize % words.len()].as_bytes());
+ if s >> 20 & 7 == 0 {
+ v.push((s >> 8) as u8);
+ }
+ }
+ v.truncate(n);
+ v
+ }
+
+ fn noise(n: usize) -> Vec<u8> {
+ let mut s = 12345u32;
+ (0..n).map(|_| { s = s.wrapping_mul(1103515245).wrapping_add(12345); (s >> 16) as u8 }).collect()
+ }
+
+ fn round(data: &[u8]) {
+ let z = deflate(data);
+ let mut back = vec![];
+ let (crc, n) = inflate(&z[..], &mut back, &mut |_| true).unwrap();
+ assert_eq!(back, data);
+ assert_eq!((crc, n), (crc32(0, data), data.len() as u64));
+ }
+
+ #[test]
+ fn crc_known_value() {
+ assert_eq!(crc32(0, b"123456789"), 0xCBF4_3926);
+ assert_eq!(crc32(crc32(0, b"1234"), b"56789"), 0xCBF4_3926);
+ }
+
+ #[test]
+ fn deflate_round_trips() {
+ round(b"");
+ round(b"a");
+ round(b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
+ round(&sample(10_000, 1));
+ round(&sample(3_000_000, 2)); // several chunks
+ round(&noise(200_000)); // stored blocks
+ round(&vec![7u8; 300_000]);
+ let mut mixed = sample(100_000, 3);
+ mixed.extend(noise(70_000));
+ mixed.extend(sample(100_000, 4));
+ round(&mixed);
+ }
+
+ #[test]
+ fn compresses_text_and_does_not_grow_noise() {
+ let t = sample(1_000_000, 5);
+ assert!(deflate(&t).len() < t.len() / 3, "text should shrink a lot");
+ let n = noise(1_000_000);
+ assert!(deflate(&n).len() < n.len() + 1000, "noise stays near its size");
+ }
+
+ fn tmp(tag: &str) -> PathBuf {
+ let d = std::env::temp_dir().join(format!("zipx-{tag}-{}", std::process::id()));
+ let _ = fs::remove_dir_all(&d);
+ fs::create_dir_all(&d).unwrap();
+ d
+ }
+
+ #[test]
+ fn archive_round_trip_and_sizes() {
+ let d = tmp("rt");
+ fs::write(d.join("text.txt"), sample(500_000, 6)).unwrap();
+ fs::write(d.join("noise.bin"), noise(100_000)).unwrap();
+ fs::write(d.join("empty"), b"").unwrap();
+ fs::write(d.join("big.txt"), sample(2_500_000, 7)).unwrap();
+ let mut w = Writer::new(fs::File::create(d.join("o.zip")).unwrap());
+ w.add_dir("dir", (0, 0x21)).unwrap();
+ for n in ["text.txt", "noise.bin", "empty"] {
+ w.add_prepared(prepare(format!("dir/{n}"), &d.join(n)).unwrap()).unwrap();
+ }
+ let mut seen = 0;
+ w.add_stream("dir/big.txt".into(), &d.join("big.txt"), |n| { seen += n; true }).unwrap();
+ assert_eq!(seen, 2_500_000);
+ w.finish().unwrap();
+ extract(&d.join("o.zip"), &d.join("back"), |_| {}, |_| true).unwrap();
+ for n in ["text.txt", "noise.bin", "empty", "big.txt"] {
+ assert_eq!(fs::read(d.join(n)).unwrap(), fs::read(d.join("back/dir").join(n)).unwrap(), "{n}");
+ }
+ let _ = fs::remove_dir_all(&d);
+ }
+
+ #[test]
+ fn rejects_zip_slip_and_damage() {
+ let o = Path::new(r"C:\x\out");
+ assert!(safe_path(o, "../evil.txt").is_err());
+ assert!(safe_path(o, r"a\..\..\evil.txt").is_err());
+ assert!(safe_path(o, "C:/evil.txt").is_err());
+ assert!(safe_path(o, "a:b.txt").is_err());
+ assert_eq!(safe_path(o, "/abs/ok.txt").unwrap(), Some(o.join("abs").join("ok.txt")));
+ assert_eq!(safe_path(o, "").unwrap(), None);
+ // a flipped byte in the data is caught by the checksum or by the decoder
+ let d = tmp("bad");
+ fs::write(d.join("a.txt"), sample(50_000, 8)).unwrap();
+ let mut w = Writer::new(fs::File::create(d.join("o.zip")).unwrap());
+ w.add_prepared(prepare("a.txt".into(), &d.join("a.txt")).unwrap()).unwrap();
+ w.finish().unwrap();
+ let mut b = fs::read(d.join("o.zip")).unwrap();
+ b[200] ^= 0x55;
+ fs::write(d.join("bad.zip"), b).unwrap();
+ assert!(extract(&d.join("bad.zip"), &d.join("out"), |_| {}, |_| true).is_err());
+ assert!(extract(&d.join("a.txt"), &d.join("out2"), |_| {}, |_| true).is_err(), "not an archive");
+ let _ = fs::remove_dir_all(&d);
+ }
+
+ #[test]
+ fn matches_reach_across_chunks() {
+ // the second chunk starts with the 20 KB that ended the first: the window carries over
+ let tail = sample(20_000, 11);
+ let mut d = sample(CHUNK - tail.len(), 12);
+ d.extend(&tail);
+ d.extend(&tail);
+ d.extend(sample(5000, 13));
+ round(&d);
+ let (a, b) = (deflate(&d).len(), deflate(&d[..CHUNK]).len() + deflate(&d[CHUNK..]).len());
+ assert!(a + 3_000 < b, "{a} vs {b}: the repeat should cost almost nothing");
+ }
+
+ #[test]
+ fn random_looking_data_is_stored_fast() {
+ let n = noise(3_000_000);
+ let t = std::time::Instant::now();
+ let z = deflate(&n);
+ assert!(z.len() < n.len() + 400, "stored blocks add only headers");
+ assert!(t.elapsed() < std::time::Duration::from_millis(500));
+ round(&n);
+ }
+
+ #[test]
+ fn zip64_fields_with_a_small_limit() {
+ let d = tmp("z64");
+ fs::write(d.join("a.txt"), sample(5000, 21)).unwrap();
+ fs::write(d.join("b.txt"), sample(9000, 22)).unwrap();
+ let mut w = Writer::new(fs::File::create(d.join("o.zip")).unwrap()).with_limit(100);
+ w.add_dir("dir", (0, 0x21)).unwrap();
+ w.add_prepared(prepare("dir/a.txt".into(), &d.join("a.txt")).unwrap()).unwrap();
+ w.add_stream("dir/b.txt".into(), &d.join("b.txt"), |_| true).unwrap(); // data descriptor with 8-byte sizes
+ w.finish().unwrap();
+ extract(&d.join("o.zip"), &d.join("back"), |_| {}, |_| true).unwrap();
+ assert_eq!(fs::read(d.join("a.txt")).unwrap(), fs::read(d.join("back/dir/a.txt")).unwrap());
+ assert_eq!(fs::read(d.join("b.txt")).unwrap(), fs::read(d.join("back/dir/b.txt")).unwrap());
+ // an independent reader finds the same through the ZIP64 directory
+ let mut z = zip::ZipArchive::new(fs::File::open(d.join("o.zip")).unwrap()).unwrap();
+ for (n, f) in [("dir/a.txt", "a.txt"), ("dir/b.txt", "b.txt")] {
+ let mut got = vec![];
+ z.by_name(n).unwrap().read_to_end(&mut got).unwrap();
+ assert_eq!(got, fs::read(d.join(f)).unwrap(), "{n}");
+ }
+ let _ = fs::remove_dir_all(&d);
+ }
+
+ #[test]
+ fn times_survive_a_round_trip() {
+ let d = tmp("time");
+ fs::create_dir(d.join("sub")).unwrap();
+ fs::write(d.join("sub/f.txt"), b"x").unwrap();
+ let then = UNIX_EPOCH + Duration::from_secs(1_500_000_000); // 2017-07-14, even seconds
+ let f = fs::OpenOptions::new().write(true).open(d.join("sub/f.txt")).unwrap();
+ f.set_modified(then).unwrap();
+ drop(f);
+ set_mtime(&d.join("sub"), then);
+ let mut w = Writer::new(fs::File::create(d.join("o.zip")).unwrap());
+ w.add_dir("sub", dos_time(&d.join("sub"))).unwrap();
+ w.add_prepared(prepare("sub/f.txt".into(), &d.join("sub/f.txt")).unwrap()).unwrap();
+ w.finish().unwrap();
+ extract(&d.join("o.zip"), &d.join("back"), |_| {}, |_| true).unwrap();
+ for p in ["back/sub/f.txt", "back/sub"] {
+ let got = fs::metadata(d.join(p)).unwrap().modified().unwrap();
+ assert_eq!(got, then, "{p}");
+ }
+ let _ = fs::remove_dir_all(&d);
+ }
+
+ #[test]
+ fn old_dos_names_and_cancel() {
+ assert_eq!(CP437_HIGH.chars().count(), 128);
+ assert_eq!(decode_name(&[0x82, b'a', 0x94], 0), "éaö");
+ assert_eq!(decode_name("é.txt".as_bytes(), 0), "é.txt");
+ assert_eq!(decode_name(&[b'a', 0xFF], 0x800), "a\u{FFFD}");
+ let d = tmp("cancel");
+ fs::write(d.join("a.txt"), sample(3_000_000, 31)).unwrap();
+ let mut w = Writer::new(fs::File::create(d.join("o.zip")).unwrap());
+ w.add_prepared(prepare("a.txt".into(), &d.join("a.txt")).unwrap()).unwrap();
+ w.finish().unwrap();
+ let (mut total, mut seen) = (0, 0);
+ let r = extract(&d.join("o.zip"), &d.join("out"), |t| total = t, |n| { seen += n; false });
+ assert_eq!(r, Err("cancelled".to_string()));
+ assert_eq!(total, 3_000_000);
+ assert!(seen > 0 && seen < 3_000_000);
+ let mut w = Writer::new(fs::File::create(d.join("p.zip")).unwrap());
+ let e = w.add_stream("a.txt".into(), &d.join("a.txt"), |_| false).unwrap_err();
+ assert_eq!(e.kind(), io::ErrorKind::Interrupted);
+ let _ = fs::remove_dir_all(&d);
+ }
+
+ #[test]
+ fn browses_an_archive_like_folders() {
+ let d = tmp("browse");
+ for (n, c) in [("a.txt", "A"), ("b.txt", "BB")] {
+ fs::write(d.join(n), c).unwrap();
+ }
+ let mut w = Writer::new(fs::File::create(d.join("o.zip")).unwrap());
+ w.add_prepared(prepare("top.txt".into(), &d.join("a.txt")).unwrap()).unwrap();
+ w.add_prepared(prepare("dir/sub/deep.txt".into(), &d.join("b.txt")).unwrap()).unwrap(); // no entry for dir/ or dir/sub/
+ w.add_prepared(prepare("dir/mid.txt".into(), &d.join("a.txt")).unwrap()).unwrap();
+ w.add_dir("empty", (0, 0x21)).unwrap();
+ w.finish().unwrap();
+ let z = d.join("o.zip");
+ let names = |inner: &str| list_dir(&z, inner).unwrap().into_iter().map(|i| format!("{}{}", i.name, if i.dir { "/" } else { "" })).collect::<Vec<_>>();
+ assert_eq!(names(""), ["dir/", "empty/", "top.txt"]);
+ assert_eq!(names("dir"), ["mid.txt", "sub/"]);
+ assert_eq!(names("dir/sub"), ["deep.txt"]);
+ assert!(names("nope").is_empty());
+ assert_eq!(list_dir(&z, "dir/sub").unwrap()[0].size, 1 + 1);
+ // taking things out: a file, and (as another selection) a folder with what is in it, each under its own name
+ extract_some(&z, &["dir/sub/deep.txt".to_string()], &d.join("o1")).unwrap();
+ extract_some(&z, &["dir".to_string(), "top.txt".to_string()], &d.join("o2")).unwrap();
+ assert_eq!(fs::read_to_string(d.join("o1/deep.txt")).unwrap(), "BB");
+ assert_eq!(fs::read_to_string(d.join("o2/dir/sub/deep.txt")).unwrap(), "BB");
+ assert!(d.join("o2/dir/mid.txt").is_file() && d.join("o2/top.txt").is_file() && !d.join("o2/empty").exists());
+ // paths into the archive
+ assert_eq!(split_zip(&z.join("dir").join("sub")), Some((z.clone(), "dir/sub".to_string())));
+ assert_eq!(split_zip(&z), Some((z.clone(), String::new())));
+ assert_eq!(split_zip(&d), None);
+ let _ = fs::remove_dir_all(&d);
+ }
+
+ /// By hand: `cargo test -p rust-explorer --release zipx::tests::bench -- --ignored --nocapture`. Ours against the zip crate (miniz_oxide) on the repo's sources.
+ #[test]
+ #[ignore]
+ fn bench() {
+ use std::io::Write;
+ let mut text = vec![];
+ let mut stack = vec![PathBuf::from(r"..\\..\\plugins"), PathBuf::from(r"..\\..\\src")];
+ while let Some(d) = stack.pop() {
+ for e in fs::read_dir(d).into_iter().flatten().flatten() {
+ let p = e.path();
+ if p.is_dir() && p.file_name().is_some_and(|n| n != "target") {
+ stack.push(p);
+ } else if p.extension().is_some_and(|x| x == "rs") {
+ text.extend(fs::read(p).unwrap());
+ }
+ }
+ }
+ let mut exe = fs::read(r"C:\Windows\System32\kernel32.dll").unwrap_or_default();
+ exe.truncate(2 << 20);
+ for (name, data) in [("sources", text), ("dll", exe), ("noise", noise(2 << 20))] {
+ if data.is_empty() {
+ continue;
+ }
+ let t = std::time::Instant::now();
+ let ours = deflate(&data);
+ let t_ours = t.elapsed();
+ let t = std::time::Instant::now();
+ let mut w = zip::ZipWriter::new(io::Cursor::new(vec![]));
+ w.start_file("a", zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated)).unwrap();
+ w.write_all(&data).unwrap();
+ let theirs = w.finish().unwrap().into_inner().len();
+ let t_theirs = t.elapsed();
+ let t = std::time::Instant::now();
+ let mut back = vec![];
+ inflate(&ours[..], &mut back, &mut |_| true).unwrap();
+ println!("BENCH {name} {} B: ours {} B in {:?} (inflate {:?}), zip crate {} B in {:?}", data.len(), ours.len(), t_ours, t.elapsed(), theirs, t_theirs);
+ }
+ }
+}
diff --git a/plugins/rust-search/src/bin/rs-launcher.rs b/plugins/rust-search/src/bin/rs-launcher.rs
index 146a78e..81c8348 100644
--- a/plugins/rust-search/src/bin/rs-launcher.rs
+++ b/plugins/rust-search/src/bin/rs-launcher.rs
@@ -295,7 +295,7 @@ unsafe fn open(h: HWND, reveal: bool) {
let Some(p) = st(|s| if s.rename.is_some() { None } else { s.results.get(s.sel).cloned() }) else { return };
hide(h);
if reveal {
- ShellExecuteW(null_mut(), wide("open").as_ptr(), wide("explorer.exe").as_ptr(), wide(&format!("/select,\"{p}\"")).as_ptr(), null(), SW_SHOWNORMAL);
+ winrust_common::reveal(&p);
} else {
ShellExecuteW(null_mut(), wide("open").as_ptr(), wide(&p).as_ptr(), null(), null(), SW_SHOWNORMAL);
}
@@ -960,7 +960,7 @@ unsafe fn purge_press(h: HWND, i: usize) {
});
if let Some(b) = b {
hide(h);
- ShellExecuteW(null_mut(), wide("open").as_ptr(), wide("explorer.exe").as_ptr(), wide(&format!("/select,\"{b}\"")).as_ptr(), null(), SW_SHOWNORMAL);
+ winrust_common::reveal(&b);
}
}
Some(_) => close_purge(h),
diff --git a/plugins/topbar/src/fly.rs b/plugins/topbar/src/fly.rs
index 3c2ae57..f37491e 100644
--- a/plugins/topbar/src/fly.rs
+++ b/plugins/topbar/src/fly.rs
@@ -993,7 +993,7 @@ unsafe fn app_menu(hw: HWND, i: usize, x: i32, y: i32) {
}
3 => {
let p = String::from_utf16_lossy(&lnk[..lnk.len().saturating_sub(1)]);
- ShellExecuteW(null_mut(), wide("open").as_ptr(), wide("explorer.exe").as_ptr(), wide(&format!("/select,\"{p}\"")).as_ptr(), null(), SW_SHOWNORMAL);
+ winrust_common::reveal(&p);
}
4 => {
start_toggle(&path);
diff --git a/plugins/topbar/src/main.rs b/plugins/topbar/src/main.rs
index e21149d..444eb5b 100644
--- a/plugins/topbar/src/main.rs
+++ b/plugins/topbar/src/main.rs
@@ -1351,7 +1351,8 @@ fn layout(w: i32) -> Layout {
let fixed = (PINS.with_borrow(|p| p.len()) as i32 + 2).min(n);
// ...but never so far right that the windows' buttons run into the tray: then the whole row shifts left
let right_limit = w - margin - (n * (bw + gap) - gap);
- Layout { x0: ((w - (fixed * (bw + gap) - gap)) / 2).min(right_limit).max(sc(GEAR_X.0 + GEAR_X.1 + 6)), bw, gap, apps, sys, clock } // clear of the gear
+ let centered = (w - (fixed * (bw + gap) - gap)) / 2;
+ Layout { x0: if i18n::style::align_left() { 0 } else { centered }.min(right_limit).max(sc(GEAR_X.0 + GEAR_X.1 + 6)), bw, gap, apps, sys, clock } // clear of the gear
}
unsafe fn hit(hwnd: HWND, x: i32, y: i32) -> i32 {
@@ -1547,14 +1548,15 @@ unsafe fn fs_update(hwnd: HWND) {
}
}
-/// The dock is in the way of the window in front (a maximized one, or one dragged down under it): it hides like over the
+/// The dock is in the way of the window in front (one dragged down under it): it hides like over the
/// desktop and comes back when the pointer touches the bottom edge.
unsafe fn dock_covered() -> bool {
if !dock() {
return false;
}
let fg = GetForegroundWindow();
- if fg.is_null() || IsIconic(fg) != 0 || fg == BAR.load(Relaxed) as HWND || fly::any_open() {
+ // a maximized window never hides the dock (it could stay gone until the window was restored)
+ if fg.is_null() || IsIconic(fg) != 0 || IsZoomed(fg) != 0 || fg == BAR.load(Relaxed) as HWND || fly::any_open() {
return false;
}
let mut r: RECT = zeroed();
@@ -2484,6 +2486,7 @@ unsafe fn style_menu() {
if k == 0 { it = it.sep(); }
items.push(it);
}
+ items.push(fly::MItem::new(t!("Кнопки слева (как в Windows 10)", "Buttons on the left (like Windows 10)")).on(i18n::style::align_left()).sep());
let head = (null_mut(), "\u{E713}", t!("Стиль", "Style").to_string(), t!("Вид панели и программ WinRust", "Look of the bar and the WinRust programs").to_string());
let tile = i18n::style::ALL.len();
fly::menu(tip_cx(&bar_layout(bar), GEAR), Some(head), items, move |i| match i18n::style::ALL.get(i) {
@@ -2492,6 +2495,11 @@ unsafe fn style_menu() {
place(bar);
}
None if i == tile => tile_windows(),
+ None if i == tile + 5 => {
+ i18n::style::set_align_left(!i18n::style::align_left());
+ place(bar);
+ bar_anim(bar);
+ }
None if i > tile + 1 => {
i18n::style::set_side([i18n::style::Side::Bottom, i18n::style::Side::Left, i18n::style::Side::Right][i - tile - 2]);
place(bar);
diff --git a/plugins/wiztree/src/main.rs b/plugins/wiztree/src/main.rs
index 3f97f20..f5e3b22 100644
--- a/plugins/wiztree/src/main.rs
+++ b/plugins/wiztree/src/main.rs
@@ -269,7 +269,7 @@ unsafe extern "system" fn proc(hwnd: HWND, msg: u32, wp: WPARAM, lp: LPARAM) ->
});
if let Some(p) = act {
// show the file in Explorer (delete it from there)
- ShellExecuteW(null_mut(), wide("open").as_ptr(), wide("explorer.exe").as_ptr(), wide(&format!("/select,\"{p}\"")).as_ptr(), null(), SW_SHOWNORMAL);
+ winrust_common::reveal(&p);
}
InvalidateRect(hwnd, null(), 0);
0