OpenNHP/opennhp · error

file not found

Error message

file not found: %w

What it means

Md5sum computes an MD5 checksum of a file for integrity verification. Before reading, it calls os.Stat on the given path; if Stat fails the file cannot be located or accessed, and the underlying error is wrapped as "file not found: %w". This gives callers a single descriptive error whether the path is missing, is a permission issue, or has a bad parent directory.

Solutions

  1. Verify the path exists: run ls -l on the exact fullFilePath used by the caller
  2. Fix the configured path (config file or hardcoded value) to the correct absolute path
  3. Check that the process user has search permission on every directory component of the path
  4. If the path should be relative, set the correct working directory or convert it to an absolute path

Example fix

// before
sum, err := utils.Md5sum(cfg.TaFile)
// after
if _, err := os.Stat(cfg.TaFile); err != nil {
    log.Error("TA file %q not accessible: %v", cfg.TaFile, err)
    return err
}
sum, err := utils.Md5sum(cfg.TaFile)
Defensive patterns

Strategy: validation

Validate before calling

if info, err := os.Stat(p); err != nil || !info.Mode().IsRegular() { return err }

Prevention

When it happens

Trigger: Calling utils.Md5sum with a path that does not exist, a dangling symlink, a typo in the filename, or a path whose parent directory is inaccessible so os.Stat returns an error.

Common situations: TA service registration passes a plugin/TA file path from config that was moved or never deployed; relative path used while the process runs from a different working directory; case-sensitivity mismatch on Linux filesystems.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of OpenNHP/opennhp@6e04ca5ff0 (2026-09-07). Data as JSON: /api/errors/d9a06219a08a0e3d. Report an issue: GitHub.

Appendix: source

Thrown at nhp/utils/crypto.go:127

func GenerateRsaKey(bits int) (string, string) {
	// Generate private key.
	privateKey, err := rsa.GenerateKey(rand.Reader, bits)
	if err != nil {
		return "", ""
	}
	pivKey := x509.MarshalPKCS1PrivateKey(privateKey)
	pubKey := x509.MarshalPKCS1PublicKey(&privateKey.PublicKey)

	return base64.StdEncoding.EncodeToString(pivKey), base64.StdEncoding.EncodeToString(pubKey)
}

// Md5sum computes MD5 checksum for file integrity verification (not cryptographic security)
//
//nolint:gosec // G401: MD5 used for file integrity checksums, not for cryptographic security
func Md5sum(fullFilePath string) (string, error) {
	fileInfo, err := os.Stat(fullFilePath)
	if err != nil {
		return "", fmt.Errorf("file not found: %w", err)
	}

	if !fileInfo.Mode().IsRegular() {
		return "", fmt.Errorf("path is not a regular file")
	}

	file, err := os.Open(fullFilePath) //nolint:gosec // G304: Path validated by os.Stat above
	if err != nil {
		return "", fmt.Errorf("failed to open file: %w", err)
	}
	defer file.Close()

	hasher := md5.New()

	if _, err := io.Copy(hasher, file); err != nil {
		return "", fmt.Errorf("failed to read file content: %w", err)
	}

View on GitHub (pinned to 6e04ca5ff0)