diff options
author | Kali Kaneko (leap communications) <kali@leap.se> | 2018-12-13 19:45:48 +0100 |
---|---|---|
committer | Kali Kaneko (leap communications) <kali@leap.se> | 2018-12-13 19:47:11 +0100 |
commit | 81843a0477c5eba4b3d4f6ff932f748f9e1244e7 (patch) | |
tree | 6068ae0ff1d8ac617e2781fb8aa68200e71403f2 /vendor/github.com/oschwald/maxminddb-golang/reader_other.go | |
parent | ac56a90e21118dc1ddfc1bd5e69d87f157710412 (diff) |
vendor packages
Diffstat (limited to 'vendor/github.com/oschwald/maxminddb-golang/reader_other.go')
-rw-r--r-- | vendor/github.com/oschwald/maxminddb-golang/reader_other.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/vendor/github.com/oschwald/maxminddb-golang/reader_other.go b/vendor/github.com/oschwald/maxminddb-golang/reader_other.go new file mode 100644 index 0000000..2a89fa6 --- /dev/null +++ b/vendor/github.com/oschwald/maxminddb-golang/reader_other.go @@ -0,0 +1,63 @@ +// +build !appengine + +package maxminddb + +import ( + "os" + "runtime" +) + +// 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 + runtime.SetFinalizer(reader, (*Reader).Close) + 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. +func (r *Reader) Close() error { + var err error + if r.hasMappedFile { + runtime.SetFinalizer(r, nil) + r.hasMappedFile = false + err = munmap(r.buffer) + } + r.buffer = nil + return err +} |