{"record":{"id":"1d9dfc2ef2864638","repo":"vxcontrol/pentagi","slug":"failed-to-create-temp-directory-w","errorCode":null,"errorMessage":"failed to create temp directory: %w","messagePattern":"failed to create temp directory: %w","errorType":"exception","errorClass":null,"httpStatus":500,"severity":"error","filePath":"backend/pkg/resources/resources.go","lineNumber":275,"sourceCode":"\t\treturn rest\n\t}\n\treturn newPrefix + \"/\" + rest\n}\n\n// EscapeLike escapes special LIKE pattern characters (%, _) in s so the string\n// can be safely embedded in a SQL LIKE clause.\nfunc EscapeLike(s string) string {\n\ts = strings.ReplaceAll(s, `\\`, `\\\\`)\n\ts = strings.ReplaceAll(s, `%`, `\\%`)\n\ts = strings.ReplaceAll(s, `_`, `\\_`)\n\treturn s\n}\n\n// SaveToTemp writes r into a new temporary file in dir and returns the path to\n// the temp file. The caller is responsible for removing the temp file on error.\nfunc SaveToTemp(r io.Reader, dir string) (tmpPath string, hash string, size int64, err error) {\n\tif err := os.MkdirAll(dir, 0755); err != nil {\n\t\treturn \"\", \"\", 0, fmt.Errorf(\"failed to create temp directory: %w\", err)\n\t}\n\n\ttmp, err := os.CreateTemp(dir, \".resource-upload-*\")\n\tif err != nil {\n\t\treturn \"\", \"\", 0, fmt.Errorf(\"failed to create temp file: %w\", err)\n\t}\n\ttmpPath = tmp.Name()\n\tdefer tmp.Close()\n\n\th := md5.New()\n\tmw := io.MultiWriter(tmp, h)\n\twritten, copyErr := io.Copy(mw, r)\n\tif copyErr != nil {\n\t\tos.Remove(tmpPath)\n\t\treturn \"\", \"\", 0, fmt.Errorf(\"failed to write temp file: %w\", copyErr)\n\t}\n\tif err := tmp.Chmod(0644); err != nil {\n\t\tos.Remove(tmpPath)","sourceCodeStart":257,"sourceCodeEnd":293,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/resources/resources.go#L257-L293","documentation":"SaveToTemp creates the target directory with os.MkdirAll(dir, 0755) before writing; this error wraps whatever MkdirAll returned. It means the temp directory could not be created or verified, typically due to filesystem permissions or an invalid path.","triggerScenarios":"Calling SaveToTemp with a dir whose parent is read-only, which is a file rather than a directory, sits on a full/read-only volume, or uses characters invalid for the filesystem. Raised via UploadResources or direct calls.","commonSituations":"Container running as non-root writing to a root-owned path; Docker volume mounted read-only; NFS/permission issues after deployment; a configured resources directory pointing at a file after a bad env var edit.","solutions":["Check permissions on dir and its parents (ls -ld) and chown/chmod so the service user can write","Verify the configured directory path is correct and not accidentally pointing at a file","Check disk space and mount status (df -h, mount) for read-only or full volumes","Create the directory manually with correct ownership as a quick unblock, then fix provisioning"],"exampleFix":"// before\ndir := \"/var/lib/pentagi/resources\" // root-owned\npath, _, _, err := resources.SaveToTemp(r, dir)\n// after\nif err := os.MkdirAll(dir, 0755); err != nil {\n    log.Fatalf(\"cannot use resources dir %s: %v\", dir, err)\n}\npath, _, _, err := resources.SaveToTemp(r, dir)","handlingStrategy":"try-catch","validationCode":"func canWriteDir(dir string) error {\n\tinfo, err := os.Stat(dir)\n\tif err == nil && !info.IsDir() {\n\t\treturn fmt.Errorf(\"%s is a file, not a directory\", dir)\n\t}\n\tif err := os.MkdirAll(dir, 0755); err != nil {\n\t\treturn err\n\t}\n\tprobe := filepath.Join(dir, \".write-probe\")\n\tif err := os.WriteFile(probe, nil, 0644); err != nil {\n\t\treturn err\n\t}\n\treturn os.Remove(probe)\n}","typeGuard":null,"tryCatchPattern":"tmpPath, hash, size, err := resources.SaveToTemp(r, dir)\nif err != nil {\n\tvar perr *fs.PathError\n\tif errors.As(err, &perr) && errors.Is(perr.Err, syscall.EACCES) {\n\t\treturn fmt.Errorf(\"resources dir %s not writable: %w\", dir, err)\n\t}\n\treturn err\n}","preventionTips":["Probe directory writability at service startup, not on first upload","Run the service as a user that owns the resources directory","Ensure volumes are mounted rw and verify after deployment","Alert on disk/inode exhaustion for the volume hosting the temp dir"],"tags":["filesystem","permissions","io"],"backgroundTag":"mkdir-permission-denied","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}