Skip to content

Commit

Permalink
feat(io): compare actual size in CheckIsFileSizeSame
Browse files Browse the repository at this point in the history
longhorn/longhorn-4105

Signed-off-by: Chin-Ya Huang <[email protected]>
  • Loading branch information
c3y1huang committed Jul 1, 2024
1 parent d78642c commit eeff2ce
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions io/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ func IsDirectoryEmpty(directory string) (bool, error) {
return false, nil
}

// CheckIsFileSizeSame verifies if all files in the provided paths have the same size.
// CheckIsFileSizeSame verifies if all files in the provided paths have the same
// apparent and actual size.
// It returns an error if any file is a directory, does not exist, or has a different size.
func CheckIsFileSizeSame(paths ...string) error {
referenceInfo, err := os.Stat(paths[0])
Expand All @@ -339,7 +340,12 @@ func CheckIsFileSizeSame(paths ...string) error {
return errors.Errorf("file %v is a directory", paths[0])
}

referenceSize := referenceInfo.Size()
referenceApparentSize := referenceInfo.Size()

referenceActualSize, err := getFileBlockSizeEstimate(paths[0])
if err != nil {
return err

Check warning on line 347 in io/file.go

View check run for this annotation

Codecov / codecov/patch

io/file.go#L347

Added line #L347 was not covered by tests
}

for _, path := range paths {
fileInfo, err := os.Stat(path)
Expand All @@ -352,10 +358,28 @@ func CheckIsFileSizeSame(paths ...string) error {

}

if fileInfo.Size() != referenceSize {
return errors.Errorf("file %v size %v is not equal to %v", path, fileInfo.Size(), referenceSize)
if fileInfo.Size() != referenceApparentSize {
return errors.Errorf("file %v apparent size %v is not equal to %v", path, fileInfo.Size(), referenceApparentSize)
}

actualSize, err := getFileBlockSizeEstimate(path)
if err != nil {
return err

Check warning on line 367 in io/file.go

View check run for this annotation

Codecov / codecov/patch

io/file.go#L367

Added line #L367 was not covered by tests
}

if actualSize != referenceActualSize {
return errors.Errorf("file %v actual size %v is not equal to %v", path, actualSize, referenceActualSize)

Check warning on line 371 in io/file.go

View check run for this annotation

Codecov / codecov/patch

io/file.go#L371

Added line #L371 was not covered by tests
}
}

return nil
}

func getFileBlockSizeEstimate(path string) (uint64, error) {
var stat syscall.Stat_t
if err := syscall.Stat(path, &stat); err != nil {
return 0, err

Check warning on line 381 in io/file.go

View check run for this annotation

Codecov / codecov/patch

io/file.go#L381

Added line #L381 was not covered by tests
}

return uint64(stat.Blocks) * uint64(stat.Blksize), nil
}

0 comments on commit eeff2ce

Please sign in to comment.