fs: Split out ClearCache from File
This commit is contained in:
parent
feb664620a
commit
5b5bb070b9
3 changed files with 11 additions and 14 deletions
|
@ -12,7 +12,4 @@ type File interface {
|
|||
io.Closer
|
||||
|
||||
Stat() (os.FileInfo, error)
|
||||
|
||||
// ClearCache removes the file's content from the OS cache.
|
||||
ClearCache() error
|
||||
}
|
||||
|
|
|
@ -6,15 +6,10 @@ import "os"
|
|||
|
||||
// Open opens a file for reading.
|
||||
func Open(name string) (File, error) {
|
||||
f, err := os.OpenFile(name, os.O_RDONLY, 0)
|
||||
return osFile{File: f}, err
|
||||
return os.OpenFile(name, os.O_RDONLY, 0)
|
||||
}
|
||||
|
||||
// osFile wraps an *os.File and adds a no-op ClearCache() method.
|
||||
type osFile struct {
|
||||
*os.File
|
||||
}
|
||||
|
||||
func (osFile) ClearCache() error {
|
||||
// ClearCache syncs and then removes the file's content from the OS cache.
|
||||
func ClearCache(f File) error {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -58,12 +58,17 @@ func (f *nonCachingFile) Read(p []byte) (int, error) {
|
|||
return n, err
|
||||
}
|
||||
|
||||
func (f *nonCachingFile) ClearCache() error {
|
||||
err := f.File.Sync()
|
||||
// ClearCache syncs and then removes the file's content from the OS cache.
|
||||
func ClearCache(file File) error {
|
||||
f, ok := file.(*os.File)
|
||||
if !ok {
|
||||
panic("ClearCache called for file not *os.File")
|
||||
}
|
||||
|
||||
err := f.Sync()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return unix.Fadvise(int(f.File.Fd()), 0, 0, _POSIX_FADV_DONTNEED)
|
||||
return unix.Fadvise(int(f.Fd()), 0, 0, _POSIX_FADV_DONTNEED)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue