-
Notifications
You must be signed in to change notification settings - Fork 63
/
file_local.go
99 lines (85 loc) · 1.87 KB
/
file_local.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
package filestore
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"sync"
"github.com/criyle/go-judge/envexec"
)
type fileLocalStore struct {
dir string // directory to store file
name map[string]string // id to name mapping if exists
mu sync.RWMutex
}
// NewFileLocalStore create new local file store
func NewFileLocalStore(dir string) FileStore {
return &fileLocalStore{
dir: filepath.Clean(dir),
name: make(map[string]string),
}
}
func (s *fileLocalStore) Add(name, path string) (string, error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.dir == filepath.Dir(path) {
id := filepath.Base(path)
s.name[id] = name
return id, nil
}
return "", fmt.Errorf("add: %s does not have prefix %s", path, s.dir)
}
func (s *fileLocalStore) Get(id string) (string, envexec.File) {
s.mu.RLock()
defer s.mu.RUnlock()
p := path.Join(s.dir, id)
if _, err := os.Stat(p); os.IsNotExist(err) {
return "", nil
}
name, ok := s.name[id]
if !ok {
name = id
}
return name, envexec.NewFileInput(p)
}
func (s *fileLocalStore) Remove(id string) bool {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.name, id)
p := path.Join(s.dir, id)
if _, err := os.Stat(p); os.IsNotExist(err) {
return false
}
os.Remove(p)
return true
}
func (s *fileLocalStore) List() map[string]string {
s.mu.RLock()
defer s.mu.RUnlock()
fi, err := os.ReadDir(s.dir)
if err != nil {
return nil
}
names := make(map[string]string, len(fi))
for _, f := range fi {
names[f.Name()] = s.name[f.Name()]
}
return names
}
func (s *fileLocalStore) New() (*os.File, error) {
for range [50]struct{}{} {
id, err := generateID()
if err != nil {
return nil, err
}
f, err := os.OpenFile(path.Join(s.dir, id), os.O_CREATE|os.O_RDWR|os.O_EXCL, 0644)
if err == nil {
return f, nil
}
if !errors.Is(err, os.ErrExist) {
return nil, err
}
}
return nil, errUniqueIDNotGenerated
}