sipeed/picoclaw · error

failed to set permissions

Error message

failed to set permissions: %w

What it means

WriteFileAtomic's tmpFile.Chmod(perm) failed after write and sync: the requested permission mode (e.g. 0600 for secrets) could not be applied to the temp file. Failing here prevents renaming a file with looser permissions than intended into place.

Solutions

  1. Check ownership of the temp file and directory
  2. Verify the filesystem supports chmod (not FAT/exFAT without metadata)
  3. Retry with an appropriate umask/ownership
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at pkg/fileutil/file.go:94 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/05acb3f6ac2d78a8. Report an issue: GitHub.

Appendix: source

Thrown at pkg/fileutil/file.go:94

		}
	}()

	// Write data to temp file
	// Note: Original file is untouched at this point
	if _, err := tmpFile.Write(data); err != nil {
		return fmt.Errorf("failed to write temp file: %w", err)
	}

	// CRITICAL: Force sync to storage medium before any other operations.
	// This ensures data is physically written to disk, not just cached.
	// Essential for SD cards, eMMC, and other flash storage on edge devices.
	if err := tmpFile.Sync(); err != nil {
		return fmt.Errorf("failed to sync temp file: %w", err)
	}

	// Set file permissions before closing
	if err := tmpFile.Chmod(perm); err != nil {
		return fmt.Errorf("failed to set permissions: %w", err)
	}

	// Close file before rename (required on Windows)
	if err := tmpFile.Close(); err != nil {
		return fmt.Errorf("failed to close temp file: %w", err)
	}

	// Atomic rename: temp file becomes the target
	// On POSIX: rename() is atomic
	// On Windows: Rename() is atomic for files
	if err := os.Rename(tmpPath, path); err != nil {
		return fmt.Errorf("failed to rename temp file: %w", err)
	}

	// Sync directory to ensure rename is durable
	// This prevents the renamed file from disappearing after a crash
	if dirFile, err := os.Open(dir); err == nil {
		_ = dirFile.Sync()

View on GitHub (pinned to 49183d7e8d)