-
Notifications
You must be signed in to change notification settings - Fork 17.5k
-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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: predictable suffix for TempFile #4896
Comments
I have run into this See cmd/pprof/internal/tempfile has a nice function https://golang.org/pkg/cmd/pprof/internal/tempfile/#New tempfile.New(dir, prefix, suffix string) (*os.File, error) although imports of internal packages aren't allowed in >=Go1.6.Yet ioutil.TempFile(dir, prefix string) and adds a suffix to the end of the path, and this messes up the intended suffix My use case is for programs that need the suffix to try to select the opener by using the suffix/extension e.g $ file pliesHello
pliesHello: ISO Media, MPEG v4 system, version 2
$ open pliesHello cmd/pprof/internal/tempfile IMO provides a better API, since it even allows for DeferPath(path) deletion and no need to Close() then os.RemoveAll() |
@odeke-em maybe I don't understand the issue, but can't you just os.Rename the temp file once it's been created ? |
Yap, same train of thought. For now that's exactly what am doing. However, notice the number of steps that it takes:
os.Rename suffers from a few problems if the /tmp dir is not on the same device Now let's compare that to using tempfile.New, and tempfile.DeferDelete and that's simpler. |
Some tools (e.g., vips) rely on the extension to determine how to process the file. When exec such commands and feeding them temp files, being able to specify suffix is very important. Right now I have to copy https://golang.org/pkg/cmd/pprof/internal/tempfile from an early go to a new package to achieve that. |
As a workaround, we just open sourced a tiny library which supports suffix, too: https://github.com/tink-ab/tempfile |
This is even more important for Windows systems where extensions mean something. For example, in one project we needed to have a ".bat" extension. |
This seems like a good one for community day. Can we add a "help wanted" label? |
I think I'm going to volunteer for this. I'm new to contributing to this project, so hand holding is appreciated. According to Contribution Guide, I should discuss the design a bit. Here is my design proposal:
|
Thanks for your work @TomOnTime. I'm not sure if I'm overengineering this, but I don't think it needs to have anything to do with extensions. What if we use a more generic solution:
Where |
If the caller does not provide an extension will I'll vote for |
I just mean that the the filename is made of Callers might assume the formula is
I think TempFileTemplate is a bit overly complex for the needs here. |
FYI: Technically the 3rd parameter is a suffix, not an extension. A better name is |
Makes sense. I'm new here so... what's next? |
Welcome and thanks for interest in contributing :) The formal process is here and is intended to ensure that new features are bring sufficient benefit to warrant expanding the API, and don't bring along with it major detriments. Periodically (I think weekly?), a group of proposal reviewers scrub through issues in the proposal milestone and determine whether to approve/disapprove the suggested API change. However, there is a large backlog of proposals, so it may take time for them to get around to this one. |
There's ioutil.TempDir already, that's one option if you want control over file suffixes. We don't yet know how we'd want to add support for suffixes, or if we really do. One option instead of TempFile2 is to define that the current prefix is a pattern so that if there's a * that's where the randomness goes.
etc. |
Does @rsc's suggestion above address the use case here? |
The use of "*" as a marker for where to insert the random bits? Yes, that would work. |
This would be a very welcome change guys, a huge functionality improvement. |
OK, let's do what I wrote above: if "*" is in the string, then replace it with the random part. If there are multiple stars, use the first one (we have to pick something). |
I volunteer to write this. Seems like a good starter-project. |
Personally, I like predictability and "following the spirit of existing API" of an original IMHO there are too many problems with
|
The new implementation replaces the first |
@TomOnTime that way "Temp*.RANDOMPART.json" will still be impossible |
@tandr Agreed.We're making it difficult to do pathologically bad things. |
Change https://golang.org/cl/105675 mentions this issue: |
Any prediction of when this feature will be merged to go release? |
@felipeek It should be in Go1.11beta1 already. You can try it out! https://groups.google.com/forum/#!topic/golang-announce/m1Szl8Mu_Fs Also, closed issues aren't monitored. If you find a bug please open a new issue. |
The text was updated successfully, but these errors were encountered: