git-lfs/localstorage/temp.go

64 lines
1.1 KiB
Go
Raw Normal View History

2015-11-24 20:26:44 +00:00
package localstorage
2015-10-21 21:47:46 +00:00
import (
"os"
"path/filepath"
"strings"
"time"
"github.com/rubyist/tracerx"
2015-10-21 21:47:46 +00:00
)
2015-11-24 20:26:44 +00:00
func (s *LocalStorage) ClearTempObjects() error {
if len(s.TempDir) == 0 {
return nil
}
2015-11-24 20:26:44 +00:00
d, err := os.Open(s.TempDir)
if err != nil {
2015-11-24 20:26:44 +00:00
return err
}
2015-10-21 21:47:46 +00:00
filenames, _ := d.Readdirnames(-1)
for _, filename := range filenames {
2015-11-24 20:26:44 +00:00
path := filepath.Join(s.TempDir, filename)
if shouldDeleteTempObject(s, path) {
os.RemoveAll(path)
}
}
2015-11-24 20:26:44 +00:00
return nil
2015-10-21 21:47:46 +00:00
}
2015-11-24 20:26:44 +00:00
func shouldDeleteTempObject(s *LocalStorage, 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
}
2015-11-24 20:26:44 +00:00
fi, err := os.Stat(s.ObjectPath(oid))
if err == nil && !fi.IsDir() {
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
}