mirror of
https://github.com/wavetermdev/waveterm
synced 2026-05-14 20:38:28 +00:00
This adds the ability to stream `tar` archives over channels between `wsh` instances. The main use cases for this are remote copy and move operations. It also completes the `wavefs` implementation of the FileShare interface to allow copy/move interoperability between wavefiles and other storage types. The tar streaming functionality has been broken out into the new `tarcopy` package for easy reuse. New `fileshare` functions are added for `CopyInternal`, which allows copying files internal to a filesystem to bypass the expensive interop layer, and `MoveInternal`, which does the same for moving a file within a filesystem. Copying between remotes is now handled by `CopyRemote`, which accepts the source `FileShareClient` as a parameter. `wsh` connections use the same implementation for `CopyInternal` and `CopyRemote` as they need to request the channel on the remote destination, since we don't offer a way to pass channels as a parameter to a remote call. This also adds a recursive `-r` flag to `wsh file rm` to allow for deleting a directory and all its contents. S3 support will be addressed in a future PR. --------- Co-authored-by: sawka <mike@commandline.dev> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
166 lines
3.9 KiB
Go
166 lines
3.9 KiB
Go
// Copyright 2025, Command Line Inc.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package connparse
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wshrpc"
|
|
"github.com/wavetermdev/waveterm/pkg/wshutil"
|
|
)
|
|
|
|
const (
|
|
ConnectionTypeWsh = "wsh"
|
|
ConnectionTypeS3 = "s3"
|
|
ConnectionTypeWave = "wavefile"
|
|
|
|
ConnHostCurrent = "current"
|
|
ConnHostWaveSrv = "wavesrv"
|
|
)
|
|
|
|
var windowsDriveRegex = regexp.MustCompile(`^[a-zA-Z]:`)
|
|
var wslConnRegex = regexp.MustCompile(`^wsl://[^/]+`)
|
|
|
|
type Connection struct {
|
|
Scheme string
|
|
Host string
|
|
Path string
|
|
}
|
|
|
|
func (c *Connection) GetSchemeParts() []string {
|
|
return strings.Split(c.Scheme, ":")
|
|
}
|
|
|
|
func (c *Connection) GetType() string {
|
|
lastInd := strings.LastIndex(c.Scheme, ":")
|
|
if lastInd == -1 {
|
|
return c.Scheme
|
|
}
|
|
return c.Scheme[lastInd+1:]
|
|
}
|
|
|
|
func (c *Connection) GetPathWithHost() string {
|
|
if c.Host == "" {
|
|
return ""
|
|
}
|
|
if strings.HasPrefix(c.Path, "/") {
|
|
return c.Host + c.Path
|
|
}
|
|
return c.Host + "/" + c.Path
|
|
}
|
|
|
|
func (c *Connection) GetFullURI() string {
|
|
return c.Scheme + "://" + c.GetPathWithHost()
|
|
}
|
|
|
|
func (c *Connection) GetSchemeAndHost() string {
|
|
return c.Scheme + "://" + c.Host
|
|
}
|
|
|
|
func ParseURIAndReplaceCurrentHost(ctx context.Context, uri string) (*Connection, error) {
|
|
conn, err := ParseURI(uri)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error parsing connection: %v", err)
|
|
}
|
|
if conn.Host == ConnHostCurrent {
|
|
source, err := GetConnNameFromContext(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error getting connection name from context: %v", err)
|
|
}
|
|
|
|
// RPC context connection is empty for local connections
|
|
if source == "" {
|
|
source = wshrpc.LocalConnName
|
|
}
|
|
conn.Host = source
|
|
}
|
|
return conn, nil
|
|
}
|
|
|
|
func GetConnNameFromContext(ctx context.Context) (string, error) {
|
|
handler := wshutil.GetRpcResponseHandlerFromContext(ctx)
|
|
if handler == nil {
|
|
return "", fmt.Errorf("error getting rpc response handler from context")
|
|
}
|
|
return handler.GetRpcContext().Conn, nil
|
|
}
|
|
|
|
// ParseURI parses a connection URI and returns the connection type, host/path, and parameters.
|
|
func ParseURI(uri string) (*Connection, error) {
|
|
split := strings.SplitN(uri, "//", 2)
|
|
var scheme string
|
|
var rest string
|
|
if len(split) > 1 {
|
|
scheme = strings.TrimSuffix(split[0], ":")
|
|
rest = split[1]
|
|
} else {
|
|
rest = split[0]
|
|
}
|
|
|
|
var host string
|
|
var remotePath string
|
|
|
|
parseGenericPath := func() {
|
|
split = strings.SplitN(rest, "/", 2)
|
|
host = split[0]
|
|
if len(split) > 1 {
|
|
remotePath = split[1]
|
|
} else {
|
|
split = strings.SplitN(rest, "/", 2)
|
|
host = split[0]
|
|
if len(split) > 1 {
|
|
remotePath = split[1]
|
|
} else {
|
|
remotePath = "/"
|
|
}
|
|
}
|
|
}
|
|
parseWshPath := func() {
|
|
if strings.HasPrefix(rest, "wsl://") {
|
|
host = wslConnRegex.FindString(rest)
|
|
remotePath = strings.TrimPrefix(rest, host)
|
|
} else {
|
|
parseGenericPath()
|
|
}
|
|
}
|
|
|
|
if scheme == "" {
|
|
scheme = ConnectionTypeWsh
|
|
if len(rest) != len(uri) {
|
|
// This accounts for when the uri starts with "//", which would get trimmed in the first split.
|
|
parseWshPath()
|
|
} else if strings.HasPrefix(rest, "/~") {
|
|
host = wshrpc.LocalConnName
|
|
remotePath = rest
|
|
} else {
|
|
host = ConnHostCurrent
|
|
remotePath = rest
|
|
}
|
|
} else if scheme == ConnectionTypeWsh {
|
|
parseWshPath()
|
|
} else {
|
|
parseGenericPath()
|
|
}
|
|
|
|
if scheme == ConnectionTypeWsh {
|
|
if host == "" {
|
|
host = wshrpc.LocalConnName
|
|
}
|
|
if strings.HasPrefix(remotePath, "/~") {
|
|
remotePath = strings.TrimPrefix(remotePath, "/")
|
|
} else if len(remotePath) > 1 && !windowsDriveRegex.MatchString(remotePath) && !strings.HasPrefix(remotePath, "/") && !strings.HasPrefix(remotePath, "~") && !strings.HasPrefix(remotePath, "./") && !strings.HasPrefix(remotePath, "../") && !strings.HasPrefix(remotePath, ".\\") && !strings.HasPrefix(remotePath, "..\\") && remotePath != ".." {
|
|
remotePath = "/" + remotePath
|
|
}
|
|
}
|
|
|
|
conn := &Connection{
|
|
Scheme: scheme,
|
|
Host: host,
|
|
Path: remotePath,
|
|
}
|
|
return conn, nil
|
|
}
|