brum 1.8.6

Multi-Pane Web Environment (File Commander/Manager) - By Woofson
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
use super::{DirectoryListing, FileContentResponse, VfsResult};
use std::fs;

#[cfg(not(windows))]
use super::{is_archive_file, FileEntry};
#[cfg(not(windows))]
use tempfile::NamedTempFile;
#[cfg(not(windows))]
use std::process::Command;

pub struct SmbClient;

#[derive(Debug, Clone)]
pub struct SmbParams {
    pub host: String,
    pub port: u16,
    pub share: String,
    pub subpath: String,
    pub username: Option<String>,
    pub password: Option<String>,
    pub domain: Option<String>,
}

impl SmbParams {
    /// Builds the user credentials prefix including domain and password if present
    pub fn auth_prefix(&self) -> String {
        let mut prefix = String::new();
        if let Some(ref u) = self.username {
            if let Some(ref d) = self.domain {
                if !d.is_empty() && d != "WORKGROUP" {
                    prefix.push_str(d);
                    prefix.push(';');
                }
            }
            prefix.push_str(u);
            if let Some(ref p) = self.password {
                prefix.push(':');
                prefix.push_str(p);
            }
            prefix.push('@');
        }
        prefix
    }

    /// Builds port suffix if non-default 445
    pub fn port_suffix(&self) -> String {
        if self.port != 445 && self.port > 0 {
            format!(":{}", self.port)
        } else {
            "".to_string()
        }
    }

    /// Reconstructs full SMB URI preserving credentials
    pub fn build_uri(&self, subpath: &str) -> String {
        let clean = subpath.trim_matches('/');
        if self.share.is_empty() {
            if clean.is_empty() {
                format!("smb://{}{}{}/", self.auth_prefix(), self.host, self.port_suffix())
            } else {
                format!("smb://{}{}{}/{}", self.auth_prefix(), self.host, self.port_suffix(), clean)
            }
        } else if clean.is_empty() {
            format!("smb://{}{}{}/{}", self.auth_prefix(), self.host, self.port_suffix(), self.share)
        } else {
            format!("smb://{}{}{}/{}/{}", self.auth_prefix(), self.host, self.port_suffix(), self.share, clean)
        }
    }
}

#[cfg(windows)]
#[link(name = "mpr")]
extern "system" {
    fn WNetAddConnection2W(
        lp_net_resource: *const NetResourceW,
        lp_password: *const u16,
        lp_user_name: *const u16,
        dw_flags: u32,
    ) -> u32;
}

#[cfg(windows)]
#[repr(C)]
struct NetResourceW {
    dw_scope: u32,
    dw_type: u32,
    dw_display_type: u32,
    dw_usage: u32,
    lp_local_name: *const u16,
    lp_remote_name: *const u16,
    lp_comment: *const u16,
    lp_provider: *const u16,
}

impl SmbClient {
    /// Parse an smb:// URI into structured parameters
    /// Formats supported:
    /// - smb://host/share/subpath
    /// - smb://user@host/share/subpath
    /// - smb://user:password@host:445/share/subpath
    /// - smb://DOMAIN;user:password@host/share/subpath
    pub fn parse_uri(uri: &str, default_user: Option<&str>, default_pass: Option<&str>) -> VfsResult<SmbParams> {
        let clean = uri.strip_prefix("smb://").ok_or_else(|| "Invalid SMB URI (must start with smb://)".to_string())?;
        
        let (user_domain_pass, host_share_path) = if let Some(idx) = clean.find('@') {
            (&clean[..idx], &clean[idx + 1..])
        } else {
            ("", clean)
        };

        let mut domain = None;
        let mut username = default_user.map(|s| s.to_string());
        let mut password = default_pass.map(|s| s.to_string());

        if !user_domain_pass.is_empty() {
            let (dom_user, pass_part) = if let Some(colon) = user_domain_pass.find(':') {
                (&user_domain_pass[..colon], Some(&user_domain_pass[colon + 1..]))
            } else {
                (user_domain_pass, None)
            };

            if let Some(p) = pass_part {
                password = Some(p.to_string());
            }

            if let Some(semi) = dom_user.find(';') {
                domain = Some(dom_user[..semi].to_string());
                username = Some(dom_user[semi + 1..].to_string());
            } else {
                username = Some(dom_user.to_string());
            }
        }

        let parts: Vec<&str> = host_share_path.splitn(2, '/').collect();
        let host_port = parts[0];
        let share_and_subpath = if parts.len() > 1 { parts[1] } else { "" };

        let (host, port) = if let Some(colon) = host_port.find(':') {
            let h = &host_port[..colon];
            let p = host_port[colon + 1..].parse::<u16>().unwrap_or(445);
            (h.to_string(), p)
        } else {
            (host_port.to_string(), 445)
        };

        let (share, subpath) = if let Some(slash) = share_and_subpath.find('/') {
            (&share_and_subpath[..slash], &share_and_subpath[slash + 1..])
        } else {
            (share_and_subpath, "")
        };

        Ok(SmbParams {
            host,
            port,
            share: share.to_string(),
            subpath: subpath.trim_matches('/').to_string(),
            username,
            password,
            domain,
        })
    }

