2
0
mirror of synced 2025-02-24 22:58:28 +00:00
Matt Joiner 0d4858ba29 Extract the transfer tests
We need them external the torrent package so we can test the API for adding dialers and listeners.
2020-02-20 13:57:24 +11:00

57 lines
784 B
Go

package tmproot
import (
"io/ioutil"
"os"
"sync"
)
type Dir struct {
mu sync.Mutex
path string
inited bool
}
func (me *Dir) init(prefix string) bool {
if me.inited {
return false
}
var err error
me.path, err = ioutil.TempDir("", prefix)
if err != nil {
panic(err)
}
me.inited = true
return true
}
func (me *Dir) Init(prefix string) {
me.mu.Lock()
defer me.mu.Unlock()
if me.inited {
panic("already inited")
}
me.init(prefix)
}
func (me *Dir) lazyDefaultInit() {
me.mu.Lock()
defer me.mu.Unlock()
me.init("")
}
func (me *Dir) NewSub() string {
me.lazyDefaultInit()
ret, err := ioutil.TempDir(me.path, "")
if err != nil {
panic(err)
}
return ret
}
func (me *Dir) RemoveAll() error {
me.lazyDefaultInit()
return os.RemoveAll(me.path)
}