Skip to content
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: TempDir() different between OSx and Linux #21318

Closed
wrfly opened this issue Aug 5, 2017 · 10 comments
Closed

os: TempDir() different between OSx and Linux #21318

wrfly opened this issue Aug 5, 2017 · 10 comments

Comments

@wrfly
Copy link

@wrfly wrfly commented Aug 5, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version 1.8.3

What operating system and processor architecture are you using (go env)?

Linux and mac OS

What did you do?

package main

import (
	"fmt"
	"os"
)

func main() {
	fmt.Println(os.TempDir())
}

What did you expect to see?

Linux and MacOS has the same output, which they both has no slash in the end.

What did you see instead?

go in OSX has a slash in fmt.Println(os.TempDir())

@davecheney

This comment has been minimized.

Copy link
Contributor

@davecheney davecheney commented Aug 5, 2017

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Aug 5, 2017

Change https://golang.org/cl/53412 mentions this issue: os/file: trim the last slash of $TMPDIR``

@wrfly

This comment has been minimized.

Copy link
Author

@wrfly wrfly commented Aug 5, 2017

hello @davecheney
The shell knows nothing... Just blank output.

But if fmt.Println(os.TempDir()), OS X will tell you /var/folders/t9/uaiyebqwpojxbaywgebzmx/T/, while the Unix told you /tmp.

If you just want to get the temp directory, that's fine, but once you want to get the file name you created in the temp directory, and trim the temp dir, it will be different.

https://play.golang.org/p/yz4DLEPoNr

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

func main() {
	file, _ := ioutil.TempFile(os.TempDir(), "file-")
	fp := strings.TrimPrefix(file.Name(), os.TempDir())
	fmt.Println(file.Name())
	fmt.Println(fp)
}

In the Unix systems, you'll get:

/tmp/file-054003078
/file-054003078

But in OS X:

/var/folders/t9/uaiyebqwpojxbaywgebzmx/T/file-054003078
file-054003078

I got a same A, but a different B.

@davecheney

This comment has been minimized.

Copy link
Contributor

@davecheney davecheney commented Aug 5, 2017

@wrfly

This comment has been minimized.

Copy link
Author

@wrfly wrfly commented Aug 5, 2017

For serving a file:
If the temp file name is /temp-file-in-temp-dir, and the server's path is /var/www/files, I can combine it with df := fmt.Sprintf("%s%s",path,filename) in Unix, but in OS X, it will be df := fmt.Sprintf("%s/%s",path,filename) since the filename doesn't contains the first slash.

It's not good that a same program behave inconsistently on different systems.

@davecheney

This comment has been minimized.

Copy link
Contributor

@davecheney davecheney commented Aug 5, 2017

@ulikunitz

This comment has been minimized.

Copy link
Contributor

@ulikunitz ulikunitz commented Aug 5, 2017

While I would also recommend filepath.Join, use always "%s/%s" with printf. UNIX treats "a//b" as "a/b" and therefore "%s/%s" works regardless whether the first path is terminated by a slash or not.

Even if we would define that TempDir should always return a cleaned path as produced by filepath.Clean you couldn't assume that it never ends in a slash, because it could return the root path "/".

@ALTree ALTree changed the title os.TempDir() different between OSx and Linux os: TempDir() different between OSx and Linux Aug 5, 2017
@wrfly

This comment has been minimized.

Copy link
Author

@wrfly wrfly commented Aug 5, 2017

That's a little embarrassed.... I've even created a code-review request in Gerrit....

BTW, is there any standard for return a directory's name?

Thanks you guys.

@ulikunitz

This comment has been minimized.

Copy link
Contributor

@ulikunitz ulikunitz commented Aug 5, 2017

I think that is the definition that is relevant:

http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_266

A pathname may optionally contain one or more trailing slashes. All what TempDir is doing is returning a pathname.

@wrfly

This comment has been minimized.

Copy link
Author

@wrfly wrfly commented Aug 5, 2017

Got that, thank you very much! @ulikunitz @davecheney

@wrfly wrfly closed this Aug 5, 2017
@golang golang locked and limited conversation to collaborators Aug 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.