| 1 |
//go:build !windows |
| 2 |
// +build !windows |
| 3 |
|
| 4 |
package desync |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"os" |
| 10 |
"path" |
| 11 |
"path/filepath" |
| 12 |
"syscall" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/pkg/xattr" |
| 16 |
) |
| 17 |
|
| 18 |
// NewLocalFS initializes a new instance of a local filesystem that |
| 19 |
// can be used for tar/untar operations. |
| 20 |
func NewLocalFS(root string, opts LocalFSOptions) *LocalFS { |
| 21 |
return &LocalFS{ |
| 22 |
Root: root, |
| 23 |
opts: opts, |
| 24 |
entries: make(chan walkEntry), |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
// setXattrs applies extended attributes to a regular file or directory using |
| 29 |
// an fd opened through the root handle, so that no symlink in the path can be |
| 30 |
// followed. |
| 31 |
func setXattrs(r *os.Root, name string, xattrs Xattrs) error { |
| 32 |
if len(xattrs) == 0 { |
| 33 |
return nil |
| 34 |
} |
| 35 |
f, err := r.Open(name) |
| 36 |
if err != nil { |
| 37 |
return fmt.Errorf("%s: %w", name, err) |
| 38 |
} |
| 39 |
defer f.Close() |
| 40 |
for key, value := range xattrs { |
| 41 |
if err := xattr.FSet(f, key, []byte(value)); err != nil { |
| 42 |
return fmt.Errorf("%s: %w", name, err) |
| 43 |
} |
| 44 |
} |
| 45 |
return nil |
| 46 |
} |
| 47 |
|
| 48 |
// setXattrsNoFollow applies extended attributes to a symlink or device node. |
| 49 |
// There is no fd-based equivalent for these, so a path under the resolved real |
| 50 |
// root is used. All intermediate components were created by us through the |
| 51 |
// root handle and are therefore confined to it. |
| 52 |
func (fs *LocalFS) setXattrsNoFollow(name string, xattrs Xattrs) error { |
| 53 |
if len(xattrs) == 0 { |
| 54 |
return nil |
| 55 |
} |
| 56 |
dst := filepath.Join(fs.rootReal, name) |
| 57 |
for key, value := range xattrs { |
| 58 |
if err := xattr.LSet(dst, key, []byte(value)); err != nil { |
| 59 |
return fmt.Errorf("%s: %w", name, err) |
| 60 |
} |
| 61 |
} |
| 62 |
return nil |
| 63 |
} |
| 64 |
|
| 65 |
// prepareDirWrite gives us the permissions needed to populate a directory, |
| 66 |
// which the mode in the archive may not include. GNU tar does the same. The |
| 67 |
// mode from the archive is restored by applyDirMetadata() once the directory |
| 68 |
// is complete. Best-effort, if it fails the write that needs the permissions |
| 69 |
// reports the real error. |
| 70 |
func (fs *LocalFS) prepareDirWrite(r *os.Root, n NodeDirectory, existing os.FileMode, created bool) { |
| 71 |
if fs.opts.NoSamePermissions { |
| 72 |
// Nothing to restore later, so only relax what has to be relaxed. |
| 73 |
// Newly created directories use the umask default and are writable. |
| 74 |
if !created && existing&0300 != 0300 { |
| 75 |
_ = r.Chmod(n.Name, existing|0700) |
| 76 |
} |
| 77 |
return |
| 78 |
} |
| 79 |
// Newly created directories are chmod'ed as well, both to undo a umask |
| 80 |
// that stripped the bits we need and to set the setuid/setgid/sticky bits |
| 81 |
// that mkdir doesn't apply. A setgid directory has to carry that bit while |
| 82 |
// its contents are created for them to inherit the group. |
| 83 |
if created || existing&0300 != 0300 { |
| 84 |
_ = r.Chmod(n.Name, n.Mode|0700) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func (fs *LocalFS) SetDirPermissions(n NodeDirectory) error { |
| 89 |
r, err := fs.writeRoot() |
| 90 |
if err != nil { |
| 91 |
return err |
| 92 |
} |
| 93 |
|
| 94 |
// The dir exists now, fix the UID/GID if needed |
| 95 |
if !fs.opts.NoSameOwner { |
| 96 |
if err := r.Chown(n.Name, n.UID, n.GID); err != nil { |
| 97 |
return fmt.Errorf("%s: %w", n.Name, err) |
| 98 |
} |
| 99 |
if err := setXattrs(r, n.Name, n.Xattrs); err != nil { |
| 100 |
return err |
| 101 |
} |
| 102 |
} |
| 103 |
if !fs.opts.NoSamePermissions { |
| 104 |
if err := r.Chmod(n.Name, n.Mode); err != nil { |
| 105 |
return fmt.Errorf("%s: %w", n.Name, err) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return nil |
| 110 |
} |
| 111 |
|
| 112 |
func (fs *LocalFS) SetFilePermissions(n NodeFile) error { |
| 113 |
r, err := fs.writeRoot() |
| 114 |
if err != nil { |
| 115 |
return err |
| 116 |
} |
| 117 |
|
| 118 |
if !fs.opts.NoSameOwner { |
| 119 |
if err := r.Chown(n.Name, n.UID, n.GID); err != nil { |
| 120 |
return fmt.Errorf("%s: %w", n.Name, err) |
| 121 |
} |
| 122 |
if err := setXattrs(r, n.Name, n.Xattrs); err != nil { |
| 123 |
return err |
| 124 |
} |
| 125 |
} |
| 126 |
if !fs.opts.NoSamePermissions { |
| 127 |
if err := r.Chmod(n.Name, n.Mode); err != nil { |
| 128 |
return fmt.Errorf("%s: %w", n.Name, err) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return nil |
| 133 |
} |
| 134 |
|
| 135 |
func (fs *LocalFS) SetSymlinkPermissions(n NodeSymlink) error { |
| 136 |
r, err := fs.writeRoot() |
| 137 |
if err != nil { |
| 138 |
return err |
| 139 |
} |
| 140 |
|
| 141 |
// TODO: On Linux, the permissions of the link don't matter so we don't |
| 142 |
// set them here. But they do matter somewhat on Mac, so should probably |
| 143 |
// add some Mac-specific logic for that here. |
| 144 |
// fchmodat() with flag AT_SYMLINK_NOFOLLOW |
| 145 |
if !fs.opts.NoSameOwner { |
| 146 |
if err := r.Lchown(n.Name, n.UID, n.GID); err != nil { |
| 147 |
return fmt.Errorf("%s: %w", n.Name, err) |
| 148 |
} |
| 149 |
if err := fs.setXattrsNoFollow(n.Name, n.Xattrs); err != nil { |
| 150 |
return err |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
return nil |
| 155 |
} |
| 156 |
|
| 157 |
func (fs *LocalFS) CreateDevice(n NodeDevice) error { |
| 158 |
r, err := fs.writeRoot() |
| 159 |
if err != nil { |
| 160 |
return err |
| 161 |
} |
| 162 |
|
| 163 |
if err := fs.completeDirs(n.Name); err != nil { |
| 164 |
return err |
| 165 |
} |
| 166 |
|
| 167 |
if err := r.Remove(n.Name); err != nil && !os.IsNotExist(err) { |
| 168 |
return fmt.Errorf("%s: %w", n.Name, err) |
| 169 |
} |
| 170 |
|
| 171 |
// os.Root has no Mknod. createDeviceNode is implemented per platform but |
| 172 |
// always confines the operation to the extraction root. |
| 173 |
if err := fs.createDeviceNode(r, n); err != nil { |
| 174 |
return fmt.Errorf("mknod %s: %w", n.Name, err) |
| 175 |
} |
| 176 |
|
| 177 |
if !fs.opts.NoSameOwner { |
| 178 |
if err := r.Chown(n.Name, n.UID, n.GID); err != nil { |
| 179 |
return fmt.Errorf("%s: %w", n.Name, err) |
| 180 |
} |
| 181 |
if err := fs.setXattrsNoFollow(n.Name, n.Xattrs); err != nil { |
| 182 |
return err |
| 183 |
} |
| 184 |
} |
| 185 |
if !fs.opts.NoSamePermissions { |
| 186 |
if err := r.Chmod(n.Name, n.Mode); err != nil { |
| 187 |
return fmt.Errorf("chmod %s: %w", n.Name, err) |
| 188 |
} |
| 189 |
} |
| 190 |
if n.MTime == time.Unix(0, 0) { |
| 191 |
return nil |
| 192 |
} |
| 193 |
return r.Chtimes(n.Name, n.MTime, n.MTime) |
| 194 |
} |
| 195 |
|
| 196 |
func mkdev(major, minor uint64) uint64 { |
| 197 |
dev := (major & 0x00000fff) << 8 |
| 198 |
dev |= (major & 0xfffff000) << 32 |
| 199 |
dev |= (minor & 0x000000ff) << 0 |
| 200 |
dev |= (minor & 0xffffff00) << 12 |
| 201 |
return dev |
| 202 |
} |
| 203 |
|
| 204 |
// Next returns the next filesystem entry or io.EOF when done. The caller is responsible |
| 205 |
// for closing the returned File object. |
| 206 |
func (fs *LocalFS) Next() (*File, error) { |
| 207 |
fs.once.Do(func() { |
| 208 |
fs.initForReading() |
| 209 |
}) |
| 210 |
|
| 211 |
entry, ok := <-fs.entries |
| 212 |
if !ok { |
| 213 |
return nil, fs.sErr |
| 214 |
} |
| 215 |
if entry.err != nil { |
| 216 |
return nil, entry.err |
| 217 |
} |
| 218 |
|
| 219 |
var ( |
| 220 |
uid, gid int |
| 221 |
major, minor uint64 |
| 222 |
) |
| 223 |
switch sys := entry.info.Sys().(type) { |
| 224 |
case *syscall.Stat_t: |
| 225 |
uid = int(sys.Uid) |
| 226 |
gid = int(sys.Gid) |
| 227 |
major = uint64((sys.Rdev >> 8) & 0xfff) |
| 228 |
minor = (uint64(sys.Rdev) % 256) | ((uint64(sys.Rdev) & 0xfff00000) >> 12) |
| 229 |
default: |
| 230 |
panic("unsupported platform") |
| 231 |
} |
| 232 |
|
| 233 |
// Extract the Xattrs if any |
| 234 |
xa := make(map[string]string) |
| 235 |
keys, err := xattr.LList(entry.path) |
| 236 |
if err != nil { |
| 237 |
return nil, err |
| 238 |
} |
| 239 |
for _, key := range keys { |
| 240 |
value, err := xattr.LGet(entry.path, key) |
| 241 |
if err != nil { |
| 242 |
return nil, err |
| 243 |
} |
| 244 |
xa[key] = string(value) |
| 245 |
} |
| 246 |
|
| 247 |
// If it's a file, open it and return a ReadCloser |
| 248 |
var r io.ReadCloser |
| 249 |
if entry.info.Mode().IsRegular() { |
| 250 |
data, err := os.Open(entry.path) |
| 251 |
if err != nil { |
| 252 |
return nil, err |
| 253 |
} |
| 254 |
r = data |
| 255 |
} |
| 256 |
|
| 257 |
// If this is a symlink we need to get the link target |
| 258 |
var linkTarget string |
| 259 |
if entry.info.Mode()&os.ModeSymlink != 0 { |
| 260 |
linkTarget, err = os.Readlink(entry.path) |
| 261 |
if err != nil { |
| 262 |
return nil, err |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
mtime := entry.info.ModTime() |
| 267 |
if fs.opts.NoTime { |
| 268 |
mtime = time.Unix(0, 0) |
| 269 |
} |
| 270 |
|
| 271 |
f := &File{ |
| 272 |
Name: entry.info.Name(), |
| 273 |
Path: path.Clean(entry.path), |
| 274 |
Mode: entry.info.Mode(), |
| 275 |
ModTime: mtime, |
| 276 |
Size: uint64(entry.info.Size()), |
| 277 |
LinkTarget: linkTarget, |
| 278 |
Uid: uid, |
| 279 |
Gid: gid, |
| 280 |
Xattrs: xa, |
| 281 |
DevMajor: major, |
| 282 |
DevMinor: minor, |
| 283 |
Data: r, |
| 284 |
} |
| 285 |
|
| 286 |
return f, nil |
| 287 |
} |
| 288 |
|
| 289 |
func (fs *LocalFS) initForReading() { |
| 290 |
if fs.opts.OneFileSystem { |
| 291 |
info, err := os.Lstat(fs.Root) |
| 292 |
if err == nil { |
| 293 |
st, ok := info.Sys().(*syscall.Stat_t) |
| 294 |
if ok { |
| 295 |
// Dev (and Rdev) elements of syscall.Stat_t are uint64 on Linux, |
| 296 |
// but int32 on MacOS. Cast it to uint64 everywhere. |
| 297 |
fs.dev = uint64(st.Dev) |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
fs.startSerializer() |
| 302 |
} |
| 303 |
|
| 304 |
func (fs *LocalFS) startSerializer() { |
| 305 |
go func() { |
| 306 |
err := filepath.Walk(fs.Root, func(path string, info os.FileInfo, err error) error { |
| 307 |
if fs.dev != 0 && info.IsDir() { |
| 308 |
// one-file-system is set, skip other filesystems |
| 309 |
st, ok := info.Sys().(*syscall.Stat_t) |
| 310 |
if ok && uint64(st.Dev) != fs.dev { |
| 311 |
return nil |
| 312 |
} |
| 313 |
} |
| 314 |
fs.entries <- walkEntry{path, info, err} |
| 315 |
return nil |
| 316 |
}) |
| 317 |
fs.sErr = err |
| 318 |
if err == nil { |
| 319 |
fs.sErr = io.EOF |
| 320 |
} |
| 321 |
close(fs.entries) |
| 322 |
}() |
| 323 |
} |