跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

fsutil_windows.go

78 行 · 3.3 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package fsutil
5
6import (
7 "context"
8 "errors"
9 "fmt"
10 "io/fs"
11 "os/exec"
12 "path/filepath"
13 "strings"
14
15 "github.com/sirupsen/logrus"
16)
17
18// WindowsSubsystemPath converts a Windows path to a Cygwin/MSYS form
19// (C:\Users\jan -> /c/Users/jan) via cygpath on PATH. A caller holding a
20// specific ssh toolchain should use WindowsSubsystemPathWithCygpath, which
21// runs that toolchain's own cygpath and respects its fstab.
22func WindowsSubsystemPath(ctx context.Context, orig string) (string, error) {
23 return WindowsSubsystemPathWithCygpath(ctx, "cygpath", orig)
24}
25
26// WindowsSubsystemPathWithCygpath converts a Windows path with the given
27// cygpathExe ("cygpath" for PATH, or an absolute path to bind the conversion
28// to one Cygwin install). When the cygpath binary is not found it falls back
29// to a native conversion of the absolute drive-letter case; a cygpath that
30// runs and fails returns its error, and non-drive-letter inputs (UNC, device,
31// extended-length) return an error.
32func WindowsSubsystemPathWithCygpath(ctx context.Context, cygpathExe, orig string) (string, error) {
33 out, err := exec.CommandContext(ctx, cygpathExe, filepath.ToSlash(orig)).CombinedOutput()
34 if err == nil {
35 return strings.TrimSpace(string(out)), nil
36 }
37 if !errors.Is(err, exec.ErrNotFound) && !errors.Is(err, fs.ErrNotExist) {
38 return "", fmt.Errorf("failed to run %#q on %#q: %#q: %w", cygpathExe, orig, strings.TrimSpace(string(out)), err)
39 }
40
41 logrus.WithError(err).Debugf("%#q not found for %#q, attempting native conversion", cygpathExe, orig)
42
43 return windowsSubsystemPathWithoutCygpath(orig)
44}
45
46func windowsSubsystemPathWithoutCygpath(orig string) (string, error) {
47 // The /c/... form this produces is the MSYS2 and Git-for-Windows
48 // convention; stock Cygwin defaults to /cygdrive/c/. Only an absolute
49 // drive-letter path ("C:\foo") has a well-defined form here. A
50 // drive-relative path ("C:foo") would become an unrelated absolute
51 // path, so reject it.
52 if !filepath.IsAbs(orig) {
53 return "", fmt.Errorf("cannot convert %#q to an MSYS-style path: input is not an absolute drive-letter path", orig)
54 }
55
56 // UNC path ("\\server\share\foo") is rejected here.
57 if vol := filepath.VolumeName(orig); len(vol) == 2 && vol[1] == ':' {
58 // orig[2:] starts with a separator for an absolute drive path
59 // (C:\foo, C:/foo); strip it so the result stays canonical.
60 tail := strings.TrimPrefix(filepath.ToSlash(orig[2:]), "/")
61 converted := "/" + strings.ToLower(vol[:1]) + "/" + tail
62 logrus.Debugf("native cygpath fallback: %#q -> %#q", orig, converted)
63 return converted, nil
64 }
65
66 return "", fmt.Errorf("cannot convert %#q to an MSYS-style path: input is not an absolute drive-letter path", orig)
67}
68
69// WindowsSubsystemPathForLinux converts a Windows path to the WSL form of
70// the given distro (e.g. C:\Users\jan -> /mnt/c/Users/jan) via wsl.exe.
71func WindowsSubsystemPathForLinux(ctx context.Context, orig, distro string) (string, error) {
72 out, err := exec.CommandContext(ctx, "wsl", "-d", distro, "--exec", "wslpath", filepath.ToSlash(orig)).CombinedOutput()
73 if err != nil {
74 logrus.WithError(err).Errorf("failed to convert path to mingw, maybe wsl command is not operational?")
75 return "", err
76 }
77 return strings.TrimSpace(string(out)), nil
78}