Work in progress

This commit is contained in:
Alex Yatskov 2015-05-26 19:15:49 +09:00
parent e3daaa5e65
commit d6304ebd87
2 changed files with 41 additions and 24 deletions

View File

@ -24,9 +24,15 @@ package main
import ( import (
"bazil.org/fuse/fs" "bazil.org/fuse/fs"
"errors"
"fmt"
"io/ioutil" "io/ioutil"
"path"
"path/filepath" "path/filepath"
"regexp"
"strconv"
"sync/atomic" "sync/atomic"
"time"
) )
type database struct { type database struct {
@ -57,7 +63,7 @@ func (this *database) load(dir string) error {
return err return err
} }
this.vers, err = this.versions(dirs) this.vers, err = this.version(dirs)
if err != nil { if err != nil {
return err return err
} }
@ -73,12 +79,17 @@ func (this *database) save() error {
return nil return nil
} }
func (this *database) versions(dirs []string) ([]*version, error) { func (this *database) version(dirs []string) ([]*version, error) {
var vers []*version var vers []*version
var parent *version var parent *version
for _, dir := range dirs { for _, dir := range dirs {
ver, err := newVersion(dir, this, parent) time, err := this.parseVerName(path.Base(dir))
if err != nil {
return nil, err
}
ver, err := newVersion(dir, time, this, parent)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -122,3 +133,26 @@ func (this *database) Root() (fs.Node, error) {
func (this *database) AllocInode() uint64 { func (this *database) AllocInode() uint64 {
return atomic.AddUint64(&this.inodeCnt, 1) return atomic.AddUint64(&this.inodeCnt, 1)
} }
func (this *database) buildVerName(timestamp time.Time) string {
return fmt.Sprintf("%x", timestamp.Unix())
}
func (this *database) parseVerName(name string) (time.Time, error) {
re, err := regexp.Compile(`vfs_([0-9a-f])$`)
if err != nil {
return time.Unix(0, 0), err
}
matches := re.FindStringSubmatch(name)
if len(matches) < 2 {
return time.Unix(0, 0), errors.New("invalid version identifier")
}
timestamp, err := strconv.ParseInt(matches[1], 16, 64)
if err != nil {
return time.Unix(0, 0), err
}
return time.Unix(timestamp, 0), nil
}

View File

@ -28,8 +28,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -47,22 +45,7 @@ type InodeAllocator interface {
AllocInode() uint64 AllocInode() uint64
} }
func newVersion(base string, allocator InodeAllocator, parent *version) (*version, error) { func newVersion(base string, timestamp time.Time, allocator InodeAllocator, 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
}
meta, err := newVersionMetadata(filepath.Join(base, "meta.json")) meta, err := newVersionMetadata(filepath.Join(base, "meta.json"))
if err != nil { if err != nil {
return nil, err return nil, err
@ -71,7 +54,7 @@ func newVersion(base string, allocator InodeAllocator, parent *version) (*versio
ver := &version{ ver := &version{
base: base, base: base,
parent: parent, parent: parent,
timestamp: time.Unix(timeval, 0), timestamp: timestamp,
meta: meta, meta: meta,
inodeAloc: allocator} inodeAloc: allocator}
@ -170,11 +153,11 @@ func (this *version) Root() (fs.Node, error) {
func (this *version) dump(root *versionedDir, depth int) { func (this *version) dump(root *versionedDir, depth int) {
indent := strings.Repeat("\t", depth) indent := strings.Repeat("\t", depth)
for name, dir := range root.dirs { for name, dir := range root.dirs {
fmt.Printf("%s+ %s [%s@%d]\n", indent, name, dir.node.path, dir.node.ver.timestamp.Unix()) fmt.Printf("%s+ %s [%s@%x]\n", indent, name, dir.node.path, this.timestamp.Unix())
this.dump(dir, depth+1) this.dump(dir, depth+1)
} }
for name, file := range root.files { for name, file := range root.files {
fmt.Printf("%s- %s [%s@%d]\n", indent, name, file.node.path, file.node.ver.timestamp.Unix()) fmt.Printf("%s- %s [%s@%x]\n", indent, name, file.node.path, this.timestamp.Unix())
} }
} }