expression

File uploads: avoid overwriting existing files

Exclusive creation fails if the destination already exists. This fixture creates a new dedicated directory within the runner scratch directory and writes a fixed trusted key; a second creation attempt cannot overwrite the first file. This is one safeguard, not a complete safe-upload pipeline. Standalone Cargo example: execute only in a fresh isolated scratch directory. Expected stdout (not executed): true hello Common mistake: Using ordinary create/truncate semantics for an upload destination. Do not execute outside a fresh scratch directory.

Formal Syntax & Structure

rust syntaxrust
OpenOptions::new().write(true).create_new(true).open(path)

Working Example

Example Snippetrust
use axum::{Router, body::{Body, to_bytes}, http::{Request, StatusCode}};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() {    use std::{fs::{self, OpenOptions}, io::Write};    fs::create_dir("v73-private-demo").unwrap();    let path = "v73-private-demo/upload-00000001.bin";    let mut f = OpenOptions::new().write(true).create_new(true).open(path).unwrap();    f.write_all(b"hello").unwrap();    drop(f);    let second = OpenOptions::new().write(true).create_new(true).open(path);    println!("{}", second.is_err());    println!("{}", fs::read_to_string(path).unwrap());}
Rust Reference