{"record":{"id":"889cecc64b847513","repo":"rust-lang/rust","slug":"path-already-exists","errorCode":null,"errorMessage":"Path already exists","messagePattern":"Path already exists","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"library/std/src/sys/fs/uefi.rs","lineNumber":873,"sourceCode":"                    return helpers::os_string_to_raw(&p);\n                }\n                _ => return None,\n            }\n        }\n    }\n\n    /// An implementation of mkdir to allow creating new directory without having to open the\n    /// volume twice (once for checking and once for creating)\n    pub(crate) fn mkdir(path: &Path) -> io::Result<()> {\n        let absolute = crate::path::absolute(path)?;\n\n        let p = helpers::OwnedDevicePath::from_text(absolute.as_os_str())?;\n        let (vol, mut path_remaining) = File::open_volume_from_device_path(p.borrow())?;\n\n        // Check if file exists\n        match File::open(vol, &mut path_remaining, file::MODE_READ, 0) {\n            Ok(_) => {\n                return Err(io::Error::new(io::ErrorKind::AlreadyExists, \"Path already exists\"));\n            }\n            Err(e) if e.kind() == io::ErrorKind::NotFound => {}\n            Err(e) => return Err(e),\n        }\n\n        let _ = File::open(\n            vol,\n            &mut path_remaining,\n            file::MODE_READ | file::MODE_WRITE | file::MODE_CREATE,\n            file::DIRECTORY,\n        )?;\n\n        Ok(())\n    }\n\n    /// EDK2 FAT driver uses EFI_UNSPECIFIED_TIMEZONE to represent localtime. So for proper\n    /// conversion to SystemTime, we use the current time to get the timezone in such cases.\n    pub(crate) fn uefi_to_systemtime(mut time: r_efi::efi::Time) -> Option<SystemTime> {","sourceCodeStart":855,"sourceCodeEnd":891,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/library/std/src/sys/fs/uefi.rs#L855-L891","documentation":"The UEFI mkdir implementation checks for an existing path by trying to open it first; if the open succeeds the target already exists, so it returns Err(io::ErrorKind::AlreadyExists) (uefi.rs:870-877). This mirrors POSIX EEXIST semantics for create_dir on the UEFI backend.","triggerScenarios":"Calling fs::create_dir(p) on UEFI when p (file or directory) already exists; create_dir_all reaching an existing leaf.","commonSituations":"Re-running setup/init code that creates a directory; a concurrent writer creating the same path; boot-time directory provisioning.","solutions":["Use fs::create_dir_all(p), which treats an existing directory as success.","Match on io::ErrorKind::AlreadyExists and treat it as non-fatal when appropriate.","Check fs::exists(p) / metadata before calling create_dir if you need to branch."],"exampleFix":"// before\nstd::fs::create_dir(\"/data/cache\")?;\n// after\nstd::fs::create_dir_all(\"/data/cache\")?;","handlingStrategy":"try-catch","validationCode":"// Best-effort existence check (note TOCTOU; prefer matching the error).\nif std::fs::exists(p)? {\n    // already present, skip creation\n}","typeGuard":null,"tryCatchPattern":"match std::fs::create_dir(p) {\n    Ok(()) => Ok(()),\n    Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(()),\n    Err(e) => Err(e),\n}","preventionTips":["Prefer create_dir_all over create_dir for idempotent setup.","Treat AlreadyExists as success when the directory's existence is the goal."],"tags":["rust","std","fs","uefi","platform","already-exists"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}