git-lfs/fs/cleanup.go

52 lines
1.0 KiB
Go
Raw Normal View History

2017-10-25 01:16:14 +00:00
package fs
import (
"os"
"path/filepath"
"strings"
"time"
"github.com/git-lfs/git-lfs/tools"
2017-10-25 01:16:14 +00:00
"github.com/rubyist/tracerx"
)
func (f *Filesystem) cleanupTmp() error {
tmpdir := f.TempDir()
if len(tmpdir) == 0 {
return nil
}
var walkErr error
tools.FastWalkGitRepo(tmpdir, func(parentDir string, info os.FileInfo, err error) {
if err != nil {
walkErr = err
}
if walkErr != nil || info.IsDir() {
return
}
path := filepath.Join(parentDir, info.Name())
parts := strings.SplitN(info.Name(), "-", 2)
oid := parts[0]
if len(parts) < 2 || len(oid) != 64 {
tracerx.Printf("Removing invalid tmp object file: %s", path)
2017-10-25 01:16:14 +00:00
os.RemoveAll(path)
return
2017-10-25 01:16:14 +00:00
}
2017-10-25 17:31:15 +00:00
fi, err := os.Stat(f.ObjectPathname(oid))
if err == nil && !fi.IsDir() {
tracerx.Printf("Removing existing tmp object file: %s", path)
os.RemoveAll(path)
return
}
2017-10-25 01:16:14 +00:00
if time.Since(info.ModTime()) > time.Hour {
tracerx.Printf("Removing old tmp object file: %s", path)
os.RemoveAll(path)
return
}
})
2017-10-25 01:16:14 +00:00
return walkErr
2017-10-25 01:16:14 +00:00
}