This commit is contained in:
Alex Yatskov 2015-05-14 20:24:11 +09:00
parent 9bcfb20a9e
commit 02d014ccea

View File

@ -27,7 +27,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -87,13 +86,9 @@ func newVersion(base string, parent *version) (*version, error) {
} }
func (this *version) scanDir(path string) (map[string]versionedNode, error) { func (this *version) scanDir(path string) (map[string]versionedNode, error) {
log.Printf("scanDir %s for %p", path, this)
var baseNodes map[string]versionedNode var baseNodes map[string]versionedNode
if this.parent != nil { if this.parent != nil {
var err error var err error
baseNodes = make(map[string]versionedNode)
baseNodes, err = this.parent.scanDir(path) baseNodes, err = this.parent.scanDir(path)
if err != nil { if err != nil {
return nil, err return nil, err
@ -102,15 +97,14 @@ func (this *version) scanDir(path string) (map[string]versionedNode, error) {
ownNodes := make(map[string]versionedNode) ownNodes := make(map[string]versionedNode)
{ {
nodes, err := ioutil.ReadDir(path) nodes, err := ioutil.ReadDir(this.rebasePath(path))
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, node := range nodes { for _, node := range nodes {
name := node.Name() name := node.Name()
subPath := filepath.Join(path, name) ownNodes[name] = versionedNode{this.rebasePath(path, name), node}
ownNodes[name] = versionedNode{subPath, node}
} }
} }
@ -126,8 +120,6 @@ func (this *version) scanDir(path string) (map[string]versionedNode, error) {
} }
func (this *version) buildDir(path string, dir *versionedDir) error { func (this *version) buildDir(path string, dir *versionedDir) error {
log.Printf("buildDir %s for %p", path, this)
nodes, err := this.scanDir(path) nodes, err := this.scanDir(path)
if err != nil { if err != nil {
return err return err
@ -151,25 +143,20 @@ func (this *version) buildDir(path string, dir *versionedDir) error {
} }
func (this *version) scanFs() error { func (this *version) scanFs() error {
log.Printf("scanFs for %p", this) node, err := os.Stat(this.rebasePath("/"))
node, err := os.Stat(this.rootPath())
if err != nil { if err != nil {
return err return err
} }
this.root = newVersionedDir( this.root = newVersionedDir(
versionedNode{this.rootPath(), node}, versionedNode{"/", node},
this.allocInode()) this.allocInode())
return this.buildDir(this.rootPath(), this.root) return this.buildDir("/", this.root)
} }
func (this *version) loadMetadata() error { func (this *version) loadMetadata() error {
log.Printf("loadMetadata %s for %p", this.metadataPath(), this) if _, err := os.Stat(this.metadataPath()); os.IsNotExist(err) {
path := this.metadataPath()
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil return nil
} }
@ -198,8 +185,9 @@ func (this *version) metadataPath() string {
return filepath.Join(this.base, "meta.json") return filepath.Join(this.base, "meta.json")
} }
func (this *version) rootPath() string { func (this *version) rebasePath(paths ...string) string {
return filepath.Join(this.base, "root") combined := append([]string{this.base, "root"}, paths...)
return filepath.Join(combined...)
} }
func (this *version) allocInode() uint64 { func (this *version) allocInode() uint64 {