-
Notifications
You must be signed in to change notification settings - Fork 463
/
Copy pathtar_file_sets.go
67 lines (52 loc) · 1.48 KB
/
tar_file_sets.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
package internal
import (
"sync"
)
type TarFileSets interface {
AddFile(name string, file string)
AddFiles(name string, files []string)
Get() map[string][]string
}
type RegularTarFileSets struct {
data map[string][]string
mutex sync.RWMutex
}
func NewRegularTarFileSets() *RegularTarFileSets {
return &RegularTarFileSets{
data: make(map[string][]string),
}
}
func (tarFileSets *RegularTarFileSets) AddFile(name string, file string) {
tarFileSets.mutex.Lock()
defer tarFileSets.mutex.Unlock()
tarFileSets.data[name] = append(tarFileSets.data[name], file)
}
func (tarFileSets *RegularTarFileSets) AddFiles(name string, files []string) {
tarFileSets.mutex.Lock()
defer tarFileSets.mutex.Unlock()
tarFileSets.data[name] = append(tarFileSets.data[name], files...)
}
func (tarFileSets *RegularTarFileSets) Get() map[string][]string {
tarFileSets.mutex.RLock()
defer tarFileSets.mutex.RUnlock()
// Create a copy to ensure thread safety
result := make(map[string][]string, len(tarFileSets.data))
for k, v := range tarFileSets.data {
newSlice := make([]string, len(v))
copy(newSlice, v)
result[k] = newSlice
}
return result
}
type NopTarFileSets struct {
}
func NewNopTarFileSets() *NopTarFileSets {
return &NopTarFileSets{}
}
func (tarFileSets *NopTarFileSets) AddFile(name string, file string) {
}
func (tarFileSets *NopTarFileSets) AddFiles(name string, files []string) {
}
func (tarFileSets *NopTarFileSets) Get() map[string][]string {
return make(map[string][]string)
}