Skip to content

Commit

Permalink
Rename DiffFiles to AreFilesIdentical
Browse files Browse the repository at this point in the history
Renames file-op utility DiffFiles to AreFilesIdentical
and changes it to return bool instead of int.
  • Loading branch information
gargnitingoogle committed Sep 13, 2023
1 parent a88c9bf commit 399be1e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
4 changes: 2 additions & 2 deletions tools/integration_tests/gzip/read_gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func verifyFileSizeAndFullFileRead(t *testing.T, filename string) {

defer operations.RemoveFile(localCopy)

diff, err := operations.DiffFiles(localCopy, mountedFilePath)
if diff != 0 {
diff, err := operations.AreFilesIdentical(localCopy, mountedFilePath)
if !diff {
t.Fatalf("Tempfile (%s, download of GCS object %s) didn't match the Mounted local file (%s): %v", localCopy, gcsObjectPath, mountedFilePath, err)
}
}
Expand Down
35 changes: 17 additions & 18 deletions tools/integration_tests/util/operations/file_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,46 +315,45 @@ func StatFile(file string) (*fs.FileInfo, error) {
// If sizes match, then compares the contents of both the files.
// Not a good idea for very large files as it loads both the files' contents in
// the memory completely.
// Returns 0 if no error and files match.
// Returns 1 if files don't match and captures reason for mismatch in err.
// Returns 2 if any error.
func DiffFiles(filepath1, filepath2 string) (int, error) {
// Returns true if no error and files match.
// Returns false if files don't match (captures reason for mismatch in err) or if any other error.
func AreFilesIdentical(filepath1, filepath2 string) (bool, error) {
if filepath1 == "" || filepath2 == "" {
return 2, fmt.Errorf("one or both files being diff'ed have empty path")
return false, fmt.Errorf("one or both files being diff'ed have empty path")
} else if filepath1 == filepath2 {
return 0, nil
return true, nil
}

fstat1, err := StatFile(filepath1)
if err != nil {
return 2, err
return false, err
}

fstat2, err := StatFile(filepath2)
if err != nil {
return 2, err
return false, err
}

file1size := (*fstat1).Size()
file2size := (*fstat2).Size()
if file1size != file2size {
return 1, fmt.Errorf("files don't match in size: %s (%d bytes), %s (%d bytes)", filepath1, file1size, filepath2, file2size)
return false, fmt.Errorf("files don't match in size: %s (%d bytes), %s (%d bytes)", filepath1, file1size, filepath2, file2size)
}

if file1size == 0 {
return 0, nil
return true, nil
}

f1, err := os.OpenFile(filepath1, os.O_RDONLY|syscall.O_DIRECT, FilePermission_0400)
if err != nil {
return 2, fmt.Errorf("failed to read file %s: %v", filepath1, err)
return false, fmt.Errorf("failed to read file %s: %v", filepath1, err)
}

defer CloseFile(f1)

f2, err := os.OpenFile(filepath2, os.O_RDONLY|syscall.O_DIRECT, FilePermission_0400)
if err != nil {
return 2, fmt.Errorf("failed to read file %s: %v", filepath2, err)
return false, fmt.Errorf("failed to read file %s: %v", filepath2, err)
}

defer CloseFile(f2)
Expand All @@ -372,26 +371,26 @@ func DiffFiles(filepath1, filepath2 string) (int, error) {

numBytesRead1, err := f1.Read(b1)
if err != nil {
return 2, fmt.Errorf("failed to read file %s: %v", filepath1, err)
return false, fmt.Errorf("failed to read file %s: %v", filepath1, err)
} else if numBytesRead1 != numBytesBeingRead {
return 2, fmt.Errorf("failed to read file %s, expected read bytes = %d, actual read bytes = %d", filepath1, numBytesBeingRead, numBytesRead1)
return false, fmt.Errorf("failed to read file %s, expected read bytes = %d, actual read bytes = %d", filepath1, numBytesBeingRead, numBytesRead1)
}

numBytesRead2, err := f2.Read(b2)
if err != nil {
return 2, fmt.Errorf("failed to read file %s", filepath2)
return false, fmt.Errorf("failed to read file %s", filepath2)
} else if numBytesRead2 != numBytesBeingRead {
return 2, fmt.Errorf("failed to read file %s, expected read bytes = %d, actual read bytes = %d", filepath2, numBytesBeingRead, numBytesRead2)
return false, fmt.Errorf("failed to read file %s, expected read bytes = %d, actual read bytes = %d", filepath2, numBytesBeingRead, numBytesRead2)
}

if !bytes.Equal(b1[:numBytesBeingRead], b2[:numBytesBeingRead]) {
return 1, fmt.Errorf("files don't match in content: %s, %s", filepath1, filepath2)
return false, fmt.Errorf("files don't match in content: %s, %s", filepath1, filepath2)
}

sizeRemaining -= numBytesBeingRead
}

return 0, nil
return true, nil
}

// Returns size of a give GCS object with path (without 'gs://').
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func compareFileFromGCSBucketAndMntDir(gcsFile, mntDirFile, localFilePathToDownl

// DiffFiles loads the entire files into memory. These are both 500 MiB files, hence would have a 1 GiB
// requirement just for this step
diff, err := operations.DiffFiles(mntDirFile, localFilePathToDownloadGcsFile)
if diff != 0 {
diff, err := operations.AreFilesIdentical(mntDirFile, localFilePathToDownloadGcsFile)
if !diff {
err = fmt.Errorf("Download of GCS object %s didn't match the Mounted local file (%s): %v", localFilePathToDownloadGcsFile, mntDirFile, err)
return
}
Expand Down

0 comments on commit 399be1e

Please sign in to comment.