Skip to content

Instantly share code, notes, and snippets.

@abg
Last active May 8, 2017 15:38
Show Gist options
  • Save abg/968bf0c2c31edbaf04298789d8cf499f to your computer and use it in GitHub Desktop.
Save abg/968bf0c2c31edbaf04298789d8cf499f to your computer and use it in GitHub Desktop.
Uncache a set of files (reimplementation of: https://dom.as/2009/06/26/uncache/)
package main
import (
"fmt"
"os"
"path/filepath"
"golang.org/x/sys/unix"
)
func uncache(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
return unix.Fadvise(int(file.Fd()), 0, 0, unix.FADV_DONTNEED)
}
func main() {
numScanned := 0
numUncached := 0
for _, location := range os.Args[1:] {
err := filepath.Walk(location, func(path string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
if fileInfo.IsDir() {
return nil
}
numScanned++
if err := uncache(path); err != nil {
fmt.Printf("Failed to uncache '%s': %s\n", err)
} else {
fmt.Printf("Uncached '%s'\n", path)
numUncached++
}
return nil
})
if err != nil {
fmt.Printf("Errors walking '%s': %s\n", location, err)
}
}
fmt.Printf("Scanned %d files\n", numScanned)
fmt.Printf("Uncached %d files\n", numUncached)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment