X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=store%2Fstore.go;h=2a6d2f8eecc466d714997faee95214df23fecf38;hb=2dba8527a9eff1f9931ccbb25df34dcb618460a7;hp=2595f88213f479a80873d63fa3c5957f74826d33;hpb=c89b19c863fc41c0312358a866aebd425f498c76;p=epoint diff --git a/store/store.go b/store/store.go index 2595f88..2a6d2f8 100644 --- a/store/store.go +++ b/store/store.go @@ -8,16 +8,23 @@ package store // TODO: this is a toy implementation import ( + "io/ioutil" "os" "path/filepath" - "io/ioutil" ) - type Conn struct { path string } +type NotFoundError struct { + path string +} + +func (e NotFoundError) Error() string { + return "not found: " + e.path +} + func Open(root string) (c *Conn, err error) { c = new(Conn) c.path, err = filepath.Abs(root) @@ -32,7 +39,13 @@ func Open(root string) (c *Conn, err error) { } func (c *Conn) Get(name, k string) (v []byte, err error) { - return ioutil.ReadFile(filepath.Join(c.path, name, k)) + v, err = ioutil.ReadFile(filepath.Join(c.path, name, k)) + if err != nil { + if p, ok := err.(*os.PathError); ok && p.Err == os.ENOENT { + err = NotFoundError{name+"/"+k} + } + } + return } func (c *Conn) Ensure(name string) (err error) { @@ -41,8 +54,7 @@ func (c *Conn) Ensure(name string) (err error) { func (c *Conn) Set(name, k string, v []byte) (err error) { fn := filepath.Join(c.path, name, k) - // os.O_SYNC - f, err := os.Create(fn+".tmp") + f, err := os.OpenFile(fn+".tmp", os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_SYNC, 0666) if err != nil { return } @@ -55,6 +67,17 @@ func (c *Conn) Set(name, k string, v []byte) (err error) { return } +func (c *Conn) Append(name, k string, v []byte) (err error) { + fn := filepath.Join(c.path, name, k) + f, err := os.OpenFile(fn, os.O_CREATE|os.O_APPEND|os.O_WRONLY|os.O_SYNC, 0666) + if err != nil { + return + } + defer f.Close() + _, err = f.Write(v) + return +} + func (c *Conn) Close() (err error) { return }