fs: Split out ClearCache from File

This commit is contained in:
Alexander Neumann 2016-03-28 15:31:09 +02:00
parent feb664620a
commit 5b5bb070b9
3 changed files with 11 additions and 14 deletions

View File

@ -12,7 +12,4 @@ type File interface {
io.Closer io.Closer
Stat() (os.FileInfo, error) Stat() (os.FileInfo, error)
// ClearCache removes the file's content from the OS cache.
ClearCache() error
} }

View File

@ -6,15 +6,10 @@ import "os"
// Open opens a file for reading. // Open opens a file for reading.
func Open(name string) (File, error) { func Open(name string) (File, error) {
f, err := os.OpenFile(name, os.O_RDONLY, 0) return os.OpenFile(name, os.O_RDONLY, 0)
return osFile{File: f}, err
} }
// osFile wraps an *os.File and adds a no-op ClearCache() method. // ClearCache syncs and then removes the file's content from the OS cache.
type osFile struct { func ClearCache(f File) error {
*os.File
}
func (osFile) ClearCache() error {
return nil return nil
} }

View File

@ -58,12 +58,17 @@ func (f *nonCachingFile) Read(p []byte) (int, error) {
return n, err return n, err
} }
func (f *nonCachingFile) ClearCache() error { // ClearCache syncs and then removes the file's content from the OS cache.
err := f.File.Sync() 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 { if err != nil {
return err 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)
} }