MCPcopy Create free account
hub / github.com/cloudquery/cloudquery / appendToFile

Function appendToFile

cli/cmd/summary.go:68–87  ·  view source on GitHub ↗
(fileName string, data []byte)

Source from the content-addressed store, hash-verified

66}
67
68func appendToFile(fileName string, data []byte) error {
69 f, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
70 if err != nil {
71 // Some filesystems (e.g. FUSE-mounted cloud storage) reject O_APPEND
72 // at open time. Fall back to read + rewrite.
73 return appendToFileFallback(fileName, data)
74 }
75 n, writeErr := f.Write(data)
76 closeErr := f.Close()
77 if writeErr != nil {
78 if n > 0 {
79 // Partial write: some bytes were already persisted, so falling back
80 // to read + rewrite would duplicate them. Treat as non-recoverable.
81 return fmt.Errorf("partial write to %s (%d/%d bytes): %w", fileName, n, len(data), writeErr)
82 }
83 // Zero bytes written — safe to retry via read + rewrite.
84 return appendToFileFallback(fileName, data)
85 }
86 return closeErr
87}
88
89// appendToFileFallback appends data by reading the existing file, then rewriting
90// with the new data added. This is used as a fallback when O_APPEND is not

Callers 2

persistSummaryFunction · 0.85

Calls 4

appendToFileFallbackFunction · 0.85
ErrorfMethod · 0.80
WriteMethod · 0.65
CloseMethod · 0.65

Tested by 1