change filepath to path and fix path namespace collision

This commit is contained in:
Hongchao Deng 2013-09-04 20:50:04 -04:00
parent aed76d0e08
commit 03af286b03
3 changed files with 33 additions and 33 deletions

View File

@ -1,7 +1,7 @@
package fileSystem package fileSystem
import ( import (
"path/filepath" "path"
"strings" "strings"
"time" "time"
@ -24,15 +24,15 @@ func New() *FileSystem {
} }
func (fs *FileSystem) Get(path string, recusive bool, index uint64, term uint64) (*Event, error) { func (fs *FileSystem) Get(key_path string, recusive bool, index uint64, term uint64) (*Event, error) {
// TODO: add recursive get // TODO: add recursive get
n, err := fs.InternalGet(path, index, term) n, err := fs.InternalGet(key_path, index, term)
if err != nil { if err != nil {
return nil, err return nil, err
} }
e := newEvent(Get, path, index, term) e := newEvent(Get, key_path, index, term)
if n.IsDir() { // node is dir if n.IsDir() { // node is dir
e.KVPairs = make([]KeyValuePair, len(n.Children)) e.KVPairs = make([]KeyValuePair, len(n.Children))
@ -60,23 +60,23 @@ func (fs *FileSystem) Get(path string, recusive bool, index uint64, term uint64)
return e, nil return e, nil
} }
func (fs *FileSystem) Set(path string, value string, expireTime time.Time, index uint64, term uint64) (*Event, error) { func (fs *FileSystem) Set(key_path string, value string, expireTime time.Time, index uint64, term uint64) (*Event, error) {
path = filepath.Clean("/" + path) key_path = path.Clean("/" + key_path)
// update file system known index and term // update file system known index and term
fs.Index, fs.Term = index, term fs.Index, fs.Term = index, term
dir, name := filepath.Split(path) dir, name := path.Split(key_path)
// walk through the path and get the last directory node // walk through the key_path and get the last directory node
d, err := fs.walk(dir, fs.checkDir) d, err := fs.walk(dir, fs.checkDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
f := newFile(path, value, fs.Index, fs.Term, d, "", expireTime) f := newFile(key_path, value, fs.Index, fs.Term, d, "", expireTime)
e := newEvent(Set, path, fs.Index, fs.Term) e := newEvent(Set, key_path, fs.Index, fs.Term)
e.Value = f.Value e.Value = f.Value
// remove previous file if exist // remove previous file if exist
@ -107,7 +107,7 @@ func (fs *FileSystem) Set(path string, value string, expireTime time.Time, index
return e, nil return e, nil
} }
func (fs *FileSystem) TestAndSet(path string, recurisive bool, index uint64, term uint64) { func (fs *FileSystem) TestAndSet(key_path string, recurisive bool, index uint64, term uint64) {
} }
@ -115,8 +115,8 @@ func (fs *FileSystem) TestIndexAndSet() {
} }
func (fs *FileSystem) Delete(path string, recurisive bool, index uint64, term uint64) (*Event, error) { func (fs *FileSystem) Delete(key_path string, recurisive bool, index uint64, term uint64) (*Event, error) {
n, err := fs.InternalGet(path, index, term) n, err := fs.InternalGet(key_path, index, term)
if err != nil { if err != nil {
return nil, err return nil, err
@ -128,7 +128,7 @@ func (fs *FileSystem) Delete(path string, recurisive bool, index uint64, term ui
return nil, err return nil, err
} }
e := newEvent(Delete, path, index, term) e := newEvent(Delete, key_path, index, term)
if n.IsDir() { if n.IsDir() {
e.Dir = true e.Dir = true
@ -139,9 +139,9 @@ func (fs *FileSystem) Delete(path string, recurisive bool, index uint64, term ui
return e, nil return e, nil
} }
// walk function walks all the path and apply the walkFunc on each directory // walk function walks all the key_path and apply the walkFunc on each directory
func (fs *FileSystem) walk(path string, walkFunc func(prev *Node, component string) (*Node, error)) (*Node, error) { func (fs *FileSystem) walk(key_path string, walkFunc func(prev *Node, component string) (*Node, error)) (*Node, error) {
components := strings.Split(path, "/") components := strings.Split(key_path, "/")
curr := fs.Root curr := fs.Root
@ -161,9 +161,9 @@ func (fs *FileSystem) walk(path string, walkFunc func(prev *Node, component stri
return curr, nil return curr, nil
} }
// InternalGet function get the node of the given path. // InternalGet function get the node of the given key_path.
func (fs *FileSystem) InternalGet(path string, index uint64, term uint64) (*Node, error) { func (fs *FileSystem) InternalGet(key_path string, index uint64, term uint64) (*Node, error) {
path = filepath.Clean("/" + path) key_path = path.Clean("/" + key_path)
// update file system known index and term // update file system known index and term
fs.Index, fs.Term = index, term fs.Index, fs.Term = index, term
@ -177,7 +177,7 @@ func (fs *FileSystem) InternalGet(path string, index uint64, term uint64) (*Node
return nil, etcdErr.NewError(100, "get") return nil, etcdErr.NewError(100, "get")
} }
f, err := fs.walk(path, walkFunc) f, err := fs.walk(key_path, walkFunc)
if err != nil { if err != nil {
return nil, err return nil, err
@ -198,7 +198,7 @@ func (fs *FileSystem) checkDir(parent *Node, dirName string) (*Node, error) {
return subDir, nil return subDir, nil
} }
n := newDir(filepath.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)
parent.Children[dirName] = n parent.Children[dirName] = n

View File

@ -2,7 +2,7 @@ package fileSystem
import ( import (
"fmt" "fmt"
"path/filepath" "path"
"sync" "sync"
"time" "time"
@ -32,9 +32,9 @@ type Node struct {
removeChan chan bool // remove channel removeChan chan bool // remove channel
} }
func newFile(path string, value string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node { func newFile(key_path string, value string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node {
return &Node{ return &Node{
Path: path, Path: key_path,
CreateIndex: createIndex, CreateIndex: createIndex,
CreateTerm: createTerm, CreateTerm: createTerm,
Parent: parent, Parent: parent,
@ -45,9 +45,9 @@ func newFile(path string, value string, createIndex uint64, createTerm uint64, p
} }
} }
func newDir(path string, createIndex uint64, createTerm uint64, parent *Node, ACL string) *Node { func newDir(key_path string, createIndex uint64, createTerm uint64, parent *Node, ACL string) *Node {
return &Node{ return &Node{
Path: path, Path: key_path,
CreateIndex: createIndex, CreateIndex: createIndex,
CreateTerm: createTerm, CreateTerm: createTerm,
Parent: parent, Parent: parent,
@ -69,7 +69,7 @@ func (n *Node) Remove(recursive bool) error {
} }
if !n.IsDir() { // file node: key-value pair if !n.IsDir() { // file node: key-value pair
_, name := filepath.Split(n.Path) _, name := path.Split(n.Path)
if n.Parent.Children[name] == n { if n.Parent.Children[name] == n {
// This is the only pointer to Node object // This is the only pointer to Node object
@ -91,7 +91,7 @@ func (n *Node) Remove(recursive bool) error {
} }
// delete self // delete self
_, name := filepath.Split(n.Path) _, name := path.Split(n.Path)
if n.Parent.Children[name] == n { if n.Parent.Children[name] == n {
delete(n.Parent.Children, name) delete(n.Parent.Children, name)
n.removeChan <- true n.removeChan <- true
@ -180,7 +180,7 @@ func (n *Node) Add(child *Node) error {
return etcdErr.NewError(104, "") return etcdErr.NewError(104, "")
} }
_, name := filepath.Split(child.Path) _, name := path.Split(child.Path)
_, ok := n.Children[name] _, ok := n.Children[name]
@ -255,7 +255,7 @@ func (n *Node) Expire() {
// For example if we have /foo/_hidden and /foo/notHidden, get "/foo" // For example if we have /foo/_hidden and /foo/notHidden, get "/foo"
// will only return /foo/notHidden // will only return /foo/notHidden
func (n *Node) IsHidden() bool { func (n *Node) IsHidden() bool {
_, name := filepath.Split(n.Path) _, name := path.Split(n.Path)
if name[0] == '_' { //hidden if name[0] == '_' { //hidden
return true return true

View File

@ -2,7 +2,7 @@ package fileSystem
import ( import (
"container/list" "container/list"
"path/filepath" "path"
"strings" "strings"
) )
@ -53,7 +53,7 @@ func (wh *watcherHub) notify(e *Event) {
// walk through all the paths // walk through all the paths
for _, segment := range segments { for _, segment := range segments {
currPath = filepath.Join(currPath, segment) currPath = path.Join(currPath, segment)
l, ok := wh.watchers[currPath] l, ok := wh.watchers[currPath]