sk-desktop 0.3.0

Skathi's desktop integration: argv, shell, single instance, custom protocols and the tray
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! The Windows shell calls, on the `sk-shell` STA thread. Open uses `ShellExecuteExW` with
//! `SEE_MASK_FLAG_NO_UI`, so "nothing is associated" returns `ERROR_NO_ASSOCIATION` instead of a dialog.
//! Trash uses `IFileOperation` with a sink whose `PreDeleteItem` vetoes any delete not routed through the
//! Recycle Bin, so "trash" never quietly becomes a permanent delete.

use std::path::Path;
use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
use std::sync::Arc;

use windows::core::{implement, Ref, HRESULT, HSTRING, PCWSTR};
use windows::Win32::Foundation::{ERROR_FILE_NOT_FOUND, E_ABORT};
use windows::Win32::System::Com::{
    CoCreateInstance, CoInitializeEx, CLSCTX_ALL, COINIT_APARTMENTTHREADED, COINIT_DISABLE_OLE1DDE,
};
use windows::Win32::UI::Shell::{
    FileOperation, IFileOperation, IFileOperationProgressSink, IFileOperationProgressSink_Impl,
    ILCreateFromPathW, ILFree, IShellItem, SHCreateItemFromParsingName, SHOpenFolderAndSelectItems,
    FOFX_ADDUNDORECORD, FOFX_EARLYFAILURE, FOFX_RECYCLEONDELETE, FOF_ALLOWUNDO, FOF_NO_UI,
    TSF_DELETE_RECYCLE_IF_POSSIBLE,
};
use windows::Win32::UI::WindowsAndMessaging::{
    DispatchMessageW, PeekMessageW, TranslateMessage, MSG, PM_REMOVE, SW_SHOWNORMAL,
};

use crate::Fail;

