This commit is contained in:
Alex Yatskov 2015-06-24 12:42:36 +09:00
parent 4e793c7b5a
commit fb7b8f070f
6 changed files with 46 additions and 49 deletions

View File

@ -26,9 +26,11 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"path" "path"
"path/filepath" "path/filepath"
"sort" "sort"
"time"
"bazil.org/fuse/fs" "bazil.org/fuse/fs"
) )
@ -42,30 +44,19 @@ type database struct {
vers verList vers verList
} }
func newDatabase(dir string, index uint, writable bool) (*database, error) { func newDatabase(path string) (*database, error) {
db := &database{base: dir} base, err := filepath.Abs(path)
if err := db.load(dir, index, writable); err != nil { if err != nil {
return nil, err return nil, err
} }
return db, nil return &database{base, nil}, nil
} }
func (db *database) load(dir string, index uint, writable bool) error { func (db *database) load(index uint) error {
var err error var err error
db.base, err = filepath.Abs(dir) db.vers, err = db.loadVers(db.base)
if err != nil {
return err
}
if writable {
if err := buildNewVersion(db.base); err != nil {
return err
}
}
db.vers, err = db.buildVersions(db.base)
if err != nil { if err != nil {
return err return err
} }
@ -78,7 +69,7 @@ func (db *database) load(dir string, index uint, writable bool) error {
db.vers = db.vers[:index] db.vers = db.vers[:index]
} }
if lastVer := db.lastVersion(); lastVer != nil { if lastVer := db.lastVer(); lastVer != nil {
return lastVer.resolve() return lastVer.resolve()
} }
@ -86,7 +77,7 @@ func (db *database) load(dir string, index uint, writable bool) error {
} }
func (db *database) save() error { func (db *database) save() error {
lastVer := db.lastVersion() lastVer := db.lastVer()
for _, ver := range db.vers { for _, ver := range db.vers {
if err := ver.finalize(ver == lastVer); err != nil { if err := ver.finalize(ver == lastVer); err != nil {
@ -97,7 +88,7 @@ func (db *database) save() error {
return nil return nil
} }
func (db *database) buildVersions(base string) (verList, error) { func (db *database) loadVers(base string) (verList, error) {
nodes, err := ioutil.ReadDir(base) nodes, err := ioutil.ReadDir(base)
if err != nil { if err != nil {
return nil, err return nil, err
@ -114,7 +105,7 @@ func (db *database) buildVersions(base string) (verList, error) {
return nil, err return nil, err
} }
ver, err := newVersion(path.Join(base, node.Name()), timestamp, db) ver, err := newVer(path.Join(base, node.Name()), timestamp, db)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -133,7 +124,7 @@ func (db *database) buildVersions(base string) (verList, error) {
return vers, nil return vers, nil
} }
func (db *database) lastVersion() *version { func (db *database) lastVer() *version {
count := len(db.vers) count := len(db.vers)
if count == 0 { if count == 0 {
return nil return nil
@ -142,6 +133,15 @@ func (db *database) lastVersion() *version {
return db.vers[count-1] return db.vers[count-1]
} }
func (db *database) createNewVer() error {
name := buildVerName(time.Now())
if err := os.MkdirAll(path.Join(db.base, name, "root"), 0755); err != nil {
return err
}
return nil
}
func (db *database) dump() { func (db *database) dump() {
for index, ver := range db.vers { for index, ver := range db.vers {
fmt.Printf("version: %d\ttime: %s\n", index+1, ver.timestamp.String()) fmt.Printf("version: %d\ttime: %s\n", index+1, ver.timestamp.String())
@ -150,5 +150,5 @@ func (db *database) dump() {
// FS // FS
func (db *database) Root() (fs.Node, error) { func (db *database) Root() (fs.Node, error) {
return db.lastVersion().root, nil return db.lastVer().root, nil
} }

2
dir.go
View File

@ -61,7 +61,7 @@ func (vd *verDir) version() error {
return nil return nil
} }
node := newVerNode(vd.node.path, vd.node.ver.db.lastVersion(), vd.node, NodeFlagDir|NodeFlagNew) node := newVerNode(vd.node.path, vd.node.ver.db.lastVer(), vd.node, NodeFlagDir|NodeFlagNew)
if err := os.MkdirAll(node.rebasedPath(), 0755); err != nil { if err := os.MkdirAll(node.rebasedPath(), 0755); err != nil {
return err return err
} }

View File

@ -57,7 +57,7 @@ func (vf *verFile) version() error {
return nil return nil
} }
node := newVerNode(vf.node.path, vf.node.ver.db.lastVersion(), vf.node, NodeFlagNew) node := newVerNode(vf.node.path, vf.node.ver.db.lastVer(), vf.node, NodeFlagNew)
if _, err := copyFile(vf.node.rebasedPath(), node.rebasedPath()); err != nil { if _, err := copyFile(vf.node.rebasedPath(), node.rebasedPath()); err != nil {
return err return err
} }

10
util.go
View File

@ -27,7 +27,6 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path"
"regexp" "regexp"
"strconv" "strconv"
"sync/atomic" "sync/atomic"
@ -62,15 +61,6 @@ func copyFile(src, dst string) (int64, error) {
return io.Copy(srcFile, dstFile) return io.Copy(srcFile, dstFile)
} }
func buildNewVersion(base string) error {
name := buildVerName(time.Now())
if err := os.MkdirAll(path.Join(base, name, "root"), 0755); err != nil {
return err
}
return nil
}
func buildVerName(timestamp time.Time) string { func buildVerName(timestamp time.Time) string {
return fmt.Sprintf("ver_%.16x", timestamp.Unix()) return fmt.Sprintf("ver_%.16x", timestamp.Unix())
} }

View File

@ -44,7 +44,7 @@ type version struct {
db *database db *database
} }
func newVersion(base string, timestamp time.Time, db *database) (*version, error) { func newVer(base string, timestamp time.Time, db *database) (*version, error) {
meta, err := newVerMeta(filepath.Join(base, "meta.json")) meta, err := newVerMeta(filepath.Join(base, "meta.json"))
if err != nil { if err != nil {
return nil, err return nil, err

29
vfs.go
View File

@ -50,25 +50,27 @@ func main() {
os.Exit(2) os.Exit(2)
} }
mount := flag.NArg() > 1 mountable := flag.NArg() > 1
writable := mount && !*readonly && *version == 0 mutable := mountable && !*readonly && *version == 0
db, err := newDatabase(flag.Arg(0), *version, writable) db, err := newDatabase(flag.Arg(0))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if mount { if mutable {
if writable { if err := db.createNewVer(); err != nil {
defer func() {
if err := db.save(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
}()
} }
if err := db.load(*version); err != nil {
log.Fatal(err)
}
if mountable {
var options []fuse.MountOption var options []fuse.MountOption
if !writable { if !mutable {
options = append(options, fuse.ReadOnly()) options = append(options, fuse.ReadOnly())
} }
@ -78,8 +80,7 @@ func main() {
} }
defer conn.Close() defer conn.Close()
err = fs.Serve(conn, db) if err := fs.Serve(conn, db); err != nil {
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -87,6 +88,12 @@ func main() {
if err := conn.MountError; err != nil { if err := conn.MountError; err != nil {
log.Fatal(err) log.Fatal(err)
} }
if mutable {
if err := db.save(); err != nil {
log.Fatal(err)
}
}
} else { } else {
db.dump() db.dump()
} }