-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
os: Readlink should return destination even if link is broken #31996
Comments
I'm sorry, I don't understand what you mean. This program shows an example of reading a symlink whose destination does not exist, and it works without error. package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
func main() {
tmpdir, err := ioutil.TempDir("", "readlink")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmpdir)
from := filepath.Join(tmpdir, "from")
to := filepath.Join(tmpdir, "to")
if err := os.Symlink(to, from); err != nil {
panic(err)
}
fmt.Println(os.Readlink(from))
} |
/proc/PID/exe is not a normal symlink; it's a resource of procfs. This might reproduce it:
|
Thanks for the example. But when I try that on my system, |
@mfojtik, you say you're using What is your environment? |
Timed out in state WaitingForInfo. Closing. (I am just a bot, though. Please speak up if this is a mistake or you have the requested information.) |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
When reading a symlink with non-existing target the
os.Readlink
will return empty string and error. That makes reading the path of the target impossible.For example, if you are in container and use shared PID namespace (which means you share the
/proc
) and you want to get a PID, one way to do so is to follow the/proc/PID/exe
link.In case of shared PID namespace, if you don't use the same image for containers sharing the
/proc
, it will return broken link (which is OK). Unfortunately,os.Readlink
can't be used to get the destination string.What did you expect to see?
I want to see the destination of a symlink, even when
os.Readlink
is not able to follow the link. It is fine to return the error, but the destination can be returned as well.The caller can decide to ignore (or not to ignore) the returned destination.
There should be no breakage to existing code.
What did you see instead?
The
os.Readlink
return "", error on broken link.The text was updated successfully, but these errors were encountered: