Skip to content

Commit 9826264

Browse files
authored
Merge pull request #62 from kuba--/enhancement-tempdir
Add TempDir
2 parents 5995254 + e7da41f commit 9826264

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
/coverage.txt
2+
/vendor
3+
Gopkg.lock
4+
Gopkg.toml
5+
go.sum

test/fs.go

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ func (s *FilesystemSuite) TestRemoveAllRelative(c *C) {
188188
}
189189

190190
func (s *FilesystemSuite) TestReadDir(c *C) {
191+
err := s.FS.MkdirAll("qux", 0644)
192+
c.Assert(err, IsNil)
193+
191194
files := []string{"foo", "bar", "qux/baz", "qux/qux"}
192195
for _, name := range files {
193196
err := util.WriteFile(s.FS, name, nil, 0644)

util/util.go

+39
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,45 @@ func TempFile(fs billy.Basic, dir, prefix string) (f billy.File, err error) {
168168
return
169169
}
170170

171+
// TempDir creates a new temporary directory in the directory dir
172+
// with a name beginning with prefix and returns the path of the
173+
// new directory. If dir is the empty string, TempDir uses the
174+
// default directory for temporary files (see os.TempDir).
175+
// Multiple programs calling TempDir simultaneously
176+
// will not choose the same directory. It is the caller's responsibility
177+
// to remove the directory when no longer needed.
178+
func TempDir(fs billy.Dir, dir, prefix string) (name string, err error) {
179+
// This implementation is based on stdlib ioutil.TempDir
180+
181+
if dir == "" {
182+
dir = os.TempDir()
183+
}
184+
185+
nconflict := 0
186+
for i := 0; i < 10000; i++ {
187+
try := filepath.Join(dir, prefix+nextSuffix())
188+
err = fs.MkdirAll(try, 0700)
189+
if os.IsExist(err) {
190+
if nconflict++; nconflict > 10 {
191+
randmu.Lock()
192+
rand = reseed()
193+
randmu.Unlock()
194+
}
195+
continue
196+
}
197+
if os.IsNotExist(err) {
198+
if _, err := os.Stat(dir); os.IsNotExist(err) {
199+
return "", err
200+
}
201+
}
202+
if err == nil {
203+
name = try
204+
}
205+
break
206+
}
207+
return
208+
}
209+
171210
type underlying interface {
172211
Underlying() billy.Basic
173212
}

util/util_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package util_test
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"regexp"
7+
"testing"
8+
9+
"gopkg.in/src-d/go-billy.v4/memfs"
10+
"gopkg.in/src-d/go-billy.v4/util"
11+
)
12+
13+
func TestTempFile(t *testing.T) {
14+
fs := memfs.New()
15+
16+
dir, err := util.TempDir(fs, "", "util_test")
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
defer util.RemoveAll(fs, dir)
21+
22+
f, err := util.TempFile(fs, dir, "foo")
23+
if f == nil || err != nil {
24+
t.Errorf("TempFile(%q, `foo`) = %v, %v", dir, f, err)
25+
}
26+
}
27+
28+
func TestTempDir(t *testing.T) {
29+
fs := memfs.New()
30+
31+
dir := os.TempDir()
32+
name, err := util.TempDir(fs, dir, "util_test")
33+
if name == "" || err != nil {
34+
t.Errorf("TempDir(dir, `util_test`) = %v, %v", name, err)
35+
}
36+
if name != "" {
37+
util.RemoveAll(fs, name)
38+
re := regexp.MustCompile("^" + regexp.QuoteMeta(filepath.Join(dir, "util_test")) + "[0-9]+$")
39+
if !re.MatchString(name) {
40+
t.Errorf("TempDir(`"+dir+"`, `util_test`) created bad name %s", name)
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)