func openFile(ctx context.Context, path string) (f file, err error) {
	fh, err := os.OpenFile(path, os.O_RDWR, 0)
	if err != nil {
		return f, Error.Wrap(err)
	}
	defer fh.Close()
	fd := int(fh.Fd())
	fi, err := fh.Stat()
	if err != nil {
		return f, Error.Wrap(err)
	}
	len := int(fi.Size())
	if len < recordHeaderSize {
		return f, Error.New("file is too small to contain metadata")
	}
	data, err := system.Mmap(fd, len,
		system.PROT_READ|system.PROT_WRITE,
		system.MAP_SHARED)
	if err != nil {
		return f, Error.Wrap(err)
	}
	meta, err := readMetadata(slice(data, len))
	if err != nil {
		return f, err
	}
	if meta.Size_ < recordHeaderSize {
		return f, Error.New("possible corruption: invalid size")
	}
	return file{
		data: data,
		len:  len,
		cap:  len/meta.Size_ - 1,
		size: meta.Size_,
		ref:  newRef(path),
	}, nil
}
openFile returns a file for the given path.
package files
import (
	"context"
	"os"
	"github.com/vivint/rothko/database/files/internal/meta"
	"github.com/vivint/rothko/database/files/internal/system"
)
type file struct {
	data uintptr  
	len  int      
	cap  int      
	size int      
	ref  ref      
}
func createFile(ctx context.Context, path string, size, cap int) (
	f file, err error) {
	fh, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		return file{}, Error.Wrap(err)
	}
	defer fh.Close()
	fd := int(fh.Fd())
	if cap+1 < cap ||
		int(int32(cap)) != cap ||
		int(int32(size)) != size ||
		int32(size)*int32(cap)/int32(cap) != int32(size) {
		return file{}, Error.New("capacity too large")
	}
	len := size * (cap + 1)
	if err := system.Allocate(fd, int64(len)); err != nil {
		return file{}, err
	}
	data, err := system.Mmap(fd, len,
		system.PROT_READ|system.PROT_WRITE,
		system.MAP_SHARED)
	if err != nil {
		return file{}, Error.Wrap(err)
	}
	err = writeMetadata(slice(data, len)[:size], meta.Metadata{
		Size_: size,
		Head:  cap - 1,
	})
	if err != nil {
		return file{}, err
	}
	return file{
		data: data,
		len:  len,
		cap:  cap,
		size: size,
		ref:  newRef(path),
	}, nil
}