diff --git a/zk/conn.go b/zk/conn.go index da9503a2..e9f7ff97 100644 --- a/zk/conn.go +++ b/zk/conn.go @@ -15,6 +15,7 @@ import ( "errors" "fmt" "io" + "log" "net" "strconv" "strings" @@ -1121,6 +1122,39 @@ func (c *Conn) CreateProtectedEphemeralSequential(path string, data []byte, acl return "", err } +//If there is no parent node, the node is created level by level +func (c *Conn) CreatingParentsIfNeededWithDefaultACL(path string, data []byte, flags int32) (string, error) { + return c.CreatingParentsIfNeededWithCustomizeACL(path, data, flags, WorldACL(PermAll)) +} + +func (c *Conn) CreatingParentsIfNeededWithCustomizeACL(path string, data []byte, flags int32, acl []ACL) (string, error) { + var zkPath string + + if path == "" { + return zkPath, ErrInvalidPath + } + + paths := strings.Split(path, "/") + + size := len(paths) + + for index, node := range paths { + log.Println("size: ", size, ", index: ", index, ", node: ", node) + if node == "" { + continue + } + zkPath = zkPath + "/" + node + if index < size-1 { + zkPath, _ := c.Create(zkPath, []byte(""), FlagPersistence, acl) + log.Println("index = ", index, "zkPath = ", zkPath) + } else { + zkPath, _ = c.Create(zkPath, data, flags, acl) + log.Println("index = ", index, "zkPath = ", zkPath) + } + } + return zkPath, nil +} + func (c *Conn) Delete(path string, version int32) error { if err := validatePath(path, false); err != nil { return err diff --git a/zk/constants.go b/zk/constants.go index ccafcfc9..885e2943 100644 --- a/zk/constants.go +++ b/zk/constants.go @@ -70,8 +70,9 @@ const ( ) const ( - FlagEphemeral = 1 - FlagSequence = 2 + FlagPersistence = 0 + FlagEphemeral = 1 + FlagSequence = 2 ) var (