<commit_before>/**
 * @file
 * @details realize file operation function in line address space
 *
 * @date 29.06.09
 * @author <NAME>
 *	- initial implementation
 * @author <NAME>
 *	- rework using vfs
 */

#include <string.h>
#include <errno.h>
#include <fs/ramfs.h>
#include <fs/fs_drv.h>
#include <fs/vfs.h>
#include <mem/misc/pool.h>
#include <util/array.h>
#include <embox/unit.h>


/* file description pool */
POOL_DEF (fdesc_pool, struct _ramfs_file_description,
		OPTION_GET(NUMBER,fdesc_quantity));

/* File operations */

static void *ramfs_fopen(struct file_desc *desc, const char *mode);
static int ramfs_fclose(struct file_desc *desc);
static size_t ramfs_fread(void *buf, size_t size, size_t count, void *file);
static size_t ramfs_fwrite(const void *buf, size_t size, size_t count,
		void *file);
static int ramfs_fseek(void *file, long offset, int whence);
static int ramfs_ioctl(void *file, int request, va_list args);

static file_operations_t ramfs_fop = { ramfs_fopen, ramfs_fclose, ramfs_fread,
		ramfs_fwrite, ramfs_fseek, ramfs_ioctl };

static void *ramfs_fopen(struct file_desc *desc, const char *mode) {
	node_t *nod;
	ramfs_file_description_t *fd;


	nod = desc->node;
	fd = (ramfs_file_description_t*) nod->fd;
	fd->cur_pointer = 0;
	fd->lock = 1;
	return desc;
}

static int ramfs_fclose(struct file_desc *desc) {
//	ramfs_file_description_t *fd;

//	node_t *nod = (node_t *) file;
//	fd = (ramfs_file_description_t*) nod->fd;
//	fd->lock = 0;
	return 0;
}

static size_t ramfs_fread(void *buf, size_t size, size_t count, void *file) {
	ramfs_file_description_t *fd;
	struct file_desc *desc;
	size_t size_to_read;

	size_to_read = size * count;
	desc = (struct file_desc *) file;
	fd = (ramfs_file_description_t*) desc->node->fd;

	if (fd == NULL) {
		return -ENOENT;
	}

	if (size * count >= (fd->size - desc->cursor)) {
		size_to_read = fd->size - desc->cursor;
	}

	memcpy((void*) buf, (const void *) (fd->start_addr + desc->cursor),
			size_to_read);
	desc->cursor += size_to_read;
	return size_to_read / size; /* number of item not characters */
}

static size_t ramfs_fwrite(const void *buf, size_t size, size_t count,
		void *file) {
	ramfs_file_description_t *fd;
	node_t *nod;
	size_t size_to_write = size * count;
	nod = (node_t *) file;
	fd = (ramfs_file_description_t*) nod->fd;

	if (fd == NULL) {
		return -ENOENT;
	}

	//FIXME: don't expand memory, need file ramfs_resize.
	if (size * count >= (fd->size - fd->cur_pointer)) {
		fd->size += size * count;
	}

	memcpy((void *) (fd->start_addr + fd->cur_pointer), buf, size_to_write);
	fd->cur_pointer += size_to_write;
	return size_to_write;
}

static int ramfs_fseek(void *file, long offset, int whence) {
	ramfs_file_description_t *fd;
	node_t *nod;
	int new_offset;
	nod = (node_t *) file;
	fd = (ramfs_file_description_t*) nod->fd;
<commit_msg>vmem: ramfs fixes<commit_after>/**
 * @file
 * @details realize file operation function in line address space
 *
 * @date 29.06.09
 * @author <NAME>
 *	- initial implementation
 * @author <NAME>
 *	- rework using vfs
 */

#include <string.h>
#include <errno.h>
#include <fs/ramfs.h>
#include <fs/fs_drv.h>
#include <fs/vfs.h>
#include <mem/misc/pool.h>
#include <util/array.h>
#include <embox/unit.h>


/* file description pool */
POOL_DEF (fdesc_pool, struct _ramfs_file_description,
		OPTION_GET(NUMBER,fdesc_quantity));

/* File operations */

static void *ramfs_fopen(struct file_desc *desc, const char *mode);
static int ramfs_fclose(struct file_desc *desc);
static size_t ramfs_fread(void *buf, size_t size, size_t count, void *file);
static size_t ramfs_fwrite(const void *buf, size_t size, size_t count,
		void *file);
static int ramfs_fseek(void *file, long offset, int whence);
static int ramfs_ioctl(void *file, int request, va_list args);

static file_operations_t ramfs_fop = { ramfs_fopen, ramfs_fclose, ramfs_fread,
		ramfs_fwrite, ramfs_fseek, ramfs_ioctl };

static void *ramfs_fopen(struct file_desc *desc, const char *mode) {
	node_t *nod;
	ramfs_file_description_t *fd;


	nod = desc->node;
	fd = (ramfs_file_description_t*) nod->fd;
	fd->cur_pointer = 0;
	fd->lock = 1;
	return desc;
}

static int ramfs_fclose(struct file_desc *desc) {
//	ramfs_file_description_t *fd;

//	node_t *nod = (node_t *) file;
//	fd = (ramfs_file_description_t*) nod->fd;
//	fd->lock = 0;
	return 0;
}

static size_t ramfs_fread(void *buf, size_t size, size_t count, void *file) {
	ramfs_file_description_t *fd;
	struct file_desc *desc;
	size_t size_to_read;

	size_to_read = size * count;
	desc = (struct file_desc *) file;
	fd = (ramfs_file_description_t*) desc->node->fd;

	if (fd == NULL) {
		return -ENOENT;
	}

	if (size * count >= (fd->size - fd->cur_pointer)) {
		size_to_read = fd->size - fd->cur_pointer;
	}

	memcpy((void*) buf, (const void *) (fd->start_addr + fd->cur_pointer),
			size_to_read);
	fd->cur_pointer += size_to_read;
	return size_to_read / size; /* number of item not characters */
}

static size_t ramfs_fwrite(const void *buf, size_t size, size_t count,
		void *file) {
	ramfs_file_description_t *fd;
	node_t *nod;
	size_t size_to_write = size * count;
	nod = (node_t *) file;
	fd = (ramfs_file_description_t*) nod->fd;

	if (fd == NULL) {
		return -ENOENT;
	}

	//FIXME: don't expand memory, need file ramfs_resize.
	if (size * count >= (fd->size - fd->cur_pointer)) {
		fd->size += size * count;
	}

	memcpy((void *) (fd->start_addr + fd->cur_pointer), buf, size_to_write);
	fd->cur_pointer += size_to_write;
	return size_to_write;
}

static int ramfs_fseek(void *file, long offset, int whence) {
	struct file_desc *desc;
	ramfs_file_description_t *fd;
	int new_offset;

	desc = (struct file_desc *) file;
	fd = (ramfs_file_description_t *)desc->node->fd;

{'fraction_non_alphanumeric': 0.1374912648497554, 'fraction_numerical': 0.0034940600978336828, 'mean_word_length': 4.520732883317262, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 74, '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': '2717595', 'n_tokens_mistral': 2276, 'n_tokens_neox': 2213, 'n_words': 562}