I've a Rocket API that allow the user to save files.
Last week, I've work on dockerisation of my app, and I encounter a problem : when I try to save a file on a shared volume, I get the error Invalid cross-device link (os error 18) !
While googling, I've found this about Python, and I suppose that I am facing something similar.
So here is my question : does Rust have something that allow me to do this ?
Or do I have to copy (or get binary from file) and save it after ?
My code looks like :
fn save_media(file: Form<TempFile<'_>>, ...) {
match file.path() {
Some(path) => {
std::fs:create_dir_all(...) // if parent not exists
std::fs::rename(file, target) // target is a computed path
},
None => handle_error()
}
}
To know : I have simplify my code for you because in my case, it is splitted in some functions/files because the function that get the source path is use by another binary (cli) so that's why I get the path of the Rocket Form<TempFile<'_>>
Thanks for your help !
On stable Rust
Rocket 0.5.0-rc (in fact it doesn't matter but if you're asking ^^)
Docker command : docker run -v /var/www/adbridge-resources:/adbridge/resources -p 8000:8000 --restart always <my_custom_image_name>
I'm not sure I follow what you're trying to do exactly.
If the user is uploading a file, you probably just want to save the file directly on the persistent volume and not move it from the docker container file system into the persistent volume.
If you have multiple steps and you only sometimes need the file to be stored persistently, you probably still want to write it to the persistent volume to avoid having to copy it[1].
I'm not super familiar with the nuances of linux mount points, it's possible you can move files across volumes without having to copy the file as long as the mount points map to the same physical device ↩︎
I've my rust rocket app running in container.
I share the volume between host & container.
But when I try to save the file on this volume, I get the error.
I think it is a docker & rust problem and not only rust lang.
The code you showed isn't saving the file though, it's trying to rename it. Obviously if you have a file on hard drive A and try to move it to hard drive B by renaming it that wouldn't work, the data isn't on that hard drive yet.
It looks to me like the error your getting is basically the same concept, except with logical volumes instead of physical hard drives. If you just save the file to the target volume in the first place, I think you won't get that error.
In fact when I was not on a docker image, it works great.
The rename is to move the file from the temp to a persisted folder.
The problem occurs when I run it on a docker container with shared volumes. If I don't share volumes (and so I do not persist it outside the docker container), it works too.
Because the Temp is handle by Rocket.
But in fact the logical behind is the same, as you said.
So now, instead of moving the file, I am copying it.