Merge branch 'ohligs' into readwrite

Conflicts:
	newlib/examples/test
This commit is contained in:
Marian Ohligs 2011-05-16 10:27:43 +02:00
commit d1e0e2304d
9 changed files with 95 additions and 94 deletions

View file

@ -29,7 +29,7 @@
static ssize_t stdin_read(vfs_node_t* node, uint8_t* buffer, size_t size, off_t offset)
{
while(size) {
size = kputs((char*)buffer);
size -= kputs((char*)buffer);
}
return size;
}

View file

@ -66,7 +66,6 @@ static ssize_t initrd_read(vfs_node_t* node, uint8_t* buffer, size_t size, off_t
pos = offset / node->block_size;
offset = offset % node->block_size;
}
do {
for(i=0; i<MAX_DATABLOCKS && !data; i++) {
if (blist->data[i]) {
@ -98,85 +97,55 @@ static ssize_t initrd_read(vfs_node_t* node, uint8_t* buffer, size_t size, off_t
static ssize_t initrd_write(vfs_node_t* node, uint8_t* buffer, size_t size, off_t offset)
{
uint32_t i, writtenbytes = 0, writebytes = 0;
char* data = NULL;
block_list_t* blist = &node->block_list;
do {
data = (char*) blist->data[0];
if ((size - writtenbytes) >= MAX_DATABLOCKS)
writebytes = MAX_DATABLOCKS;
else
writebytes = size - writtenbytes;
memcpy(data, buffer, writebytes);
writtenbytes += writebytes;
//kprintf("geschrieben: %i", writtenbytes);
if (!blist->next) {
blist->next = (block_list_t*) kmalloc(sizeof(block_list_t));
if (blist->next) {
memset(blist->next, 0x00, sizeof(block_list_t));
}
}
blist = blist->next;
} while(size > writtenbytes);
return writtenbytes;
/*
uint32_t i, pos = 0, found = 0;
off_t nodeoffset = offset;
char* data = NULL;
block_list_t* blist = &node->block_list;
kprintf("tatsachen offset %i\n", offset);
if (BUILTIN_EXPECT(node->type != FS_FILE, 0))
return NULL;
// searching for the valid data block
/* searching for the valid data block */
if (offset) {
pos = offset / node->block_size;
offset = offset % node->block_size;
pos = offset / MAX_DATAENTRIES;
offset = offset % MAX_DATAENTRIES;
}
kprintf("Pos: %i, Offset: %i, %i", pos, offset, node->block_size);
do {
for(i=0; i<MAX_DATABLOCKS && !data; i++) {
if (blist->data[i]) {
found++;
if (found > pos)
data = (char*) blist->data[i];
break;
}
}
if all blocks have already been used, we have to allocate a new one
for (i = 0; i < MAX_DATABLOCKS && !data; i++) {
if ((size + offset) >= MAX_DATAENTRIES)
size = MAX_DATAENTRIES - offset;
if(!blist->data[i]) {
blist->data[i] = (data_block_t*) kmalloc(sizeof(data_block_t));
if (blist->data[i])
memset(blist->data[i], 0x00, sizeof(data_block_t));
}
found++;
if (found > pos) {
data = (char*) blist->data[i];
}
}
if (!blist->next) {
blist->next = (block_list_t*) kmalloc(sizeof(block_list_t));
if (blist->next) {
kprintf("?");
if (blist->next)
memset(blist->next, 0x00, sizeof(block_list_t));
}
}
blist = blist->next;
} while(blist && !data);
if (BUILTIN_EXPECT(!data, 0))
return 0;
*/
/* you may have to increase nodesize */
if (node->block_size < (nodeoffset + size))
node->block_size = nodeoffset + size;
/*
* If the data block is not large engough,
* we copy only the rest of the current block.
* The user has to restart the write operation
* for the next block.
*/
/* if (offset+size >= node->block_size)
size = node->block_size - offset;
memcpy(data + offset, buffer, size);
*/ //return size;
return size;
}
@ -257,7 +226,7 @@ static vfs_node_t* initrd_mkdir(vfs_node_t* node, const char* name)
new_node->mkdir = &initrd_mkdir;
spinlock_init(&new_node->lock);
/* create default directory block */
/* create default directory entry */
dir_block = (dir_block_t*) kmalloc(sizeof(dir_block_t));
if (BUILTIN_EXPECT(!dir_block, 0))
goto out;

View file

@ -66,6 +66,8 @@ typedef struct vfs_node *(*mkdir_type_t) (struct vfs_node *, const char *name);
#define MAX_DATABLOCKS 12
#define MAX_DIRENTRIES 32
#define MAX_DATAENTRIES 4096
/** @brief Block list structure. VFS nodes keep those in a list */
typedef struct block_list {
@ -120,6 +122,11 @@ typedef struct {
dirent_t entries[MAX_DIRENTRIES];
} dir_block_t;
typedef struct {
///Array of data entries
char entries[MAX_DATAENTRIES];
} data_block_t;
extern vfs_node_t* fs_root; // The root of the filesystem.
/** @defgroup fsfunc FS related functions

View file

@ -32,8 +32,8 @@ typedef struct fildes {
off_t offset; /* */
} fildes_t;
#define MAX_FILDES 10
#define FS_INIT { [0 ... MAX_FILDES-1] = {NULL, 0} }
#define NR_OPEN 10
#define FS_INIT { [0 ... NR_OPEN-1] = {NULL, 0} }
#ifdef __cplusplus
}

View file

@ -73,7 +73,7 @@ typedef struct task {
/// List of VMAs
vma_t* vma_list;
/// Filedescriptor table
fildes_t fildes_table[MAX_FILDES];
fildes_t fildes_table[NR_OPEN];
/// Additional status flags. For instance, to signalize the using of the FPU
uint32_t flags;
/// starting time/tick of the task

View file

@ -26,24 +26,11 @@
#include <metalsvm/spinlock.h>
#include <metalsvm/time.h>
static int sys_read(int fd, const char *buf, size_t len)
{
unsigned int readbytes;
readbytes = read_fs(
per_core(current_task)->fildes_table[fd].node,
(uint8_t*)buf, len,
per_core(current_task)->fildes_table[fd].offset);
per_core(current_task)->fildes_table[fd].offset += readbytes;
//kprintf("fd:%i, Dateilaenge:%i, Dateiinhalt: %s \n", fd, len, buf);
/* Beware: still reading above file limit! */
return readbytes;
}
static int sys_write(int fd, const char *buf, size_t len)
{
unsigned int wrotebytes;
wrotebytes = write_fs(per_core(current_task)->fildes_table[fd].node, (uint8_t*)buf, len, per_core(current_task)->fildes_table[fd].offset);
//kprintf("ins Dateis. geschr. -- fd:%i, Dateilaenge:%i, Schreiblaenge: %i, Dateiinhalt: %s \n", fd, len, wrotebytes, buf);
kprintf("writing into filesystem -- fd:%i, Filelength:%i, Writtenbytes: %i, Bufferlength: %s \n", fd, len, wrotebytes, buf);
per_core(current_task)->fildes_table[fd].offset += wrotebytes;
return wrotebytes;
@ -52,13 +39,13 @@ static int sys_write(int fd, const char *buf, size_t len)
static int sys_open(const char* file, int flags, int mode)
{
int fd;
for (fd = 3; fd < MAX_FILDES; fd++) {
for (fd = 3; fd < NR_OPEN; fd++) {
if (per_core(current_task)->fildes_table[fd].node == NULL) {
per_core(current_task)->fildes_table[fd].node = findnode_fs((char*) file);
return fd;
}
}
if (fd >= MAX_FILDES) {
if (fd >= NR_OPEN) {
kprintf("Unable to create filedescriptor");
return -EINVAL;
}
@ -71,7 +58,26 @@ static int sys_close(int fd)
per_core(current_task)->fildes_table[fd].offset = 0;
return 0;
}
}
static int sys_read(int fd, const char *buf, size_t len)
{
unsigned int readbytes;
readbytes = read_fs(
per_core(current_task)->fildes_table[fd].node,
(uint8_t*)buf, len,
per_core(current_task)->fildes_table[fd].offset);
per_core(current_task)->fildes_table[fd].offset += readbytes;
/*kprintf("fd:%i, Filelength:%i, Bufferlength: %s X: %i\n", fd, len, buf, readbytes); */
/* Beware: still reading above file limit! */
return readbytes;
}
static int sys_lseek(int fd, off_t pos, int origin)
{
kprintf("lseek used, but not ready yet");
return 0;
}
static int sys_sbrk(int incr)
{
@ -114,13 +120,6 @@ int syscall_handler(uint32_t sys_nr, ...)
sys_exit(va_arg(vl, uint32_t));
ret = 0;
break;
case __NR_read: {
int fd = va_arg(vl, int);
const char* buf = va_arg(vl, const char*);
size_t len = va_arg(vl, size_t);
ret = sys_read(fd, buf, len);
break;
}
case __NR_write: {
int fd = va_arg(vl, int);
const char* buf = va_arg(vl, const char*);
@ -140,6 +139,21 @@ int syscall_handler(uint32_t sys_nr, ...)
ret = sys_close(fd);
break;
}
case __NR_read: {
int fd = va_arg(vl, int);
const char* buf = va_arg(vl, const char*);
size_t len = va_arg(vl, size_t);
ret = sys_read(fd, buf, len);
break;
}
case __NR_lseek: {
int fd = va_arg(vl, int);
off_t pos = va_arg(vl, off_t);
int origin = va_arg(vl, int);
ret = sys_lseek(fd, pos, origin);
kprintf("hallo!!! %i", ret);
break;
}
case __NR_sbrk: {
int incr = va_arg(vl, int);

View file

@ -27,20 +27,22 @@ extern int errno;
int main(int argc, char** argv)
{
//int i;
char* str = (char *)malloc(20 * sizeof(char));
int i;
long offset_x = 5;
char* str = (char *)malloc(50 * sizeof(char));
FILE* testfile;
testfile = fopen("/bin/test", "w+r");
setbuf(testfile, NULL);
fflush(NULL);
fwrite("wsblablaxxxyyyyzzzzzz", 1, 19, testfile);
fwrite("wsx", 3, 1, testfile);
fwrite("nextnextnext", 1, 10, testfile);
fclose(testfile);
testfile = fopen("/bin/test", "w+r");
setbuf(testfile, NULL);
fread(str, 1, 20, testfile);
fflush(NULL);
printf("Aus Datei gelesen (/bin/test):%s\n", str);
fread(str, 2, 40, testfile);
printf("reading (/bin/test):%s\n", str);
testfile = fopen("/bin/test", "w+r");
setbuf(testfile, NULL);

View file

@ -1 +1 @@
HalloXA!
d

View file

@ -24,12 +24,21 @@
#undef errno
extern int errno;
#include "warning.h"
#include "syscall.h"
int
_DEFUN (lseek, (file, ptr, dir),
_DEFUN (_lseek, (file, ptr, dir),
int file _AND
int ptr _AND
int dir)
{
return 0;
int ret;
ret = SYSCALL3(__NR_lseek, file, ptr, dir);
if (ret < 0) {
errno = -ret;
ret = -1;
}
return ret;
}