/// A COM single-threaded apartment, which `IFileOperation` and the folder views want.
pub fn thread_init() {
    unsafe {
        let _ = CoInitializeEx(None, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    }
}

fn drain_messages() {
    unsafe {
        let mut msg = MSG::default();
        while PeekMessageW(&mut msg, None, 0, 0, PM_REMOVE).as_bool() {
            let _ = TranslateMessage(&msg);
            DispatchMessageW(&msg);
        }
    }
}

/// `\\?\C:\x` → `C:\x`, `\\?\UNC\s\x` → `\\s\x`: `IShellItem` parsing does not understand the long-path prefix.
fn shell_path(p: &Path) -> HSTRING {
    let s = p.as_os_str().to_string_lossy();
    let s = if let Some(rest) = s.strip_prefix(r"\\?\UNC\") {
        format!(r"\\{rest}")
    } else if let Some(rest) = s.strip_prefix(r"\\?\") {
        rest.to_string()
    } else {
        s.into_owned()
    };
    HSTRING::from(s)
}

fn win32_of(hr: HRESULT) -> Option<u32> {
    let v = hr.0 as u32;
    if v & 0xFFFF_0000 == 0x8007_0000 {
        Some(v & 0xFFFF)
    } else if v != 0 && v < 0x1_0000 {
        Some(v)
    } else {
        None
    }
}

fn fail(verb: &str, target: &str, err: &windows::core::Error) -> Fail {
    let hr = err.code();
    let os = win32_of(hr);
    let f = match os {
        Some(1155) | Some(1156) => Fail::new(
            "NotFoundError",
            format!("{verb}: no application is associated with {target}"),
        )
        .code("ENOASSOC"),
        Some(2) | Some(3) => Fail::new(
            "NotFoundError",
            format!(
                "{verb}: {target} — a requested file or directory could not be found at the time an operation was processed."
            ),
        )
        .code("ENOENT"),
        Some(5) => Fail::new("NotAllowedError", format!("{verb}: {target} — access is denied")).code("EACCES"),
        Some(32) | Some(33) => Fail::new(
            "NoModificationAllowedError",
            format!("{verb}: {target} is in use by another program"),
        )
        .code("EBUSY"),
        Some(1223) => Fail::new("AbortError", format!("{verb}: {target} — the operation was cancelled")).code("ECANCELED"),
        _ => Fail::new("InvalidStateError", format!("{verb}: {target} — {}", err.message())).code("EIO"),
    };
    match os {
        Some(code) => f.os(code as i32),
        None => f.os(hr.0),
    }
}

fn shell_execute(file: &HSTRING, directory: Option<&HSTRING>) -> windows::core::Result<()> {
    use windows_sys::Win32::UI::Shell::{
        ShellExecuteExW, SEE_MASK_FLAG_NO_UI, SEE_MASK_NOASYNC, SHELLEXECUTEINFOW,
    };
    let verb = HSTRING::from("open");
    let mut info = SHELLEXECUTEINFOW {
        cbSize: std::mem::size_of::<SHELLEXECUTEINFOW>() as u32,
        fMask: SEE_MASK_NOASYNC | SEE_MASK_FLAG_NO_UI,
        lpVerb: verb.as_ptr(),
        lpFile: file.as_ptr(),
        lpDirectory: directory.map_or(std::ptr::null(), |d| d.as_ptr()),
        nShow: SW_SHOWNORMAL.0,
        ..Default::default()
    };
    // The error is read before the message pump can overwrite the thread's last error.
    let r = if unsafe { ShellExecuteExW(&mut info) } != 0 {
        Ok(())
    } else {
        Err(windows::core::Error::from_thread())
    };
    drain_messages();
    r
}

pub fn open_external(url: &str) -> Result<(), Fail> {
    shell_execute(&HSTRING::from(url), None).map_err(|e| fail("openExternal", url, &e))
}

pub fn open_path(path: &Path) -> Result<(), Fail> {
    let dir = path.parent().map(shell_path);
    shell_execute(&shell_path(path), dir.as_ref())
        .map_err(|e| fail("openPath", &path.display().to_string(), &e))
}

struct Pidl(*mut windows::Win32::UI::Shell::Common::ITEMIDLIST);

impl Pidl {
    fn of(p: &Path) -> Option<Pidl> {
        let s = shell_path(p);
        let raw = unsafe { ILCreateFromPathW(PCWSTR(s.as_ptr())) };
        (!raw.is_null()).then_some(Pidl(raw))
    }
}

impl Drop for Pidl {
    fn drop(&mut self) {
        unsafe { ILFree(Some(self.0)) };
    }
}

pub fn show_item(path: &Path) -> Result<(), Fail> {
    let target = path.display().to_string();
    let not_found = || {
        Fail::new(
            "NotFoundError",
            format!(
                "showItemInFolder: {target} — a requested file or directory could not be found at the time an operation was processed."
            ),
        )
        .code("ENOENT")
    };
    let Some(parent) = path.parent() else {
        return shell_execute(&shell_path(path), None)
            .map_err(|e| fail("showItemInFolder", &target, &e));
    };
    let dir = Pidl::of(parent).ok_or_else(not_found)?;
    let item = Pidl::of(path).ok_or_else(not_found)?;
    let selection = [item.0 as *const _];
    let r = unsafe { SHOpenFolderAndSelectItems(dir.0, Some(&selection), 0) };
    drain_messages();
    match r {
        Ok(()) => Ok(()),
        // On some systems this fails with "file not found" although the file exists; opening the folder still works.
        Err(e) if win32_of(e.code()) == Some(ERROR_FILE_NOT_FOUND.0) => {
            shell_execute(&shell_path(parent), None)
                .map_err(|e| fail("showItemInFolder", &target, &e))
        }
        Err(e) => Err(fail("showItemInFolder", &target, &e)),
    }
}

#[implement(IFileOperationProgressSink)]
struct TrashSink {
    vetoed: Arc<AtomicBool>,
    item_error: Arc<AtomicI32>,
}

#[allow(non_snake_case)]
impl IFileOperationProgressSink_Impl for TrashSink_Impl {
    fn StartOperations(&self) -> windows::core::Result<()> {
        Ok(())
    }
    fn FinishOperations(&self, _hr: HRESULT) -> windows::core::Result<()> {
        Ok(())
    }
    fn PreRenameItem(
        &self,
        _f: u32,
        _i: Ref<IShellItem>,
        _n: &PCWSTR,
    ) -> windows::core::Result<()> {
        Ok(())
    }
    fn PostRenameItem(
        &self,
        _f: u32,
        _i: Ref<IShellItem>,
        _n: &PCWSTR,
        _hr: HRESULT,
        _c: Ref<IShellItem>,
    ) -> windows::core::Result<()> {
        Ok(())
    }
    fn PreMoveItem(
        &self,
        _f: u32,
        _i: Ref<IShellItem>,
        _d: Ref<IShellItem>,
        _n: &PCWSTR,
    ) -> windows::core::Result<()> {
        Ok(())
    }
    fn PostMoveItem(
        &self,
        _f: u32,
        _i: Ref<IShellItem>,
        _d: Ref<IShellItem>,
        _n: &PCWSTR,
        _hr: HRESULT,
        _c: Ref<IShellItem>,
    ) -> windows::core::Result<()> {
        Ok(())
    }
    fn PreCopyItem(
        &self,
        _f: u32,
        _i: Ref<IShellItem>,
        _d: Ref<IShellItem>,
        _n: &PCWSTR,
    ) -> windows::core::Result<()> {
        Ok(())
    }
    fn PostCopyItem(
        &self,
        _f: u32,
        _i: Ref<IShellItem>,
        _d: Ref<IShellItem>,
        _n: &PCWSTR,
        _hr: HRESULT,
        _c: Ref<IShellItem>,
    ) -> windows::core::Result<()> {
        Ok(())
    }
    fn PreDeleteItem(&self, dwflags: u32, _i: Ref<IShellItem>) -> windows::core::Result<()> {
        if dwflags & (TSF_DELETE_RECYCLE_IF_POSSIBLE.0 as u32) == 0 {
            self.vetoed.store(true, Ordering::SeqCst);
            return Err(E_ABORT.into());
        }
        Ok(())
    }
    fn PostDeleteItem(
        &self,
        _f: u32,
        _i: Ref<IShellItem>,
        hr: HRESULT,
        _c: Ref<IShellItem>,
    ) -> windows::core::Result<()> {
        if hr.is_err() {
            let _ = self
                .item_error
                .compare_exchange(0, hr.0, Ordering::SeqCst, Ordering::SeqCst);
        }
        Ok(())
    }
    fn PreNewItem(&self, _f: u32, _d: Ref<IShellItem>, _n: &PCWSTR) -> windows::core::Result<()> {
        Ok(())
    }
    fn PostNewItem(
        &self,
        _f: u32,
        _d: Ref<IShellItem>,
        _n: &PCWSTR,
        _t: &PCWSTR,
        _a: u32,
        _hr: HRESULT,
        _c: Ref<IShellItem>,
    ) -> windows::core::Result<()> {
        Ok(())
    }
    fn UpdateProgress(&self, _total: u32, _so_far: u32) -> windows::core::Result<()> {
        Ok(())
    }
    fn ResetTimer(&self) -> windows::core::Result<()> {
        Ok(())
    }
    fn PauseTimer(&self) -> windows::core::Result<()> {
        Ok(())
    }
    fn ResumeTimer(&self) -> windows::core::Result<()> {
        Ok(())
    }
}

pub fn trash(path: &Path) -> Result<(), Fail> {
    let target = path.display().to_string();
    let vetoed = Arc::new(AtomicBool::new(false));
    let item_error = Arc::new(AtomicI32::new(0));
    let result = (|| -> windows::core::Result<bool> {
        unsafe {
            let op: IFileOperation = CoCreateInstance(&FileOperation, None, CLSCTX_ALL)?;
            op.SetOperationFlags(
                FOF_NO_UI
                    | FOF_ALLOWUNDO
                    | FOFX_ADDUNDORECORD
                    | FOFX_RECYCLEONDELETE
                    | FOFX_EARLYFAILURE,
            )?;
            let item: IShellItem = SHCreateItemFromParsingName(&shell_path(path), None)?;
            let sink: IFileOperationProgressSink = TrashSink {
                vetoed: vetoed.clone(),
                item_error: item_error.clone(),
            }
            .into();
            op.DeleteItem(&item, &sink)?;
            let performed = op.PerformOperations();
            let aborted = op
                .GetAnyOperationsAborted()
                .map(|b| b.as_bool())
                .unwrap_or(false);
            performed?;
            Ok(aborted)
        }
    })();
    drain_messages();
    if vetoed.load(Ordering::SeqCst) {
        return Err(Fail::new(
            "InvalidStateError",
            format!(
                "trashItem: {target} cannot go to the Recycle Bin (the drive has none, or the item is too large for it) — nothing was deleted"
            ),
        )
        .code("ENOTRASH"));
    }
    let item_hr = item_error.load(Ordering::SeqCst);
    if item_hr != 0 {
        return Err(fail(
            "trashItem",
            &target,
            &windows::core::Error::from(HRESULT(item_hr)),
        ));
    }
    match result {
        Ok(false) => Ok(()),
        Ok(true) => Err(Fail::new(
            "AbortError",
            format!("trashItem: {target} — the operation was aborted"),
        )
        .code("ECANCELED")),
        Err(e) => Err(fail("trashItem", &target, &e)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn long_path_prefixes_are_stripped_for_the_shell() {
        assert_eq!(
            shell_path(Path::new(r"\\?\C:\a\b.txt")).to_string(),
            r"C:\a\b.txt"
        );
        assert_eq!(
            shell_path(Path::new(r"\\?\UNC\srv\share\b.txt")).to_string(),
            r"\\srv\share\b.txt"
        );
        assert_eq!(shell_path(Path::new(r"C:\a")).to_string(), r"C:\a");
    }

    #[test]
    fn win32_codes_come_out_of_either_encoding() {
        assert_eq!(win32_of(HRESULT(0x8007_0483_u32 as i32)), Some(1155));
        assert_eq!(win32_of(HRESULT(2)), Some(2));
        assert_eq!(win32_of(E_ABORT), None);
        let f = fail(
            "openPath",
            "x.xyz",
            &windows::core::Error::from(HRESULT(0x8007_0483_u32 as i32)),
        );
        assert_eq!((f.name, f.code), ("NotFoundError", Some("ENOASSOC")));
    }

    /// Not in the default run: it leaves a Recycle Bin entry the test must purge. `-- --ignored trash_moves`.
    #[test]
    #[ignore]
    fn trash_moves_a_file_to_the_recycle_bin() {
        thread_init();
        let dir = std::env::temp_dir().join(format!("sk-desktop-trash-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let file = dir.join("sk-desktop-trash-probe.txt");
        let marker = format!("sk-desktop trash probe {}", std::process::id());
        std::fs::write(&file, &marker).unwrap();
        trash(&file).expect("trash");
        assert!(!file.exists(), "the file is gone from its folder");
        let _ = std::fs::remove_dir(&dir);
        let drive = file.to_string_lossy()[..2].to_string();
        let bin = Path::new(&format!(r"{drive}\")).join("$Recycle.Bin");
        let mut purged = false;
        if let Ok(sids) = std::fs::read_dir(&bin) {
            for sid in sids.flatten() {
                let Ok(items) = std::fs::read_dir(sid.path()) else {
                    continue;
                };
                for it in items.flatten() {
                    let name = it.file_name().to_string_lossy().into_owned();
                    if name.starts_with("$R")
                        && std::fs::read_to_string(it.path()).ok().as_deref()
                            == Some(marker.as_str())
                    {
                        let _ = std::fs::remove_file(it.path());
                        let _ = std::fs::remove_file(sid.path().join(name.replacen("$R", "$I", 1)));
                        purged = true;
                    }
                }
            }
        }
        assert!(
            purged,
            "the probe was found in (and purged from) the Recycle Bin"
        );
    }
}