Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unftp-sbe-fs: fix the format of the LIST command #504

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/unftp-sbe-fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ tracing-attributes = "0.1.27"
async_ftp = "6.0.0"
async-trait = "0.1.77"
more-asserts = "0.3.1"
nix = { version = "0.26.4", default-features = false, features = ["user"] }
pretty_assertions = "1.4.0"
pretty_env_logger = "0.5.0"
regex = "1.10.3"
rstest = "0.18.2"
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
Expand Down
22 changes: 16 additions & 6 deletions crates/unftp-sbe-fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use cfg_if::cfg_if;
use futures::{future::TryFutureExt, stream::TryStreamExt};
use lazy_static::lazy_static;
use libunftp::auth::UserDetail;
use libunftp::storage::{Error, ErrorKind, Fileinfo, Metadata, Result, StorageBackend};
use libunftp::storage::{Error, ErrorKind, Fileinfo, Metadata, Permissions, Result, StorageBackend};
use std::{
fmt::Debug,
io,
Expand All @@ -40,8 +40,8 @@ use std::{
};
use tokio::io::AsyncSeekExt;

#[cfg(target_os = "unix")]
use std::os::unix::fs::MetadataExt;
#[cfg(unix)]
use std::os::unix::fs::{MetadataExt, PermissionsExt};

/// The Filesystem struct is an implementation of the StorageBackend trait that keeps its files
/// inside a specific root directory on local disk.
Expand Down Expand Up @@ -250,7 +250,7 @@ impl Metadata for Meta {

fn gid(&self) -> u32 {
cfg_if! {
if #[cfg(target_os = "unix")] {
if #[cfg(unix)] {
self.inner.gid()
} else {
0
Expand All @@ -260,7 +260,7 @@ impl Metadata for Meta {

fn uid(&self) -> u32 {
cfg_if! {
if #[cfg(target_os = "unix")] {
if #[cfg(unix)] {
self.inner.uid()
} else {
0
Expand All @@ -270,13 +270,23 @@ impl Metadata for Meta {

fn links(&self) -> u64 {
cfg_if! {
if #[cfg(target_os = "unix")] {
if #[cfg(unix)] {
self.inner.nlink()
} else {
1
}
}
}

fn permissions(&self) -> Permissions {
cfg_if! {
if #[cfg(unix)] {
Permissions(self.inner.permissions().mode())
} else {
Permissions(0o7755)
}
}
}
}

#[cfg(test)]
Expand Down
38 changes: 38 additions & 0 deletions crates/unftp-sbe-fs/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,44 @@ async fn put(#[future] harness: Harness) {
mod list {
use super::*;

/// test the exact format of the output
#[cfg(unix)]
#[rstest]
#[awt]
#[tokio::test]
async fn format(#[future] harness: Harness) {
use regex::Regex;
use std::os::unix::fs::{fchown, MetadataExt, OpenOptionsExt};

// Create a filename in the ftp root that we will look for in the `LIST` output
let path = harness.root.join("test.txt");
let f = std::fs::OpenOptions::new().read(true).write(true).create(true).mode(0o754).open(path).unwrap();
// Because most OSes set the file's gid to its parent directory's, and the parent
// directory's is often root, deliberately set it to something more interesting.
fchown(&f, None, Some(nix::unistd::Gid::effective().as_raw())).unwrap();
let md = f.metadata().unwrap();
let uid = md.uid();
let gid = md.gid();
let link_count = md.nlink();
let size = md.len();

let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();

ensure_login_required(ftp_stream.list(None).await);

ftp_stream.login("hoi", "jij").await.unwrap();
let list = ftp_stream.list(None).await.unwrap();
let pat = format!("^-rwxr-xr--\\s+{link_count}\\s+{uid}\\s+{gid}\\s+{size}.*test.txt");
let re = Regex::new(&pat).unwrap();
for entry in list {
if entry.contains("test.txt") {
assert!(re.is_match(&entry), "\"{entry}\" did not match pattern {re:?}");
return;
}
}
panic!("Entry not found");
}

#[rstest]
#[awt]
#[tokio::test]
Expand Down
Loading