]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: in TestFallocate, only check number of blocks on Darwin
authorCherry Mui <cherryyz@google.com>
Sat, 11 Oct 2025 14:36:49 +0000 (10:36 -0400)
committerCherry Mui <cherryyz@google.com>
Sat, 11 Oct 2025 15:51:54 +0000 (08:51 -0700)
The number-of-blocks check was introduced when fixing a Darwin-
specific bug. On Darwin, the file allocation syscall is a bit
tricky. On Linux and BSDs, it is more straightforward and unlikely
to go wrong.

The test itself, on the other hand, is less reliable on Linux (and
perhaps BSDs), as it is considered less portable and is an
implementation detail of the file system.

Given these two reasons, only check it on Darwin.

Fixes #75795.

Change-Id: I3da891fd60a141c3eca5d0f5ec20c2cad65b8862
Reviewed-on: https://go-review.googlesource.com/c/go/+/711095
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/link/internal/ld/fallocate_test.go

index 3c6b7ef752edd3d6a8a39f5f7fc3198f2ea07273..f463b5b63b3b69ffd303570e64461a78a2b90cab 100644 (file)
@@ -10,6 +10,7 @@ import (
        "errors"
        "os"
        "path/filepath"
+       "runtime"
        "syscall"
        "testing"
 )
@@ -53,12 +54,24 @@ func TestFallocate(t *testing.T) {
                if got := stat.Size(); got != sz {
                        t.Errorf("unexpected file size: got %d, want %d", got, sz)
                }
-               // The number of blocks must be enough for the requested size.
-               // We used to require an exact match, but it appears that
-               // some file systems allocate a few extra blocks in some cases.
-               // See issue #41127.
-               if got, want := stat.Sys().(*syscall.Stat_t).Blocks, (sz+511)/512; got < want {
-                       t.Errorf("unexpected disk usage: got %d blocks, want at least %d", got, want)
+               if runtime.GOOS == "darwin" {
+                       // Check the number of allocated blocks on Darwin. On Linux (and
+                       // perhaps BSDs), stat's Blocks field may not be portable as it
+                       // is an implementation detail of the file system. On Darwin, it
+                       // is documented as "the actual number of blocks allocated for
+                       // the file in 512-byte units".
+                       // The check is introduced when fixing a Darwin-specific bug. On
+                       // Darwin, the file allocation syscall is a bit tricky. On Linux
+                       // and BSDs, it is more straightforward and unlikely to go wrong.
+                       // Given these two reasons, only check it on Darwin.
+                       //
+                       // The number of blocks must be enough for the requested size.
+                       // We used to require an exact match, but it appears that
+                       // some file systems allocate a few extra blocks in some cases.
+                       // See issue #41127.
+                       if got, want := stat.Sys().(*syscall.Stat_t).Blocks, (sz+511)/512; got < want {
+                               t.Errorf("unexpected disk usage: got %d blocks, want at least %d", got, want)
+                       }
                }
                out.munmap()
        }