2015-10-21 21:47:46 +00:00
|
|
|
package lfs
|
|
|
|
|
|
|
|
import (
|
2015-10-22 15:02:28 +00:00
|
|
|
"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() {
|
2015-10-22 15:19:43 +00:00
|
|
|
if len(LocalObjectTempDir) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-22 15:02:28 +00:00
|
|
|
d, err := os.Open(LocalObjectTempDir)
|
|
|
|
if err != nil {
|
2015-10-22 15:19:43 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Error opening %q to clear old temp files: %s\n", LocalObjectTempDir, err)
|
2015-10-22 15:02:28 +00:00
|
|
|
return
|
|
|
|
}
|
2015-10-21 21:47:46 +00:00
|
|
|
|
2015-10-22 15:02:28 +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
|
|
|
}
|
|
|
|
|
2015-10-22 15:02:28 +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)
|
2015-10-21 21:49:46 +00:00
|
|
|
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:03:43 +00:00
|
|
|
if FileExists(LocalStorage.ObjectPath(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
|
|
|
|
}
|