/
cleanup.go
51 lines (44 loc) · 1.01 KB
/
cleanup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package fs
import (
"os"
"path/filepath"
"strings"
"time"
"github.com/git-lfs/git-lfs/tools"
"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)
os.RemoveAll(path)
return
}
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
}
if time.Since(info.ModTime()) > time.Hour {
tracerx.Printf("Removing old tmp object file: %s", path)
os.RemoveAll(path)
return
}
})
return walkErr
}