Skip to content

Commit 5ba23f4

Browse files
committed
util: utils move to his own package
1 parent 019bd95 commit 5ba23f4

File tree

12 files changed

+135
-107
lines changed

12 files changed

+135
-107
lines changed

fs.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,6 @@ type Chroot interface {
137137
Root() string
138138
}
139139

140-
type underlying interface {
141-
Underlying() Basic
142-
}
143-
type removerAll interface {
144-
RemoveAll(string) error
145-
}
146-
147140
// File represent a file, being a subset of the os.File
148141
type File interface {
149142
// Name returns the name of the file as presented to Open.

helper/chroot/chroot.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ type ChrootHelper struct {
1313
underlying billy.Basic
1414
base string
1515

16-
tempFileSupport bool
1716
dirSupport bool
1817
symlinkSupport bool
18+
tempFileSupport bool
1919
}
2020

2121
// New creates a new filesystem wrapping up the given 'fs'.
2222
// The created filesystem has its base in the given ChrootHelperectory of the
2323
// underlying filesystem.
2424
func New(fs billy.Basic, base string) billy.Filesystem {
2525
helper := &ChrootHelper{underlying: fs, base: base}
26-
_, helper.tempFileSupport = fs.(billy.TempFile)
2726
_, helper.dirSupport = fs.(billy.Dir)
2827
_, helper.symlinkSupport = fs.(billy.Symlink)
28+
_, helper.tempFileSupport = fs.(billy.TempFile)
2929

3030
return helper
3131
}

helper/mount/mount_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"gopkg.in/src-d/go-billy.v2"
99
"gopkg.in/src-d/go-billy.v2/memfs"
1010
"gopkg.in/src-d/go-billy.v2/test"
11+
"gopkg.in/src-d/go-billy.v2/util"
1112

1213
. "gopkg.in/check.v1"
1314
)
@@ -160,7 +161,7 @@ func (s *MountSuite) TestRenameCross(c *C) {
160161
underlying := memfs.New()
161162
source := memfs.New()
162163

163-
billy.WriteFile(underlying, "file", []byte("foo"), 0777)
164+
util.WriteFile(underlying, "file", []byte("foo"), 0777)
164165

165166
fs := New(underlying, "/foo", source)
166167
err := fs.Rename("file", "foo/file")

memfs/memory.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"gopkg.in/src-d/go-billy.v2"
1414
"gopkg.in/src-d/go-billy.v2/helper/chroot"
15+
"gopkg.in/src-d/go-billy.v2/util"
1516
)
1617

1718
const separator = filepath.Separator
@@ -137,22 +138,8 @@ func (fs *Memory) MkdirAll(path string, perm os.FileMode) error {
137138
return err
138139
}
139140

140-
var maxTempFiles = 1024 * 4
141-
142141
func (fs *Memory) TempFile(dir, prefix string) (billy.File, error) {
143-
var fullpath string
144-
for {
145-
if fs.tempCount >= maxTempFiles {
146-
return nil, errors.New("max. number of tempfiles reached")
147-
}
148-
149-
fullpath = fs.getTempFilename(dir, prefix)
150-
if _, ok := fs.s.files[fullpath]; !ok {
151-
break
152-
}
153-
}
154-
155-
return fs.Create(fullpath)
142+
return util.TempFile(fs, dir, prefix)
156143
}
157144

158145
func (fs *Memory) getTempFilename(dir, prefix string) string {
@@ -183,7 +170,7 @@ func (fs *Memory) Symlink(target, link string) error {
183170
return err
184171
}
185172

186-
return billy.WriteFile(fs, link, []byte(target), 0777|os.ModeSymlink)
173+
return util.WriteFile(fs, link, []byte(target), 0777|os.ModeSymlink)
187174
}
188175

189176
func (fs *Memory) Readlink(link string) (string, error) {

memfs/memory_test.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package memfs
33
import (
44
"testing"
55

6-
. "gopkg.in/check.v1"
76
"gopkg.in/src-d/go-billy.v2/test"
7+
8+
. "gopkg.in/check.v1"
89
)
910

1011
func Test(t *testing.T) { TestingT(t) }
@@ -19,15 +20,3 @@ var _ = Suite(&MemorySuite{})
1920
func (s *MemorySuite) SetUpTest(c *C) {
2021
s.FilesystemSuite = test.NewFilesystemSuite(New())
2122
}
22-
23-
func (s *MemorySuite) TestTempFileMaxTempFiles(c *C) {
24-
for i := 0; i < maxTempFiles; i++ {
25-
f, err := s.FilesystemSuite.FS.TempFile("", "")
26-
c.Assert(err, IsNil)
27-
c.Assert(f, NotNil)
28-
}
29-
30-
f, err := s.FilesystemSuite.FS.TempFile("", "")
31-
c.Assert(err, NotNil)
32-
c.Assert(f, IsNil)
33-
}

test/basic.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
. "gopkg.in/check.v1"
1212
. "gopkg.in/src-d/go-billy.v2"
13+
"gopkg.in/src-d/go-billy.v2/util"
1314
)
1415

