mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-05 21:39:54 +02:00
On Windows, os.Symlink requires Developer Mode or admin privileges. Extract symlink creation into platform-specific files: on non-Windows, behavior is unchanged (os.Symlink). On Windows, try os.Symlink first, then fall back to directory junctions (mklink /J) for dirs and file copy for files. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
33 lines
827 B
Go
33 lines
827 B
Go
//go:build windows
|
|
|
|
package execenv
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
// createDirLink tries os.Symlink first (requires Developer Mode or admin on
|
|
// Windows). If that fails, it falls back to a directory junction (mklink /J)
|
|
// which works without elevated privileges.
|
|
func createDirLink(src, dst string) error {
|
|
if err := os.Symlink(src, dst); err == nil {
|
|
return nil
|
|
}
|
|
out, err := exec.Command("cmd", "/c", "mklink", "/J", dst, src).CombinedOutput()
|
|
if err != nil {
|
|
return fmt.Errorf("mklink /J %s %s: %s: %w", dst, src, out, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// createFileLink tries os.Symlink first. If that fails, it falls back to
|
|
// copying the file so the content is still available.
|
|
func createFileLink(src, dst string) error {
|
|
if err := os.Symlink(src, dst); err == nil {
|
|
return nil
|
|
}
|
|
return copyFile(src, dst)
|
|
}
|