github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/fs/cleanup.go (about)

     1  package fs
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/git-lfs/git-lfs/tools"
    10  	"github.com/rubyist/tracerx"
    11  )
    12  
    13  func (f *Filesystem) cleanupTmp() error {
    14  	tmpdir := f.TempDir()
    15  	if len(tmpdir) == 0 {
    16  		return nil
    17  	}
    18  
    19  	var walkErr error
    20  	tools.FastWalkGitRepo(tmpdir, func(parentDir string, info os.FileInfo, err error) {
    21  		if err != nil {
    22  			walkErr = err
    23  		}
    24  		if walkErr != nil || info.IsDir() {
    25  			return
    26  		}
    27  		path := filepath.Join(parentDir, info.Name())
    28  		parts := strings.SplitN(info.Name(), "-", 2)
    29  		oid := parts[0]
    30  		if len(parts) < 2 || len(oid) != 64 {
    31  			tracerx.Printf("Removing invalid tmp object file: %s", path)
    32  			os.RemoveAll(path)
    33  			return
    34  		}
    35  
    36  		fi, err := os.Stat(f.ObjectPathname(oid))
    37  		if err == nil && !fi.IsDir() {
    38  			tracerx.Printf("Removing existing tmp object file: %s", path)
    39  			os.RemoveAll(path)
    40  			return
    41  		}
    42  
    43  		if time.Since(info.ModTime()) > time.Hour {
    44  			tracerx.Printf("Removing old tmp object file: %s", path)
    45  			os.RemoveAll(path)
    46  			return
    47  		}
    48  	})
    49  
    50  	return walkErr
    51  }