Fix windows mmap on zero length file.
parent
48797873ee
commit
0fcab02074
|
@ -8,26 +8,29 @@ import (
|
||||||
|
|
||||||
// Map memory-maps a file.
|
// Map memory-maps a file.
|
||||||
func Map(path string, sz int64) ([]byte, error) {
|
func Map(path string, sz int64) ([]byte, error) {
|
||||||
|
fi, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Truncate file to size if too small.
|
||||||
|
if fi.Size() < sz {
|
||||||
|
if err := os.Truncate(path, sz); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sz = fi.Size()
|
||||||
|
}
|
||||||
|
if sz == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
fi, err := f.Stat()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use file size if map size is not passed in.
|
|
||||||
// TODO(edd): test.
|
|
||||||
// if sz == 0 {
|
|
||||||
// }
|
|
||||||
sz = fi.Size()
|
|
||||||
if fi.Size() == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
lo, hi := uint32(sz), uint32(sz>>32)
|
lo, hi := uint32(sz), uint32(sz>>32)
|
||||||
fmap, err := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, hi, lo, nil)
|
fmap, err := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, hi, lo, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue