Skip to content

Commit

Permalink
fix: parse special permissions StT in ls output
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Jul 9, 2024
1 parent a034e23 commit 6b99298
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

- [Changelog](#changelog)
- [0.3.1](#031)
- [0.3.0](#030)
- [0.2.1](#021)
- [0.2.0](#020)
Expand All @@ -13,6 +14,12 @@

---

## 0.3.1

Released on 09/07/2024

- Fix: parse special permissions `StT` in ls output

## 0.3.0

Released on 09/07/2024
Expand Down
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ license = "MIT"
name = "remotefs-ssh"
readme = "README.md"
repository = "https://github.com/veeso/remotefs-rs-ssh"
version = "0.3.0"
version = "0.3.1"

[dependencies]
chrono = "^0.4"
lazy_static = "^1.4"
lazy-regex = "3"
log = "^0.4"
regex = "^1.7"
remotefs = "^0.2"
ssh2-config = "^0.2"
ssh2 = "^0.9"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<p align="center">~ Remotefs SSH client ~</p>

<p align="center">Developed by <a href="https://veeso.github.io/" target="_blank">@veeso</a></p>
<p align="center">Current version: 0.3.0 (09/07/2024)</p>
<p align="center">Current version: 0.3.1 (09/07/2024)</p>

<p align="center">
<a href="https://opensource.org/licenses/MIT"
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

// -- crates
#[macro_use]
extern crate lazy_static;
extern crate lazy_regex;
#[macro_use]
extern crate log;

Expand Down
44 changes: 37 additions & 7 deletions src/ssh/scp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ops::Range;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};

use regex::Regex;
use lazy_regex::{Lazy, Regex};
use remotefs::fs::{
FileType, Metadata, ReadStream, RemoteError, RemoteErrorType, RemoteFs, RemoteResult, UnixPex,
UnixPexClass, Welcome, WriteStream,
Expand All @@ -19,6 +19,11 @@ pub use ssh2::Session as SshSession;
use super::{commons, SshOpts};
use crate::utils::{fmt as fmt_utils, parser as parser_utils, path as path_utils};

/// NOTE: about this damn regex <https://stackoverflow.com/questions/32480890/is-there-a-regex-to-parse-the-values-from-an-ftp-directory-listing>
static LS_RE: Lazy<Regex> = lazy_regex!(
r#"^([\-ld])([\-rwxsStT]{9})\s+(\d+)\s+(.+)\s+(.+)\s+(\d+)\s+(\w{3}\s+\d{1,2}\s+(?:\d{1,2}:\d{1,2}|\d{4}))\s+(.+)$"#
);

/// SCP "filesystem" client
pub struct ScpFs {
session: Option<SshSession>,
Expand Down Expand Up @@ -52,15 +57,9 @@ impl ScpFs {
}
}

/// ### parse_ls_output
///
/// Parse a line of `ls -l` output and tokenize the output into a `FsFile`
fn parse_ls_output(&self, path: &Path, line: &str) -> Result<File, ()> {
// Prepare list regex
// NOTE: about this damn regex <https://stackoverflow.com/questions/32480890/is-there-a-regex-to-parse-the-values-from-an-ftp-directory-listing>
lazy_static! {
static ref LS_RE: Regex = Regex::new(r#"^([\-ld])([\-rwxs]{9})\s+(\d+)\s+(.+)\s+(.+)\s+(\d+)\s+(\w{3}\s+\d{1,2}\s+(?:\d{1,2}:\d{1,2}|\d{4}))\s+(.+)$"#).unwrap();
}
trace!("Parsing LS line: '{}'", line);
// Apply regex to result
match LS_RE.captures(line) {
Expand Down Expand Up @@ -1385,6 +1384,37 @@ mod test {
);
}

#[test]
fn test_should_parse_special_permissions_ls_output() {
let client = ScpFs::new(SshOpts::new("localhost"));
assert!(client
.parse_ls_output(
Path::new("/tmp"),
"-rw-rwSrw- 1 manufact manufact 241813 Apr 22 09:31 L9800.SPF",
)
.is_ok());
assert!(client
.parse_ls_output(
Path::new("/tmp"),
"-rw-rwsrw- 1 manufact manufact 241813 Apr 22 09:31 L9800.SPF",
)
.is_ok());

assert!(client
.parse_ls_output(
Path::new("/tmp"),
"-rw-rwtrw- 1 manufact manufact 241813 Apr 22 09:31 L9800.SPF",
)
.is_ok());

assert!(client
.parse_ls_output(
Path::new("/tmp"),
"-rw-rwTrw- 1 manufact manufact 241813 Apr 22 09:31 L9800.SPF",
)
.is_ok());
}

#[test]
fn should_return_errors_on_uninitialized_client() {
let mut client = ScpFs::new(SshOpts::new("localhost"));
Expand Down

0 comments on commit 6b99298

Please sign in to comment.