<commit_before>package dyconf

import (
	"os"
	"syscall"

	"github.com/facebookgo/stackerr"
)

func Init(fileName string) error {
	return defaultConfig.read_init(fileName)
}

func WriteInit(fileName string) error {
	return defaultConfig.write_init(fileName)
}

func Get(key string) ([]byte, error) {
	return defaultConfig.getBytes(key)
}

func Set(key string, value []byte) error {
	return defaultConfig.set(key, value)
}

func Close() error {
	return defaultConfig.close()
}

type config struct {
	fileName string
	file     *os.File
	block    []byte
}

var defaultConfig = &config{}

func (c *config) read_init(fileName string) error {
	c.fileName = fileName
	var err error
	c.file, err = os.Open(fileName)
	if err != nil {
		return stackerr.Newf("dyconf: failed to open the file [%s]. error: [%s]", fileName, err.Error())
	}
	// read lock the file
	if err = c.rlock(); err != nil {
		return err
	}
	defer c.unlock()

	// mmap
	c.block, err = syscall.Mmap(
		int(c.file.Fd()),
		0,
		int(defaultTotalSize),
		syscall.PROT_READ,
		syscall.MAP_SHARED,
	)
	if err != nil {
		return stackerr.Newf("dyconf: failed to mmap the config file [%s]. error: [%s]", fileName, err.Error())
	}

	return nil
}

func (c *config) write_init(fileName string) error {
	c.fileName = fileName
	var err error
	c.file, err = os.Create(fileName)
	if err != nil {
		return stackerr.Newf("dyconf: failed to open the file [%s]. error: [%s]", fileName, err.Error())
	}

	// write lock the file
	if err = c.wlock(); err != nil {
		return err
	}
	defer c.unlock()

	// mmap
	c.block, err = syscall.Mmap(
		int(c.file.Fd()),
		0,
		int(defaultTotalSize),
		syscall.PROT_WRITE,
		syscall.MAP_SHARED,
	)
	if err != nil {
		return stackerr.Newf("dyconf: failed to mmap the config file [%s]. error: [%s]", fileName, err.Error())
	}

	return nil
}
func (c *config) getBytes(key string) ([]byte, error) {
	// read lock the file
	if err := c.rlock(); err != nil {
		return nil, err
	}
	defer c.unlock()<commit_msg>Fixed the "unexpected fault address 0x..." error while calling mmap()<commit_after>package dyconf

import (
	"os"
	"syscall"

	"github.com/facebookgo/stackerr"
)

func Init(fileName string) error {
	return defaultConfig.read_init(fileName)
}

func WriteInit(fileName string) error {
	return defaultConfig.write_init(fileName)
}

func Get(key string) ([]byte, error) {
	return defaultConfig.getBytes(key)
}

func Set(key string, value []byte) error {
	return defaultConfig.set(key, value)
}

func Close() error {
	return defaultConfig.close()
}

type config struct {
	fileName string
	file     *os.File
	block    []byte
}

var defaultConfig = &config{}

func (c *config) read_init(fileName string) error {
	c.fileName = fileName
	var err error
	c.file, err = os.Open(fileName)
	if err != nil {
		return stackerr.Newf("dyconf: failed to open the file [%s]. error: [%s]", fileName, err.Error())
	}
	// read lock the file
	if err = c.rlock(); err != nil {
		return err
	}
	defer c.unlock()

	// mmap
	c.block, err = syscall.Mmap(
		int(c.file.Fd()),
		0,
		int(defaultTotalSize),
		syscall.PROT_READ,
		syscall.MAP_SHARED,
	)
	if err != nil {
		return stackerr.Newf("dyconf: failed to mmap the config file [%s]. error: [%s]", fileName, err.Error())
	}

	return nil
}

func (c *config) write_init(fileName string) error {
	c.fileName = fileName
	var err error
	c.file, err = os.Create(fileName)
	if err != nil {
		return stackerr.Newf("dyconf: failed to open the file [%s]. error: [%s]", fileName, err.Error())
	}

	// We now seek to the end of the file and write an empty byte. This is to bloat the file upto the
	// size we expect to mmap. If we don't do this mmap fails with the error "unexpected fault address"
	seekOffset, err := c.file.Seek(int64(defaultTotalSize), 0)
	if err != nil {
		return stackerr.Newf(
			"dyconf: failed to initialize for writing. Unexpected error occured while seeking to the "+
				"end [%#v] of the config file [%s]. error: [%s]",
			defaultTotalSize,
			fileName,
			err.Error(),
		)
	}
	if seekOffset != defaultTotalSize {
		return stackerr.Newf(
			"dyconf: failed to initialize for writing. Could not seek the file [%s] till the "+
				"required number of bytes [%#v]. Current seek offset: [%#v]",
			fileName,
			defaultTotalSize,
			seekOffset,
		)
	}
	_, err = c.file.Write([]byte("x"))
	if err != nil {
		return stackerr.Newf(
			"dyconf: failed to initialize for writing. Could not write the empty byte at the "+
				"end of the file [%s]. error: [%s]",
			fileName,
			err.Error(),
		)
	}

	// write lock the file
	if err = c.wlock(); err != nil {
		return err
	}
	defer c.unlock()

	// mmap
	c.block, err = syscall.Mmap(
		int(c.file.Fd()),
		0,
		int(defaultTotalSize),
		syscall.PROT_WRITE,
		syscall.MAP_SHARED,
	)
	if err != nil {
		return stackerr.Newf("dyconf: failed to mmap the config file [%s]. error: [%s]", fileName, err.Error())
	}

	return nil
}
func (c *config) getBytes(key string) ([]byte, error) {
	// read lock the file
	if err := c.rlock(); err != nil {
		return nil, err
	}
	defer c.unlock()
{'fraction_non_alphanumeric': 0.14163517008155957, 'fraction_numerical': 0.0015914064054107818, 'mean_word_length': 3.558476881233001, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '697569', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1828, 'n_words': 563}