{"record":{"id":"93c9fbb2e0e0e8fb","repo":"uutils/coreutils","slug":"path-component-exists-but-is-not-a-directory","errorCode":null,"errorMessage":"path component exists but is not a directory: {}","messagePattern":"path component exists but is not a directory: (.+?)","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"src/uucore/src/lib/features/safe_traversal.rs","lineNumber":602,"sourceCode":"/// * `parent_fd` - The parent directory file descriptor\n/// * `name` - The name of the subdirectory to open or create\n/// * `mode` - The mode to use when creating a new directory\n///\n/// # Returns\n/// A DirFd for the subdirectory\nfn open_or_create_subdir(parent_fd: &DirFd, name: &OsStr, mode: u32) -> io::Result<DirFd> {\n    match parent_fd.stat_at(name, SymlinkBehavior::NoFollow) {\n        Ok(stat) => {\n            let file_type = (stat.st_mode as libc::mode_t) & libc::S_IFMT;\n            match file_type {\n                libc::S_IFDIR => parent_fd.open_subdir(name, SymlinkBehavior::NoFollow),\n                libc::S_IFLNK => {\n                    // Follow symlinks to directories (GNU coreutils behavior).\n                    // O_DIRECTORY in open_subdir ensures we only succeed if the\n                    // symlink resolves to a directory; dangling or non-dir symlinks error out.\n                    parent_fd.open_subdir(name, SymlinkBehavior::Follow)\n                }\n                _ => Err(io::Error::new(\n                    io::ErrorKind::AlreadyExists,\n                    format!(\n                        \"path component exists but is not a directory: {}\",\n                        name.display()\n                    ),\n                )),\n            }\n        }\n        Err(e) if e.kind() == io::ErrorKind::NotFound => match parent_fd.mkdir_at(name, mode) {\n            Ok(()) => parent_fd.open_subdir(name, SymlinkBehavior::NoFollow),\n            // Another process created `name` between the stat and the mkdir\n            // (issue #12355). Open what it created; O_NOFOLLOW keeps a symlink\n            // that raced into the name from being followed.\n            Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {\n                parent_fd.open_subdir(name, SymlinkBehavior::NoFollow)\n            }\n            Err(e) => Err(e),\n        },","sourceCodeStart":584,"sourceCodeEnd":620,"githubUrl":"https://github.com/uutils/coreutils/blob/92995feca29e324a7c559ede91db81c32d9f5d59/src/uucore/src/lib/features/safe_traversal.rs#L584-L620","documentation":"In safe_traversal's create_dir_all_safe, open_or_create_subdir walks path components using directory file descriptors. When an existing component's file type is neither a directory nor a symlink, it returns AlreadyExists with 'path component exists but is not a directory: {name}' to mirror GNU coreutils' mkdir -p behavior.","triggerScenarios":"create_dir_all_safe(\"a/b/c\") where `a` or `a/b` exists as a regular file, fifo, socket, etc., so opening it as a subdirectory is impossible.","commonSituations":"A file shadowing a directory that a previous run created; build scripts leaving a marker file where a directory is expected; package installs conflicting with existing files.","solutions":["Remove or rename the file occupying the component path","Use a different target directory","Check each component with metadata/symlink_metadata before creating"],"exampleFix":"// before\ncreate_dir_all_safe(\"out/data\") // out is a regular file\n// after\nif fs::symlink_metadata(\"out\")?.is_file() { fs::remove_file(\"out\")?; }\ncreate_dir_all_safe(\"out/data\");","handlingStrategy":"validation","validationCode":"fn component_is_dir(p: &Path) -> bool {\n    std::fs::metadata(p).map(|m| m.is_dir()).unwrap_or(true) // missing => ok to create\n}","typeGuard":"fn is_dir_component(p: &Path) -> Option<bool> {\n    std::fs::symlink_metadata(p).ok().map(|m| m.is_dir())\n}","tryCatchPattern":"match create_dir_all_safe(path) {\n    Err(e) if e.to_string().contains(\"exists but is not a directory\") => {\n        eprintln!(\"remove or rename the file occupying {path:?}\");\n    }\n    r => r?,\n}","preventionTips":["Don't create plain files at paths reserved for directories","Clean build output trees between runs","Check symlink_metadata on each path prefix before mkdir -p style operations"],"tags":["filesystem","mkdir","traversal","path"],"backgroundTag":"path-is-not-a-directory","analyzedSha":"92995feca29e324a7c559ede91db81c32d9f5d59","analyzedAt":"2026-09-20T12:39:35.476Z","contentChangedAt":"2026-09-20T12:39:35.476Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}