The local backend built every OS path by joining the root with the source name converted through the configured encoding, so the encoding was the only thing keeping a name from turning into path syntax. With an encoding which omits Dot (Slash, None, Raw) rclone's standard ".." decodes back to a real "..", and with an encoding which omits BackSlash a name like "..\file" becomes a native path on Windows. filepath.Join then resolved those out of the destination the user chose, so a source object called "../marker.txt" - an s3 key of "tenant/../marker.txt" listed with the remote rooted at "tenant", say - created or overwrote a file outside it. localPath now joins the name to the root and checks with filepath.Rel that the result is still inside it. localPath is the only place the root is joined to a name, so threading the error through newObject and newDirectory covers every operation. Default configurations were not affected, as encoder.OS includes Dot on all platforms and BackSlash on Windows. Fixes GHSA-7p4m-qxvv-g567
101 lines
2.6 KiB
Go
101 lines
2.6 KiB
Go
//go:build darwin && cgo
|
|
|
|
// Package local provides a filesystem interface
|
|
package local
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
"github.com/go-darwin/apfs"
|
|
"github.com/rclone/rclone/fs"
|
|
)
|
|
|
|
// Copy src to this remote using server-side copy operations.
|
|
//
|
|
// # This is stored with the remote path given
|
|
//
|
|
// # It returns the destination Object and a possible error
|
|
//
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
//
|
|
// If it isn't possible then return fs.ErrorCantCopy
|
|
func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (fs.Object, error) {
|
|
if runtime.GOOS != "darwin" || f.opt.NoClone {
|
|
return nil, fs.ErrorCantCopy
|
|
}
|
|
srcObj, ok := src.(*Object)
|
|
if !ok {
|
|
fs.Debugf(src, "Can't clone - not same remote type")
|
|
return nil, fs.ErrorCantCopy
|
|
}
|
|
if f.opt.TranslateSymlinks && srcObj.translatedLink { // in --links mode, use cloning only for regular files
|
|
return nil, fs.ErrorCantCopy
|
|
}
|
|
|
|
// Fetch metadata if --metadata is in use
|
|
meta, err := fs.GetMetadataOptions(ctx, f, src, fs.MetadataAsOpenOptions(ctx))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("copy: failed to read metadata: %w", err)
|
|
}
|
|
|
|
// Create destination
|
|
dstObj, err := f.newObject(remote)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = dstObj.mkdirAll()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
srcPath := srcObj.path
|
|
if f.opt.FollowSymlinks { // in --copy-links mode, find the real file being pointed to and pass that in instead
|
|
srcPath, err = filepath.EvalSymlinks(srcPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
dstPath, err := f.localPath(remote)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = Clone(srcPath, dstPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Set metadata if --metadata is in use
|
|
if meta != nil {
|
|
err = dstObj.writeMetadata(meta)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("copy: failed to set metadata: %w", err)
|
|
}
|
|
}
|
|
|
|
return f.NewObject(ctx, remote)
|
|
}
|
|
|
|
// Clone uses APFS cloning if possible, otherwise falls back to copying (with full metadata preservation)
|
|
// note that this is closely related to unix.Clonefile(src, dst, unix.CLONE_NOFOLLOW) but not 100% identical
|
|
// https://opensource.apple.com/source/copyfile/copyfile-173.40.2/copyfile.c.auto.html
|
|
func Clone(src, dst string) error {
|
|
state := apfs.CopyFileStateAlloc()
|
|
defer func() {
|
|
if err := apfs.CopyFileStateFree(state); err != nil {
|
|
fs.Errorf(dst, "free state error: %v", err)
|
|
}
|
|
}()
|
|
cloned, err := apfs.CopyFile(src, dst, state, apfs.COPYFILE_CLONE)
|
|
fs.Debugf(dst, "isCloned: %v, error: %v", cloned, err)
|
|
return err
|
|
}
|
|
|
|
// Check the interfaces are satisfied
|
|
var (
|
|
_ fs.Copier = &Fs{}
|
|
)
|