Debian Codegraph @

backend.go

raw
1 package gitobj
2
3 import (
4 "bufio"
5 "hash"
6 "io"
7 "os"
8 "path"
9 "regexp"
10 "strconv"
11 "strings"
12
13 "github.com/git-lfs/gitobj/v2/pack"
14 "github.com/git-lfs/gitobj/v2/storage"
15 )
16
17 // NewFilesystemBackend initializes a new filesystem-based backend,
18 // optionally with additional alternates as specified in the
19 // `alternates` variable. The syntax is that of the Git environment variable
20 // GIT_ALTERNATE_OBJECT_DIRECTORIES. The hash algorithm used is specified by
21 // the algo parameter.
22 func NewFilesystemBackend(root, tmp, alternates string, algo hash.Hash) (storage.Backend, error) {
23 fsobj := newFileStorer(root, tmp)
24 packs, err := pack.NewStorage(root, algo)
25 if err != nil {
26 return nil, err
27 }
28
29 storage, err := findAllBackends(fsobj, packs, root, algo)
30 if err != nil {
31 return nil, err
32 }
33
34 storage, err = addAlternatesFromEnvironment(storage, alternates, algo)
35 if err != nil {
36 return nil, err
37 }
38
39 return &filesystemBackend{
40 fs: fsobj,
41 backends: storage,
42 }, nil
43 }
44
45 func findAllBackends(mainLoose *fileStorer, mainPacked *pack.Storage, root string, algo hash.Hash) ([]storage.Storage, error) {
46 storage := make([]storage.Storage, 2)
47 storage[0] = mainLoose
48 storage[1] = mainPacked
49 f, err := os.Open(path.Join(root, "info", "alternates"))
50 if err != nil {
51 // No alternates file, no problem.
52 if err != os.ErrNotExist {
53 return storage, nil
54 }
55 return nil, err
56 }
57 defer f.Close()
58
59 scanner := bufio.NewScanner(f)
60 for scanner.Scan() {
61 storage, err = addAlternateDirectory(storage, scanner.Text(), algo)
62 if err != nil {
63 return nil, err
64 }
65 }
66
67 if err := scanner.Err(); err != nil {
68 return nil, err
69 }
70
71 return storage, nil
72 }
73
74 func addAlternateDirectory(s []storage.Storage, dir string, algo hash.Hash) ([]storage.Storage, error) {
75 s = append(s, newFileStorer(dir, ""))
76 pack, err := pack.NewStorage(dir, algo)
77 if err != nil {
78 return s, err
79 }
80 s = append(s, pack)
81 return s, nil
82 }
83
84 func addAlternatesFromEnvironment(s []storage.Storage, env string, algo hash.Hash) ([]storage.Storage, error) {
85 if len(env) == 0 {
86 return s, nil
87 }
88
89 for _, dir := range splitAlternateString(env, alternatesSeparator) {
90 var err error
91 s, err = addAlternateDirectory(s, dir, algo)
92 if err != nil {
93 return nil, err
94 }
95 }
96 return s, nil
97 }
98
99 var (
100 octalEscape = regexp.MustCompile("\\\\[0-7]{1,3}")
101 hexEscape = regexp.MustCompile("\\\\x[0-9a-fA-F]{2}")
102 replacements = []struct {
103 olds string
104 news string
105 }{
106 {`\a`, "\a"},
107 {`\b`, "\b"},
108 {`\t`, "\t"},
109 {`\n`, "\n"},
110 {`\v`, "\v"},
111 {`\f`, "\f"},
112 {`\r`, "\r"},
113 {`\\`, "\\"},
114 {`\"`, "\""},
115 {`\'`, "'"},
116 }
117 )
118
119 func splitAlternateString(env string, separator string) []string {
120 dirs := strings.Split(env, separator)
121 for i, s := range dirs {
122 if !strings.HasPrefix(s, `"`) || !strings.HasSuffix(s, `"`) {
123 continue
124 }
125
126 // Strip leading and trailing quotation marks
127 s = s[1 : len(s)-1]
128 for _, repl := range replacements {
129 s = strings.Replace(s, repl.olds, repl.news, -1)
130 }
131 s = octalEscape.ReplaceAllStringFunc(s, func(inp string) string {
132 val, _ := strconv.ParseUint(inp[1:], 8, 64)
133 return string([]byte{byte(val)})
134 })
135 s = hexEscape.ReplaceAllStringFunc(s, func(inp string) string {
136 val, _ := strconv.ParseUint(inp[2:], 16, 64)
137 return string([]byte{byte(val)})
138 })
139 dirs[i] = s
140 }
141 return dirs
142 }
143
144 // NewMemoryBackend initializes a new memory-based backend.
145 //
146 // A value of "nil" is acceptable and indicates that no entries should be added
147 // to the memory backend at construction time.
148 func NewMemoryBackend(m map[string]io.ReadWriter) (storage.Backend, error) {
149 return &memoryBackend{ms: newMemoryStorer(m)}, nil
150 }
151
152 type filesystemBackend struct {
153 fs *fileStorer
154 backends []storage.Storage
155 }
156
157 func (b *filesystemBackend) Storage() (storage.Storage, storage.WritableStorage) {
158 return storage.MultiStorage(b.backends...), b.fs
159 }
160
161 type memoryBackend struct {
162 ms *memoryStorer
163 }
164
165 func (b *memoryBackend) Storage() (storage.Storage, storage.WritableStorage) {
166 return b.ms, b.ms
167 }