// 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)
}
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) {
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
}
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
}