From 5b5bb070b9715cbaf010695b7e7c9da85a20bc8a Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 28 Mar 2016 15:31:09 +0200 Subject: [PATCH] fs: Split out ClearCache from File --- src/restic/fs/file.go | 3 --- src/restic/fs/file_all.go | 11 +++-------- src/restic/fs/file_linux.go | 11 ++++++++--- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/restic/fs/file.go b/src/restic/fs/file.go index 84c003c5d..68a0fb07a 100644 --- a/src/restic/fs/file.go +++ b/src/restic/fs/file.go @@ -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 } diff --git a/src/restic/fs/file_all.go b/src/restic/fs/file_all.go index b3ee2ba95..fbb0c93cb 100644 --- a/src/restic/fs/file_all.go +++ b/src/restic/fs/file_all.go @@ -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 } diff --git a/src/restic/fs/file_linux.go b/src/restic/fs/file_linux.go index d79968be3..ed7793ce0 100644 --- a/src/restic/fs/file_linux.go +++ b/src/restic/fs/file_linux.go @@ -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) }