Fix ReadWithSizeAndUnlink on systems that have syscall based mmap

This commit is contained in:
Kovid Goyal 2023-02-28 13:44:09 +05:30
parent 4d3ce47813
commit ce12fd3515
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 13 additions and 9 deletions

View File

@ -118,6 +118,7 @@ func read_till_buf_full(f *os.File, buf []byte) ([]byte, error) {
if len(p) == 0 && errors.Is(err, io.EOF) {
err = nil
}
err = fmt.Errorf("Failed to read from SHM file with error: %w", err)
return buf[:len(buf)-len(p)], err
}
}

View File

@ -4,6 +4,7 @@
package shm
import (
"encoding/binary"
"errors"
"fmt"
"io/fs"
@ -182,17 +183,19 @@ func Open(name string, size uint64) (MMap, error) {
}
func ReadWithSizeAndUnlink(name string, file_callback ...func(*os.File) error) ([]byte, error) {
f, err := shm_open(name, os.O_RDONLY, 0)
mmap, err := Open(name, 4)
if err != nil {
return nil, err
}
defer f.Close()
defer shm_unlink(f.Name())
for _, cb := range file_callback {
err = cb(f)
if err != nil {
return nil, err
}
size := uint64(binary.BigEndian.Uint32(mmap.Slice()))
mmap.Close()
mmap, err = Open(name, 4+size)
if err != nil {
return nil, err
}
return read_with_size(f)
ans := make([]byte, size)
copy(ans, mmap.Slice()[4:])
mmap.Close()
mmap.Unlink()
return ans, nil
}