initrd_write:
- writing strings of any length now possible
This commit is contained in:
parent
4460d92843
commit
b94875ccfe
8 changed files with 52 additions and 73 deletions
|
@ -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;
|
||||
}
|
||||
|
|
83
fs/initrd.c
83
fs/initrd.c
|
@ -65,7 +65,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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -72,7 +72,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
|
||||
|
|
|
@ -29,7 +29,7 @@ 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;
|
||||
|
@ -38,13 +38,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;
|
||||
}
|
||||
|
@ -67,14 +67,15 @@ static int sys_read(int fd, const char *buf, size_t len)
|
|||
(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 X: %i\n", fd, len, buf, 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)
|
||||
{
|
||||
return 2;
|
||||
kprintf("lseek used, but not ready yet");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sys_sbrk(int incr)
|
||||
|
|
|
@ -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);
|
||||
|
||||
return errno;
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
1
|
||||
d
|
||||
|
|
Loading…
Add table
Reference in a new issue