File: cleanup.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (83 lines) | stat: -rw-r--r-- 1,879 bytes parent folder | download | duplicates (2)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package fs

import (
	"os"
	"path/filepath"
	"strings"
	"sync"
	"time"

	"github.com/git-lfs/git-lfs/v3/tools"
	"github.com/rubyist/tracerx"
)

func (f *Filesystem) cleanupTmp() error {
	tmpdir := f.TempDir()
	if len(tmpdir) == 0 {
		return nil
	}

	// No temporary directory?  No problem.
	if _, err := os.Stat(tmpdir); err != nil && os.IsNotExist(err) {
		return nil
	}

	traversedDirectories := &sync.Map{}

	var walkErr error
	tools.FastWalkDir(tmpdir, func(parentDir string, info os.FileInfo, err error) {
		if err != nil {
			walkErr = err
		}
		if walkErr != nil {
			return
		}
		path := filepath.Join(parentDir, info.Name())
		if info.IsDir() {
			traversedDirectories.Store(path, info)
			return
		}
		parts := strings.SplitN(info.Name(), "-", 2)
		oid := parts[0]
		if len(parts) == 2 && len(oid) == 64 {
			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
			}
		}

		// Don't prune items in a directory younger than an hour.  These
		// items could be hard links to files from other repositories,
		// which would have an older timestamp but which are still in
		// use by some active process.  Exempt the main temporary from
		// this check, since we frequently modify it and we'd never
		// prune otherwise.
		if tmpdir != parentDir {
			var dirInfo os.FileInfo
			entry, ok := traversedDirectories.Load(parentDir)
			if ok {
				dirInfo = entry.(os.FileInfo)
			} else {
				dirInfo, err = os.Stat(parentDir)
				if err != nil {
					return
				}
				traversedDirectories.Store(path, dirInfo)
			}

			if time.Since(dirInfo.ModTime()) <= time.Hour {
				return
			}
		}

		if time.Since(info.ModTime()) > time.Hour {
			tracerx.Printf("Removing old tmp object file: %s", path)
			os.RemoveAll(path)
			return
		}
	})

	return walkErr
}