]> Cypherpunks repositories - gostls13.git/commitdiff
io/ioutil: add TempDir
authorRuss Cox <rsc@golang.org>
Thu, 3 Mar 2011 19:51:49 +0000 (14:51 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 3 Mar 2011 19:51:49 +0000 (14:51 -0500)
It's a little confusing that os.TempDir and ioutil.TempDir have
different meanings.  I don't know what to change the names to,
if anything.  At least they also have different signatures.

R=golang-dev, bradfitzgo, r, gri
CC=golang-dev
https://golang.org/cl/4247051

src/pkg/io/ioutil/tempfile.go
src/pkg/io/ioutil/tempfile_test.go

index 114eca2b50e40134b6e016d7b35f0f8dc52e7aa7..c7cc67b1b7462a25c642bfd6db242065ff60e73c 100644 (file)
@@ -46,6 +46,7 @@ func TempFile(dir, prefix string) (f *os.File, err os.Error) {
 
        nconflict := 0
        for i := 0; i < 10000; i++ {
+               // TODO(rsc): use filepath.Join
                name := dir + "/" + prefix + nextSuffix()
                f, err = os.Open(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
                if pe, ok := err.(*os.PathError); ok && pe.Error == os.EEXIST {
@@ -58,3 +59,34 @@ func TempFile(dir, prefix string) (f *os.File, err os.Error) {
        }
        return
 }
+
+// TempDir creates a new temporary directory in the directory dir
+// with a name beginning with prefix and returns the path of the
+// new directory.  If dir is the empty string, TempDir uses the
+// default directory for temporary files (see os.TempDir).
+// Multiple programs calling TempDir simultaneously
+// will not choose the same directory.  It is the caller's responsibility
+// to remove the directory when no longer needed.
+func TempDir(dir, prefix string) (name string, err os.Error) {
+       if dir == "" {
+               dir = os.TempDir()
+       }
+
+       nconflict := 0
+       for i := 0; i < 10000; i++ {
+               // TODO(rsc): use filepath.Join
+               try := dir + "/" + prefix + nextSuffix()
+               err = os.Mkdir(try, 0700)
+               if pe, ok := err.(*os.PathError); ok && pe.Error == os.EEXIST {
+                       if nconflict++; nconflict > 10 {
+                               rand = reseed()
+                       }
+                       continue
+               }
+               if err == nil {
+                       name = try
+               }
+               break
+       }
+       return
+}
index d949a86cf0961810816aa2ced2f6ee6588af692e..6013ec1d4a42aa87ba5afe640a39550a917b992c 100644 (file)
@@ -23,11 +23,31 @@ func TestTempFile(t *testing.T) {
                t.Errorf("TempFile(dir, `ioutil_test`) = %v, %v", f, err)
        }
        if f != nil {
+               f.Close()
+               os.Remove(f.Name())
                re := regexp.MustCompile("^" + regexp.QuoteMeta(dir) + "/ioutil_test[0-9]+$")
                if !re.MatchString(f.Name()) {
                        t.Errorf("TempFile(`"+dir+"`, `ioutil_test`) created bad name %s", f.Name())
                }
-               os.Remove(f.Name())
        }
-       f.Close()
+}
+
+func TestTempDir(t *testing.T) {
+       name, err := TempDir("/_not_exists_", "foo")
+       if name != "" || err == nil {
+               t.Errorf("TempDir(`/_not_exists_`, `foo`) = %v, %v", name, err)
+       }
+
+       dir := os.TempDir()
+       name, err = TempDir(dir, "ioutil_test")
+       if name == "" || err != nil {
+               t.Errorf("TempDir(dir, `ioutil_test`) = %v, %v", name, err)
+       }
+       if name != "" {
+               os.Remove(name)
+               re := regexp.MustCompile("^" + regexp.QuoteMeta(dir) + "/ioutil_test[0-9]+$")
+               if !re.MatchString(name) {
+                       t.Errorf("TempDir(`"+dir+"`, `ioutil_test`) created bad name %s", name)
+               }
+       }
 }