<commit_before>#include "FileSystem.h"
#include "Log.h"

#ifndef PLATFORM_WINDOWS
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <alloca.h>

FD FS_Open(const char* filename, int flags)
{
	int		mode;
	int		oflags;
	int		ret;
	
	mode = S_IRUSR | S_IWUSR;
	oflags = 0;
	if ((flags & FS_CREATE) == FS_CREATE)
		oflags |= O_CREAT;
	if ((flags & FS_READWRITE) == FS_READWRITE)
		oflags |= O_RDWR;

	ret = open(filename, oflags, mode);
	if (ret < 0)
	{
		Log_Errno();
		return INVALID_FD;
	}
	return ret;
}

void FS_FileClose(FD fd)
{
	int	ret;
	
	ret = close(fd);
	if (ret < 0)
		Log_Errno();
}

int64_t FS_FileSeek(FD fd, uint64_t offset, int whence_)
{
	off_t	ret;
	int		whence;
	
	whence = -1;
	switch (whence_)
	{
	case FS_SEEK_SET:
		whence = SEEK_SET;
		break;
	case FS_SEEK_CUR:
		whence = SEEK_CUR;
		break;
	case FS_SEEK_END:
		whence = SEEK_END;
		break;
	default:
		return -1;
	}
	
	ret = lseek(fd, offset, whence);
	if (ret < 0)
		Log_Errno();
	return ret;
}

int FS_FileTruncate(FD fd, uint64_t length)
{
	int	ret;
	
	ret = ftruncate(fd, length);
	if (ret < 0)
		Log_Errno();
	return ret;
}

int64_t FS_FileSize(FD fd)
{
	int64_t		ret;
	struct stat	buf;
	
	ret = fstat(fd, &buf);
	if (ret < 0)
	{
		Log_Errno();
		return ret;
	}
	
	return buf.st_size;
}

ssize_t	FS_FileWrite(FD fd, const void* buf, size_t count)
{
	ssize_t	ret;
	
	ret = write(fd, buf, count);
	if (ret < 0)
		Log_Errno();
	return ret;
}

ssize_t	FS_FileWriteVector(FD fd, unsigned num, const void** buf, size_t *count)
{
	ssize_t			ret;
	struct iovec*	vecbuf;
	unsigned		i;
	
	vecbuf = (struct iovec*) alloca(sizeof(struct iovec) * num);
	for (i = 0; i < num; i++)
	{
		vecbuf[i].iov_base = (void*) buf[i];
		vecbuf[i].iov_len = count[i];
	}
	
	ret = writev(fd, vecbuf, num);
	if (ret < 0)
		Log_Errno();
	return ret;	
}

ssize_t FS_FileRead(FD fd, void* buf, size_t count)
{
	ssize_t	ret;
	
	ret = read(fd, buf, count);
	if (ret < 0)
		Log_Errno();<commit_msg>Changed alloca to standard stack allocation.<commit_after>#include "FileSystem.h"
#include "Log.h"

#ifndef PLATFORM_WINDOWS
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <alloca.h>

FD FS_Open(const char* filename, int flags)
{
	int		mode;
	int		oflags;
	int		ret;
	
	mode = S_IRUSR | S_IWUSR;
	oflags = 0;
	if ((flags & FS_CREATE) == FS_CREATE)
		oflags |= O_CREAT;
	if ((flags & FS_READWRITE) == FS_READWRITE)
		oflags |= O_RDWR;

	ret = open(filename, oflags, mode);
	if (ret < 0)
	{
		Log_Errno();
		return INVALID_FD;
	}
	return ret;
}

void FS_FileClose(FD fd)
{
	int	ret;
	
	ret = close(fd);
	if (ret < 0)
		Log_Errno();
}

int64_t FS_FileSeek(FD fd, uint64_t offset, int whence_)
{
	off_t	ret;
	int		whence;
	
	whence = -1;
	switch (whence_)
	{
	case FS_SEEK_SET:
		whence = SEEK_SET;
		break;
	case FS_SEEK_CUR:
		whence = SEEK_CUR;
		break;
	case FS_SEEK_END:
		whence = SEEK_END;
		break;
	default:
		return -1;
	}
	
	ret = lseek(fd, offset, whence);
	if (ret < 0)
		Log_Errno();
	return ret;
}

int FS_FileTruncate(FD fd, uint64_t length)
{
	int	ret;
	
	ret = ftruncate(fd, length);
	if (ret < 0)
		Log_Errno();
	return ret;
}

int64_t FS_FileSize(FD fd)
{
	int64_t		ret;
	struct stat	buf;
	
	ret = fstat(fd, &buf);
	if (ret < 0)
	{
		Log_Errno();
		return ret;
	}
	
	return buf.st_size;
}

ssize_t	FS_FileWrite(FD fd, const void* buf, size_t count)
{
	ssize_t	ret;
	
	ret = write(fd, buf, count);
	if (ret < 0)
		Log_Errno();
	return ret;
}

ssize_t	FS_FileWriteVector(FD fd, unsigned num, const void** buf, size_t *count)
{
	ssize_t			ret;
	struct iovec	vecbuf[num];
	unsigned		i;
	
	for (i = 0; i < num; i++)
	{
		vecbuf[i].iov_base = (void*) buf[i];
		vecbuf[i].iov_len = count[i];
	}
	
	ret = writev(fd, vecbuf, num);
	if (ret < 0)
		Log_Errno();
	return ret;	
}

ssize_t FS_FileRead(FD fd, void* buf, size_t count)
{
	ssize_t	ret;
	
	ret = read(fd, buf, count);
	if (ret < 0)
		Log_Errno();
{'fraction_non_alphanumeric': 0.14591009579955785, 'fraction_numerical': 0.010808155244411692, 'mean_word_length': 3.5548098434004474, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 19, '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': '2196006', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1875, 'n_words': 464}