| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build !windows |
| 6 |
|
| 7 |
package testenv |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"runtime" |
| 14 |
"sync" |
| 15 |
) |
| 16 |
|
| 17 |
var hasSymlink = sync.OnceValues(func() (ok bool, reason string) { |
| 18 |
switch runtime.GOOS { |
| 19 |
case "plan9": |
| 20 |
return false, "" |
| 21 |
case "android", "wasip1": |
| 22 |
// For wasip1, some runtimes forbid absolute symlinks, |
| 23 |
// or symlinks that escape the current working directory. |
| 24 |
// Perform a simple test to see whether the runtime |
| 25 |
// supports symlinks or not. If we get a permission |
| 26 |
// error, the runtime does not support symlinks. |
| 27 |
dir, err := os.MkdirTemp("", "") |
| 28 |
if err != nil { |
| 29 |
return false, "" |
| 30 |
} |
| 31 |
defer func() { |
| 32 |
_ = os.RemoveAll(dir) |
| 33 |
}() |
| 34 |
fpath := filepath.Join(dir, "testfile.txt") |
| 35 |
if err := os.WriteFile(fpath, nil, 0644); err != nil { |
| 36 |
return false, "" |
| 37 |
} |
| 38 |
if err := os.Symlink(fpath, filepath.Join(dir, "testlink")); err != nil { |
| 39 |
if SyscallIsNotSupported(err) { |
| 40 |
return false, fmt.Sprintf("symlinks unsupported: %s", err.Error()) |
| 41 |
} |
| 42 |
return false, "" |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
return true, "" |
| 47 |
}) |