BarryServer : Git

All the code for all my projects
// BarryServer : Git / Orion / blob / 1628fcfdfdf2978ed9ccac96ee7d13bb3dc43a01 / vfs / ext2fs / file.c

// Related

Orion

Barry Keyboard/Mouse drivers + POSIX names for structs 1628fcf (2 years, 4 months ago)
/*
 * This file controls access to Ext2FS Files.  It contains the functions called
 * by the VFS for any operation on a Ext2FS File struct.
 */

#include <stddef.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include "fs.h"
#include "../vfs.h"

size_t ext2fs_read(File *file, char *buf, size_t size, off_t offset);
int ext2fs_readdir(File *file, DirEnt *dent, off_t index);
int ext2fs_open(File *file);

FileOps ext2fsFileOps = {
	.read = ext2fs_read,
	.readdir = ext2fs_readdir,
	.open = ext2fs_open,
};

/* Read a file */
size_t
ext2fs_read(File *file, char *buf, size_t size, off_t offset)
{
	Ext2Inode inode;
	uint16_t min;
	size_t count = 0, i = offset / 4096;
	uint32_t blk;
	char ebuf[4096];
	ext2_read_inode(file->inode->super, file->inode->ino, &inode);
	if (offset > inode.lsize)
		return 0;
	if (size + offset > inode.lsize)
		size = inode.lsize - offset;
	while (size) {
		min = (size > 0x1000) ? 0x1000 : size;
		blk = ext2_get_data_addr(file->inode->super, &inode, i);
		ext2_read_block(file->inode->super, blk, ebuf);
		memcpy(buf + count, ebuf + (offset % 4096), min);
		size -= min;
		count += min;
		i++;
	}
	if (count >= 0x1000)
	return count;
}

/* Read a directory entry */
int
ext2fs_readdir(File *file, DirEnt *dent, off_t index)
{
	char buf[4096];
	uint32_t block, blk = 0;
	Ext2DirEntry *de;
	Ext2Inode inode;
	ext2_read_inode(file->inode->super, file->inode->ino, &inode);
	for (blk = 0; blk < 0xFFFF; blk++) {
		block = ext2_get_data_addr(file->inode->super, &inode, blk);
		if (!block)
			return -ENOENT;
		ext2_read_block(file->inode->super, block, buf);
		for (de = (Ext2DirEntry *) buf;
		     index && de < (Ext2DirEntry *) (buf + 4096);
		     de = (void *) ((char *) de + de->size), index--);
		if (de >= (Ext2DirEntry *) (buf + 4096))
			return -ENOENT;
		if (!index)
			break;
	}
	if (!de->ino)
		return -ENOENT;
	dent->d_ino = de->ino;
	dent->d_type = de->type;
	dent->d_namelen = de->nameLen + 1;
	strncpy(dent->d_name, de->name, de->size);
	return 0;
}

/* Open a file */
int
ext2fs_open(File *file)
{
	return 0;
}