-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.go
227 lines (178 loc) · 4.37 KB
/
create.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
package main
import(
"os"
"fmt"
"golang.org/x/sys/unix"
"io"
)
//The "Create" functions
func (fs OverlayFS) createErrorCheck(create_path, path string) {
_,err:=os.Lstat(create_path)
if err==nil{
fs.removefromDeleted(path)
}else{
fmt.Println("createErrorCheck: ",path)
fmt.Println("createErrorCheck Error: ",err)
}
fmt.Println("Error check completed!")
}
func (fs OverlayFS) Rename(oldpath, newpath string) error{
if !fs.checkIfExists(oldpath){
return os.ErrNotExist
}
fmt.Println("Rename: ",newpath)
newPath,err:=fs.createPath(newpath)
if err!=nil{
return err
}
oldStat,_:=fs.Lstat(oldpath)
oldMode:=fs.getModeofFirstExisting(oldpath)
oldPath:=fs.findFirstExisting(oldpath)
if !oldStat.IsDir(){
if oldMode=="RW"{ //Can remove from oldpath
err:=os.Rename(oldPath,newPath)
if err!=nil{
return err
}else{
fs.createErrorCheck(newPath,newpath)
}
}else{
err:=fs.Remove(newpath) //This matters when dealing with non-(regular files), eg. directories.
if err!=nil && !os.IsNotExist(err){
return err
}
if oldStat.Mode().IsRegular(){
old,err:=fs.Open(oldpath)
if err!=nil{
return err
}
new,err:=fs.OpenFile(newpath,os.O_CREATE|os.O_TRUNC|os.O_WRONLY,0666)
if err!=nil{
return err
}
_,err=io.Copy(new, old)
new.Close()
if err!=nil{
return err
}
} else if oldStat.Mode() & os.ModeSymlink != 0 {
oldTarget, err:=fs.Readlink(oldpath)
if err!=nil{
return err
}
err=fs.Symlink(oldTarget, newpath)
if err != nil{
return err
}
}else{ //If it's something like a device, I just don't want to deal with all the different options. I may deal with them later if it becomes an issue
return unspecifiedError
}
}
}else{
err=fs.Remove(newpath)
if err!=nil && !os.IsNotExist(err){
return err
}
err=fs.Mkdir(newpath,0666)
if err!=nil && !os.IsExist(err){
return err
}
items, err:=fs.ReadDir(oldpath)
if err!=nil{
return err
}
for _, item:= range items{
name:=item.Name()
err=fs.Rename(fs.Join(oldpath,name),fs.Join(newpath,name))
if err!=nil{
return err
}
}
}
if !(oldMode=="RW" && !oldStat.IsDir()){ //So there was no os.Rename
setPermissions(oldPath,newPath)
err:=fs.Remove(oldpath)
if err!=nil{
return err
}
}
return nil
}
func (fs OverlayFS) Mkdir(path string, perm os.FileMode) error {
fmt.Println("Mkdir:",path)
filename,err:=fs.createPath(path)
if err!=nil{
return err
}
if _, err := os.Lstat(filename); !os.IsNotExist(err) { //Don't create if it already exists
return nil
}
defer fs.createErrorCheck(filename,path)
return os.Mkdir(filename, perm)
}
func (fs OverlayFS) MkdirAll(path string, perm os.FileMode) error {
fmt.Println("MkdirAll:",path)
dirs:=parentDirs(path)
dirs=append(dirs,path)
for _, dir := range dirs{
err:=fs.Mkdir(dir,perm)
if err!=nil{
return err
}
}
return nil
}
func (fs OverlayFS) Mknod(path string, mode uint32, major uint32, minor uint32) error {
fmt.Println("Mknod:",path)
filename,err:=fs.createPath(path)
if err!=nil{
return err
}
dev := unix.Mkdev(major, minor)
defer fs.createErrorCheck(filename,path)
return unix.Mknod(filename, mode, int(dev))
}
func (fs OverlayFS) Mkfifo(path string, mode uint32) error {
fmt.Println("Mkfifo:",path)
filename,err:=fs.createPath(path)
if err!=nil{
return err
}
defer fs.createErrorCheck(filename,path)
return unix.Mkfifo(filename, mode)
}
func (fs OverlayFS) Link(link string, path string) error {
if !fs.checkIfExists(link){
return nil
}
fmt.Println("Link:",path)
filename,err:=fs.createPath(path)
if err!=nil{
return err
}
defer fs.createErrorCheck(filename,path)
return unix.Link(fs.findFirstExisting(link), filename)
}
func (fs OverlayFS) Symlink(link string, path string) error {
fmt.Println("Symlink:",path)
filename,err:=fs.createPath(path)
if err!=nil{
fmt.Println("Symlink error: ",err)
return err
}
defer fs.createErrorCheck(filename,path)
return unix.Symlink(link, filename)
}
func (fs OverlayFS) Socket(path string) error {
fmt.Println("Socket:",path)
filename,err:=fs.createPath(path)
if err!=nil{
return err
}
fd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_STREAM, 0)
if err != nil {
return err
}
defer fs.createErrorCheck(filename,path)
return unix.Bind(fd, &unix.SockaddrUnix{Name: filename})
}