2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-05 04:47:51 +00:00

Merge pull request #670 from restic/remove-fadvise

Remove fadvise
This commit is contained in:
Alexander Neumann 2016-11-10 23:42:21 +01:00
commit 56009dd16e
4 changed files with 5 additions and 79 deletions

View File

@ -157,11 +157,6 @@ func writeToTempfile(tempdir string, p []byte) (filename string, err error) {
return "", errors.Wrap(err, "Syncn") return "", errors.Wrap(err, "Syncn")
} }
err = fs.ClearCache(tmpfile)
if err != nil {
return "", errors.Wrap(err, "ClearCache")
}
err = tmpfile.Close() err = tmpfile.Close()
if err != nil { if err != nil {
return "", errors.Wrap(err, "Close") return "", errors.Wrap(err, "Close")

View File

@ -125,6 +125,11 @@ func Create(name string) (*os.File, error) {
return os.Create(fixpath(name)) return os.Create(fixpath(name))
} }
// Open opens a file for reading.
func Open(name string) (File, error) {
return os.Open(fixpath(name))
}
// OpenFile is the generalized open call; most users will use Open // OpenFile is the generalized open call; most users will use Open
// or Create instead. It opens the named file with specified flag // or Create instead. It opens the named file with specified flag
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,

View File

@ -1,59 +0,0 @@
// +build linux,go1.4
package fs
import (
"os"
"syscall"
"restic/errors"
"golang.org/x/sys/unix"
)
// Open opens a file for reading, without updating the atime and without caching data on read.
func Open(name string) (File, error) {
file, err := os.OpenFile(name, os.O_RDONLY|syscall.O_NOATIME, 0)
if os.IsPermission(errors.Cause(err)) {
file, err = os.OpenFile(name, os.O_RDONLY, 0)
}
return &nonCachingFile{File: file}, err
}
// nonCachingFile wraps an *os.File and calls fadvise() to instantly forget
// data that has been read or written.
type nonCachingFile struct {
*os.File
readOffset int64
}
func (f *nonCachingFile) Read(p []byte) (int, error) {
n, err := f.File.Read(p)
if n > 0 {
ferr := unix.Fadvise(int(f.File.Fd()), f.readOffset, int64(n), unix.FADV_DONTNEED)
f.readOffset += int64(n)
if err == nil {
err = ferr
}
}
return n, err
}
// 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.Fd()), 0, 0, unix.FADV_DONTNEED)
}

View File

@ -1,15 +0,0 @@
// +build !linux !go1.4
package fs
import "os"
// Open opens a file for reading.
func Open(name string) (File, error) {
return os.OpenFile(fixpath(name), os.O_RDONLY, 0)
}
// ClearCache syncs and then removes the file's content from the OS cache.
func ClearCache(f File) error {
return nil
}