Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion pkg/dotc1z/manager/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,36 @@ func (l *localManager) copyFileToTmp(ctx context.Context) error {
}
defer f.Close()

_, err = io.Copy(tmp, f)
// Get source file size for verification
sourceStat, err := f.Stat()
if err != nil {
return fmt.Errorf("failed to stat source file: %w", err)
}
expectedSize := sourceStat.Size()

written, err := io.Copy(tmp, f)
if err != nil {
return err
}

// CRITICAL: Sync to ensure all data is written before file is used.
// This is especially important on ZFS ARC where writes may be cached
// and reads can happen before buffers are flushed to disk.
if err := tmp.Sync(); err != nil {
return fmt.Errorf("failed to sync temp file: %w", err)
}

// Verify file size matches what we wrote (defensive check)
stat, err := tmp.Stat()
if err != nil {
return fmt.Errorf("failed to stat temp file: %w", err)
}
if stat.Size() != written {
return fmt.Errorf("file size mismatch: wrote %d bytes but file is %d bytes", written, stat.Size())
}
if written != expectedSize {
return fmt.Errorf("copy size mismatch: expected %d bytes from source but copied %d bytes", expectedSize, written)
}
}

return nil
Expand Down Expand Up @@ -147,6 +173,12 @@ func (l *localManager) SaveC1Z(ctx context.Context) error {
return err
}

// CRITICAL: Sync to ensure data is written before function returns.
// This is especially important on ZFS ARC where writes may be cached.
if err := dstFile.Sync(); err != nil {
return fmt.Errorf("failed to sync destination file: %w", err)
}

log.Debug(
"successfully saved c1z locally",
zap.String("file_path", l.filePath),
Expand Down
21 changes: 20 additions & 1 deletion pkg/dotc1z/manager/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,30 @@ func (s *s3Manager) copyToTempFile(ctx context.Context, r io.Reader) error {
s.tmpFile = f.Name()

if r != nil {
_, err = io.Copy(f, r)
written, err := io.Copy(f, r)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We call io.copy() in local manager too. Should we error if the bytes written in there don't match? I think we should either check the size vs bytes written and error in both cases, or error in neither case, not just the s3 case.

if err != nil {
_ = f.Close()
return err
}

// CRITICAL: Sync to ensure all data is written before file is used.
// This is especially important on ZFS ARC where writes may be cached
// and reads can happen before buffers are flushed to disk.
if err := f.Sync(); err != nil {
_ = f.Close()
return fmt.Errorf("failed to sync temp file: %w", err)
}

// Verify file size matches what we wrote (defensive check)
stat, err := f.Stat()
if err != nil {
_ = f.Close()
return fmt.Errorf("failed to stat temp file: %w", err)
}
if stat.Size() != written {
_ = f.Close()
return fmt.Errorf("file size mismatch: wrote %d bytes but file is %d bytes", written, stat.Size())
}
}

return nil
Expand Down
Loading