-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
196 lines (172 loc) · 3.73 KB
/
file.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2023 The Yock Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package fs
import (
"encoding/json"
"fmt"
"os"
"strings"
du "github.com/ansurfen/yock/daemon/util"
"github.com/ansurfen/yock/util"
)
type File interface {
Open(owner, wanter string) ([]string, error)
Close(fd string)
Info(owner string) FileInfo
Append(owner string, meta FileInfo)
}
const gen_fd = "abcdefg"
type SharedFile struct {
Files map[string]*atomicFileSet
Name string
}
func encodeFd(file, owner, consumer string) string {
return util.EncodeAESWithKey(gen_fd, file+"#"+owner+"#"+consumer)
}
func decodeFd(fd string) (file, owner, consumer string) {
res := strings.Split(util.DecodeAESWithKey(gen_fd, fd), "#")
if len(res) != 3 {
return
}
return res[0], res[1], res[2]
}
func (fp *SharedFile) Append(producer string, meta FileInfo) {
if cf, ok := fp.Files[producer]; ok {
cf.atomicFile.Meta = meta
} else {
fp.Files[producer] = &atomicFileSet{
atomicFile: atomicFile{
Meta: meta,
},
}
}
}
func (fp *SharedFile) Open(producer, consumer string) (ret []string, err error) {
if producer == "*" {
for name, f := range fp.Files {
err = f.Open(consumer)
if err != nil {
for _, r := range ret {
fp.Close(r)
}
ret = nil
return
}
ret = append(ret, encodeFd(fp.Name, name, consumer))
}
}
if file, ok := fp.Files[producer]; ok {
err = file.Open(consumer)
if err != nil {
return
}
ret = append(ret, util.EncodeAESWithKey(gen_fd, fp.Name+"#"+producer+"#"+consumer))
}
return
}
func (file *SharedFile) Close(fd string) {
_, owner, consumer := decodeFd(fd)
if f, ok := file.Files[owner]; ok {
f.mut.Unlock(consumer)
}
}
func (file *SharedFile) Info(producer string) FileInfo {
return file.Files[producer].Meta
}
type atomicFileSet struct {
atomicFile
Replication []atomicFile
}
type atomicFile struct {
mut *fileLocker
Meta FileInfo
ptr *os.File
}
func newSingleFile(meta FileInfo) atomicFile {
return atomicFile{
Meta: meta,
mut: &fileLocker{},
}
}
func (file *atomicFileSet) Clone(newOwner string) {
found := false
for _, replication := range file.Replication {
if replication.Meta.Owner == newOwner {
found = true
break
}
}
if !found {
meta := file.Meta
meta.Owner = newOwner
file.Replication = append(file.Replication, atomicFile{
Meta: meta,
mut: &fileLocker{},
})
}
}
type fileLocker struct {
holder string
count int
}
func (locker *fileLocker) Unlock(owner string) {
if locker.holder == owner {
locker.count--
if locker.count == 0 {
locker.holder = ""
locker.count = 0
}
}
}
func (locker *fileLocker) Lock(consumer string) bool {
if len(locker.holder) == 0 {
locker.holder = consumer
locker.count = 1
return true
}
if locker.holder == consumer {
locker.count++
return true
}
return false
}
func (locker *fileLocker) IsLock(owner string) bool {
return len(locker.holder) != 0 && locker.holder != owner
}
func (fp *atomicFile) Open(consumer string) error {
if fp.mut.IsLock(consumer) {
return fmt.Errorf("file is opened")
}
if fp.Meta.Owner == du.ID {
if !fp.mut.Lock(consumer) {
return fmt.Errorf("file fails to lock")
}
path := decode(fp.Meta.Path)
ptr, err := os.Open(path)
if err != nil {
return err
}
fp.ptr = ptr
} else {
return fmt.Errorf("file not found")
}
return nil
}
func (f *SharedFile) Save() string {
raw, err := json.Marshal(f)
if err != nil {
panic(err)
}
return string(raw)
}
type FileInfo struct {
Owner string `json:"owner"`
Size int64 `json:"size"`
Hash string `json:"hash"`
CreateAt int64 `json:"createAt"`
Path string `json:"path"`
}
func decode(path string) string {
return path
}