hyperledger/fabric · error

error creating temp file in directory '%s'

Error message

error creating temp file in directory '%s'

What it means

WriteFile creates a uniquely-named temporary file (prefix .ccpackage.) inside the target directory via os.CreateTemp before atomically renaming it. If the OS refuses to create that temp file, the underlying error is wrapped with this message.

Source

Thrown at core/chaincode/persistence/persistence.go:50

	Remove(name string) error
	WriteFile(string, string, []byte) error
	MakeDir(string, os.FileMode) error
	Exists(path string) (bool, error)
}

// FilesystemIO is the production implementation of the IOWriter interface
type FilesystemIO struct{}

// WriteFile writes a file to the filesystem; it does so atomically
// by first writing to a temp file and then renaming the file so that
// if the operation crashes midway we're not stuck with a bad package
func (f *FilesystemIO) WriteFile(path, name string, data []byte) error {
	if path == "" {
		return errors.New("empty path not allowed")
	}
	tmpFile, err := os.CreateTemp(path, ".ccpackage.")
	if err != nil {
		return errors.Wrapf(err, "error creating temp file in directory '%s'", path)
	}
	defer os.Remove(tmpFile.Name())

	if n, err := tmpFile.Write(data); err != nil || n != len(data) {
		if err == nil {
			err = errors.Errorf(
				"failed to write the entire content of the file, expected %d, wrote %d",
				len(data), n,
			)
		}
		return errors.Wrapf(err, "error writing to temp file '%s'", tmpFile.Name())
	}

	if err := tmpFile.Close(); err != nil {
		return errors.Wrapf(err, "error closing temp file '%s'", tmpFile.Name())
	}

	if err := os.Rename(tmpFile.Name(), filepath.Join(path, name)); err != nil {

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Ensure the target directory exists and is owned/writable by the peer user (e.g. `chown -R peer:peer /var/hyperledger/production/lifecycle/chaincodes`)
  2. Check disk space (`df -h`) and mount status of the volume
  3. Call NewStore/Initialize first so MakeDir creates the directory with 0750 before writing
  4. Review the wrapped underlying OS error for the exact cause (ENOENT vs EACCES vs ENOSPC)

Example fix

// before
store := &persistence.Store{Path: p, ReadWriter: &persistence.FilesystemIO{}} // dir may not exist

// after
store, err := persistence.NewStore(p, &persistence.FilesystemIO{}) // Initialize() creates the dir
Defensive patterns

Strategy: validation

Validate before calling

func ensureWritableDir(p string) error {
    info, err := os.Stat(p)
    if os.IsNotExist(err) { return errors.New("install directory does not exist") }
    if !info.IsDir() { return errors.New("install path is not a directory") }
    if err := unix.Access(p, unix.W_OK); err != nil { return errors.New("install directory not writable") }
    return nil
}

Try / catch

if err := io.WriteFile(path, name, data); err != nil {
    var werr *os.PathError
    if errors.As(err, &werr) && errors.Is(werr.Err, syscall.EACCES) {
        return fmt.Errorf("fix permissions on %s: %w", path, err)
    }
    return err
}

Prevention

When it happens

Trigger: os.CreateTemp(path, ".ccpackage.") fails because the directory does not exist, is not writable by the peer process, or a filesystem/permission/quota error occurs.

Common situations: Peer running as non-root while the chaincode install directory is owned by root; disk full; the _lifecycle chaincodes directory was never created (Initialization skipped) or was deleted; read-only mounted volume (e.g. container filesystem).

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04). Data as JSON: /api/errors/144f056abd981447. Report an issue: GitHub.