1516
// BasicSuite is a convenient test suite to validate any implementation of
@@ -242,7 +243,7 @@ func (s *BasicSuite) TestFileWriteClose(c *C) {
242243
}
243244

244245
func (s *BasicSuite) TestFileRead(c *C) {
245-
err := WriteFile(s.FS, "foo", []byte("foo"), 0644)
246+
err := util.WriteFile(s.FS, "foo", []byte("foo"), 0644)
246247
c.Assert(err, IsNil)
247248

248249
f, err := s.FS.Open("foo")
@@ -255,7 +256,7 @@ func (s *BasicSuite) TestFileRead(c *C) {
255256
}
256257

257258
func (s *BasicSuite) TestFileClosed(c *C) {
258-
err := WriteFile(s.FS, "foo", []byte("foo"), 0644)
259+
err := util.WriteFile(s.FS, "foo", []byte("foo"), 0644)
259260
c.Assert(err, IsNil)
260261

261262
f, err := s.FS.Open("foo")
@@ -267,7 +268,7 @@ func (s *BasicSuite) TestFileClosed(c *C) {
267268
}
268269

269270
func (s *BasicSuite) TestFileNonRead(c *C) {
270-
err := WriteFile(s.FS, "foo", []byte("foo"), 0644)
271+
err := util.WriteFile(s.FS, "foo", []byte("foo"), 0644)
271272
c.Assert(err, IsNil)
272273

273274
f, err := s.FS.OpenFile("foo", os.O_WRONLY, 0)
@@ -292,7 +293,7 @@ func (s *BasicSuite) TestFileSeekEnd(c *C) {
292293
}
293294

294295
func (s *BasicSuite) testFileSeek(c *C, offset int64, whence int) {
295-
err := WriteFile(s.FS, "foo", []byte("0123456789abcdefghijklmnopqrstuvwxyz"), 0644)
296+
err := util.WriteFile(s.FS, "foo", []byte("0123456789abcdefghijklmnopqrstuvwxyz"), 0644)
296297
c.Assert(err, IsNil)
297298

298299
f, err := s.FS.Open("foo")
@@ -335,7 +336,7 @@ func (s *BasicSuite) TestSeekToEndAndWrite(c *C) {
335336
}
336337

337338
func (s *BasicSuite) TestFileSeekClosed(c *C) {
338-
err := WriteFile(s.FS, "foo", []byte("foo"), 0644)
339+
err := util.WriteFile(s.FS, "foo", []byte("foo"), 0644)
339340
c.Assert(err, IsNil)
340341

341342
f, err := s.FS.Open("foo")
@@ -355,7 +356,7 @@ func (s *BasicSuite) TestFileCloseTwice(c *C) {
355356
}
356357

357358
func (s *BasicSuite) TestStat(c *C) {
358-
WriteFile(s.FS, "foo/bar", []byte("foo"), customMode)
359+
util.WriteFile(s.FS, "foo/bar", []byte("foo"), customMode)
359360

360361
fi, err := s.FS.Stat("foo/bar")
361362
c.Assert(err, IsNil)
@@ -374,7 +375,7 @@ func (s *BasicSuite) TestStatNonExistent(c *C) {
374375
}
375376

376377
func (s *BasicSuite) TestRename(c *C) {
377-
err := WriteFile(s.FS, "foo", nil, 0644)
378+
err := util.WriteFile(s.FS, "foo", nil, 0644)
378379
c.Assert(err, IsNil)
379380

380381
err = s.FS.Rename("foo", "bar")
@@ -390,7 +391,7 @@ func (s *BasicSuite) TestRename(c *C) {
390391
}
391392

392393
func (s *BasicSuite) TestOpenAndWrite(c *C) {
393-
err := WriteFile(s.FS, "foo", nil, 0644)
394+
err := util.WriteFile(s.FS, "foo", nil, 0644)
394395
c.Assert(err, IsNil)
395396

396397
foo, err := s.FS.Open("foo")
@@ -405,7 +406,7 @@ func (s *BasicSuite) TestOpenAndWrite(c *C) {
405406
}
406407

407408
func (s *BasicSuite) TestOpenAndStat(c *C) {
408-
err := WriteFile(s.FS, "foo", []byte("foo"), 0644)
409+
err := util.WriteFile(s.FS, "foo", []byte("foo"), 0644)
409410
c.Assert(err, IsNil)
410411

411412
foo, err := s.FS.Open("foo")
@@ -437,7 +438,7 @@ func (s *BasicSuite) TestRemoveNonExisting(c *C) {
437438
}
438439

439440
func (s *BasicSuite) TestRemoveNotEmptyDir(c *C) {
440-
err := WriteFile(s.FS, "foo", nil, 0644)
441+
err := util.WriteFile(s.FS, "foo", nil, 0644)
441442
c.Assert(err, IsNil)
442443

443444
err = s.FS.Remove("no-exists")
@@ -466,7 +467,7 @@ func (s *BasicSuite) TestReadAtOnReadWrite(c *C) {
466467
}
467468

468469
func (s *BasicSuite) TestReadAtOnReadOnly(c *C) {
469-
err := WriteFile(s.FS, "foo", []byte("abcdefg"), 0644)
470+
err := util.WriteFile(s.FS, "foo", []byte("abcdefg"), 0644)
470471
c.Assert(err, IsNil)
471472

472473
f, err := s.FS.Open("foo")
@@ -484,7 +485,7 @@ func (s *BasicSuite) TestReadAtOnReadOnly(c *C) {
484485
}
485486

486487
func (s *BasicSuite) TestReadAtEOF(c *C) {
487-
err := WriteFile(s.FS, "foo", []byte("TEST"), 0644)
488+
err := util.WriteFile(s.FS, "foo", []byte("TEST"), 0644)
488489
c.Assert(err, IsNil)
489490

490491
f, err := s.FS.Open("foo")
@@ -501,7 +502,7 @@ func (s *BasicSuite) TestReadAtEOF(c *C) {
501502
}
502503

503504
func (s *BasicSuite) TestReadAtOffset(c *C) {
504-
err := WriteFile(s.FS, "foo", []byte("TEST"), 0644)
505+
err := util.WriteFile(s.FS, "foo", []byte("TEST"), 0644)
505506
c.Assert(err, IsNil)
506507

507508
f, err := s.FS.Open("foo")
@@ -549,7 +550,7 @@ func (s *BasicSuite) TestReadWriteLargeFile(c *C) {
549550
}
550551

551552
func (s *BasicSuite) TestWriteFile(c *C) {
552-
err := WriteFile(s.FS, "foo", []byte("bar"), 0777)
553+
err := util.WriteFile(s.FS, "foo", []byte("bar"), 0777)
553554
c.Assert(err, IsNil)
554555

555556
f, err := s.FS.Open("foo")

test/chroot.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
. "gopkg.in/check.v1"
77
. "gopkg.in/src-d/go-billy.v2"
8+
"gopkg.in/src-d/go-billy.v2/util"
89
)
910

1011
// ChrootSuite is a convenient test suite to validate any implementation of
@@ -43,7 +44,7 @@ func (s *ChrootSuite) TestOpenWithChroot(c *C) {
4344
}
4445

4546
func (s *ChrootSuite) TestOpenOutOffBoundary(c *C) {
46-
err := WriteFile(s.FS, "bar", nil, 0644)
47+
err := util.WriteFile(s.FS, "bar", nil, 0644)
4748
c.Assert(err, IsNil)
4849

4950
fs, _ := s.FS.Chroot("foo")
@@ -53,7 +54,7 @@ func (s *ChrootSuite) TestOpenOutOffBoundary(c *C) {
5354
}
5455

5556
func (s *ChrootSuite) TestStatOutOffBoundary(c *C) {
56-
err := WriteFile(s.FS, "bar", nil, 0644)
57+
err := util.WriteFile(s.FS, "bar", nil, 0644)
5758
c.Assert(err, IsNil)
5859

5960
fs, _ := s.FS.Chroot("foo")
@@ -65,7 +66,7 @@ func (s *ChrootSuite) TestStatOutOffBoundary(c *C) {
6566
func (s *ChrootSuite) TestStatWithChroot(c *C) {
6667
files := []string{"foo", "bar", "qux/baz", "qux/qux"}
6768
for _, name := range files {
68-
err := WriteFile(s.FS, name, nil, 0644)
69+
err := util.WriteFile(s.FS, name, nil, 0644)
6970
c.Assert(err, IsNil)
7071
}
7172

@@ -94,10 +95,10 @@ func (s *ChrootSuite) TestStatWithChroot(c *C) {
9495
}
9596

9697
func (s *ChrootSuite) TestRenameOutOffBoundary(c *C) {
97-
err := WriteFile(s.FS, "foo/foo", nil, 0644)
98+
err := util.WriteFile(s.FS, "foo/foo", nil, 0644)
9899
c.Assert(err, IsNil)
99100

100-
err = WriteFile(s.FS, "bar", nil, 0644)
101+
err = util.WriteFile(s.FS, "bar", nil, 0644)
101102
c.Assert(err, IsNil)
102103

103104
fs, _ := s.FS.Chroot("foo")
@@ -109,7 +110,7 @@ func (s *ChrootSuite) TestRenameOutOffBoundary(c *C) {
109110
}
110111

111112
func (s *ChrootSuite) TestRemoveOutOffBoundary(c *C) {
112-
err := WriteFile(s.FS, "bar", nil, 0644)
113+
err := util.WriteFile(s.FS, "bar", nil, 0644)
113114
c.Assert(err, IsNil)
114115

115116
fs, _ := s.FS.Chroot("foo")

test/dir.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
. "gopkg.in/check.v1"
77
. "gopkg.in/src-d/go-billy.v2"
8+
"gopkg.in/src-d/go-billy.v2/util"
89
)
910

1011
// DirSuite is a convenient test suite to validate any implementation of
@@ -97,7 +98,7 @@ func (s *DirSuite) TestStatDir(c *C) {
9798
func (s *BasicSuite) TestStatDeep(c *C) {
9899
files := []string{"foo", "bar", "qux/baz", "qux/qux"}
99100
for _, name := range files {
100-
err := WriteFile(s.FS, name, nil, 0644)
101+
err := util.WriteFile(s.FS, name, nil, 0644)
101102
c.Assert(err, IsNil)
102103
}
103104

@@ -121,7 +122,7 @@ func (s *BasicSuite) TestStatDeep(c *C) {
121122
func (s *DirSuite) TestReadDir(c *C) {
122123
files := []string{"foo", "bar", "qux/baz", "qux/qux"}
123124
for _, name := range files {
124-
err := WriteFile(s.FS, name, nil, 0644)
125+
err := util.WriteFile(s.FS, name, nil, 0644)
125126
c.Assert(err, IsNil)
126127
}
127128

@@ -140,7 +141,7 @@ func (s *DirSuite) TestReadDirWithMkDirAll(c *C) {
140141

141142
files := []string{"qux/baz", "qux/qux"}
142143
for _, name := range files {
143-
err := WriteFile(s.FS, name, nil, 0644)
144+
err := util.WriteFile(s.FS, name, nil, 0644)
144145
c.Assert(err, IsNil)
145146
}
146147

@@ -155,7 +156,7 @@ func (s *DirSuite) TestReadDirWithMkDirAll(c *C) {
155156
}
156157

157158
func (s *DirSuite) TestReadDirFileInfo(c *C) {
158-
err := WriteFile(s.FS, "foo", []byte{'F', 'O', 'O'}, 0644)
159+
err := util.WriteFile(s.FS, "foo", []byte{'F', 'O', 'O'}, 0644)
159160
c.Assert(err, IsNil)
160161

161162
info, err := s.FS.ReadDir("/")
@@ -170,7 +171,7 @@ func (s *DirSuite) TestReadDirFileInfo(c *C) {
170171
func (s *DirSuite) TestReadDirFileInfoDirs(c *C) {
171172
files := []string{"qux/baz/foo"}
172173
for _, name := range files {
173-
err := WriteFile(s.FS, name, []byte{'F', 'O', 'O'}, 0644)
174+
err := util.WriteFile(s.FS, name, []byte{'F', 'O', 'O'}, 0644)
174175
c.Assert(err, IsNil)
175176
}
176177

@@ -190,7 +191,7 @@ func (s *DirSuite) TestReadDirFileInfoDirs(c *C) {
190191
}
191192

192193
func (s *DirSuite) TestRenameToDir(c *C) {
193-
err := WriteFile(s.FS, "foo", nil, 0644)
194+
err := util.WriteFile(s.FS, "foo", nil, 0644)
194195
c.Assert(err, IsNil)
195196

196197
err = s.FS.Rename("foo", "bar/qux")
@@ -213,7 +214,7 @@ func (s *DirSuite) TestRenameDir(c *C) {
213214
err := s.FS.MkdirAll("foo", 0644)
214215
c.Assert(err, IsNil)
215216

216-
err = WriteFile(s.FS, "foo/bar", nil, 0644)
217+
err = util.WriteFile(s.FS, "foo/bar", nil, 0644)
217218
c.Assert(err, IsNil)
218219

219220
err = s.FS.Rename("foo", "bar")

0 commit comments

Comments
 (0)