mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-31 07:30:32 +00:00
fix(cache): keep the in-place truncate on windows
Unlinking before re-creating fixes the premature-EOF spin on unix, but Windows refuses to remove a file another handle still has open and returns a sharing violation. Because Create surfaces that error, every cache miss on a path with a live reader would have failed outright - worse than the spin it was meant to fix. Split the create behind a build tag: unix unlinks for a fresh inode, Windows keeps truncating in place and stays exposed to the spin, which is the behaviour it already had. The unix-only spec is skipped there.
This commit is contained in:
parent
4b9d2178cc
commit
18f1236595
7
utils/cache/spread_fs.go
vendored
7
utils/cache/spread_fs.go
vendored
@ -128,12 +128,7 @@ func (sfs *spreadFS) Create(name string) (stream.File, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Unlink instead of truncating: an older stream may still be serving readers from
|
||||
// this path, and shrinking the file under them spins them at a premature EOF.
|
||||
if err := os.Remove(name); err != nil && !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
return os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
||||
return createDataFile(name)
|
||||
}
|
||||
|
||||
func (sfs *spreadFS) Open(name string) (stream.File, error) {
|
||||
|
||||
4
utils/cache/spread_fs_test.go
vendored
4
utils/cache/spread_fs_test.go
vendored
@ -4,6 +4,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/djherbis/stream"
|
||||
@ -45,6 +46,9 @@ var _ = Describe("Spread FS", func() {
|
||||
It("leaves an already-open reader's bytes intact", func() {
|
||||
// A re-created entry must not shrink the file an older stream is still
|
||||
// serving: its reader would spin forever at the premature EOF.
|
||||
if runtime.GOOS == "windows" {
|
||||
Skip("Windows cannot unlink a file with open handles, so Create reuses the inode")
|
||||
}
|
||||
name := filepath.Join(rootDir, "aa", "bb", "data")
|
||||
s, err := stream.NewStream(name, fs)
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
18
utils/cache/spread_fs_unix.go
vendored
Normal file
18
utils/cache/spread_fs_unix.go
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
//go:build !windows
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/djherbis/stream"
|
||||
)
|
||||
|
||||
// createDataFile unlinks instead of truncating, so a re-created entry gets a fresh
|
||||
// inode and readers still holding the old one see its full contents.
|
||||
func createDataFile(name string) (stream.File, error) {
|
||||
if err := os.Remove(name); err != nil && !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
return os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
||||
}
|
||||
15
utils/cache/spread_fs_windows.go
vendored
Normal file
15
utils/cache/spread_fs_windows.go
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
//go:build windows
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/djherbis/stream"
|
||||
)
|
||||
|
||||
// createDataFile truncates in place: Windows refuses to unlink a file another handle
|
||||
// has open, and failing the create would break every miss on a path with a live reader.
|
||||
func createDataFile(name string) (stream.File, error) {
|
||||
return os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user