{"record":{"id":"72698687bf544ac3","repo":"cloudflare/cloudflared","slug":"creating-temporary-log-file-s-w","errorCode":null,"errorMessage":"creating temporary log file %s: %w","messagePattern":"creating temporary log file (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"diagnostic/log_collector_utils.go","lineNumber":83,"sourceCode":"\n\treturn NewLogInformation(outputHandle.Name(), true, false), nil\n}\n\nfunc CopyFilesFromDirectory(path string) (string, error) {\n\tconst defaultLogFilename = \"cloudflared.log\"\n\n\t// rolling logs have as suffix the current date thus\n\t// when iterating the path files they are already in\n\t// chronological order\n\tfiles, err := os.ReadDir(path)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"error reading directory %s: %w\", path, err)\n\t}\n\n\t// nolint: gosec\n\toutputHandle, err := os.Create(filepath.Join(os.TempDir(), logFilename))\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"creating temporary log file %s: %w\", logFilename, err)\n\t}\n\tdefer func() { _ = outputHandle.Close() }()\n\n\tfor _, file := range files {\n\t\t// nolint: gosec\n\t\tlogHandle, err := os.Open(filepath.Join(path, file.Name()))\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"error opening file %s: %w\", file.Name(), err)\n\t\t}\n\t\t_, err = io.Copy(outputHandle, logHandle)\n\t\t_ = logHandle.Close()\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"error copying file %s: %w\", file.Name(), err)\n\t\t}\n\t}\n\n\t// nolint: gosec\n\tlogHandle, err := os.Open(filepath.Join(path, defaultLogFilename))","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/diagnostic/log_collector_utils.go#L65-L101","documentation":"Wrapped os.Create failure in diagnostic CopyFilesFromDirectory: the temporary merged log file (cloudflared.log in the OS temp dir) that individual rolling logs are concatenated into could not be created, so log collection aborts.","triggerScenarios":"os.Create(filepath.Join(os.TempDir(), logFilename)) fails: /tmp is full, read-only, or the process lacks write permission; TMPDIR points somewhere invalid.","commonSituations":"Containers with read-only /tmp or tiny tmpfs; restrictive TMPDIR; disk-quota exhaustion during diagnostics.","solutions":["Check disk space and write permissions on os.TempDir() (TMPDIR)","Free space or point TMPDIR at a writable location","Re-run the diagnostic collection"],"exampleFix":"// before\nTMPDIR=/ro/mount cloudflared diagnostic collect\n// after\nTMPDIR=/var/tmp cloudflared diagnostic collect","handlingStrategy":"try-catch","validationCode":"if err := checkWritable(os.TempDir()); err != nil { os.Setenv(\"TMPDIR\", \"/var/tmp\") }","typeGuard":null,"tryCatchPattern":"out, err := diagnostic.CopyFilesFromDirectory(ctx, fs, logDir, logFilename)\nif err != nil && strings.Contains(err.Error(), \"creating temporary log file\") {\n\treturn fmt.Errorf(\"cannot write temp logs: %w — check disk space/TMPDIR\", err)\n}","preventionTips":["Ensure /tmp (or TMPDIR) is writable and has headroom","Avoid read-only tmpfs in containers used for diagnostics","Clean stale temp files periodically"],"tags":["filesystem","temp-file","diagnostics"],"backgroundTag":"file-write-permission-denied","analyzedSha":"2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f","analyzedAt":"2026-09-06T04:14:33.757Z","contentChangedAt":"2026-09-06T04:14:33.757Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}