Files
go-i2p/lib/netdb/std.go

117 lines
2.6 KiB
Go
Raw Normal View History

2016-01-28 10:16:26 -05:00
package netdb
import (
2016-08-17 09:19:56 -04:00
"bytes"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/hkparker/go-i2p/lib/bootstrap"
"github.com/hkparker/go-i2p/lib/common"
"github.com/hkparker/go-i2p/lib/common/base64"
2016-01-29 07:22:31 -05:00
"io"
"os"
2016-08-17 09:19:56 -04:00
"path/filepath"
2016-01-28 10:16:26 -05:00
)
2016-08-17 09:19:56 -04:00
// standard network database implementation using local filesystem skiplist
2016-01-28 10:16:26 -05:00
type StdNetDB string
2016-08-17 09:19:56 -04:00
func (db StdNetDB) GetRouterInfo(hash common.Hash) (chnl chan common.RouterInfo) {
fname := db.SkiplistFile(hash)
f, err := os.Open(fname)
if err != nil {
return nil
}
buff := new(bytes.Buffer)
_, err = io.Copy(buff, f)
f.Close()
chnl = make(chan common.RouterInfo)
chnl <- common.RouterInfo(buff.Bytes())
return
}
// get the skiplist file that a RouterInfo with this hash would go in
func (db StdNetDB) SkiplistFile(hash common.Hash) (fpath string) {
fname := base64.EncodeToString(hash[:])
2016-08-17 09:28:45 -04:00
fpath = filepath.Join(db.Path(), fmt.Sprintf("r%c", fname[0]), fmt.Sprintf("routerInfo-%s.dat", fname))
2016-08-17 09:19:56 -04:00
return
}
2016-01-28 10:16:26 -05:00
// get netdb path
func (db StdNetDB) Path() string {
2016-01-29 07:22:31 -05:00
return string(db)
2016-01-28 10:16:26 -05:00
}
//
// return how many routers we know about in our network database
//
2016-08-17 09:19:56 -04:00
func (db StdNetDB) Size() (routers int) {
2016-01-29 07:22:31 -05:00
return
2016-01-28 10:16:26 -05:00
}
// return true if the network db directory exists and is writable
func (db StdNetDB) Exists() bool {
2016-08-17 09:19:56 -04:00
p := db.Path()
// check root directory
_, err := os.Stat(p)
if err == nil {
// check subdirectories for skiplist
for _, c := range base64.Alphabet {
2016-08-17 09:28:45 -04:00
if _, err = os.Stat(filepath.Join(p, fmt.Sprintf("r%c", c))); err != nil {
2016-08-17 09:19:56 -04:00
return false
}
}
}
return err == nil
2016-01-28 10:16:26 -05:00
}
2016-01-28 10:46:09 -05:00
func (db StdNetDB) SaveEntry(e *Entry) (err error) {
2016-01-29 07:22:31 -05:00
var f io.WriteCloser
2016-08-17 09:19:56 -04:00
var h common.Hash
h, err = e.ri.IdentHash()
2016-01-29 07:22:31 -05:00
if err == nil {
2016-08-17 09:19:56 -04:00
f, err = os.OpenFile(db.SkiplistFile(h), os.O_WRONLY|os.O_CREATE, 0700)
if err == nil {
err = e.WriteTo(f)
f.Close()
2016-01-29 07:22:31 -05:00
}
2016-08-17 09:19:56 -04:00
}
if err != nil {
2016-01-29 07:22:31 -05:00
log.Errorf("failed to save netdb entry: %s", err.Error())
}
return
2016-01-28 10:46:09 -05:00
}
// reseed if we have less than minRouters known routers
// returns error if reseed failed
2016-08-17 09:19:56 -04:00
func (db StdNetDB) Reseed(b bootstrap.Bootstrap, minRouters int) (err error) {
2016-01-29 07:22:31 -05:00
return
2016-01-28 10:46:09 -05:00
}
2016-08-17 09:19:56 -04:00
// ensure that the network database exists
func (db StdNetDB) Ensure() (err error) {
2016-01-29 07:22:31 -05:00
if !db.Exists() {
err = db.Create()
}
return
2016-01-28 10:46:09 -05:00
}
2016-01-28 10:16:26 -05:00
// create base network database directory
func (db StdNetDB) Create() (err error) {
2016-08-17 09:28:45 -04:00
mode := os.FileMode(0700)
2016-08-17 09:19:56 -04:00
p := db.Path()
log.Infof("Create network database in %s", p)
// create root for skiplist
err = os.Mkdir(p, mode)
if err == nil {
// create all subdirectories for skiplist
for _, c := range base64.Alphabet {
2016-08-17 09:28:45 -04:00
err = os.Mkdir(filepath.Join(p, fmt.Sprintf("r%c", c)), mode)
2016-08-17 09:19:56 -04:00
if err != nil {
return
}
}
}
2016-01-29 07:22:31 -05:00
return
2016-01-28 10:16:26 -05:00
}