git-lfs/lfs/localstorage.go

59 lines
1.1 KiB
Go
Raw Normal View History

2015-10-21 21:47:46 +00:00
package lfs
import (
"fmt"
2015-10-21 21:47:46 +00:00
"os"
"path/filepath"
"strings"
"time"
"github.com/github/git-lfs/vendor/_nuts/github.com/rubyist/tracerx"
)
func ClearTempObjects() {
d, err := os.Open(LocalObjectTempDir)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening %q to clear old temp files: %s", LocalObjectTempDir, err)
return
}
2015-10-21 21:47:46 +00:00
filenames, _ := d.Readdirnames(-1)
for _, filename := range filenames {
path := filepath.Join(LocalObjectTempDir, filename)
if shouldDeleteTempObject(path) {
os.RemoveAll(path)
}
}
2015-10-21 21:47:46 +00:00
}
func shouldDeleteTempObject(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
if info.IsDir() {
2015-10-21 21:47:46 +00:00
return false
}
base := filepath.Base(path)
parts := strings.SplitN(base, "-", 2)
oid := parts[0]
if len(parts) < 2 || len(oid) != 64 {
2015-10-21 21:47:46 +00:00
tracerx.Printf("Removing invalid tmp object file: %s", path)
return true
}
if FileExists(localMediaPathNoCreate(oid)) {
2015-10-21 21:47:46 +00:00
tracerx.Printf("Removing existing tmp object file: %s", path)
return true
}
2015-10-21 21:53:52 +00:00
if time.Since(info.ModTime()) > time.Hour {
2015-10-21 21:47:46 +00:00
tracerx.Printf("Removing old tmp object file: %s", path)
return true
}
return false
}