This commit is contained in:
Alex Yatskov 2015-05-24 20:56:29 +09:00
parent 7e1207065e
commit f28add5773
4 changed files with 44 additions and 33 deletions

16
dir.go
View File

@ -38,32 +38,30 @@ type versionedDir struct {
parent *versionedDir parent *versionedDir
} }
func newVersionedDir(node *versionedNode, inode uint64, parent *versionedDir) *versionedDir { func newVersionedDir(node *versionedNode, parent *versionedDir) *versionedDir {
return &versionedDir{ return &versionedDir{
dirs: make(map[string]*versionedDir), dirs: make(map[string]*versionedDir),
files: make(map[string]*versionedFile), files: make(map[string]*versionedFile),
node: node, node: node,
inode: inode, inode: node.ver.inodeAloc.AllocInode(),
parent: parent} parent: parent}
} }
func (this *versionedDir) createDir(name string) (*versionedDir, error) { func (this *versionedDir) createDir(name string) (*versionedDir, error) {
childPath := path.Join(this.node.path, name) childPath := path.Join(this.node.path, name)
childPathFull := this.node.ver.rebasePath(childPath)
if err := os.Mkdir(childPathFull, 0755); err != nil { if err := os.Mkdir(this.node.ver.rebasePath(childPath), 0755); err != nil {
return nil, err return nil, err
} }
info, err := os.Stat(childPathFull) node, err := newVersionedNode(childPath, this.node.ver)
if err != nil { if err != nil {
return nil, err return nil, err
} }
node := &versionedNode{childPath, info, this.node.ver} dir := newVersionedDir(node, this)
dir := newVersionedDir(node, this.node.ver.inodeAloc.AllocInode(), this)
this.dirs[name] = dir this.dirs[name] = dir
return dir, nil return dir, nil
} }
@ -83,7 +81,7 @@ func (this *versionedDir) createFile(name string, flags int) (*versionedFile, er
} }
node := &versionedNode{childPath, info, this.node.ver} node := &versionedNode{childPath, info, this.node.ver}
file := newVersionedFile(node, this.node.ver.inodeAloc.AllocInode(), this) file := newVersionedFile(node, this)
this.files[name] = file this.files[name] = file
return file, nil return file, nil

View File

@ -35,10 +35,10 @@ type versionedFile struct {
parent *versionedDir parent *versionedDir
} }
func newVersionedFile(node *versionedNode, inode uint64, parent *versionedDir) *versionedFile { func newVersionedFile(node *versionedNode, parent *versionedDir) *versionedFile {
return &versionedFile{ return &versionedFile{
node: node, node: node,
inode: inode, inode: node.ver.inodeAloc.AllocInode(),
parent: parent} parent: parent}
} }

13
node.go
View File

@ -38,6 +38,19 @@ type versionedNode struct {
type versionedNodeMap map[string]*versionedNode type versionedNodeMap map[string]*versionedNode
func newVersionedNode(path string, ver *version) (*versionedNode, error) {
info, err := os.Stat(ver.rebasePath(path))
if err != nil {
return nil, err
}
return newVersionedNodeStat(path, ver, info), nil
}
func newVersionedNodeStat(path string, ver *version, info os.FileInfo) *versionedNode {
return &versionedNode{path, info, ver}
}
func (this *versionedNode) rebasedPath() string { func (this *versionedNode) rebasedPath() string {
return this.ver.rebasePath(this.path) return this.ver.rebasePath(this.path)
} }

View File

@ -63,22 +63,19 @@ func newVersion(base string, allocator InodeAllocator, parent *version) (*versio
return nil, err return nil, err
} }
ver := &version{ meta, err := newVersionMetadata(filepath.Join(base, "meta.json"))
base: base,
parent: parent,
timestamp: time.Unix(timeval, 0),
inodeAloc: allocator}
ver.meta, err = newVersionMetadata(ver.metadataPath())
if err != nil { if err != nil {
return nil, err return nil, err
} }
return ver, nil ver := &version{
} base: base,
parent: parent,
timestamp: time.Unix(timeval, 0),
meta: meta,
inodeAloc: allocator}
func (this *version) newVersionedNode(path string, info os.FileInfo) *versionedNode { return ver, nil
return &versionedNode{path, info, this}
} }
func (this *version) scanDir(path string) (versionedNodeMap, error) { func (this *version) scanDir(path string) (versionedNodeMap, error) {
@ -96,14 +93,16 @@ func (this *version) scanDir(path string) (versionedNodeMap, error) {
ownNodes := make(versionedNodeMap) ownNodes := make(versionedNodeMap)
{ {
nodes, err := ioutil.ReadDir(this.rebasePath(path)) infos, err := ioutil.ReadDir(this.rebasePath(path))
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, node := range nodes { for _, info := range infos {
ownNodes[node.Name()] = this.newVersionedNode(filepath.Join(path, node.Name()), node) childName := info.Name()
childPath := filepath.Join(path, childName)
ownNodes[childName] = newVersionedNodeStat(childPath, this, info)
} }
} }
@ -129,14 +128,14 @@ func (this *version) buildVerDir(dir *versionedDir) error {
for name, node := range nodes { for name, node := range nodes {
if node.info.IsDir() { if node.info.IsDir() {
subDir := newVersionedDir(node, this.inodeAloc.AllocInode(), dir) subDir := newVersionedDir(node, dir)
if err := this.buildVerDir(subDir); err != nil { if err := this.buildVerDir(subDir); err != nil {
return err return err
} }
dir.dirs[name] = subDir dir.dirs[name] = subDir
} else { } else {
dir.files[name] = newVersionedFile(node, this.inodeAloc.AllocInode(), dir) dir.files[name] = newVersionedFile(node, dir)
} }
} }
@ -144,17 +143,18 @@ func (this *version) buildVerDir(dir *versionedDir) error {
} }
func (this *version) resolve() error { func (this *version) resolve() error {
node, err := os.Stat(this.rebasePath("/")) node, err := newVersionedNode("/", this)
if err != nil { if err != nil {
return err return err
} }
this.root = newVersionedDir( root := newVersionedDir(node, nil)
this.newVersionedNode("/", node), if err = this.buildVerDir(root); err != nil {
this.inodeAloc.AllocInode(), return err
nil) }
return this.buildVerDir(this.root) this.root = root
return nil
} }
func (this *version) metadataPath() string { func (this *version) metadataPath() string {