Hi all,
I'm facing a strange problem trying to read entries in a directory.
I'm very new to rust so this problem is probably related to me not fully understanding the language
Anyway,
I've got the following code (I haven't included the crates and such that are required to compile this code and it's partially snippets from the real code so it may not compile):
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_websockets=debug,tower_http=error".into()),
)
.with(tracing_subscriber::fmt::layer())
.init();
let (tx, rx) = channel::<String>(1);
let state: AppState = AppState {
recv: Arc::new(Mutex::new(rx)),
send: tx,
};
// Adding the API functions
let router: Router = Router::new()
.route("/func", post(program))
.layer(
TraceLayer::new_for_http()
.make_span_with(DefaultMakeSpan::default().include_headers(true)),
)
.layer(ServiceBuilder::new().layer(CorsLayer::very_permissive()))
.with_state(state);
let app: Router = Router::new().nest("/api", router);
println!("Starting server on port: {}", port);
let server_string: String = format!("127.0.0.1:{:?}", port);
let listener: tokio::net::TcpListener = tokio::net::TcpListener::bind(server_string)
.await
.unwrap();
axum::serve(
listener,
app.into_make_service_with_connect_info::<SocketAddr>(),
)
.await
.unwrap();
Ok(())
}
async fn func(State(state): State<AppState>) -> Response {
find_file("some_path");
StatusCode::OK.into_response()
}
async fn find_file(path: &String, match_string: &String) -> Result<String, Box<dyn Error>> {
let p = Path::new(path);
if p.is_dir() {
let mut entries = fs::read_dir(p).await?;
while let Ok(Some(entry)) = entries.next_entry().await {
println!("Entry: {:?}", entry.path());
}
} else {
println!("{:?} is not a directory", p);
}
Ok(())
}
This code starts a web server on port localhost::8081 and waits for input.
To execute the code, I use the following:
curl -v http://localhost:8081/api/program --data "func" --header "Content-type: application/json"
The idea is that when you call the program correctly you should get a listing of all files in a directory.
The problem that I have is with the find_file
function. It works perfectly the first time I call the it, but the second time it stubbornly refuses to read anything from the directory.
I've also tried this with glob
but I get exactly the same behavior. It also doesn't matter if the function is async
or not.
I've been struggling with this for some time and I really don't understand what's going on here.
I generally assume that if a function works once, it works other times as well, but I must be missing something here.
I'm using the 2021 edition of Rust (according to my cargo.toml
file) pasted below:
[package]
name = "program"
version = "0.1.0"
edition = "2021"
publish = false
description = "My program"
[dependencies]
libc = "0.2"
ihex = "3.0"
tokio = { version = "1.28", features = ["full"] }
tokio-tungstenite = "0.21"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
headers = "0.4"
axum = { version = "0.7.5", features = ["ws"] }
axum-extra = { version = "0.9.3", features = ["typed-header"] }
serde = { version = "1.0.147", features = ["derive"] }
serde_json = "1.0.91"
tower-http = { version = "0.5.0", features = ["fs", "trace", "cors"] }
tower = { version = "0.4", features = ["util"] }
chrono = { version = "0.4", features = ["serde"] }
lazy_static = "1.4.0"
# uuid = "1.3.1"
libffi = "3.2.0"
thiserror = "1.0.61"
clap = { version = "4.5.10", features = ["derive"] }
config = "0.14.0"
flate2 = "1.0.30"
tar = "0.4.41"
rand = "0.8.5"
openssl = "0.10.66"
aes-gcm = "0.10.3"
crc32fast = "1.4.2"
error-chain = "0.12.4"
glob = "0.3.1"
byteorder = "1.5.0"
[dependencies.uuid]
version = "1.10.0"
features = [
"v4", # Lets you generate random UUIDs
"fast-rng", # Use a faster (but still sufficiently random) RNG
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs
"serde", # adds the ability to serialize and deserialize a UUID using `serde`
"bytemuck", # adds a `Pod` trait implementation to `Uuid` for byte manipulation
"borsh",
]