sipeed/picoclaw · error

failed to create directory

Error message

failed to create directory: %w

What it means

WriteFileAtomic wraps the os.MkdirAll failure for the target file's parent directory: the directory could not be created (permission denied, a path component is an existing file, read-only filesystem). The write is aborted before any temp file is made, leaving the original file untouched.

Solutions

  1. Check write permissions on the parent directory path
  2. Ensure no path component is an existing regular file
  3. Resolve mount/read-only issues (e.g. remount rw) before retrying
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at pkg/fileutil/file.go:55 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of sipeed/picoclaw@49183d7e8d (2026-08-15). Data as JSON: /api/errors/a38394c181c7eb8d. Report an issue: GitHub.

Appendix: source

Thrown at pkg/fileutil/file.go:55

// Parameters:
//   - path: Target file path
//   - data: Data to write
//   - perm: File permission mode (e.g., 0o600 for secure, 0o644 for readable)
//
// Returns:
//   - Error if any step fails, nil on success
//
// Example:
//
//	// Secure config file (owner read/write only)
//	err := utils.WriteFileAtomic("config.json", data, 0o600)
//
//	// Public readable file
//	err := utils.WriteFileAtomic("public.txt", data, 0o644)
func WriteFileAtomic(path string, data []byte, perm os.FileMode) error {
	dir := filepath.Dir(path)
	if err := os.MkdirAll(dir, 0o755); err != nil {
		return fmt.Errorf("failed to create directory: %w", err)
	}

	// Create temp file in the same directory (ensures atomic rename works)
	// Using a hidden prefix (.tmp-) to avoid issues with some tools
	tmpFile, err := os.OpenFile(
		filepath.Join(dir, fmt.Sprintf(".tmp-%d-%d", os.Getpid(), time.Now().UnixNano())),
		os.O_WRONLY|os.O_CREATE|os.O_EXCL,
		perm,
	)
	if err != nil {
		return fmt.Errorf("failed to create temp file: %w", err)
	}

	tmpPath := tmpFile.Name()
	cleanup := true

	defer func() {
		if cleanup {

View on GitHub (pinned to 49183d7e8d)