Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

A server's shares: connect first when the list is refused (NAS roots)

gelenmedya46e575be9c3a
diff --git a/crates/rf-win/src/network.rs b/crates/rf-win/src/network.rs
index 6a980c02..9e113a46 100644
--- a/crates/rf-win/src/network.rs
+++ b/crates/rf-win/src/network.rs
@@ -29,8 +29,30 @@ pub struct Share {
pub remark: String,
}
-/// Disk shares of `server` (hidden `$` and admin shares excluded).
+/// Disk shares of `server` (hidden `$` and admin shares excluded). A server that refuses the list to a stranger (a Synology NAS
+/// without a session yet) gets one: a connection to its `IPC$` with the credentials Windows keeps for it, or Windows' own sign-in
+/// window when it has none, and the list is asked for again. Discussion 737: `\\synology` failed until a share of it had been
+/// opened, which made the session; File Explorer connects first the same way.
pub fn shares(server: &str) -> std::io::Result<Vec<Share>> {
+ match list_shares(server) {
+ Err(e) if connect_ipc(server) => list_shares(server).map_err(|_| e),
+ r => r,
+ }
+}
+
+/// A connection to `\\server\IPC$` (temporary, not remembered), asking for credentials only when Windows has none that work.
+fn connect_ipc(server: &str) -> bool {
+ use windows::Win32::NetworkManagement::WNet::{CONNECT_INTERACTIVE, CONNECT_TEMPORARY, NETRESOURCEW, RESOURCETYPE_ANY, WNetAddConnection2W};
+ let mut remote = wide(format!(r"\\{server}\IPC$"));
+ let res = NETRESOURCEW { dwType: RESOURCETYPE_ANY, lpRemoteName: windows::core::PWSTR(remote.as_mut_ptr()), ..Default::default() };
+ let rc = unsafe { WNetAddConnection2W(&res, PCWSTR::null(), PCWSTR::null(), CONNECT_TEMPORARY | CONNECT_INTERACTIVE) };
+ if rc.0 != 0 {
+ log::info!("shares: connecting to {server} failed ({})", rc.0);
+ }
+ rc.0 == 0
+}
+
+fn list_shares(server: &str) -> std::io::Result<Vec<Share>> {
let w = wide(format!(r"\\{server}"));
let mut out = Vec::new();
let mut resume: u32 = 0;