-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
287 lines (222 loc) · 6.49 KB
/
main.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
billy "github.com/go-git/go-billy/v5"
nfs "github.com/willscott/go-nfs"
nfshelper "github.com/willscott/go-nfs/helpers"
"flag"
"fmt"
"io"
fs1 "io/fs"
"log"
"net"
"os/signal"
"syscall"
"slices"
"os"
"time"
"path"
)
func (fs OverlayFS) Join(elem ...string) string{
fmt.Println("Join:",elem)
return path.Join(elem...)
}
func (fs OverlayFS) ReadDir(path string) ([]os.FileInfo, error){
if !fs.checkIfExists(path){
return make([]os.FileInfo,0), os.ErrNotExist
}
fmt.Println("ReadDir:",path)
dirs:=fs.joinRelative(path)[fs.indexOfFirstExisting(path):]
dirMap:=make(map[string]os.FileInfo)
result:=make([]os.FileInfo,0)
for i, dir :=range dirs{
dirEntries,err:=os.ReadDir(dir)
if err!=nil{
if i==0{ //Since the top-level directory determines the behavior of everything else, if it isn't a directory, no point in continuing
fmt.Printf("Problem directory: %s\n",dir)
return make([]os.FileInfo,0), err
}else{ //The higher-level directories cover for it
continue
}
}
for _, entry:= range dirEntries{
_,ok:=dirMap[entry.Name()]
if ok{ //If there's A/C and B/C, take A/C
continue
}else{
info, err:=fs.Lstat(fs.Join(path,entry.Name()))
fmt.Printf("Done with %s!\n",fs.Join(path,entry.Name()))
fmt.Println()
if err!=nil{
continue
}
dirMap[entry.Name()]=info
}
}
}
for _,info := range dirMap{
result=append(result,info)
}
err:=unspecifiedError
err=nil
return result,err
}
func (fs OverlayFS) Chtimes(name string, atime time.Time, mtime time.Time) error{
if !fs.checkIfExists(name){
return os.ErrNotExist
}
fmt.Println("Chtimes:",name)
if fs.getModeofFirstExisting(name)=="RW"{
return os.Chtimes(fs.findFirstExisting(name),atime,mtime)
}else{
return nil //On RO, it's a no-op
}
}
func (fs OverlayFS) Open(filename string) (billy.File, error){
fmt.Println("Open:",filename)
return fs.OpenFile(filename,os.O_RDONLY,0)
}
func (fs OverlayFS) Create(filename string) (billy.File, error){
fmt.Println("Create:",filename)
return fs.OpenFile(filename,os.O_CREATE | os.O_TRUNC, 0666)
}
func (fs OverlayFS) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error){
writeConstants:=os.O_RDWR|os.O_WRONLY|os.O_APPEND|os.O_TRUNC
isWrite:=(flag & writeConstants !=0)
isCreate:=(flag & os.O_CREATE !=0)
original_filename:=filename
fmt.Println("Openfile:",filename)
if !fs.checkIfExists(filename){ //If file has been explicitly deleted, any call to Open will fail...
if !isCreate{ //...except for CREATE
return nil, os.ErrNotExist
}
}
if _,err:=fs.Lstat(filename); err==nil && fs.getModeofFirstExisting(filename)=="RO" && isWrite{
fs.deletedMap[filename]=true //Done to force createPath to create file above the current existing one
COW:=false
COWPath:=""
originalPath:=fs.findFirstExisting(filename)
create_path, err:= fs.createPath(filename)
fmt.Println(create_path)
delete(fs.deletedMap,filename)
if err!=nil{
return nil, err
}else{
COW=true
COWPath=create_path
}
fileStat,_:=os.Lstat(originalPath)
fileMode:=fileStat.Mode()
isRegular := fileMode.IsRegular()
isSymlink := (fileMode & fs1.ModeSymlink != 0)
COW=COW && (isRegular || isSymlink) //Check that file is regular or is a symlink
if COW {
num_parent_dirs:=len(parentDirs(filename))
originalParentDirs:=parentDirs(originalPath)
COWParentDirs:=parentDirs(COWPath)
slices.Reverse(originalParentDirs) //From innermost to outermost
slices.Reverse(COWParentDirs)
for i := 0; i < num_parent_dirs; i++{ //Create parent directories of file in COW directories. From innermost to outermost
originalDir:=originalParentDirs[i]
COWDir:=COWParentDirs[i]
fmt.Println(COWDir)
fmt.Println(originalDir)
os.MkdirAll(COWDir,0700)
setPermissions(originalDir,COWDir)
}
src,_:=os.Open(originalPath)
if isRegular{
dest,_:=os.Create(COWPath)
io.Copy(dest,src)
dest.Close()
}else if isSymlink{
target,_ :=os.Readlink(originalPath)
os.Symlink(target,COWPath)
}
setPermissions(originalPath, COWPath) //Make file with the same permissions as before
filename=COWPath
}else{
return nil, os.ErrInvalid
}
}else{
if isCreate{
create_path, err:=fs.createPath(filename)
if err!=nil{
return nil, err
}
filename=create_path
}else{
filename=fs.findFirstExisting(filename)
}
}
if isCreate{
defer fs.createErrorCheck(filename, original_filename)
}
open,err:=os.OpenFile(filename,flag,perm)
fmt.Println("Openfile finished!")
return &OverlayFile{open,original_filename},err
}
func (fs OverlayFS) Remove(filename string) error{
if !fs.checkIfExists(filename){
return os.ErrNotExist
}
fmt.Println("Remove:",filename)
var err error
if fs.getModeofFirstExisting(filename)=="RW"{
err=os.Remove(fs.findFirstExisting(filename))
if err!=nil{
fmt.Println("Remove Error: ",err)
return err
}
}else{
fileStat,_:=fs.Lstat(filename)
if fileStat.IsDir(){
fileRead,_:=fs.ReadDir(filename)
if len(fileRead)!=0{ //Can't remove a non-empty directory
return unspecifiedError
}
}
}
fs.addToDeleted(filename)
return nil
}
var port chan int = make(chan int,1)
func runServer(options []string,mountpoint string){
listener, err := net.Listen("tcp", ":0") //Get random unused port
listenerAddr:=listener.Addr()
addr,_:=net.ResolveTCPAddr(listenerAddr.Network(),listenerAddr.String())
port <- addr.Port
panicOnErr(err, "starting TCP listener")
fs:=NewFS(options,mountpoint)
handler := nfshelper.NewNullAuthHandler(fs)
cacheHelper := nfshelper.NewCachingHandler(handler, 100000000000000)
panicOnErr(nfs.Serve(listener, cacheHelper), "serving nfs")
}
func panicOnErr(err error, desc ...interface{}) {
if err == nil {
return
}
log.Println(desc...)
log.Panicln(err)
}
func umount (mountpoint string) {
runCommand("sudo","umount", "-l",mountpoint)
}
func main(){
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
done := make(chan bool, 1)
go func() {
<-sigs
done <- true
}()
flag.Parse()
args:=flag.Args()
options:=args[:len(args)-1]
mountpoint:=args[len(args)-1]
umount(mountpoint)
defer func (){umount(mountpoint); fmt.Println()}() //Unmount, even when panicking
go runServer(options,mountpoint)
serverPort:= <- port
runCommand("sudo","mount", "-t", "nfs", fmt.Sprintf("-oport=%[1]d,mountport=%[1]d,vers=3,tcp,noacl,nolock,soft",serverPort),"-vvv","127.0.0.1:/", mountpoint)
<-done //wait until SIGTERM or SIGINT
}