From f032504f7e11022abe29832d14fecc7cd4422d66 Mon Sep 17 00:00:00 2001 From: Ibrahim Jarif Date: Tue, 10 Nov 2020 20:59:23 +0530 Subject: [PATCH] fix(MmapFile): Don't error out if fileSize > sz --- z/file.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/z/file.go b/z/file.go index 914d670b..9aa5e033 100644 --- a/z/file.go +++ b/z/file.go @@ -35,7 +35,7 @@ type MmapFile struct { var NewFile = errors.New("Create a new file") -func OpenMmapFileUsing(fd *os.File, maxSz int, writable bool) (*MmapFile, error) { +func OpenMmapFileUsing(fd *os.File, sz int, writable bool) (*MmapFile, error) { filename := fd.Name() fi, err := fd.Stat() if err != nil { @@ -44,20 +44,13 @@ func OpenMmapFileUsing(fd *os.File, maxSz int, writable bool) (*MmapFile, error) var rerr error fileSize := fi.Size() - if maxSz > 0 { - // We have a legit maxSz provided. - if fileSize == 0 { - // If file is empty, truncate it to maxSz. - if err := fd.Truncate(int64(maxSz)); err != nil { - return nil, errors.Wrapf(err, "error while truncation") - } - fileSize = int64(maxSz) - rerr = NewFile - - } else if fileSize > int64(maxSz) { - return nil, errors.Errorf("file size %d greater than max size %d", - fileSize, maxSz) + if sz > 0 && fileSize == 0 { + // If file is empty, truncate it to sz. + if err := fd.Truncate(int64(sz)); err != nil { + return nil, errors.Wrapf(err, "error while truncation") } + fileSize = int64(sz) + rerr = NewFile } // fmt.Printf("Mmaping file: %s with writable: %v filesize: %d\n", fd.Name(), writable, fileSize)