pax_global_header00006660000000000000000000000064133430310160014504gustar00rootroot0000000000000052 comment=ee190cb5934293f187a9d43ee34de7d5cf9ceb83 tempfile-0.0.1/000077500000000000000000000000001334303101600133075ustar00rootroot00000000000000tempfile-0.0.1/LICENSE000066400000000000000000000027341334303101600143220ustar00rootroot00000000000000BSD 3-Clause License Copyright (c) 2018, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. tempfile-0.0.1/README.md000066400000000000000000000017731334303101600145760ustar00rootroot00000000000000tempfile ======== An alternative implementation of ioutil.TempFile that provides a little more control over the temporary files being created. It allows the caller to provide a file suffix and/or file mode to set the permissions to something other than the default 0600. ## Examples ```go // Make a temp file in /var/tmp with the default file mode 0600. This is // equivalent to what ioutil.TempFile does. f, err := tempfile.New("/var/tmp", "myfile") if err != nil { panic(err) } defer os.Remove(f.Name()) defer f.Close() // Make a temporaty file with the given prefix and suffix f, err = tempfile.NewSuffix("", "myfile", ".tmp") ... // Create a world-readable temporary file in the OS' temp dir by providing // a file mode. f, err = tempfile.NewMode("", "myfile", 0644) ... // Combining all of the above f, err = tempfile.NewSuffixAndMode("", "myfile", ".ext", 0644) ... ``` ## Links - ioutil package - https://golang.org/pkg/io/ioutil/ - GoDoc for tempfile - https://godoc.org/github.com/folbricht/tempfile tempfile-0.0.1/go.mod000066400000000000000000000000451334303101600144140ustar00rootroot00000000000000module github.com/folbricht/tempfile tempfile-0.0.1/tempfile.go000066400000000000000000000035651334303101600154540ustar00rootroot00000000000000package tempfile import ( "fmt" "math/rand" "os" "path/filepath" "sync" "time" ) var ( mu sync.Mutex r *rand.Rand ) const ( maxAttempts = 1000 reseedAfter = 10 ) func init() { reseed() } // New creates a new temporary file in the specified directory the same way // ioutils.TempFile does. If dir is an empty string, it'll be created in the // OS' temp directory. The caller is responsible for removing the file. Drop-in // replacement for ioutils.TempFile func New(dir, prefix string) (f *os.File, err error) { return NewSuffixAndMode(dir, prefix, "", 0600) } // NewSuffix does the same as New() but allows a suffix to be added to the name. func NewSuffix(dir, prefix, suffix string) (f *os.File, err error) { return NewSuffixAndMode(dir, prefix, suffix, 0600) } // NewMode does the same as New() but allows the caller to provide a file mode // to give the file permissions different from the default 0600. func NewMode(dir, prefix string, perm os.FileMode) (f *os.File, err error) { return NewSuffixAndMode(dir, prefix, "", perm) } // NewSuffixAndMode does the same as New(), but allows the caller to provide // a suffix as well as a file mode to be used for the temporary file. func NewSuffixAndMode(dir, prefix, suffix string, perm os.FileMode) (f *os.File, err error) { if dir == "" { dir = os.TempDir() } var conflicts int for i := 0; i < maxAttempts; i++ { name := filepath.Join(dir, prefix+nextRand()+suffix) f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, perm) if os.IsExist(err) { if conflicts++; conflicts > reseedAfter { reseed() } continue } return } return nil, fmt.Errorf("unable to create tempfile after %d attempts", maxAttempts) } func nextRand() string { mu.Lock() defer mu.Unlock() return fmt.Sprintf(".%d", r.Uint32()) } func reseed() { mu.Lock() r = rand.New(rand.NewSource(time.Now().UnixNano())) mu.Unlock() } tempfile-0.0.1/tempfile_test.go000066400000000000000000000047201334303101600165050ustar00rootroot00000000000000package tempfile import ( "math/rand" "os" "path/filepath" "strings" "testing" ) func TestArgs(t *testing.T) { // Basic form without params. Should create random file in OS TempDir // with 0600 perms f, err := New("", "") if err != nil { t.Fatal(err) } defer os.Remove(f.Name()) defer f.Close() dir := filepath.Dir(f.Name()) if dir != os.TempDir() { t.Fatalf("expected temp file in '%s', got '%s'", os.TempDir(), dir) } stat, err := f.Stat() if err != nil { t.Fatal(err) } if stat.Mode() != 0600 { t.Fatalf("expected perms 0600, got %s", stat.Mode()) } // With filename prefix and in the current dir f, err = New(".", "prefix") if err != nil { t.Fatal(err) } defer os.Remove(f.Name()) defer f.Close() dir = filepath.Dir(f.Name()) base := filepath.Base(f.Name()) if dir != "." { t.Fatalf("expected temp file in current dir, got '%s'", dir) } if !strings.HasPrefix(base, "prefix") { t.Fatalf("expected filename with prefix, got %s", base) } // Filename with suffix f, err = NewSuffix("", "", "suffix") if err != nil { t.Fatal(err) } defer os.Remove(f.Name()) defer f.Close() base = filepath.Base(f.Name()) if !strings.HasSuffix(base, "suffix") { t.Fatalf("expected filename with suffix, got %s", base) } // File with non-standard perms f, err = NewMode("", "", 0755) if err != nil { t.Fatal(err) } defer os.Remove(f.Name()) defer f.Close() stat, err = f.Stat() if err != nil { t.Fatal(err) } if stat.Mode() != 0755 { t.Fatalf("expected perms 0755, got %s", stat.Mode()) } } func TestUnique(t *testing.T) { // Create several tempfiles and store them in a map. Fail if any is duplicated fmap := make(map[string]struct{}) for i := 0; i < 20; i++ { f, err := New("", "") if err != nil { t.Fatal(err) } defer os.Remove(f.Name()) defer f.Close() if _, ok := fmap[f.Name()]; ok { t.Fatalf("file %s existed already", f.Name()) } fmap[f.Name()] = struct{}{} } } func TestCollision(t *testing.T) { // Make several files starting with a fixed seed r = rand.New(rand.NewSource(0)) for i := 0; i < 20; i++ { f, err := New("", "") if err != nil { t.Fatal(err) } defer os.Remove(f.Name()) defer f.Close() } // Reset the seed and make more files, those should conflict and trigger // a re-seed of the sequence after a few tries r = rand.New(rand.NewSource(0)) for i := 0; i < 20; i++ { f, err := New("", "") if err != nil { t.Fatal(err) } defer os.Remove(f.Name()) defer f.Close() } }