From c35a348bf99109d56ceca70475d70576efa80a7c Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 10 May 2015 14:14:15 +0900 Subject: [PATCH] Work in progress --- database.go | 75 ++++++++++++++++++++--------------------------------- version.go | 27 ++++++++++++++----- 2 files changed, 48 insertions(+), 54 deletions(-) diff --git a/database.go b/database.go index b4df801..f41b34e 100644 --- a/database.go +++ b/database.go @@ -23,18 +23,13 @@ package main import ( - "fmt" "io/ioutil" "path/filepath" - "regexp" - "strconv" - "strings" - "time" ) type Database struct { base string - versions []Version + versions []*Version } func NewDatabase(dir string) (*Database, error) { @@ -52,65 +47,51 @@ func (this *Database) load(dir string) error { return err } - this.base = base - - dirs, err := this.scan(dir) + dirs, err := this.scan(base) if err != nil { return err } - this.version(dirs) + versions, err := this.version(dirs) + if err != nil { + return err + } + + this.base = base + this.versions = versions + return nil } -func (this *Database) version(dirs []string) []Version { - versions := make([]Version, 0, len(dirs)) +func (this *Database) version(dirs []string) ([]*Version, error) { + var versions []*Version + var parent *Version - // var parent *Version - // for _, dir := range dirs { - // base := filepath.Join(this.base, dir) - // // timestamp := this.timestamp(dir) - // // version := NewVersion(base, timestamp, parent) - // // parent = version - // } + for _, dir := range dirs { + version, err := NewVersion(dir, parent) + if err != nil { + return nil, err + } - return versions + parent = version + versions = append(versions, version) + } + + return versions, nil } -func (this *Database) scan(dir string) ([]VersionMeta, error) { +func (this *Database) scan(dir string) ([]string, error) { nodes, err := ioutil.ReadDir(dir) if err != nil { return nil, err } - re, err := regexp.Compile(`^vfs_([0-9a-f])$`) - - var meta []VersionMeta + var dirs []string for _, node := range nodes { - if !node.IsDir() { - continue + if node.IsDir() { + dirs = append(dirs, node.Name()) } - - matches := re.FindStringSubmatch(node.Name()) - if len(matches) < 2 { - continue - } - - timestamp, err := strconv.ParseInt(matches[1], 16, 64) - if err != nil { - continue - } - - item := VersionMeta{ - path: filepath.Join(dir, matches[0]), - timestamp: time.Unix(timestamp, 0)} - - meta = append(meta, item) } - return meta, nil -} - -func (this *Database) scanProps(path string) error { - + return dirs, nil } diff --git a/version.go b/version.go index cdcef97..ca99208 100644 --- a/version.go +++ b/version.go @@ -23,20 +23,33 @@ package main import ( + "fmt" + "regexp" + "strconv" "time" ) -type VersionMeta struct { - path string - timestamp time.Time -} - type Version struct { base string parent *Version timestamp time.Time } -func NewVersion(base string, timestamp time.Time, parent *Version) *Version { - return &Version{base, parent, timestamp} +func NewVersion(base string, parent *Version) (*Version, error) { + re, err := regexp.Compile(`/vfs_([0-9a-f])$`) + if err != nil { + return nil, err + } + + matches := re.FindStringSubmatch(base) + if len(matches) < 2 { + return nil, fmt.Errorf("invalid version identifier for %s", base) + } + + timeval, err := strconv.ParseInt(matches[1], 16, 64) + if err != nil { + return nil, err + } + + return &Version{base: base, parent: parent, timestamp: time.Unix(timeval, 0)}, nil }