/
git_patch.go
121 lines (96 loc) · 2.96 KB
/
git_patch.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package gitdata
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
type GitPatchDesc struct {
MetadataPath string
PatchPath string
Metadata *PatchMetadata
Size uint64
CacheBasePath string
}
func (entry *GitPatchDesc) GetPaths() []string {
return []string{entry.MetadataPath, entry.PatchPath}
}
func (entry *GitPatchDesc) GetSize() uint64 {
return entry.Size
}
func (entry *GitPatchDesc) GetLastAccessAt() time.Time {
return time.Unix(entry.Metadata.LastAccessTimestamp, 0)
}
func (entry *GitPatchDesc) GetCacheBasePath() string {
return entry.CacheBasePath
}
func GetExistingGitPatches(cacheVersionRoot string) ([]*GitPatchDesc, error) {
var res []*GitPatchDesc
if _, err := os.Stat(cacheVersionRoot); os.IsNotExist(err) {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf("error accessing dir %q: %w", cacheVersionRoot, err)
}
files, err := ioutil.ReadDir(cacheVersionRoot)
if err != nil {
return nil, fmt.Errorf("error reading dir %q: %w", cacheVersionRoot, err)
}
for _, finfo := range files {
repoPatchesRootDir := filepath.Join(cacheVersionRoot, finfo.Name())
if !finfo.IsDir() {
if err := os.RemoveAll(repoPatchesRootDir); err != nil {
return nil, fmt.Errorf("unable to remove %q: %w", repoPatchesRootDir, err)
}
}
hashDirs, err := ioutil.ReadDir(repoPatchesRootDir)
if err != nil {
return nil, fmt.Errorf("error reading repo archives dir %q: %w", repoPatchesRootDir, err)
}
for _, finfo := range hashDirs {
hashDir := filepath.Join(repoPatchesRootDir, finfo.Name())
if !finfo.IsDir() {
if err := os.RemoveAll(hashDir); err != nil {
return nil, fmt.Errorf("unable to remove %q: %w", hashDir, err)
}
}
patchesFiles, err := ioutil.ReadDir(hashDir)
if err != nil {
return nil, fmt.Errorf("error reading repo patches from dir %q: %w", hashDir, err)
}
for _, finfo := range patchesFiles {
path := filepath.Join(hashDir, finfo.Name())
if finfo.IsDir() {
if err := os.RemoveAll(path); err != nil {
return nil, fmt.Errorf("unable to remove %q: %w", path, err)
}
}
if !strings.HasSuffix(finfo.Name(), ".meta.json") {
continue
}
desc := &GitPatchDesc{MetadataPath: path}
res = append(res, desc)
if data, err := ioutil.ReadFile(path); err != nil {
return nil, fmt.Errorf("error reading metadata file %q: %w", path, err)
} else {
if err := json.Unmarshal(data, &desc.Metadata); err != nil {
return nil, fmt.Errorf("error unmarshalling json from %q: %w", path, err)
}
}
patchPath := filepath.Join(hashDir, fmt.Sprintf("%s.patch", strings.TrimSuffix(finfo.Name(), ".meta.json")))
patchInfo, err := os.Stat(patchPath)
if err != nil {
if os.IsNotExist(err) {
continue
}
return nil, fmt.Errorf("error accessing %q: %w", patchPath, err)
}
desc.PatchPath = patchPath
desc.Size = uint64(patchInfo.Size())
}
}
}
return res, nil
}