|
10 | 10 |
|
11 | 11 | pub mod watcher; |
12 | 12 |
|
| 13 | +pub use crate::file_system::volume::SmbConnectionState; |
| 14 | + |
13 | 15 | use crate::file_system::linux_mounts::{self, MountEntry}; |
14 | 16 | use serde::{Deserialize, Serialize}; |
15 | 17 | use std::collections::HashSet; |
16 | 18 | use std::path::Path; |
17 | 19 |
|
| 20 | +/// Information about an SMB mount extracted from `/proc/mounts`. |
| 21 | +#[derive(Debug, Clone)] |
| 22 | +pub struct SmbMountInfo { |
| 23 | + /// Server hostname or IP (for example, "192.168.1.111"). |
| 24 | + pub server: String, |
| 25 | + /// Share name (for example, "naspi"). |
| 26 | + pub share: String, |
| 27 | + /// Username if present in the mount source (for example, "david"). |
| 28 | + pub username: Option<String>, |
| 29 | +} |
| 30 | + |
| 31 | +/// Extracts SMB server, share, and username from a mount path via `/proc/mounts`. |
| 32 | +/// |
| 33 | +/// On Linux, CIFS mounts have a device field like: |
| 34 | +/// - `//192.168.1.111/share` (no credentials in device) |
| 35 | +/// - `//user@192.168.1.111/share` (some configurations) |
| 36 | +/// |
| 37 | +/// Returns `None` if the path is not a CIFS mount or parsing fails. |
| 38 | +pub fn get_smb_mount_info(mount_path: &str) -> Option<SmbMountInfo> { |
| 39 | + let mounts = linux_mounts::parse_proc_mounts(); |
| 40 | + let entry = mounts |
| 41 | + .iter() |
| 42 | + .filter(|e| e.fstype == "cifs") |
| 43 | + .find(|e| e.mountpoint == mount_path)?; |
| 44 | + parse_smb_mount_source(&entry.device) |
| 45 | +} |
| 46 | + |
| 47 | +/// Parses an SMB mount source string like `//user@host/share` or `//host/share`. |
| 48 | +fn parse_smb_mount_source(source: &str) -> Option<SmbMountInfo> { |
| 49 | + let rest = source.strip_prefix("//")?; |
| 50 | + let (server_part, share) = rest.split_once('/')?; |
| 51 | + if share.is_empty() { |
| 52 | + return None; |
| 53 | + } |
| 54 | + |
| 55 | + let (username, server) = if let Some((user, host)) = server_part.split_once('@') { |
| 56 | + (Some(user.to_string()), host.to_string()) |
| 57 | + } else { |
| 58 | + (None, server_part.to_string()) |
| 59 | + }; |
| 60 | + |
| 61 | + // Strip port if present (for example, "192.168.1.111:9445") |
| 62 | + let server = server.split(':').next().unwrap_or(&server).to_string(); |
| 63 | + |
| 64 | + Some(SmbMountInfo { |
| 65 | + server, |
| 66 | + share: share.to_string(), |
| 67 | + username, |
| 68 | + }) |
| 69 | +} |
| 70 | + |
18 | 71 | /// Category of a location item. |
19 | 72 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
20 | 73 | #[serde(rename_all = "snake_case")] |
@@ -454,16 +507,7 @@ pub fn find_volume_for_path(path: &str) -> Option<String> { |
454 | 507 | .map(|loc| loc.id.clone()) |
455 | 508 | } |
456 | 509 |
|
457 | | -/// Convert a mount path to a safe ID string. |
458 | | -pub(crate) fn path_to_id(path: &str) -> String { |
459 | | - if path == "/" { |
460 | | - return DEFAULT_VOLUME_ID.to_string(); |
461 | | - } |
462 | | - path.chars() |
463 | | - .filter(|c| c.is_alphanumeric() || *c == '-') |
464 | | - .collect::<String>() |
465 | | - .to_lowercase() |
466 | | -} |
| 510 | +pub(crate) use crate::file_system::volume::path_to_id; |
467 | 511 |
|
468 | 512 | /// Extract a display name from a mount path. |
469 | 513 | fn mount_display_name(mountpoint: &str) -> String { |
|
0 commit comments