more
This commit is contained in:
2
Makefile
2
Makefile
@ -19,7 +19,7 @@ $(EXE):
|
||||
$(GO) build -v -o $(EXE)
|
||||
|
||||
test:
|
||||
$(GO) test -v
|
||||
$(GO) test ./...
|
||||
|
||||
clean:
|
||||
$(GO) clean -v
|
||||
|
@ -90,6 +90,10 @@ func (key_certificate KeyCertificate) Data() ([]byte, error) {
|
||||
func (key_certificate KeyCertificate) SigningPublicKeyType() (signing_pubkey_type int, err error) {
|
||||
data, err := key_certificate.Data()
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) SigningPublicKeyType",
|
||||
"reason": err.Error(),
|
||||
}).Error("error getting signing public key")
|
||||
return
|
||||
}
|
||||
data_len := len(data)
|
||||
@ -231,6 +235,11 @@ func (key_certificate KeyCertificate) SignatureSize() (size int) {
|
||||
}
|
||||
key_type, err := key_certificate.SigningPublicKeyType()
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) SignatureSize",
|
||||
"key_type": key_type,
|
||||
"reason": "failed to read signing public key type",
|
||||
}).Error("error getting signature size")
|
||||
return 0
|
||||
}
|
||||
return sizes[int(key_type)]
|
||||
|
@ -30,6 +30,9 @@ type NetworkDatabase interface {
|
||||
// return how many router infos we have
|
||||
Size() int
|
||||
|
||||
// Recaculate size of netdb from backend
|
||||
RecalculateSize() error
|
||||
|
||||
// ensure underlying resources exist , i.e. directories, files, configs
|
||||
Ensure() error
|
||||
}
|
||||
|
@ -7,9 +7,13 @@ import (
|
||||
"github.com/hkparker/go-i2p/lib/bootstrap"
|
||||
"github.com/hkparker/go-i2p/lib/common"
|
||||
"github.com/hkparker/go-i2p/lib/common/base64"
|
||||
"github.com/hkparker/go-i2p/lib/util"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// standard network database implementation using local filesystem skiplist
|
||||
@ -45,6 +49,60 @@ func (db StdNetDB) Path() string {
|
||||
// return how many routers we know about in our network database
|
||||
//
|
||||
func (db StdNetDB) Size() (routers int) {
|
||||
// TODO: implement this
|
||||
var err error
|
||||
var data []byte
|
||||
if !util.CheckFileExists(db.cacheFilePath()) {
|
||||
// regenerate
|
||||
err = db.RecalculateSize()
|
||||
if err != nil {
|
||||
// TODO : what now? let's panic for now
|
||||
util.Panicf("could not recalculate netdb size: %s", err)
|
||||
}
|
||||
}
|
||||
data, err = ioutil.ReadFile(db.cacheFilePath())
|
||||
if err == nil {
|
||||
routers, err = strconv.Atoi(string(data))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// name of file to hold precomputed size of netdb
|
||||
const CacheFileName = "sizecache.txt"
|
||||
|
||||
// get filepath for storing netdb info cache
|
||||
func (db StdNetDB) cacheFilePath() string {
|
||||
return filepath.Join(db.Path(), CacheFileName)
|
||||
}
|
||||
|
||||
func (db StdNetDB) CheckFilePathValid(fpath string) bool {
|
||||
// TODO: make this better
|
||||
return strings.HasSuffix(fpath, ".dat")
|
||||
}
|
||||
|
||||
// recalculateSize recalculates cached size of netdb
|
||||
func (db StdNetDB) RecalculateSize() (err error) {
|
||||
fpath := db.cacheFilePath()
|
||||
count := 0
|
||||
err = filepath.Walk(fpath, func(fname string, info os.FileInfo, err error) error {
|
||||
if info.IsDir() {
|
||||
return err
|
||||
}
|
||||
if db.CheckFilePathValid(fname) {
|
||||
// TODO: make sure it's in a skiplist directory
|
||||
count++
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err == nil {
|
||||
str := fmt.Sprintf("%d", count)
|
||||
var f *os.File
|
||||
f, err = os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err == nil {
|
||||
_, err = io.WriteString(f, str)
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
12
lib/util/checkfile.go
Normal file
12
lib/util/checkfile.go
Normal file
@ -0,0 +1,12 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// Check if a file exists and is readable etc
|
||||
// returns false if not
|
||||
func CheckFileExists(fpath string) bool {
|
||||
_, e := os.Stat(fpath)
|
||||
return e == nil
|
||||
}
|
11
lib/util/panicf.go
Normal file
11
lib/util/panicf.go
Normal file
@ -0,0 +1,11 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Panicf allows passing formated string to panic()
|
||||
func Panicf(format string, args ...interface{}) {
|
||||
s := fmt.Sprintf(format, args...)
|
||||
panic(s)
|
||||
}
|
Reference in New Issue
Block a user