kpmy 8 年之前
父节点
当前提交
a6c9afdbc2
共有 5 个文件被更改,包括 31 次插入28 次删除
  1. 14 7
      dav_cmd/main.go
  2. 4 5
      wdfs/files.go
  3. 3 4
      wdfs/filesys.go
  4. 4 5
      wdfs/locations.go
  5. 6 7
      wdfs/locksys.go

+ 14 - 7
dav_cmd/main.go

@@ -16,13 +16,12 @@ import (
 	. "github.com/kpmy/ypk/tc"
 	"github.com/peterbourgon/diskv"
 	"golang.org/x/net/webdav"
+	"io"
 )
 
 var KV *diskv.Diskv
 
 func init() {
-	log.SetFlags(0)
-
 	KV = diskv.New(diskv.Options{
 		BasePath: ".diskv",
 		Transform: func(s string) []string {
@@ -31,19 +30,27 @@ func init() {
 	})
 }
 
+const emptyDir = "QmdniF66q5wYDEyp2PYp6wXwgTUg3ssmb8NYSyfytwyf2j"
+
 func main() {
-	log.Println(os.Getwd())
-	defaultRoot := "QmdniF66q5wYDEyp2PYp6wXwgTUg3ssmb8NYSyfytwyf2j"
+	dir, _ := os.Getwd()
+	log.Println("started at", dir)
+
+	defaultRoot := emptyDir
 	if r, err := KV.Read("root"); err == nil && len(r) > 0 {
 		defaultRoot = string(r)
 	} else {
 		KV.Write("root", []byte(defaultRoot))
 	}
 
+	log.Println("root hash", defaultRoot)
+
 	if r, err := KV.Read("ipfs"); err == nil {
 		ipfs_api.Addr = string(r)
 	}
 
+	log.Println("ipfs api at", ipfs_api.Addr)
+
 	rootCh := make(chan string, 16)
 	go func(ch chan string) {
 		for {
@@ -52,11 +59,11 @@ func main() {
 				if s != "" {
 					if old, err := KV.Read("root"); err == nil && s != string(old) {
 						history := new(bytes.Buffer)
+						history.Write(old)
+						history.Write([]byte("\n"))
 						if hs, err := KV.Read("root.history"); err == nil {
-							history.Write(hs)
-							history.Write([]byte("\n"))
+							io.CopyN(history, bytes.NewBuffer(hs), int64(history.Len()*128)) //лимит истории
 						}
-						history.Write(old)
 						KV.Write("root.history", history.Bytes())
 						i++
 					}

+ 4 - 5
wdfs/files.go

@@ -16,7 +16,6 @@ import (
 	. "github.com/kpmy/ypk/tc"
 	"golang.org/x/net/webdav"
 	"io"
-	"log"
 	"strconv"
 	"strings"
 )
@@ -232,18 +231,18 @@ func (f *file) writePropsObject(props map[xml.Name]dom.Element) {
 }
 
 func (f *file) DeadProps() (ret map[xml.Name]webdav.Property, err error) {
-	log.Println("file prop get")
+	//log.Println("file prop get")
 	pm, _ := f.readPropsObject()
 	ret = props2webdav(pm)
-	log.Println("read file props", ret)
+	//log.Println("read file props", ret)
 	return
 }
 
 func (f *file) Patch(patch []webdav.Proppatch) (ret []webdav.Propstat, err error) {
-	log.Println("file prop patch", patch)
+	//log.Println("file prop patch", patch)
 	pe, _ := f.readPropsObject()
 	ret = propsPatch(pe, patch)
-	log.Println("write file props", pe)
+	//log.Println("write file props", pe)
 	f.writePropsObject(pe)
 	return
 }

+ 3 - 4
wdfs/filesys.go

@@ -8,7 +8,6 @@ import (
 	"github.com/kpmy/ypk/fn"
 	. "github.com/kpmy/ypk/tc"
 	"golang.org/x/net/webdav"
-	"log"
 	"os"
 	"strings"
 	"time"
@@ -32,7 +31,7 @@ func (f *filesystem) Mkdir(name string, perm os.FileMode) (err error) {
 					prop.Attr("modified", fmt.Sprint(time.Now().Unix()))
 					propHash, _ := ipfs_api.Shell().Add(dom.EncodeWithHeader(prop))
 					if tail.Hash, err = ipfs_api.Shell().PatchLink(tail.Hash, "*", propHash, false); err != nil {
-						log.Fatal(err)
+						Halt(100, err)
 						return
 					}
 					onlyOne = false
@@ -54,7 +53,7 @@ func (f *filesystem) Mkdir(name string, perm os.FileMode) (err error) {
 }
 
 func (f *filesystem) OpenFile(name string, flag int, perm os.FileMode) (ret webdav.File, err error) {
-	log.Println("open", name, flag, perm)
+	//log.Println("open", name, flag, perm)
 	path := newChain(f.root.mirror(), f.root.Hash+"/"+strings.Trim(name, "/"))
 	switch tail := path.tail(); {
 	case tail.exists() && tail.IsDir():
@@ -75,7 +74,7 @@ func (f *filesystem) OpenFile(name string, flag int, perm os.FileMode) (ret webd
 			err = os.ErrNotExist
 		}
 	default:
-		log.Println("open error", name, flag, perm)
+		//log.Println("open error", name, flag, perm)
 		err = os.ErrNotExist
 	}
 	return

+ 4 - 5
wdfs/locations.go

@@ -7,7 +7,6 @@ import (
 	"github.com/kpmy/ypk/fn"
 	. "github.com/kpmy/ypk/tc"
 	"golang.org/x/net/webdav"
-	"log"
 	"os"
 	"strconv"
 	"strings"
@@ -143,18 +142,18 @@ func (l *loc) writePropsObject(props map[xml.Name]dom.Element) {
 }
 
 func (l *loc) DeadProps() (ret map[xml.Name]webdav.Property, err error) {
-	log.Println("loc props get")
+	//log.Println("loc props get")
 	pm, _ := l.readPropsObject()
 	ret = props2webdav(pm)
-	log.Println(ret)
+	//log.Println(ret)
 	return
 }
 
 func (l *loc) Patch(patch []webdav.Proppatch) (ret []webdav.Propstat, err error) {
-	log.Println("loc prop patch", patch)
+	//log.Println("loc prop patch", patch)
 	pe, _ := l.readPropsObject()
 	ret = propsPatch(pe, patch)
-	log.Println("loc file props", pe)
+	//log.Println("loc file props", pe)
 	l.writePropsObject(pe)
 	return
 

+ 6 - 7
wdfs/locksys.go

@@ -3,7 +3,6 @@ package wdfs
 import (
 	"fmt"
 	"golang.org/x/net/webdav"
-	"log"
 	"strings"
 	"sync"
 	"time"
@@ -21,7 +20,7 @@ type locksystem struct {
 
 func (l *locksystem) Confirm(now time.Time, name0, name1 string, conditions ...webdav.Condition) (release func(), err error) {
 	const noLock = "DAV:no-lock"
-	log.Println("confirm", name0, name1, conditions)
+	//log.Println("confirm", name0, name1, conditions)
 	l.RLock()
 	tok0, ok0 := l.locks[name0]
 	if _, ok1 := l.locks[name1]; ok1 {
@@ -48,14 +47,14 @@ func (l *locksystem) Confirm(now time.Time, name0, name1 string, conditions ...w
 			break
 		}
 	}
-	log.Println(ok)
+	//log.Println(ok)
 	if ok && !l.holds[name0] {
 		l.RUnlock()
 		l.Lock()
 		l.holds[name0] = true
 		release = func() {
 			delete(l.holds, name0)
-			log.Println(name0, "release")
+			//log.Println(name0, "release")
 		}
 		l.RWMutex.Unlock()
 	} else {
@@ -91,7 +90,7 @@ func (l *locksystem) ParentLocks(name string) (ret []string) {
 }
 
 func (l *locksystem) Create(now time.Time, details webdav.LockDetails) (token string, err error) {
-	log.Println("lock", details)
+	//log.Println("lock", details)
 	l.RLock()
 	if _, ok := l.locks[details.Root]; !ok && len(l.ParentLocks(details.Root)) == 0 {
 		l.RUnlock()
@@ -99,7 +98,7 @@ func (l *locksystem) Create(now time.Time, details webdav.LockDetails) (token st
 		token = <-l.idx + ":" + fmt.Sprint(now.UnixNano())
 		l.locks[details.Root] = token
 		l.tokens[token] = details
-		log.Println("locked", token)
+		//log.Println("locked", token)
 		l.RWMutex.Unlock()
 	} else {
 		l.RUnlock()
@@ -122,7 +121,7 @@ func (l *locksystem) Refresh(now time.Time, token string, duration time.Duration
 }
 
 func (l *locksystem) Unlock(now time.Time, token string) (err error) {
-	log.Println("unlock", token)
+	//log.Println("unlock", token)
 	l.Lock()
 	if details, ok := l.tokens[token]; ok {
 		if !l.holds[details.Root] {