2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-24 11:55:28 +00:00

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
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.
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
}

View File

@ -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)
}