    /// Read file contents from SMB share (with base64 binary support)
    pub fn read_file(params: &SmbParams) -> VfsResult<FileContentResponse> {
        let bytes = Self::read_bytes(params)?;
        let file_name = params.subpath.rsplit('/').next().unwrap_or(&params.subpath).to_string();
        let mime = mime_guess::from_path(&file_name).first_or_octet_stream().to_string();
        let is_binary = bytes.iter().take(1024).any(|&b| b == 0);

        let content = if is_binary {
            base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &bytes)
        } else {
            String::from_utf8_lossy(&bytes).to_string()
        };

        Ok(FileContentResponse {
            path: params.build_uri(&params.subpath),
            name: file_name,
            content,
            is_binary,
            size: bytes.len() as u64,
            mime_type: mime,
        })
    }
}

// -----------------------------------------------------------------------------
// WINDOWS NATIVE SMB IMPLEMENTATION (via Win32 WNet and OS filesystem)
// -----------------------------------------------------------------------------
#[cfg(windows)]
impl SmbClient {
    fn to_wide(s: &str) -> Vec<u16> {
        s.encode_utf16().chain(std::iter::once(0)).collect()
    }

    /// Authenticate / establish network share connection on Windows
    pub fn ensure_windows_connection(params: &SmbParams) -> VfsResult<()> {
        if params.share.is_empty() {
            return Ok(());
        }

        let remote_unc = format!(r"\\{}\{}", params.host, params.share);
        let remote_wide = Self::to_wide(&remote_unc);

        let user_wide = params.username.as_ref().map(|u| {
            let full_user = if let Some(ref dom) = params.domain {
                if !dom.is_empty() && dom != "WORKGROUP" {
                    format!("{}\\{}", dom, u)
                } else {
                    u.clone()
                }
            } else {
                u.clone()
            };
            Self::to_wide(&full_user)
        });

        let pass_wide = params.password.as_ref().map(|p| Self::to_wide(p));

        let net_res = NetResourceW {
            dw_scope: 0,
            dw_type: 1, // RESOURCETYPE_DISK
            dw_display_type: 0,
            dw_usage: 0,
            lp_local_name: std::ptr::null(),
            lp_remote_name: remote_wide.as_ptr(),
            lp_comment: std::ptr::null(),
            lp_provider: std::ptr::null(),
        };

        let user_ptr = user_wide.as_ref().map(|w| w.as_ptr()).unwrap_or(std::ptr::null());
        let pass_ptr = pass_wide.as_ref().map(|w| w.as_ptr()).unwrap_or(std::ptr::null());

        let res = unsafe {
            WNetAddConnection2W(&net_res, pass_ptr, user_ptr, 0)
        };

        // 0 = NO_ERROR, 1219 = ERROR_SESSION_CREDENTIAL_CONFLICT (already connected with session),
        // 85 = ERROR_ALREADY_ASSIGNED
        if res == 0 || res == 1219 || res == 85 {
            Ok(())
        } else {
            let unc_dir = format!(r"\\{}\{}\", params.host, params.share);
            if std::path::Path::new(&unc_dir).exists() {
                return Ok(());
            }
            let err_msg = match res {
                5 => "Access is denied (os error 5). Incorrect username or password, or permission denied.",
                1326 => "Logon failure (os error 1326): Unknown user name or bad password.",
                53 => "Network path not found (os error 53): The host server could not be reached.",
                67 => "Network name not found (os error 67): The share name does not exist on the server.",
                _ => "Windows network connection failed",
            };
            Err(format!("SMB connection to '\\\\{}\\{}' failed (code {}): {}", params.host, params.share, res, err_msg))
        }
    }

    /// Check if native Windows SMB is available (always true on Windows)
    pub fn is_available() -> bool {
        true
    }

    /// List directory contents over SMB/CIFS on Windows
    pub fn list_dir(params: &SmbParams) -> VfsResult<DirectoryListing> {
        if params.share.is_empty() {
            let local_listing = crate::vfs::local::LocalFs::list_unc_server_shares(&params.host, false)
                .map_err(|e| format!("SMB share enumeration failed: {}", e))?;

            let mut entries = Vec::new();
            for entry in local_listing.entries {
                let mut e = entry;
                e.path = params.build_uri(&e.name);
                entries.push(e);
            }

            return Ok(DirectoryListing {
                current_path: params.build_uri(""),
                parent_path: None,
                entries,
                total_files: 0,
                total_dirs: local_listing.total_dirs,
                total_size: 0,
                protocol: "smb".to_string(),
                is_truncated: None,
                max_limit: None,
            });
        }

        let _ = Self::ensure_windows_connection(params);
        let unc_path = if params.subpath.is_empty() {
            format!(r"\\{}\{}\", params.host, params.share)
        } else {
            format!(r"\\{}\{}\{}", params.host, params.share, params.subpath.replace('/', "\\"))
        };

        let local_listing = crate::vfs::local::LocalFs::list_dir(&unc_path, false)
            .map_err(|e| format!("SMB list failed: {}", e))?;

        let mut entries = Vec::new();
        for entry in local_listing.entries {
            let full_subpath = if params.subpath.is_empty() {
                entry.name.clone()
            } else {
                format!("{}/{}", params.subpath, entry.name)
            };
            let mut e = entry;
            e.path = params.build_uri(&full_subpath);
            entries.push(e);
        }

        let current_path = params.build_uri(&params.subpath);
        let parent_path = if params.subpath.is_empty() {
            Some(format!("smb://{}{}{}/", params.auth_prefix(), params.host, params.port_suffix()))
        } else {
            let p = std::path::Path::new(&params.subpath);
            let parent_sub = p.parent().map(|p| p.to_string_lossy().to_string()).unwrap_or_default();
            Some(params.build_uri(&parent_sub))
        };

        Ok(DirectoryListing {
            current_path,
            parent_path,
            entries,
            total_files: local_listing.total_files,
            total_dirs: local_listing.total_dirs,
            total_size: local_listing.total_size,
            protocol: "smb".to_string(),
            is_truncated: local_listing.is_truncated,
            max_limit: local_listing.max_limit,
        })
    }

    /// Read raw file bytes from SMB on Windows
    pub fn read_bytes(params: &SmbParams) -> VfsResult<Vec<u8>> {
        let _ = Self::ensure_windows_connection(params);
        let unc_path = format!(r"\\{}\{}\{}", params.host, params.share, params.subpath.replace('/', "\\"));
        fs::read(&unc_path).map_err(|e| format!("SMB read failed: {}", e))
    }

    /// Download file directly from SMB to local path on Windows
    pub fn download_to_file(params: &SmbParams, local_dest: &std::path::Path) -> VfsResult<()> {
        let _ = Self::ensure_windows_connection(params);
        if let Some(parent) = local_dest.parent() {
            let _ = fs::create_dir_all(parent);
        }
        let unc_path = format!(r"\\{}\{}\{}", params.host, params.share, params.subpath.replace('/', "\\"));
        fs::copy(&unc_path, local_dest).map_err(|e| format!("SMB download failed: {}", e))?;
        Ok(())
    }

    /// Upload file directly from local path to SMB share on Windows
    pub fn upload_from_file(params: &SmbParams, local_src: &std::path::Path) -> VfsResult<()> {
        let _ = Self::ensure_windows_connection(params);
        let unc_path = format!(r"\\{}\{}\{}", params.host, params.share, params.subpath.replace('/', "\\"));
        if let Some(parent) = std::path::Path::new(&unc_path).parent() {
            let _ = fs::create_dir_all(parent);
        }
        fs::copy(local_src, &unc_path).map_err(|e| format!("SMB upload failed: {}", e))?;
        Ok(())
    }

    /// Write file contents to SMB share on Windows
    pub fn write_file(params: &SmbParams, content: &[u8]) -> VfsResult<()> {
        let _ = Self::ensure_windows_connection(params);
        let unc_path = format!(r"\\{}\{}\{}", params.host, params.share, params.subpath.replace('/', "\\"));
        if let Some(parent) = std::path::Path::new(&unc_path).parent() {
            let _ = fs::create_dir_all(parent);
        }
        fs::write(&unc_path, content).map_err(|e| format!("SMB write failed: {}", e))
    }

    /// Create directory in SMB share on Windows
    pub fn mkdir(params: &SmbParams) -> VfsResult<()> {
        let _ = Self::ensure_windows_connection(params);
        let unc_path = format!(r"\\{}\{}\{}", params.host, params.share, params.subpath.replace('/', "\\"));
        fs::create_dir_all(&unc_path).map_err(|e| format!("SMB mkdir failed: {}", e))
    }

    /// Delete file or directory in SMB share on Windows
    pub fn delete(params: &SmbParams, is_dir: bool) -> VfsResult<()> {
        let _ = Self::ensure_windows_connection(params);
        let unc_path = format!(r"\\{}\{}\{}", params.host, params.share, params.subpath.replace('/', "\\"));
        if is_dir {
            fs::remove_dir_all(&unc_path).map_err(|e| format!("SMB rmdir failed: {}", e))
        } else {
            fs::remove_file(&unc_path).map_err(|e| format!("SMB del failed: {}", e))
        }
    }

    /// Rename file or directory in SMB share on Windows
    pub fn rename(params: &SmbParams, new_subpath: &str) -> VfsResult<()> {
        let _ = Self::ensure_windows_connection(params);
        let old_unc = format!(r"\\{}\{}\{}", params.host, params.share, params.subpath.replace('/', "\\"));
        let new_unc = format!(r"\\{}\{}\{}", params.host, params.share, new_subpath.replace('/', "\\"));
        fs::rename(&old_unc, &new_unc).map_err(|e| format!("SMB rename failed: {}", e))
    }

    /// Test SMB connection on Windows
    pub fn test_connection(params: &SmbParams) -> VfsResult<String> {
        if params.share.is_empty() {
            let res = crate::vfs::local::LocalFs::list_unc_server_shares(&params.host, false)
                .map_err(|e| format!("SMB host '\\\\{}' connection failed: {}", params.host, e))?;
            return Ok(format!("✓ Successfully connected to SMB Host '\\\\{}' (found {} shares)", params.host, res.entries.len()));
        }
        Self::ensure_windows_connection(params)?;
        let unc_dir = format!(r"\\{}\{}\", params.host, params.share);
        if std::path::Path::new(&unc_dir).exists() {
            Ok(format!("✓ Successfully connected to SMB Share '\\\\{}\\{}'", params.host, params.share))
        } else {
            Err(format!("SMB share '\\\\{}\\{}' connected but directory is inaccessible", params.host, params.share))
        }
    }
}

// -----------------------------------------------------------------------------
// NON-WINDOWS SMB IMPLEMENTATION (via smbclient CLI)
// -----------------------------------------------------------------------------
#[cfg(not(windows))]
impl SmbClient {
    /// Format credentials argument for smbclient (-U "DOMAIN\user%pass" or -N)
    fn build_auth_arg(params: &SmbParams) -> Vec<String> {
        let mut args = Vec::new();
        if let Some(ref user) = params.username {
            let full_user = if let Some(ref dom) = params.domain {
                if !dom.is_empty() && dom != "WORKGROUP" {
                    format!("{}\\{}", dom, user)
                } else {
                    user.clone()
                }
            } else {
                user.clone()
            };

            let auth_str = if let Some(ref pass) = params.password {
                format!("{}%{}", full_user, pass)
            } else {
                full_user
            };
            args.push("-U".to_string());
            args.push(auth_str);
        } else {
            args.push("-N".to_string()); // Anonymous / Guest
        }

        if params.port != 445 && params.port > 0 {
            args.push("-p".to_string());
            args.push(params.port.to_string());
        }

        args
    }

    /// Audits arguments interpolated into smbclient -c command strings.
    /// Rejects command delimiters (semicolons, newlines, carriage returns) and double quotation marks.
    pub fn escape_smb_cmd_arg(arg: &str) -> VfsResult<String> {
        if arg.contains('\n') || arg.contains('\r') {
            return Err("SMB path contains illegal newline or carriage return characters".to_string());
        }
        if arg.contains(';') {
            return Err("SMB path contains illegal semicolon command delimiter".to_string());
        }
        if arg.contains('"') {
            return Err("SMB path contains illegal double quotation mark characters".to_string());
        }
        Ok(arg.to_string())
    }

    /// Check if smbclient is available in PATH
    pub fn is_available() -> bool {
        Command::new("smbclient")
            .arg("--version")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }

    /// List directory contents over SMB/CIFS
    pub fn list_dir(params: &SmbParams) -> VfsResult<DirectoryListing> {
        if !Self::is_available() {
            return Err("Samba client utility ('smbclient') is not installed on the host. Install it with: sudo apt install smbclient (Debian/Ubuntu) or sudo dnf install samba-client (Fedora/RHEL).".to_string());
        }

        if params.share.is_empty() {
            let mut cmd = Command::new("smbclient");
            cmd.arg("-L").arg(format!("//{}", params.host));
            for a in Self::build_auth_arg(params) {
                cmd.arg(a);
            }
            let output = cmd.output().map_err(|e| format!("Failed to execute smbclient: {}", e))?;
            if !output.status.success() {
                let err_msg = String::from_utf8_lossy(&output.stderr);
                let out_msg = String::from_utf8_lossy(&output.stdout);
                let combined = if err_msg.trim().is_empty() { out_msg.to_string() } else { err_msg.to_string() };
                return Err(format!("SMB share enumeration failed: {}", combined.trim()));
            }

            let stdout = String::from_utf8_lossy(&output.stdout);
            let mut entries = Vec::new();
            let mut in_share_section = false;

            for line in stdout.lines() {
                let trimmed = line.trim();
                if trimmed.contains("Sharename") && trimmed.contains("Type") {
                    in_share_section = true;
                    continue;
                }
                if in_share_section {
                    if trimmed.starts_with("---") {
                        continue;
                    }
                    if trimmed.is_empty() || trimmed.contains("Server") || trimmed.contains("Workgroup") {
                        break;
                    }
                    let tokens: Vec<&str> = trimmed.split_whitespace().collect();
                    if tokens.len() >= 2 {
                        let name = tokens[0].to_string();
                        let share_type = tokens[1];
                        if share_type.eq_ignore_ascii_case("Disk") {
                            let entry_path = params.build_uri(&name);
                            entries.push(FileEntry {
                                name: name.clone(),
                                path: entry_path,
                                is_dir: true,
                                is_symlink: false,
                                is_empty: None,
                                size: 0,
                                modified: None,
                                permissions: "drwxr-xr-x".to_string(),
                                mode_octal: "0755".to_string(),
                                owner: "smb".to_string(),
                                group: "smb".to_string(),
                                uid: 1000,
                                gid: 1000,
                                mime_type: None,
                                is_archive: false,
                            });
                        }
                    }
                }
            }

            let total_dirs = entries.len();
            entries.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));

            return Ok(DirectoryListing {
                current_path: params.build_uri(""),
                parent_path: None,
                entries,
                total_files: 0,
                total_dirs,
                total_size: 0,
                protocol: "smb".to_string(),
                is_truncated: None,
                max_limit: None,
            });
        }

        let share_target = format!("//{}/{}", params.host, params.share);
        let smb_cmd = if params.subpath.is_empty() {
            "ls".to_string()
        } else {
            let win_path = Self::escape_smb_cmd_arg(&params.subpath.replace('/', "\\"))?;
            format!("cd \"{}\"; ls", win_path)
        };

        let mut cmd = Command::new("smbclient");
        cmd.arg(&share_target);
        for a in Self::build_auth_arg(params) {
            cmd.arg(a);
        }
        cmd.arg("-c").arg(&smb_cmd);

        let output = cmd.output().map_err(|e| format!("Failed to execute smbclient: {}", e))?;

        if !output.status.success() {
            let err_msg = String::from_utf8_lossy(&output.stderr);
            let out_msg = String::from_utf8_lossy(&output.stdout);
            let combined = if err_msg.trim().is_empty() { out_msg.to_string() } else { err_msg.to_string() };
            return Err(format!("SMB list failed: {}", combined.trim()));
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let mut entries = Vec::new();
        let mut total_files = 0;
        let mut total_dirs = 0;
        let mut total_size: u64 = 0;

        for line in stdout.lines() {
            let trimmed = line.trim();
            if trimmed.is_empty()
                || trimmed.contains("blocks of size")
                || trimmed.contains("blocks available")
                || trimmed.starts_with("Domain=")
                || trimmed.starts_with("OS=")
                || trimmed.starts_with("Server=")
                || trimmed.starts_with("smb:")
            {
                continue;
            }

            let tokens: Vec<&str> = trimmed.split_whitespace().collect();
            if tokens.len() >= 7 {
                let attr = tokens[tokens.len() - 7];
                let size: u64 = tokens[tokens.len() - 6].parse().unwrap_or(0);
                let name = tokens[..tokens.len() - 7].join(" ");

                if name == "." || name == ".." {
                    continue;
                }

                let is_dir = attr.contains('D');

                let full_subpath = if params.subpath.is_empty() {
                    name.clone()
                } else {
                    format!("{}/{}", params.subpath, name)
                };

                let entry_path = params.build_uri(&full_subpath);

                if is_dir {
                    total_dirs += 1;
                    entries.push(FileEntry {
                        name: name.clone(),
                        path: entry_path,
                        is_dir: true,
                        is_symlink: false,
                        is_empty: None,
                        size: 0,
                        modified: None,
                        permissions: "drwxr-xr-x".to_string(),
                        mode_octal: "0755".to_string(),
                        owner: "smb".to_string(),
                        group: "smb".to_string(),
                        uid: 1000,
                        gid: 1000,
                        mime_type: None,
                        is_archive: false,
                    });
                } else {
                    total_files += 1;
                    total_size += size;
                    let is_archive = is_archive_file(&name);
                    entries.push(FileEntry {
                        name: name.clone(),
                        path: entry_path,
                        is_dir: false,
                        is_symlink: false,
                        is_empty: None,
                        size,
                        modified: None,
                        permissions: "-rw-r--r--".to_string(),
                        mode_octal: "0644".to_string(),
                        owner: "smb".to_string(),
                        group: "smb".to_string(),
                        uid: 1000,
                        gid: 1000,
                        mime_type: Some(mime_guess::from_path(&name).first_or_octet_stream().to_string()),
                        is_archive,
                    });
                }
            }
        }

        // Sort: directories first, then alphabetical
        entries.sort_by(|a, b| {
            if a.is_dir == b.is_dir {
                a.name.to_lowercase().cmp(&b.name.to_lowercase())
            } else if a.is_dir {
                std::cmp::Ordering::Less
            } else {
                std::cmp::Ordering::Greater
            }
        });

        let current_path = params.build_uri(&params.subpath);
        let parent_path = if params.subpath.is_empty() {
            Some(format!("smb://{}{}{}/", params.auth_prefix(), params.host, params.port_suffix()))
        } else {
            let p = std::path::Path::new(&params.subpath);
            let parent_sub = p.parent().map(|p| p.to_string_lossy().to_string()).unwrap_or_default();
            Some(params.build_uri(&parent_sub))
        };

        Ok(DirectoryListing {
            current_path,
            parent_path,
            entries,
            total_files,
            total_dirs,
            total_size,
            protocol: "smb".to_string(),
            is_truncated: None,
            max_limit: None,
        })
    }

    /// Read raw file bytes from SMB
    pub fn read_bytes(params: &SmbParams) -> VfsResult<Vec<u8>> {
        if !Self::is_available() {
            return Err("Samba client utility ('smbclient') is not installed.".to_string());
        }

        let tmp_file = NamedTempFile::new().map_err(|e| format!("Temp file error: {}", e))?;
        let tmp_path = tmp_file.path().to_string_lossy().to_string();

        let share_target = format!("//{}/{}", params.host, params.share);
        let win_path = Self::escape_smb_cmd_arg(&params.subpath.replace('/', "\\"))?;
        let safe_tmp = Self::escape_smb_cmd_arg(&tmp_path)?;
        let smb_cmd = format!("get \"{}\" \"{}\"", win_path, safe_tmp);

        let mut cmd = Command::new("smbclient");
        cmd.arg(&share_target);
        for a in Self::build_auth_arg(params) {
            cmd.arg(a);
        }
        cmd.arg("-c").arg(&smb_cmd);

        let output = cmd.output().map_err(|e| format!("Failed to read SMB file: {}", e))?;
        if !output.status.success() {
            return Err(format!("SMB get failed: {}", String::from_utf8_lossy(&output.stderr)));
        }

        fs::read(&tmp_path).map_err(|e| format!("Read temp file error: {}", e))
    }

    /// Download file directly from SMB to local path
    pub fn download_to_file(params: &SmbParams, local_dest: &std::path::Path) -> VfsResult<()> {
        if !Self::is_available() {
            return Err("Samba client utility ('smbclient') is not installed.".to_string());
        }

        if let Some(parent) = local_dest.parent() {
            let _ = fs::create_dir_all(parent);
        }

        let share_target = format!("//{}/{}", params.host, params.share);
        let win_path = Self::escape_smb_cmd_arg(&params.subpath.replace('/', "\\"))?;
        let dest_str = local_dest.to_string_lossy();
        let safe_dest = Self::escape_smb_cmd_arg(&dest_str)?;
        let smb_cmd = format!("get \"{}\" \"{}\"", win_path, safe_dest);

        let mut cmd = Command::new("smbclient");
        cmd.arg(&share_target);
        for a in Self::build_auth_arg(params) {
            cmd.arg(a);
        }
        cmd.arg("-c").arg(&smb_cmd);

        let output = cmd.output().map_err(|e| format!("Failed to download SMB file: {}", e))?;
        if !output.status.success() {
            let err = String::from_utf8_lossy(&output.stderr);
            let out = String::from_utf8_lossy(&output.stdout);
            let msg = if err.trim().is_empty() { out.to_string() } else { err.to_string() };
            return Err(format!("SMB get failed: {}", msg.trim()));
        }

        Ok(())
    }

    /// Upload file directly from local path to SMB share
    pub fn upload_from_file(params: &SmbParams, local_src: &std::path::Path) -> VfsResult<()> {
        if !Self::is_available() {
            return Err("Samba client utility ('smbclient') is not installed.".to_string());
        }

        if !local_src.exists() {
            return Err(format!("Local source file does not exist: {}", local_src.display()));
        }

        let share_target = format!("//{}/{}", params.host, params.share);
        let win_path = Self::escape_smb_cmd_arg(&params.subpath.replace('/', "\\"))?;
        let src_str = local_src.to_string_lossy();
        let safe_src = Self::escape_smb_cmd_arg(&src_str)?;
        let smb_cmd = format!("put \"{}\" \"{}\"", safe_src, win_path);

        let mut cmd = Command::new("smbclient");
        cmd.arg(&share_target);
        for a in Self::build_auth_arg(params) {
            cmd.arg(a);
        }
        cmd.arg("-c").arg(&smb_cmd);

        let output = cmd.output().map_err(|e| format!("Failed to upload SMB file: {}", e))?;
        if !output.status.success() {
            let err = String::from_utf8_lossy(&output.stderr);
            let out = String::from_utf8_lossy(&output.stdout);
            let msg = if err.trim().is_empty() { out.to_string() } else { err.to_string() };
            return Err(format!("SMB put failed: {}", msg.trim()));
        }

        Ok(())
    }

    /// Write file contents to SMB share
    pub fn write_file(params: &SmbParams, content: &[u8]) -> VfsResult<()> {
        if !Self::is_available() {
            return Err("Samba client utility ('smbclient') is not installed.".to_string());
        }

        let tmp_file = NamedTempFile::new().map_err(|e| format!("Temp file error: {}", e))?;
        let tmp_path = tmp_file.path().to_string_lossy().to_string();
        fs::write(&tmp_path, content).map_err(|e| format!("Failed to write temp file: {}", e))?;

        let share_target = format!("//{}/{}", params.host, params.share);
        let win_path = Self::escape_smb_cmd_arg(&params.subpath.replace('/', "\\"))?;
        let safe_tmp = Self::escape_smb_cmd_arg(&tmp_path)?;
        let smb_cmd = format!("put \"{}\" \"{}\"", safe_tmp, win_path);

        let mut cmd = Command::new("smbclient");
        cmd.arg(&share_target);
        for a in Self::build_auth_arg(params) {
            cmd.arg(a);
        }
        cmd.arg("-c").arg(&smb_cmd);

        let output = cmd.output().map_err(|e| format!("Failed to write SMB file: {}", e))?;
        if !output.status.success() {
            return Err(format!("SMB put failed: {}", String::from_utf8_lossy(&output.stderr)));
        }

        Ok(())
    }

    /// Create directory in SMB share
    pub fn mkdir(params: &SmbParams) -> VfsResult<()> {
        if !Self::is_available() {
            return Err("Samba client utility ('smbclient') is not installed.".to_string());
        }

        let share_target = format!("//{}/{}", params.host, params.share);
        let win_path = Self::escape_smb_cmd_arg(&params.subpath.replace('/', "\\"))?;
        let smb_cmd = format!("mkdir \"{}\"", win_path);

        let mut cmd = Command::new("smbclient");
        cmd.arg(&share_target);
        for a in Self::build_auth_arg(params) {
            cmd.arg(a);
        }
        cmd.arg("-c").arg(&smb_cmd);

        let output = cmd.output().map_err(|e| format!("Failed to create SMB directory: {}", e))?;
        if !output.status.success() {
            return Err(format!("SMB mkdir failed: {}", String::from_utf8_lossy(&output.stderr)));
        }

        Ok(())
    }

    /// Delete file or directory in SMB share
    pub fn delete(params: &SmbParams, is_dir: bool) -> VfsResult<()> {
        if !Self::is_available() {
            return Err("Samba client utility ('smbclient') is not installed.".to_string());
        }

        let share_target = format!("//{}/{}", params.host, params.share);
        let win_path = Self::escape_smb_cmd_arg(&params.subpath.replace('/', "\\"))?;
        
        let smb_cmd = if is_dir {
            format!("deltree \"{}\"", win_path)
        } else {
            format!("del \"{}\"", win_path)
        };

        let mut cmd = Command::new("smbclient");
        cmd.arg(&share_target);
        for a in Self::build_auth_arg(params) {
            cmd.arg(a);
        }
        cmd.arg("-c").arg(&smb_cmd);

        let output = cmd.output().map_err(|e| format!("Failed to delete SMB entry: {}", e))?;
        if !output.status.success() {
            // If del failed and we assumed file, try deltree
            if !is_dir {
                let rmdir_cmd = format!("deltree \"{}\"", win_path);
                let mut retry = Command::new("smbclient");
                retry.arg(&share_target);
                for a in Self::build_auth_arg(params) {
                    retry.arg(a);
                }
                retry.arg("-c").arg(&rmdir_cmd);
                if let Ok(out) = retry.output() {
                    if out.status.success() {
                        return Ok(());
                    }
                }
            }
            return Err(format!("SMB delete failed: {}", String::from_utf8_lossy(&output.stderr)));
        }

        Ok(())
    }

    /// Rename file or directory in SMB share
    pub fn rename(params: &SmbParams, new_subpath: &str) -> VfsResult<()> {
        if !Self::is_available() {
            return Err("Samba client utility ('smbclient') is not installed.".to_string());
        }

        let share_target = format!("//{}/{}", params.host, params.share);
        let old_win_path = Self::escape_smb_cmd_arg(&params.subpath.replace('/', "\\"))?;
        let new_win_path = Self::escape_smb_cmd_arg(&new_subpath.replace('/', "\\"))?;
        let smb_cmd = format!("rename \"{}\" \"{}\"", old_win_path, new_win_path);

        let mut cmd = Command::new("smbclient");
        cmd.arg(&share_target);
        for a in Self::build_auth_arg(params) {
            cmd.arg(a);
        }
        cmd.arg("-c").arg(&smb_cmd);

        let output = cmd.output().map_err(|e| format!("Failed to rename SMB entry: {}", e))?;
        if !output.status.success() {
            return Err(format!("SMB rename failed: {}", String::from_utf8_lossy(&output.stderr)));
        }

        Ok(())
    }

    /// Test SMB connection
    pub fn test_connection(params: &SmbParams) -> VfsResult<String> {
        if !Self::is_available() {
            return Err("Samba client ('smbclient') is not installed on the server.\nInstall with: sudo apt install smbclient".to_string());
        }

        if params.share.is_empty() {
            let mut cmd = Command::new("smbclient");
            cmd.arg("-L").arg(format!("//{}", params.host));
            for a in Self::build_auth_arg(params) {
                cmd.arg(a);
            }
            let output = cmd.output().map_err(|e| format!("Failed to run smbclient: {}", e))?;
            if output.status.success() {
                Ok(format!("✓ Successfully connected to SMB Host '//{}'", params.host))
            } else {
                let err = String::from_utf8_lossy(&output.stderr);
                let out = String::from_utf8_lossy(&output.stdout);
                let msg = if err.trim().is_empty() { out.to_string() } else { err.to_string() };
                Err(format!("SMB Connection Failed: {}", msg.trim()))
            }
        } else {
            let share_target = format!("//{}/{}", params.host, params.share);
            let mut cmd = Command::new("smbclient");
            cmd.arg(&share_target);
            for a in Self::build_auth_arg(params) {
                cmd.arg(a);
            }
            cmd.arg("-c").arg("ls");

            let output = cmd.output().map_err(|e| format!("Failed to run smbclient: {}", e))?;
            if output.status.success() {
                Ok(format!("✓ Successfully connected to SMB Share '//{}/{}'", params.host, params.share))
            } else {
                let err = String::from_utf8_lossy(&output.stderr);
                let out = String::from_utf8_lossy(&output.stdout);
                let msg = if err.trim().is_empty() { out.to_string() } else { err.to_string() };
                Err(format!("SMB Connection Failed: {}", msg.trim()))
            }
        }
    }
}

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

    #[test]
    fn test_escape_smb_cmd_arg_valid() {
        assert_eq!(
            SmbClient::escape_smb_cmd_arg("regular\\path\\file.txt").unwrap(),
            "regular\\path\\file.txt"
        );
        assert_eq!(
            SmbClient::escape_smb_cmd_arg("name with spaces.pdf").unwrap(),
            "name with spaces.pdf"
        );
    }

    #[test]
    fn test_escape_smb_cmd_arg_rejects_quotes() {
        assert!(SmbClient::escape_smb_cmd_arg("file_with_\"quote\".txt").is_err());
        assert!(SmbClient::escape_smb_cmd_arg("a\\\"; rm x").is_err());
    }

    #[test]
    fn test_escape_smb_cmd_arg_rejects_semicolon_command_injection() {
        let injected = "folder\"; deltree \"root; ls";
        let res = SmbClient::escape_smb_cmd_arg(injected);
        assert!(res.is_err(), "Semicolon injection must be rejected");
        assert!(res.unwrap_err().contains("semicolon command delimiter"));
    }

    #[test]
    fn test_escape_smb_cmd_arg_rejects_newlines_and_carriage_returns() {
        let res_nl = SmbClient::escape_smb_cmd_arg("folder\ndeltree root");
        assert!(res_nl.is_err(), "Newline injection must be rejected");

        let res_cr = SmbClient::escape_smb_cmd_arg("folder\rdeltree root");
        assert!(res_cr.is_err(), "Carriage return injection must be rejected");
    }
}