Debian Codegraph @

fs/cleanup.go

raw symbols SCIP
1 package fs
2
3 import (
4 "os"
5 "path/filepath"
6 "strings"
7 "sync"
8 "time"
9
10 "github.com/git-lfs/git-lfs/v3/tools"
11 "github.com/rubyist/tracerx"
12 )
13
14 func (f *Filesystem) cleanupTmp() error {
15 tmpdir := f.TempDir()
16 if len(tmpdir) == 0 {
17 return nil
18 }
19
20 // No temporary directory? No problem.
21 if _, err := os.Stat(tmpdir); err != nil && os.IsNotExist(err) {
22 return nil
23 }
24
25 traversedDirectories := &sync.Map{}
26
27 var walkErr error
28 tools.FastWalkDir(tmpdir, func(parentDir string, info os.FileInfo, err error) {
29 if err != nil {
30 walkErr = err
31 }
32 if walkErr != nil {
33 return
34 }
35 path := filepath.Join(parentDir, info.Name())
36 if info.IsDir() {
37 traversedDirectories.Store(path, info)
38 return
39 }
40 parts := strings.SplitN(info.Name(), "-", 2)
41 oid := parts[0]
42 if len(parts) == 2 && len(oid) == 64 {
43 fi, err := os.Stat(f.ObjectPathname(oid))
44 if err == nil && !fi.IsDir() {
45 tracerx.Printf("Removing existing tmp object file: %s", path)
46 os.RemoveAll(path)
47 return
48 }
49 }
50
51 // Don't prune items in a directory younger than an hour. These
52 // items could be hard links to files from other repositories,
53 // which would have an older timestamp but which are still in
54 // use by some active process. Exempt the main temporary from
55 // this check, since we frequently modify it and we'd never
56 // prune otherwise.
57 if tmpdir != parentDir {
58 var dirInfo os.FileInfo
59 entry, ok := traversedDirectories.Load(parentDir)
60 if ok {
61 dirInfo = entry.(os.FileInfo)
62 } else {
63 dirInfo, err = os.Stat(parentDir)
64 if err != nil {
65 return
66 }
67 traversedDirectories.Store(path, dirInfo)
68 }
69
70 if time.Since(dirInfo.ModTime()) <= time.Hour {
71 return
72 }
73 }
74
75 if time.Since(info.ModTime()) > time.Hour {
76 tracerx.Printf("Removing old tmp object file: %s", path)
77 os.RemoveAll(path)
78 return
79 }
80 })
81
82 return walkErr
83 }