xrealwd() in unix.c in Go 1.4

50 views
Skip to first unread message

Chris Guthrey

unread,
Jul 19, 2024, 12:38:38 AM (3 days ago) Jul 19
to golang-nuts
Hi,

I'm trying to learn how the last version of Go that is bootstrapped from C works and as a learning exercise, maybe try porting to a new OS (on x86 for now)...   

So I'm trying to get my head around the xrealdwd() function  in /src/cmd/dist/unix.c - I just can't grok why it is needed. 

A quick search indicates that xrealdwd() is only ever used by build.c, and the comment in build.c says:

// xgetwd might return a path with symlinks fully resolved, and if
// there happens to be symlinks in goroot, then the hasprefix test
// will never succeed. Instead, we use xrealwd to get a canonical
// goroot/src before the comparison to avoid this problem.

I guess my understanding and knowledge of symlinks is lacking, but I guess i just don't understand why hasprefix() will never succeed if there happens to be symlinks in goroot...

The windows.c file also has a similar (at least similar in logic but not in OS calls) approach to xrealdwd() so I must assume that hasprefix() does not like symlinks on Windows either?

I think if this were ported to an OS that doesnt have symlinks, I might be able to get away with making xrealwd() always just return the current directory without trying to get a canonical path to goroot/src...  

Cheers,
Chris


Ian Lance Taylor

unread,
Jul 19, 2024, 12:46:06 AM (3 days ago) Jul 19
to Chris Guthrey, golang-nuts
On Thu, Jul 18, 2024 at 9:38 PM Chris Guthrey <ch...@fantail.net.nz> wrote:
>
> I'm trying to learn how the last version of Go that is bootstrapped from C works and as a learning exercise, maybe try porting to a new OS (on x86 for now)...
>
> So I'm trying to get my head around the xrealdwd() function in /src/cmd/dist/unix.c - I just can't grok why it is needed.
>
> A quick search indicates that xrealdwd() is only ever used by build.c, and the comment in build.c says:
>
> // xgetwd might return a path with symlinks fully resolved, and if
> // there happens to be symlinks in goroot, then the hasprefix test
> // will never succeed. Instead, we use xrealwd to get a canonical
> // goroot/src before the comparison to avoid this problem.
>
> I guess my understanding and knowledge of symlinks is lacking, but I guess i just don't understand why hasprefix() will never succeed if there happens to be symlinks in goroot...

hasprefix is a simple string comparison. If /home is a symlink to
/real/home, and GOROOT is /home/go, then xgetwd can return
/real/home/go which won't match /home/go.


> The windows.c file also has a similar (at least similar in logic but not in OS calls) approach to xrealdwd() so I must assume that hasprefix() does not like symlinks on Windows either?

Yes, again, hasprefix is a simple string comparison.

> I think if this were ported to an OS that doesnt have symlinks, I might be able to get away with making xrealwd() always just return the current directory without trying to get a canonical path to goroot/src...

Yes, I suppose. But I don't recommend following the path of porting
Go 1.4. Instead, port the current Go tip, and build it on a supported
machine, and then use that to cross-compile. See bootstrap.bash.

Ian
Reply all
Reply to author
Forward
0 new messages