2016-07-04 09:12:59 +00:00
|
|
|
// +build !appengine
|
|
|
|
|
|
|
|
package maxminddb
|
|
|
|
|
2017-04-05 14:34:41 +00:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
)
|
2016-07-04 09:12:59 +00:00
|
|
|
|
|
|
|
// Open takes a string path to a MaxMind DB file and returns a Reader
|
|
|
|
// structure or an error. The database file is opened using a memory map,
|
|
|
|
// except on Google App Engine where mmap is not supported; there the database
|
|
|
|
// is loaded into memory. Use the Close method on the Reader object to return
|
|
|
|
// the resources to the system.
|
|
|
|
func Open(file string) (*Reader, error) {
|
|
|
|
mapFile, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if rerr := mapFile.Close(); rerr != nil {
|
|
|
|
err = rerr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
stats, err := mapFile.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileSize := int(stats.Size())
|
|
|
|
mmap, err := mmap(int(mapFile.Fd()), fileSize)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := FromBytes(mmap)
|
|
|
|
if err != nil {
|
|
|
|
if err2 := munmap(mmap); err2 != nil {
|
|
|
|
// failing to unmap the file is probably the more severe error
|
|
|
|
return nil, err2
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.hasMappedFile = true
|
2017-04-05 14:34:41 +00:00
|
|
|
runtime.SetFinalizer(reader, (*Reader).Close)
|
2016-07-04 09:12:59 +00:00
|
|
|
return reader, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close unmaps the database file from virtual memory and returns the
|
|
|
|
// resources to the system. If called on a Reader opened using FromBytes
|
|
|
|
// or Open on Google App Engine, this method does nothing.
|
2017-04-05 14:34:41 +00:00
|
|
|
func (r *Reader) Close() error {
|
|
|
|
if !r.hasMappedFile {
|
|
|
|
return nil
|
2016-07-04 09:12:59 +00:00
|
|
|
}
|
2017-04-05 14:34:41 +00:00
|
|
|
runtime.SetFinalizer(r, nil)
|
|
|
|
r.hasMappedFile = false
|
|
|
|
return munmap(r.buffer)
|
2016-07-04 09:12:59 +00:00
|
|
|
}
|