{"record":{"id":"335d290fbc72f739","repo":"shadow1ng/fscan","slug":"failed-to-create-realtime-backup-file-w","errorCode":null,"errorMessage":"failed to create realtime backup file: %w","messagePattern":"failed to create realtime backup file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/output/writers.go","lineNumber":101,"sourceCode":"\tclosed       bool\n\tbuffer       *ResultBuffer // 内存分类缓冲\n\trealtimeFile *os.File      // 实时备份文件\n\trealtimePath string        // 实时备份文件路径\n}\n\n// NewTXTWriter 创建文本写入器\nfunc NewTXTWriter(filePath string) (*TXTWriter, error) {\n\tfile, err := os.OpenFile(filePath, DefaultFileFlags, DefaultFilePermissions)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create TXT file: %w\", err)\n\t}\n\n\t// 创建实时备份文件（防崩溃丢数据）\n\trealtimePath := filePath + \".realtime.tmp\"\n\trealtimeFile, err := os.OpenFile(realtimePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, DefaultFilePermissions)\n\tif err != nil {\n\t\tfile.Close()\n\t\treturn nil, fmt.Errorf(\"failed to create realtime backup file: %w\", err)\n\t}\n\n\treturn &TXTWriter{\n\t\tfile:         file,\n\t\tbufWriter:    bufio.NewWriter(file),\n\t\tbuffer:       NewResultBuffer(),\n\t\trealtimeFile: realtimeFile,\n\t\trealtimePath: realtimePath,\n\t}, nil\n}\n\n// WriteHeader 写入头部\nfunc (w *TXTWriter) WriteHeader() error {\n\treturn nil\n}\n\n// Write 收集扫描结果到分类缓冲，同时实时备份\nfunc (w *TXTWriter) Write(result *ScanResult) error {","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/shadow1ng/fscan/blob/95cc12e753bf43de7004e5aef42a9ffba3934303/common/output/writers.go#L83-L119","documentation":"After opening the main TXT file, NewTXTWriter opens a crash-safety backup file at <path>.realtime.tmp; failure to open that secondary file closes the main file and returns 'failed to create realtime backup file: %w'.","triggerScenarios":"Same causes as file creation (permissions, missing dir, disk full) but for the .realtime.tmp path; also cases where a stale .realtime.tmp exists as a directory or is locked/open exclusively by another process.","commonSituations":"Leftover .realtime.tmp from a crashed run owned by another user, tmp-cleanup tools replacing the file with a directory, concurrent scan processes writing to the same output path.","solutions":["Check the wrapped error and inspect the .realtime.tmp path for blocking files/directories","Delete stale <file>.realtime.tmp files from previous crashed runs","Ensure only one process writes to the same output path, or use per-process output files"],"exampleFix":"// before\nw, err := NewTXTWriter(\"out.txt\") // stale out.txt.realtime.tmp dir blocks\n// after\nos.RemoveAll(\"out.txt.realtime.tmp\")\nw, err := NewTXTWriter(\"out.txt\")","handlingStrategy":"validation","validationCode":"tmp := filePath + \".realtime.tmp\"\nif info, err := os.Stat(tmp); err == nil && info.IsDir() {\n    os.RemoveAll(tmp)\n}","typeGuard":null,"tryCatchPattern":"w, err := NewTXTWriter(path)\nif err != nil && strings.Contains(err.Error(), \"realtime backup\") {\n    os.Remove(path + \".realtime.tmp\")\n    w, err = NewTXTWriter(path)\n}","preventionTips":["Clean stale .realtime.tmp files after crashed runs","Ensure a single process owns each output path","Use unique output files per process/run to avoid temp-file collisions"],"tags":["output","file","temp-file"],"backgroundTag":"file-open-failed","analyzedSha":"95cc12e753bf43de7004e5aef42a9ffba3934303","analyzedAt":"2026-09-06T17:07:30.094Z","contentChangedAt":"2026-09-06T17:07:30.094Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}