-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsys_linux.go
More file actions
259 lines (211 loc) · 5.13 KB
/
Copy pathsys_linux.go
File metadata and controls
259 lines (211 loc) · 5.13 KB
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
//
//
// +build linux
package main
import (
"encoding/base64"
"io"
"os"
"strings"
"syscall"
"time"
"unsafe"
)
//
func cleanPath(p string) string {
return p
// return filepath.Clean(p)
}
//
const (
SYSCALL_O_NOATIME = syscall.O_NOATIME
)
//
func getRdevMajor(rdev uint64) uint64 {
return uint64((rdev >> 8) & 0xfff)
}
//
func getRdevMinor(rdev uint64) uint64 {
return uint64((rdev & 0xff) | ((rdev >> 12) & 0xfff00))
}
//
func getRdev(major uint64, minor uint64) uint64 {
return (major << 8) | (minor & 0xff) | ((minor & 0xfff00) << 12)
}
//
const (
AT_FDCWD uint = 0xFFFFFFFFFFFFFF9C
AT_SYMLINK_NOFOLLOW = 0x100
)
//
func lChtimes(name string, atime time.Time, mtime time.Time) (err error) {
var utimes [2]syscall.Timespec
utimes[0] = syscall.NsecToTimespec(atime.UnixNano())
utimes[1] = syscall.NsecToTimespec(mtime.UnixNano())
var _p0 *byte
_p0, err = syscall.BytePtrFromString(name)
if err != nil {
return
}
_, _, ierr := syscall.Syscall6(syscall.SYS_UTIMENSAT, uintptr(AT_FDCWD), uintptr(unsafe.Pointer(_p0)), uintptr(unsafe.Pointer((*[2]syscall.Timespec)(unsafe.Pointer(&utimes[0])))), uintptr(AT_SYMLINK_NOFOLLOW), 0, 0)
if ierr == 0 {
return nil
}
return err
}
//
func lockFile(f *os.File, exclusive bool) (err error) {
t := syscall.F_RDLCK
if exclusive {
t = syscall.F_WRLCK
}
flockT := syscall.Flock_t{
Type: int16(t),
Whence: io.SeekStart,
Start: 0,
Len: 0,
}
err = syscall.FcntlFlock(f.Fd(), syscall.F_SETLKW, &flockT)
return
}
//
func getMetaDataSys(m *Meta, path string, fi os.FileInfo) error {
// linux
var s syscall.Stat_t
syscall.Lstat(path, &s)
m.Device = s.Dev
m.Inode = s.Ino
m.Nlink = s.Nlink
m.Uid = int64(s.Uid)
m.Gid = int64(s.Gid)
m.Major = getRdevMajor(s.Rdev)
m.Minor = getRdevMinor(s.Rdev)
m.Atime, m.AtimeNs = s.Atim.Unix()
m.Ctime, m.CtimeNs = s.Ctim.Unix()
// symlink
if fi.Mode()&os.ModeType == os.ModeSymlink {
spath, err := os.Readlink(path)
if err != nil {
return err
}
if len(spath) == 0 {
return os.ErrInvalid
}
m.Symlink = spath
}
// extended attribute
exAttr = exAttr[:0]
if fi.Mode()&os.ModeType != os.ModeSymlink {
getExAttr(path, &exAttr)
}
m.ExAttr = exAttr
return nil
}
//
const (
SIZE_EXATTR = 1048576
)
var eaL []byte = make([]byte, SIZE_EXATTR, SIZE_EXATTR)
var eaB []byte = make([]byte, SIZE_EXATTR, SIZE_EXATTR)
//
func getExAttr(path string, exattr *[]byte) {
eaL = eaL[:SIZE_EXATTR]
eaB = eaB[:SIZE_EXATTR]
n, err := syscall.Listxattr(path, eaL)
if err != nil {
eMsg(E_ERR, nil, "can not get Extended Attributes")
return
}
if n == SIZE_EXATTR {
eMsg(E_ERR, nil, "Extended Attributes are too large")
return
}
eaL = eaL[:n]
ll := strings.Split(string(eaL), "\000")
for _, v := range ll {
if len(v) == 0 {
break
}
eaL = eaL[:SIZE_EXATTR]
eaB = eaB[:SIZE_EXATTR]
n, _ := syscall.Getxattr(path, v, eaL)
eaL = eaL[:n]
base64.StdEncoding.Encode(eaB, eaL)
eaB = eaB[:base64.StdEncoding.EncodedLen(len(eaL))]
*exattr = append(*exattr, []byte(v)...)
*exattr = append(*exattr, '\000')
*exattr = append(*exattr, eaB...)
*exattr = append(*exattr, '\000')
}
}
//
func setExAttr(path string, exattr []byte) {
ll := strings.Split(string(exattr), "\000")
for i := 0; i < len(ll); i += 2 {
k := ll[i]
if len(k) == 0 {
break
}
v, _ := base64.StdEncoding.DecodeString(ll[i+1])
syscall.Setxattr(path, k, v, 0)
}
}
//
func EncodeMetaSys(o *[]byte, ci *CipherInfo, m *Meta) {
EncodeMetaU(o, ID_DEVICE, uint64(m.Device))
EncodeMetaU(o, ID_INODE, uint64(m.Inode))
EncodeMetaU(o, ID_NLINK, uint64(m.Nlink))
EncodeMetaU(o, ID_UID, uint64(m.Uid))
EncodeMetaU(o, ID_GID, uint64(m.Gid))
EncodeMetaU(o, ID_MAJOR, uint64(m.Major))
EncodeMetaU(o, ID_MINOR, uint64(m.Minor))
EncodeMetaU(o, ID_ATIME, uint64(m.Atime))
EncodeMetaU(o, ID_ATIME_NS, uint64(m.AtimeNs))
EncodeMetaU(o, ID_CTIME, uint64(m.Ctime))
EncodeMetaU(o, ID_CTIME_NS, uint64(m.CtimeNs))
if len(m.Symlink) != 0 {
EncodeMetaB(o, ID_SYMLINK, []byte(m.Symlink))
}
if len(m.ExAttr) != 0 {
EncodeMetaB(o, ID_EX_ATTR, m.ExAttr)
}
}
//
func FinalizeFileSys(ci *CipherInfo, meta *Meta) (err error) {
mode := os.FileMode(meta.Mode)
switch mode & os.ModeType {
case os.ModeNamedPipe:
err = syscall.Mkfifo(meta.PathDst, 0600)
case os.ModeSocket:
eMsg(E_ERR, nil, "socket can not extract")
case os.ModeDevice:
m := uint32(syscall.S_IFBLK)
if mode&os.ModeCharDevice == os.ModeCharDevice {
m = uint32(syscall.S_IFCHR)
}
syscall.Mknod(meta.PathDst, m, int(getRdev(meta.Major, meta.Minor)))
}
mtime := time.Unix(meta.Mtime, meta.MtimeNs)
atime := time.Unix(meta.Atime, meta.AtimeNs)
// err = os.Chtimes(meta.PathDst, atime, mtime)
err = lChtimes(meta.PathDst, atime, mtime)
if err != nil {
eMsg(E_INFO, err, "%s", meta.PathDst)
err = nil
}
if (meta.Uid != -1) && (meta.Gid != -1) {
err = os.Lchown(meta.PathDst, int(meta.Uid), int(meta.Gid))
if err != nil {
eMsg(E_INFO, err, "")
err = nil
}
}
if mode&os.ModeType != os.ModeSymlink {
err = os.Chmod(meta.PathDst, os.FileMode(meta.Mode))
if err != nil {
eMsg(E_WARN, err, "%s", meta.PathDst)
}
setExAttr(meta.PathDst, meta.ExAttr)
}
return
}