diff --git a/database.go b/database.go index f0b89a9..b4df801 100644 --- a/database.go +++ b/database.go @@ -23,38 +23,94 @@ package main import ( + "fmt" "io/ioutil" - "os" + "path/filepath" + "regexp" + "strconv" + "strings" + "time" ) type Database struct { + base string + versions []Version +} + +func NewDatabase(dir string) (*Database, error) { + db := &Database{base: dir} + if err := db.load(dir); err != nil { + return nil, err + } + + return db, nil } func (this *Database) load(dir string) error { + base, err := filepath.Abs(dir) + if err != nil { + return err + } + + this.base = base + dirs, err := this.scan(dir) if err != nil { return err } - return err + this.version(dirs) + return nil } -func (this *Database) scan(dir string) ([]os.FileInfo, error) { +func (this *Database) version(dirs []string) []Version { + versions := make([]Version, 0, len(dirs)) + + // var parent *Version + // for _, dir := range dirs { + // base := filepath.Join(this.base, dir) + // // timestamp := this.timestamp(dir) + // // version := NewVersion(base, timestamp, parent) + // // parent = version + // } + + return versions +} + +func (this *Database) scan(dir string) ([]VersionMeta, error) { nodes, err := ioutil.ReadDir(dir) if err != nil { return nil, err } - var dirs []os.FileInfo + re, err := regexp.Compile(`^vfs_([0-9a-f])$`) + + var meta []VersionMeta for _, node := range nodes { - if node.IsDir() { - dirs = append(dirs, node) + if !node.IsDir() { + continue } + + 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 dirs, nil + return meta, nil } -func (this *Database) save(dir string) { +func (this *Database) scanProps(path string) error { } diff --git a/version.go b/version.go index ce1ef47..cdcef97 100644 --- a/version.go +++ b/version.go @@ -26,12 +26,17 @@ import ( "time" ) -type Version struct { - root string - parent *Version - version time.Time +type VersionMeta struct { + path string + timestamp time.Time } -func (this *Version) NewVersion(root string, parent *Version) *Version { - return &Version{root: root, parent: parent} +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} } diff --git a/vfs.go b/vfs.go index 1942f8b..1c682ec 100644 --- a/vfs.go +++ b/vfs.go @@ -23,5 +23,6 @@ package main func main() { - + var db Database + db.load("fs") }