forked from Azure/azure-storage-azcopy
/
sourceInfoProvider-Local_linux.go
234 lines (180 loc) · 5.81 KB
/
sourceInfoProvider-Local_linux.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
//go:build linux
// +build linux
package ste
import (
"fmt"
"strings"
"time"
"github.com/aymanjarrousms/azure-storage-file-go/azfile"
"github.com/johnmic/azure-storage-azcopy/v10/common"
"github.com/johnmic/azure-storage-azcopy/v10/sddl"
"golang.org/x/sys/unix"
)
func (f localFileSourceInfoProvider) HasUNIXProperties() bool {
return true
}
func (f localFileSourceInfoProvider) GetUNIXProperties() (common.UnixStatAdapter, error) {
{ // attempt to call statx, if ENOSYS is returned, statx is unavailable
var stat unix.Statx_t
// dirfd is a null pointer, because we should only ever be passing relative paths here, and directories will be passed via transferInfo.Source.
// AT_SYMLINK_NOFOLLOW is not used, because we automagically resolve symlinks. TODO: Add option to not follow symlinks, and use AT_SYMLINK_NOFOLLOW when resolving is disabled.
err := unix.Statx(0, f.transferInfo.Source,
unix.AT_STATX_SYNC_AS_STAT,
unix.STATX_ALL,
&stat)
if err != nil && err != unix.ENOSYS {
return nil, err
} else if err == nil {
return statxTAdapter(stat), nil
}
}
var stat unix.Stat_t
err := unix.Stat(f.transferInfo.Source, &stat)
if err != nil {
return nil, err
}
return statTAdapter(stat), nil
}
type statxTAdapter unix.Statx_t
func (s statxTAdapter) Extended() bool {
return true
}
func (s statxTAdapter) StatxMask() uint32 {
return s.Mask
}
func (s statxTAdapter) Attribute() uint64 {
return s.Attributes
}
func (s statxTAdapter) AttributeMask() uint64 {
return s.Attributes_mask
}
func (s statxTAdapter) BTime() time.Time {
return time.Unix(s.Btime.Sec, int64(s.Btime.Nsec))
}
func (s statxTAdapter) NLink() uint64 {
return uint64(s.Nlink)
}
func (s statxTAdapter) Owner() uint32 {
return s.Uid
}
func (s statxTAdapter) Group() uint32 {
return s.Gid
}
func (s statxTAdapter) FileMode() uint32 {
return uint32(s.Mode)
}
func (s statxTAdapter) INode() uint64 {
return s.Ino
}
func (s statxTAdapter) Device() uint64 {
return unix.Mkdev(s.Dev_major, s.Dev_minor)
}
func (s statxTAdapter) RDevice() uint64 {
return unix.Mkdev(s.Rdev_major, s.Rdev_minor)
}
func (s statxTAdapter) ATime() time.Time {
return time.Unix(s.Atime.Sec, int64(s.Atime.Nsec))
}
func (s statxTAdapter) MTime() time.Time {
return time.Unix(s.Mtime.Sec, int64(s.Mtime.Nsec))
}
func (s statxTAdapter) CTime() time.Time {
return time.Unix(s.Ctime.Sec, int64(s.Ctime.Nsec))
}
type statTAdapter unix.Stat_t
func (s statTAdapter) Extended() bool {
return false
}
func (s statTAdapter) StatxMask() uint32 {
return 0
}
func (s statTAdapter) Attribute() uint64 {
return 0
}
func (s statTAdapter) AttributeMask() uint64 {
return 0
}
func (s statTAdapter) BTime() time.Time {
return time.Time{}
}
func (s statTAdapter) NLink() uint64 {
return s.Nlink
}
func (s statTAdapter) Owner() uint32 {
return s.Uid
}
func (s statTAdapter) Group() uint32 {
return s.Gid
}
func (s statTAdapter) FileMode() uint32 {
return s.Mode
}
func (s statTAdapter) INode() uint64 {
return s.Ino
}
func (s statTAdapter) Device() uint64 {
return s.Dev
}
func (s statTAdapter) RDevice() uint64 {
return s.Rdev
}
func (s statTAdapter) ATime() time.Time {
return time.Unix(s.Atim.Unix())
}
func (s statTAdapter) MTime() time.Time {
return time.Unix(s.Mtim.Unix())
}
func (s statTAdapter) CTime() time.Time {
return time.Unix(s.Ctim.Unix())
}
// This file os-triggers the ISMBPropertyBearingSourceInfoProvider interface on a local SIP.
// Note: Linux SIP doesn't implement the ICustomLocalOpener since it doesn't need to do anything special, unlike
// Windows where we need to pass FILE_FLAG_BACKUP_SEMANTICS flag for opening file.
func (f localFileSourceInfoProvider) GetSDDL() (string, error) {
// We only need Owner, Group, and DACLs for azure files, CIFS_XATTR_CIFS_NTSD gets us that.
const securityInfoFlags sddl.SECURITY_INFORMATION = sddl.DACL_SECURITY_INFORMATION | sddl.OWNER_SECURITY_INFORMATION | sddl.GROUP_SECURITY_INFORMATION
// Query the Security Descriptor object for the given file.
sd, err := sddl.QuerySecurityObject(f.jptm.Info().Source, securityInfoFlags)
if err != nil {
return "", fmt.Errorf("sddl.QuerySecurityObject(%s, 0x%x) failed: %w",
f.jptm.Info().Source, securityInfoFlags, err)
}
// Convert the binary Security Descriptor to string in SDDL format.
// This is the Windows equivalent of ConvertSecurityDescriptorToStringSecurityDescriptorW().
sdStr, err := sddl.SecurityDescriptorToString(sd)
if err != nil {
// Panic, as it's unexpected and we would want to know.
panic(fmt.Errorf("Cannot parse binary Security Descriptor returned by QuerySecurityObject(%s, 0x%x): %v", f.jptm.Info().Source, securityInfoFlags, err))
}
fSDDL, err := sddl.ParseSDDL(sdStr)
if err != nil {
return "", fmt.Errorf("sddl.ParseSDDL(%s) failed: %w", sdStr, err)
}
if strings.TrimSpace(fSDDL.String()) != strings.TrimSpace(sdStr) {
panic("SDDL sanity check failed (parsed string output != original string)")
}
return fSDDL.PortableString(), nil
}
func (f localFileSourceInfoProvider) GetSMBProperties() (TypedSMBPropertyHolder, error) {
info, err := common.GetFileInformation(f.jptm.Info().Source)
return HandleInfo{info}, err
}
type HandleInfo struct {
common.ByHandleFileInformation
}
func (hi HandleInfo) FileCreationTime() time.Time {
// This returns nanoseconds since Unix Epoch.
return time.Unix(0, hi.CreationTime.Nanoseconds())
}
func (hi HandleInfo) FileLastWriteTime() time.Time {
// This returns nanoseconds since Unix Epoch.
return time.Unix(0, hi.LastWriteTime.Nanoseconds())
}
func (hi HandleInfo) FileChangeTime() time.Time {
// This returns nanoseconds since Unix Epoch.
return time.Unix(0, hi.LastChangeTime.Nanoseconds())
}
func (hi HandleInfo) FileAttributes() azfile.FileAttributeFlags {
// Can't shorthand it because the function name overrides.
return azfile.FileAttributeFlags(hi.ByHandleFileInformation.FileAttributes)
}