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"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"time"
"bazil.org/fuse/fs"
)
@ -42,30 +44,19 @@ type database struct {
vers verList
}
func newDatabase(dir string, index uint, writable bool) (*database, error) {
db := &database{base: dir}
if err := db.load(dir, index, writable); err != nil {
func newDatabase(path string) (*database, error) {
base, err := filepath.Abs(path)
if err != nil {
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
db.base, err = filepath.Abs(dir)
if err != nil {
return err
}
if writable {
if err := buildNewVersion(db.base); err != nil {
return err
}
}
db.vers, err = db.buildVersions(db.base)
db.vers, err = db.loadVers(db.base)
if err != nil {
return err
}
@ -78,7 +69,7 @@ func (db *database) load(dir string, index uint, writable bool) error {
db.vers = db.vers[:index]
}
if lastVer := db.lastVersion(); lastVer != nil {
if lastVer := db.lastVer(); lastVer != nil {
return lastVer.resolve()
}
@ -86,7 +77,7 @@ func (db *database) load(dir string, index uint, writable bool) error {
}
func (db *database) save() error {
lastVer := db.lastVersion()
lastVer := db.lastVer()
for _, ver := range db.vers {
if err := ver.finalize(ver == lastVer); err != nil {
@ -97,7 +88,7 @@ func (db *database) save() error {
return nil
}
func (db *database) buildVersions(base string) (verList, error) {
func (db *database) loadVers(base string) (verList, error) {
nodes, err := ioutil.ReadDir(base)
if err != nil {
return nil, err
@ -114,7 +105,7 @@ func (db *database) buildVersions(base string) (verList, error) {
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 {
return nil, err
}
@ -133,7 +124,7 @@ func (db *database) buildVersions(base string) (verList, error) {
return vers, nil
}
func (db *database) lastVersion() *version {
func (db *database) lastVer() *version {
count := len(db.vers)
if count == 0 {
return nil
@ -142,6 +133,15 @@ func (db *database) lastVersion() *version {
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() {
for index, ver := range db.vers {
fmt.Printf("version: %d\ttime: %s\n", index+1, ver.timestamp.String())
@ -150,5 +150,5 @@ func (db *database) dump() {
// FS
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
}
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 {
return err
}

View File

@ -57,7 +57,7 @@ func (vf *verFile) version() error {
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 {
return err
}

10
util.go
View File

@ -27,7 +27,6 @@ import (
"fmt"
"io"
"os"
"path"
"regexp"
"strconv"
"sync/atomic"
@ -62,15 +61,6 @@ func copyFile(src, dst string) (int64, error) {
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 {
return fmt.Sprintf("ver_%.16x", timestamp.Unix())
}

View File

@ -44,7 +44,7 @@ type version struct {
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"))
if err != nil {
return nil, err

33
vfs.go
View File

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