Also provide a runnable example to illustrate that behavior.
This should help users to avoid the common mistake of expecting
os.Readlink to return an absolute path.
Fixes #57766.
Change-Id: I8f60aa111ebda0cae985758615019aaf26d5cb41
Reviewed-on: https://go-review.googlesource.com/c/go/+/546995
Auto-Submit: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
log.Fatal(err)
}
}
+
+func ExampleReadlink() {
+ // First, we create a relative symlink to a file.
+ d, err := os.MkdirTemp("", "")
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer os.RemoveAll(d)
+ targetPath := filepath.Join(d, "hello.txt")
+ if err := os.WriteFile(targetPath, []byte("Hello, Gophers!"), 0644); err != nil {
+ log.Fatal(err)
+ }
+ linkPath := filepath.Join(d, "hello.link")
+ if err := os.Symlink("hello.txt", filepath.Join(d, "hello.link")); err != nil {
+ if errors.Is(err, errors.ErrUnsupported) {
+ // Allow the example to run on platforms that do not support symbolic links.
+ fmt.Printf("%s links to %s\n", filepath.Base(linkPath), "hello.txt")
+ return
+ }
+ log.Fatal(err)
+ }
+
+ // Readlink returns the relative path as passed to os.Symlink.
+ dst, err := os.Readlink(linkPath)
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Printf("%s links to %s\n", filepath.Base(linkPath), dst)
+
+ var dstAbs string
+ if filepath.IsAbs(dst) {
+ dstAbs = dst
+ } else {
+ // Symlink targets are relative to the directory containing the link.
+ dstAbs = filepath.Join(filepath.Dir(linkPath), dst)
+ }
+
+ // Check that the target is correct by comparing it with os.Stat
+ // on the original target path.
+ dstInfo, err := os.Stat(dstAbs)
+ if err != nil {
+ log.Fatal(err)
+ }
+ targetInfo, err := os.Stat(targetPath)
+ if err != nil {
+ log.Fatal(err)
+ }
+ if !os.SameFile(dstInfo, targetInfo) {
+ log.Fatalf("link destination (%s) is not the same file as %s", dstAbs, targetPath)
+ }
+
+ // Output:
+ // hello.link links to hello.txt
+}
return rename(oldpath, newpath)
}
+// Readlink returns the destination of the named symbolic link.
+// If there is an error, it will be of type *PathError.
+//
+// If the link destination is relative, Readlink returns the relative path
+// without resolving it to an absolute one.
+func Readlink(name string) (string, error) {
+ return readlink(name)
+}
+
// Many functions in package syscall return a count of -1 instead of 0.
// Using fixCount(call()) instead of call() corrects the count.
func fixCount(n int, err error) (int, error) {
return &LinkError{"symlink", oldname, newname, syscall.EPLAN9}
}
-// Readlink returns the destination of the named symbolic link.
-// If there is an error, it will be of type *PathError.
-func Readlink(name string) (string, error) {
+func readlink(name string) (string, error) {
return "", &PathError{Op: "readlink", Path: name, Err: syscall.EPLAN9}
}
return nil
}
-// Readlink returns the destination of the named symbolic link.
-// If there is an error, it will be of type *PathError.
-func Readlink(name string) (string, error) {
+func readlink(name string) (string, error) {
for len := 128; ; len *= 2 {
b := make([]byte, len)
var (
return "", errors.New("GetFinalPathNameByHandle returned unexpected path: " + s)
}
-func readlink(path string) (string, error) {
+func readReparseLink(path string) (string, error) {
h, err := openSymlink(path)
if err != nil {
return "", err
}
}
-// Readlink returns the destination of the named symbolic link.
-// If there is an error, it will be of type *PathError.
-func Readlink(name string) (string, error) {
- s, err := readlink(fixLongPath(name))
+func readlink(name string) (string, error) {
+ s, err := readReparseLink(fixLongPath(name))
if err != nil {
return "", &PathError{Op: "readlink", Path: name, Err: err}
}