shadow1ng/fscan · error

failed to create realtime backup file: %w

Error message

failed to create realtime backup file: %w

What it means

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'.

Source

Thrown at common/output/writers.go:101

	closed       bool
	buffer       *ResultBuffer // 内存分类缓冲
	realtimeFile *os.File      // 实时备份文件
	realtimePath string        // 实时备份文件路径
}

// NewTXTWriter 创建文本写入器
func NewTXTWriter(filePath string) (*TXTWriter, error) {
	file, err := os.OpenFile(filePath, DefaultFileFlags, DefaultFilePermissions)
	if err != nil {
		return nil, fmt.Errorf("failed to create TXT file: %w", err)
	}

	// 创建实时备份文件(防崩溃丢数据)
	realtimePath := filePath + ".realtime.tmp"
	realtimeFile, err := os.OpenFile(realtimePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, DefaultFilePermissions)
	if err != nil {
		file.Close()
		return nil, fmt.Errorf("failed to create realtime backup file: %w", err)
	}

	return &TXTWriter{
		file:         file,
		bufWriter:    bufio.NewWriter(file),
		buffer:       NewResultBuffer(),
		realtimeFile: realtimeFile,
		realtimePath: realtimePath,
	}, nil
}

// WriteHeader 写入头部
func (w *TXTWriter) WriteHeader() error {
	return nil
}

// Write 收集扫描结果到分类缓冲,同时实时备份
func (w *TXTWriter) Write(result *ScanResult) error {

View on GitHub (pinned to 95cc12e753)

Solutions

  1. Check the wrapped error and inspect the .realtime.tmp path for blocking files/directories
  2. Delete stale <file>.realtime.tmp files from previous crashed runs
  3. Ensure only one process writes to the same output path, or use per-process output files

Example fix

// before
w, err := NewTXTWriter("out.txt") // stale out.txt.realtime.tmp dir blocks
// after
os.RemoveAll("out.txt.realtime.tmp")
w, err := NewTXTWriter("out.txt")
Defensive patterns

Strategy: validation

Validate before calling

tmp := filePath + ".realtime.tmp"
if info, err := os.Stat(tmp); err == nil && info.IsDir() {
    os.RemoveAll(tmp)
}

Try / catch

w, err := NewTXTWriter(path)
if err != nil && strings.Contains(err.Error(), "realtime backup") {
    os.Remove(path + ".realtime.tmp")
    w, err = NewTXTWriter(path)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.

Related errors


AI-assisted analysis of shadow1ng/fscan@95cc12e753 (2026-09-06). Data as JSON: /api/errors/335d290fbc72f739. Report an issue: GitHub.