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

io/ioutil: document that TempDir's "prefix" should not contain a slash #10003

Closed
peterstace opened this issue Feb 25, 2015 · 8 comments
Closed
Milestone

Comments

@peterstace
Copy link

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

  • go version go1.4.2 windows/amd64

What operating system and processor architecture are you using?

  • Windows 7
  • AMD64

What did you do?

  • Ran the following program using go run bug.go. This is a minimal reproduction of the problem -- the real situation occurred when username given by the os/user package is embedded within the prefix. On Windows, the username often contains backslashes.
package main

import (
    "io/ioutil"
    "log"
)

func main() {
    dir, err := ioutil.TempDir("", `foo\bar`)
    log.Print(dir, err)
}

What did you expect to see?

  • Any number of things would be fine, e.g.:
  • 2015/02/26 10:01:04 C:\Users\petsta\AppData\Local\Temp\foo\bar428871999<nil>
  • 2015/02/26 10:01:04 C:\Users\petsta\AppData\Local\Temp\foo_bar428871999<nil>
  • 2015/02/26 10:01:04 C:\Users\petsta\AppData\Local\Temp\foobar428871999<nil>

What did you see instead?

  • 2015/02/26 09:59:53 mkdir C:\Users\petsta\AppData\Local\Temp\foo\bar288812899: The system cannot find the path specified.
@adg
Copy link
Contributor

adg commented Feb 25, 2015

I think it would fail even if it contained a slash. The prefix is for the file name, not for the path.

@bradfitz
Copy link
Contributor

On Windows, the username often contains backslashes.

What does that have to do with this bug?

The documentation for TempDir says nothing about usernames.

Perhaps the documentation of TempDir just needs to say not to put slashes in the prefix.

@adg adg changed the title ioutil.TempDir fails when the supplied prefix contains a backslash io/ioutil: document that TempDir's "prefix" should not contain a slash Feb 25, 2015
@adg
Copy link
Contributor

adg commented Feb 25, 2015

Maybe it's worth documenting that, but the docs do say the "TempFile creates a new temporary file in the directory dir with a name beginning with prefix." A file name shouldn't contain a slash, so the prefix shouldn't contain a slash. Not sure how explicit we should be.

@davecheney
Copy link
Contributor

I think it would fail even if it contained a slash. The prefix is for the file name, not for the path.

As well as documenting this we should probably check inside the preamble of the function that the prefix doesn't split according to the native filepath.SplitList and return an error if it does.

@minux
Copy link
Member

minux commented Feb 25, 2015

On Wed, Feb 25, 2015 at 6:23 PM, Dave Cheney notifications@github.com
wrote:

I think it would fail even if it contained a slash. The prefix is for the
file name, not for the path.

As well as documenting this we should probably check inside the preamble
of the function that the prefix doesn't split according to the native
filepath.SplitList and return an error if it does.

If we go to such strength to check for this error, we might be just allow
this use case
by filepath.Split and then MkdirAll.

@adg
Copy link
Contributor

adg commented Feb 25, 2015

Yeah, I have actually wanted to do this before.

On 26 February 2015 at 10:27, Minux Ma notifications@github.com wrote:

On Wed, Feb 25, 2015 at 6:23 PM, Dave Cheney notifications@github.com
wrote:

I think it would fail even if it contained a slash. The prefix is for the
file name, not for the path.

As well as documenting this we should probably check inside the preamble
of the function that the prefix doesn't split according to the native
filepath.SplitList and return an error if it does.

If we go to such strength to check for this error, we might be just allow
this use case
by filepath.Split and then MkdirAll.


Reply to this email directly or view it on GitHub
#10003 (comment).

@peterstace
Copy link
Author

Looking at the documentation again, it looks to me like ioutil.TempDir is working as intended (I think the documentation is probably clear enough as well).

@rsc rsc added this to the Go1.5 milestone Apr 10, 2015
@rsc
Copy link
Contributor

rsc commented Jun 29, 2015

I think this is documented clearly enough and working as intended. If the subdirectory exists I see no reason to reject the call, but I also see no reason to create the subdirectory. It seems fine as is. The original reporter (@peterstace) seems to agree, so closing.

@rsc rsc closed this as completed Jun 29, 2015
@golang golang locked and limited conversation to collaborators Jun 28, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants