{"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":586,"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":568,"sourceCodeEnd":604,"githubUrl":"https://github.com/uutils/coreutils/blob/85295bbf788bfd7a6926ba692031563504b304b7/src/uucore/src/lib/features/safe_traversal.rs#L568-L604","documentation":"In safe traversal, `open_or_create_subdir` inspects each path component's file type. If the component exists but is neither a directory nor a symlink, it returns `AlreadyExists` with \"path component exists but is not a directory: {name}\", because the traversal expected to descend into (or create) a directory there.","triggerScenarios":"Calling `create_dir_all_safe` on a path where an intermediate component is a regular file, FIFO, socket, etc. (e.g. path `a/b/c` where `a/b` is a regular file).","commonSituations":"A file was created where a directory was expected, leftover state from a previous run, typo'd paths colliding with an existing file name.","solutions":["Remove/rename the non-directory component so the directory can be created","Use a different target path","Check the existing path type and reconcile it before traversal"],"exampleFix":"# before\nmkdir -p out/data   # out/data is a regular file\n# after\nmv out/data out/data.bak && mkdir -p out/data","handlingStrategy":"validation","validationCode":"for ancestor in target.ancestors().skip(1) {\n    if ancestor.exists() && !ancestor.is_dir() {\n        return Err(format!(\"{} exists and is not a directory\", ancestor.display()));\n    }\n}","typeGuard":"fn component_is_dir(p: &Path) -> bool {\n    p.symlink_metadata().map(|m| m.is_dir()).unwrap_or(false)\n}","tryCatchPattern":"match create_dir_all_safe(&target) {\n    Err(e) if e.kind() == io::ErrorKind::AlreadyExists\n        && e.to_string().contains(\"not a directory\") => {\n        eprintln!(\"{} blocks directory creation\", target.display());\n    }\n    other => other?,\n}","preventionTips":["Check intermediate path components before mkdir -p style logic","Clean stale state between runs","Avoid mixing file and directory names at the same path"],"tags":["filesystem","traversal","mkdir"],"backgroundTag":"not-a-directory","analyzedSha":"85295bbf788bfd7a6926ba692031563504b304b7","analyzedAt":"2026-08-31T11:11:36.175Z","contentChangedAt":"2026-08-31T11:11:36.175Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}