Can a file be opened multiple times?

Hello community,
I'm writing a cache manager for my HTTP proxy. I want it to write downloaded data to the disk first before serving data to the client. So there are two cases, while still downloading, the downloading task holds a tokio::fs::File to the cache data, and when completed, a new File has to be created to read the data.

I haven't dealt with file I/O much and I wonder if I can just write a function like async fn load_data(start: u64, end:u64)-> Result<Vec, anyhow::Error> which opens the cache file every time it gets called, even if it's already opened somewhere else. Otherwise I'll need to write two similar functions, only that one will need to take a mutable File reference then seek positions back and forth.

You can open a file multiple times.

However, you may end up serving incomplete data (reading a partially written or synced file). You may want to consider writing files to one name/location[1] and then upon success moving them to the intended location[2] (or upon failure, removing the partial file).

  1. "filename.part" ↩︎

  2. "filename" ↩︎


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.