Fix macOS osAllocate.

Mozilla is just wrong.
https://searchfox.org/mozilla-central/source/xpcom/glue/FileUtils.cpp
This commit is contained in:
Nuno Cruces
2023-12-17 05:10:55 +00:00
parent c938577763
commit b0b27439b5
2 changed files with 54 additions and 3 deletions

View File

@@ -39,12 +39,11 @@ func osAllocate(file *os.File, size int64) error {
return nil
}
// https://stackoverflow.com/a/11497568/867786
store := unix.Fstore_t{
Flags: unix.F_ALLOCATECONTIG,
Flags: unix.F_ALLOCATEALL | unix.F_ALLOCATECONTIG,
Posmode: unix.F_PEOFPOSMODE,
Offset: 0,
Length: size,
Length: size - off,
}
// Try to get a continuous chunk of disk space.

52
vfs/os_unix_test.go Normal file
View File

@@ -0,0 +1,52 @@
//go:build unix && !sqlite3_flock && !sqlite3_nosys
package vfs
import (
"crypto/rand"
"io"
"os"
"runtime"
"testing"
"golang.org/x/sys/unix"
)
func Test_osAllocate(t *testing.T) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skip()
}
f, err := os.CreateTemp(t.TempDir(), "file")
if err != nil {
t.Fatal(err)
}
_, err = io.CopyN(f, rand.Reader, 1024*1024)
if err != nil {
t.Fatal(err)
}
n, err := f.Seek(0, io.SeekEnd)
if err != nil {
t.Fatal(err)
}
if n != 1024*1024 {
t.Fatalf("got %d, want %d", n, 1024*1024)
}
err = osAllocate(f, 16*1024*1024)
if err != nil {
t.Fatal(err)
}
var stat unix.Stat_t
err = unix.Stat(f.Name(), &stat)
if err != nil {
t.Fatal(err)
}
if stat.Blocks*512 != 16*1024*1024 {
t.Fatalf("got %d, want %d", stat.Blocks*512, 16*1024*1024)
}
}