yorukot/superfile · error

failed to create destination directory: %w

Error message

failed to create destination directory: %w

What it means

copyDir creates the destination directory with os.MkdirAll(dst, srcInfo.Mode()); failure is wrapped as 'failed to create destination directory'. This surfaces OS-level errors like missing parent directories that cannot be created, permission denied, or ENOSPC.

Source

Thrown at src/internal/file_operations.go:94

// copyElement handles copying of both files and directories
func copyElement(src, dst string) error {
	srcInfo, err := os.Stat(src)
	if err != nil {
		return fmt.Errorf("failed to stat source: %w", err)
	}

	if srcInfo.IsDir() {
		return copyDir(src, dst, srcInfo)
	}
	return copyFile(src, dst, srcInfo)
}

// copyDir recursively copies a directory
func copyDir(src, dst string, srcInfo os.FileInfo) error {
	err := os.MkdirAll(dst, srcInfo.Mode())
	if err != nil {
		return fmt.Errorf("failed to create destination directory: %w", err)
	}

	entries, err := os.ReadDir(src)
	if err != nil {
		return fmt.Errorf("failed to read source directory: %w", err)
	}

	for _, entry := range entries {
		srcPath := filepath.Join(src, entry.Name())
		dstPath := filepath.Join(dst, entry.Name())

		entryInfo, err := entry.Info()
		if err != nil {
			return fmt.Errorf("failed to get entry info: %w", err)
		}

		if entryInfo.IsDir() {
			err = copyDir(srcPath, dstPath, entryInfo)

View on GitHub (pinned to b72f550bc6)

Solutions

  1. Ensure the destination parent exists and is writable (mkdir -p, chown/chmod).
  2. Remove or rename a non-directory entry already occupying dst.
  3. Check the destination filesystem is not read-only or full (mount, df -h).
  4. Pick a different destination path if the name collides.

Example fix

// before
moveElement(src, "/backup/dest") // /backup/dest is a regular file
// after
rm /backup/dest            // or choose another name
moveElement(src, "/backup/dest")
Defensive patterns

Strategy: validation

Validate before calling

if st, err := os.Lstat(dst); err == nil && !st.IsDir() {
    return fmt.Errorf("destination %s exists and is not a directory", dst)
}
parent := filepath.Dir(dst)
if st, err := os.Stat(parent); err != nil || !st.IsDir() {
    return fmt.Errorf("destination parent %s missing or invalid", parent)
}
// check writability
f, err := os.CreateTemp(parent, ".probe")
if err != nil { return fmt.Errorf("parent not writable: %w", err) }
f.Close(); os.Remove(f.Name())

Type guard

func canCreateDir(dst string) bool {
    if st, err := os.Lstat(dst); err == nil && !st.IsDir() { return false }
    p := filepath.Dir(dst)
    f, err := os.CreateTemp(p, ".probe")
    if err != nil { return false }
    f.Close(); os.Remove(f.Name())
    return true
}

Try / catch

if err := copyElement(srcDir, dstDir); err != nil {
    if strings.Contains(err.Error(), "failed to create destination directory") {
        log.Errorf("Cannot create %s: %v", dstDir, err)
        return proposeAlternativeDestination(srcDir)
    }
    return err
}

Prevention

When it happens

Trigger: copyElement -> copyDir where os.MkdirAll(dst) fails: dst's parent doesn't exist and can't be created (EACCES/EROFS), dst exists as a regular file, or the filesystem is full/read-only.

Common situations: Pasting into a destination where a same-named file (not dir) exists; destination on a read-only or full volume; missing write permission on the parent directory; deep destination path on a mount that is unavailable.

Understand the failure class

Background: mkdir permission denied (EACCES): failed to create directory errors explained — this error's family across 32 libraries.

Related errors


AI-assisted analysis of yorukot/superfile@b72f550bc6 (2026-09-01). Data as JSON: /api/errors/f6041bda570e6876. Report an issue: GitHub.