From 1f81f57b9ca9534a41e45f08f577f98bfbca5664 Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Thu, 8 Feb 2018 23:57:35 +1100
Subject: [PATCH] There was no error for missing file, and no way to close the
 mmap returned from iplist.MmapPacked

The function is also renamed due to the changed behaviour.
---
 iplist/packed.go | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/iplist/packed.go b/iplist/packed.go
index 427e5572..143bca03 100644
--- a/iplist/packed.go
+++ b/iplist/packed.go
@@ -114,12 +114,20 @@ func (pil PackedIPList) Lookup(ip net.IP) (r Range, ok bool) {
 	return lookup(pil.getFirst, pil.getRange, pil.len(), ip4)
 }
 
-func MMapPacked(filename string) (ret Ranger, err error) {
+type closerFunc func() error
+
+func (me closerFunc) Close() error {
+	return me()
+}
+
+func MMapPackedFile(filename string) (
+	ret interface {
+		Ranger
+		io.Closer
+	},
+	err error,
+) {
 	f, err := os.Open(filename)
-	if os.IsNotExist(err) {
-		err = nil
-		return
-	}
 	if err != nil {
 		return
 	}
@@ -128,7 +136,9 @@ func MMapPacked(filename string) (ret Ranger, err error) {
 	if err != nil {
 		return
 	}
-	// TODO: Need a destructor that unmaps this.
-	ret = NewFromPacked(mm)
+	ret = struct {
+		Ranger
+		io.Closer
+	}{NewFromPacked(mm), closerFunc(mm.Unmap)}
 	return
 }
-- 
2.51.0