mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
support create directory
This commit is contained in:
parent
4f99b60291
commit
948044093b
@ -19,7 +19,7 @@ type FileSystem struct {
|
||||
|
||||
func New() *FileSystem {
|
||||
return &FileSystem{
|
||||
Root: newDir("/", 0, 0, nil, ""),
|
||||
Root: newDir("/", 0, 0, nil, "", Permanent),
|
||||
WatcherHub: newWatchHub(1000),
|
||||
}
|
||||
|
||||
@ -36,6 +36,7 @@ func (fs *FileSystem) Get(keyPath string, recusive bool, index uint64, term uint
|
||||
e := newEvent(Get, keyPath, index, term)
|
||||
|
||||
if n.IsDir() { // node is dir
|
||||
e.Dir = true
|
||||
|
||||
children, _ := n.List()
|
||||
e.KVPairs = make([]KeyValuePair, len(children))
|
||||
@ -57,7 +58,6 @@ func (fs *FileSystem) Get(keyPath string, recusive bool, index uint64, term uint
|
||||
|
||||
// eliminate hidden nodes
|
||||
e.KVPairs = e.KVPairs[:i]
|
||||
|
||||
} else { // node is file
|
||||
e.Value = n.Value
|
||||
}
|
||||
@ -91,11 +91,22 @@ func (fs *FileSystem) Create(keyPath string, value string, expireTime time.Time,
|
||||
}
|
||||
|
||||
e := newEvent(Set, keyPath, fs.Index, fs.Term)
|
||||
|
||||
var n *Node
|
||||
|
||||
if len(value) != 0 { // create file
|
||||
e.Value = value
|
||||
|
||||
f := newFile(keyPath, value, fs.Index, fs.Term, d, "", expireTime)
|
||||
n = newFile(keyPath, value, fs.Index, fs.Term, d, "", expireTime)
|
||||
|
||||
err = d.Add(f)
|
||||
} else { // create directory
|
||||
e.Dir = true
|
||||
|
||||
n = newDir(keyPath, fs.Index, fs.Term, d, "", expireTime)
|
||||
|
||||
}
|
||||
|
||||
err = d.Add(n)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -103,8 +114,8 @@ func (fs *FileSystem) Create(keyPath string, value string, expireTime time.Time,
|
||||
|
||||
// Node with TTL
|
||||
if expireTime != Permanent {
|
||||
go f.Expire()
|
||||
e.Expiration = &f.ExpireTime
|
||||
go n.Expire()
|
||||
e.Expiration = &n.ExpireTime
|
||||
e.TTL = int64(expireTime.Sub(time.Now()) / time.Second)
|
||||
}
|
||||
|
||||
@ -268,7 +279,7 @@ func (fs *FileSystem) checkDir(parent *Node, dirName string) (*Node, error) {
|
||||
return subDir, nil
|
||||
}
|
||||
|
||||
n := newDir(path.Join(parent.Path, dirName), fs.Index, fs.Term, parent, parent.ACL)
|
||||
n := newDir(path.Join(parent.Path, dirName), fs.Index, fs.Term, parent, parent.ACL, Permanent)
|
||||
|
||||
parent.Children[dirName] = n
|
||||
|
||||
|
@ -21,12 +21,32 @@ func TestCreateAndGet(t *testing.T) {
|
||||
}
|
||||
|
||||
// meet file, create should fail
|
||||
_, err = fs.Create("/foo/bar/bar", "bar", Permanent, 1, 1)
|
||||
_, err = fs.Create("/foo/bar/bar", "bar", Permanent, 2, 1)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Create should fail")
|
||||
}
|
||||
|
||||
// create a directory
|
||||
_, err = fs.Create("/fooDir", "", Permanent, 3, 1)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal("Cannot create /fooDir")
|
||||
}
|
||||
|
||||
e, err := fs.Get("/fooDir", false, 3, 1)
|
||||
|
||||
if err != nil || e.Dir != true {
|
||||
t.Fatal("Cannot create /fooDir ")
|
||||
}
|
||||
|
||||
// create a file under directory
|
||||
_, err = fs.Create("/fooDir/bar", "bar", Permanent, 4, 1)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal("Cannot create /fooDir/bar = bar")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestUpdateFile(t *testing.T) {
|
||||
|
@ -49,7 +49,7 @@ func newFile(keyPath string, value string, createIndex uint64, createTerm uint64
|
||||
}
|
||||
}
|
||||
|
||||
func newDir(keyPath string, createIndex uint64, createTerm uint64, parent *Node, ACL string) *Node {
|
||||
func newDir(keyPath string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node {
|
||||
return &Node{
|
||||
Path: keyPath,
|
||||
CreateIndex: createIndex,
|
||||
@ -57,6 +57,7 @@ func newDir(keyPath string, createIndex uint64, createTerm uint64, parent *Node,
|
||||
Parent: parent,
|
||||
ACL: ACL,
|
||||
stopExpire: make(chan bool, 1),
|
||||
ExpireTime: expireTime,
|
||||
Children: make(map[string]*Node),
|
||||
}
|
||||
}
|
||||
@ -210,7 +211,7 @@ func (n *Node) Clone() *Node {
|
||||
return newFile(n.Path, n.Value, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
|
||||
}
|
||||
|
||||
clone := newDir(n.Path, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL)
|
||||
clone := newDir(n.Path, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
|
||||
|
||||
for key, child := range n.Children {
|
||||
clone.Children[key] = child.Clone()
|
||||
|
Loading…
x
Reference in New Issue
Block a user