| 1 | |
| 2 | |
| 3 | |
| 4 | package copytool |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | "path/filepath" |
| 13 | "runtime" |
| 14 | "strings" |
| 15 | |
| 16 | "github.com/sirupsen/logrus" |
| 17 | |
| 18 | "github.com/lima-vm/lima/v2/pkg/fsutil" |
| 19 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 20 | "github.com/lima-vm/lima/v2/pkg/store" |
| 21 | ) |
| 22 | |
| 23 | type Backend string |
| 24 | |
| 25 | const ( |
| 26 | BackendAuto Backend = "auto" |
| 27 | BackendRsync Backend = "rsync" |
| 28 | BackendSCP Backend = "scp" |
| 29 | ) |
| 30 | |
| 31 | type Path struct { |
| 32 | InstanceName string |
| 33 | Path string |
| 34 | IsRemote bool |
| 35 | Instance *limatype.Instance |
| 36 | } |
| 37 | |
| 38 | |
| 39 | type Options struct { |
| 40 | Recursive bool |
| 41 | Verbose bool |
| 42 | AdditionalArgs []string |
| 43 | } |
| 44 | |
| 45 | |
| 46 | type CopyTool interface { |
| 47 | |
| 48 | Name() string |
| 49 | |
| 50 | Command(ctx context.Context, paths []string, opts *Options) (*exec.Cmd, error) |
| 51 | |
| 52 | IsAvailableOnGuest(ctx context.Context, paths []string) bool |
| 53 | } |
| 54 | |
| 55 | |
| 56 | func New(ctx context.Context, backend string, paths []string, opts *Options) (CopyTool, error) { |
| 57 | switch Backend(backend) { |
| 58 | case BackendSCP: |
| 59 | return newSCPTool(opts) |
| 60 | case BackendRsync: |
| 61 | rsync, err := newRsyncTool(opts) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | |
| 67 | if _, err := parseCopyPaths(ctx, paths); err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | if !rsync.IsAvailableOnGuest(ctx, paths) { |
| 71 | return nil, errors.New("rsync not available on guest(s)") |
| 72 | } |
| 73 | return rsync, nil |
| 74 | case BackendAuto: |
| 75 | |
| 76 | bothRemote, err := hasRemoteSourceAndDestination(ctx, paths) |
| 77 | if err != nil { |
| 78 | |
| 79 | |
| 80 | return nil, err |
| 81 | } |
| 82 | if !bothRemote { |
| 83 | rsync, err := newRsyncTool(opts) |
| 84 | if err == nil { |
| 85 | if rsync.IsAvailableOnGuest(ctx, paths) { |
| 86 | return rsync, nil |
| 87 | } |
| 88 | logrus.Debugf("rsync not available on guest(s), falling back to scp") |
| 89 | } else { |
| 90 | logrus.Debugf("rsync not found on host, falling back to scp: %v", err) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | tool, err := newSCPTool(opts) |
| 95 | if err != nil { |
| 96 | |
| 97 | |
| 98 | return nil, fmt.Errorf("no usable copy tool on host: %w", err) |
| 99 | } |
| 100 | return tool, nil |
| 101 | default: |
| 102 | return nil, fmt.Errorf("invalid backend %#q, must be one of: scp, rsync, auto", backend) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func hasRemoteSourceAndDestination(ctx context.Context, paths []string) (bool, error) { |
| 107 | copyPaths, err := parseCopyPaths(ctx, paths) |
| 108 | if err != nil { |
| 109 | return false, err |
| 110 | } |
| 111 | |
| 112 | var hasRemoteSource, hasRemoteDestination bool |
| 113 | for _, cp := range copyPaths { |
| 114 | if cp.IsRemote { |
| 115 | if hasRemoteSource { |
| 116 | hasRemoteDestination = true |
| 117 | } else { |
| 118 | hasRemoteSource = true |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return hasRemoteSource && hasRemoteDestination, nil |
| 124 | } |
| 125 | |
| 126 | func parseCopyPaths(ctx context.Context, paths []string) ([]*Path, error) { |
| 127 | var copyPaths []*Path |
| 128 | |
| 129 | for _, path := range paths { |
| 130 | cp := &Path{} |
| 131 | if runtime.GOOS == "windows" { |
| 132 | if filepath.IsAbs(path) { |
| 133 | var err error |
| 134 | path, err = fsutil.WindowsSubsystemPath(ctx, path) |
| 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | } else { |
| 139 | path = filepath.ToSlash(path) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | parts := strings.SplitN(path, ":", 2) |
| 144 | switch len(parts) { |
| 145 | case 1: |
| 146 | cp.Path = path |
| 147 | cp.IsRemote = false |
| 148 | case 2: |
| 149 | cp.InstanceName = parts[0] |
| 150 | cp.Path = parts[1] |
| 151 | cp.IsRemote = true |
| 152 | |
| 153 | inst, err := store.Inspect(ctx, cp.InstanceName) |
| 154 | if err != nil { |
| 155 | if errors.Is(err, os.ErrNotExist) { |
| 156 | return nil, fmt.Errorf("instance %#q does not exist, run `limactl create %s` to create a new instance", cp.InstanceName, cp.InstanceName) |
| 157 | } |
| 158 | return nil, err |
| 159 | } |
| 160 | if inst.Status == limatype.StatusStopped { |
| 161 | return nil, fmt.Errorf("instance %#q is stopped, run `limactl start %s` to start the instance", cp.InstanceName, cp.InstanceName) |
| 162 | } |
| 163 | cp.Instance = inst |
| 164 | default: |
| 165 | return nil, fmt.Errorf("path %#q contains multiple colons", path) |
| 166 | } |
| 167 | |
| 168 | copyPaths = append(copyPaths, cp) |
| 169 | } |
| 170 | |
| 171 | return copyPaths, nil |
| 172 | } |