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

Error when listing directories containing spaces in their names #530

Open
Ryan-Blignaut opened this issue Jan 18, 2025 · 1 comment
Open

Comments

@Ryan-Blignaut
Copy link

When trying to list items in a directory with a space in the file name data after the space gets truncated.
I have created a small test for this:

use unftp_sbe_fs::ServerExt;

#[tokio::main(flavor = "current_thread")]
async fn main() {
    pretty_env_logger::init();
    let addr = "127.0.0.1:2121";
    let server = libunftp::Server::with_fs("./test/").build().unwrap();
    println!("Starting ftp server on {}", addr);
    server.listen(addr).await.unwrap();
}

#[cfg(test)]
mod tests {

    #[test]
    fn test_list_item_with_space() {
        pretty_env_logger::init();
        use ftp::FtpStream;
        let mut ftp_stream =
            FtpStream::connect("localhost:2121").unwrap_or_else(|err| panic!("{}", err));
        ftp_stream.login("anonymous", "anonymous").unwrap();

        let folder_name_with_space = "test with space";
        // Make test folder as well as subfolder
        ftp_stream.mkdir(folder_name_with_space).unwrap();
        ftp_stream.mkdir(format!("{}/{}", folder_name_with_space, "cool").as_str()).unwrap();

        ftp_stream
            .list(folder_name_with_space.into())
            .unwrap()
            .iter()
            .for_each(|item| {
                println!("{:?}", item);
            });

        let _ = ftp_stream.quit();
    }
}

The expected output of the print statement would be something along the lines of:
"drwxr-xr-x 1 0 0 0 Jan 18 21:30 cool"

However test fails with an error of:
InvalidResponse("Expected code [226, 250], got response: 550 File not found\r\n")

It looks like from the logs the path got truncated:
DEBUG libunftp::server::datachan > Data channel command received: List { options: None, path: Some("test") }, username: anonymous, source: 127.0.0.1:58510, trace-id: 0xbc8265330a20d513, path: test

@Ryan-Blignaut
Copy link
Author

Ryan-Blignaut commented Jan 19, 2025

After having a quick look seems like the issue is stemming from:

let path = line
.split(|&b| b == b' ')
.filter(|s| !line.is_empty() && !s.starts_with(b"-"))
.map(|s| String::from_utf8_lossy(s).to_string())
.next();

I am not 100% sure what this function should be doing but the assumption is that it is trying to parse out the provided line and skip any items that start with a '-'.
Maybe we could collect the results back into a string for example:

 let path = line
                .split(|&b| b == b' ')
                .filter(|s| !line.is_empty() && !s.starts_with(b"-"))
                .map(|s| String::from_utf8_lossy(s).to_string())
                .collect::<Vec<_>>()
                .join(" ");

However, not sure if this is exactly what we want, not too sure of the FTP spec for list and what it could contain. As with the above we will still have an issue if an argument has extra info separated by a space eg --test somevalue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant