kunt

golang IRC bot
git clone git://git.2f30.org/kunt
Log | Files | Refs | LICENSE

commit 2549ca60ec8b9555fac1ad506b6aa7fda6feead8
parent 05ceca0d8f8f5a782603ae9737f160c39ec9f15b
Author: sin <sin@2f30.org>
Date:   Mon, 29 Apr 2013 12:26:35 +0100

mapfs: Factor out I/O into io.go

Diffstat:
Asrc/mapfs/io.go | 30++++++++++++++++++++++++++++++
Msrc/mapfs/mapfs.go | 24------------------------
2 files changed, 30 insertions(+), 24 deletions(-)

diff --git a/src/mapfs/io.go b/src/mapfs/io.go @@ -0,0 +1,30 @@ +package mapfs + +import ( + "fmt" + "io/ioutil" + "os" +) + +func (m *Mapfs) fsRead(key int) ([]byte, error) { + path := fmt.Sprintf("%s/%s%d.txt", m.path, m.prefix, key) + b, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + return b, err +} + +func (m *Mapfs) fsWrite(key int, buf []byte) error { + path := fmt.Sprintf("%s/%s%d.txt", m.path, m.prefix, key) + _, err := os.Stat(path) + if err == nil { + return fmt.Errorf("Entry %s already exists in %s db", + path, m.name) + } + err = ioutil.WriteFile(path, buf, 0644) + if err != nil { + return err + } + return nil +} diff --git a/src/mapfs/mapfs.go b/src/mapfs/mapfs.go @@ -3,7 +3,6 @@ package mapfs import ( "fmt" - "io/ioutil" "math/rand" "os" "path" @@ -36,29 +35,6 @@ func NewMapfs(name string, path string, prefix string) *Mapfs { } } -func (m *Mapfs) fsRead(key int) ([]byte, error) { - path := fmt.Sprintf("%s/%s%d.txt", m.path, m.prefix, key) - b, err := ioutil.ReadFile(path) - if err != nil { - return nil, err - } - return b, err -} - -func (m *Mapfs) fsWrite(key int, buf []byte) error { - path := fmt.Sprintf("%s/%s%d.txt", m.path, m.prefix, key) - _, err := os.Stat(path) - if err == nil { - return fmt.Errorf("Entry %s already exists in %s db", - path, m.name) - } - err = ioutil.WriteFile(path, buf, 0644) - if err != nil { - return err - } - return nil -} - // Sync dirty entries to disk func (m *Mapfs) Sync() error { m.lock.Lock()