summaryrefslogtreecommitdiff
path: root/fs/fd.c
blob: 0ec134b11c1de47d702ef977b09e112d169146c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "kernel.h"
#include "fd.h"
#include "fifo.h"
#include "kmalloc.h"
#include "ext2.h"
#include "log.h"

bool fd_write(fd* f,uint8_t c)
{
    return f->write(f->data,c);
}

uint8_t fd_read(fd* f)
{
    return f->read(f->data);
}

bool fd_has(fd* f)
{
    return f->has(f->data);
}

bool fd_close(fd* f)
{
    return f->close(f->data);
}

fd fd_from_fifo(fifo* fif)
{
    fd f;
    f.type=FD_TYPE_FIFO_BUFFERED;
    f.data=fif;
    f.read=fifo_get;
    f.write=fifo_put;
//    f.close=fd_close;
    f.has=fifo_has;
    return f;
}

//----
void* ext2_init(char *path)
{
    uint32_t *data=kballoc(1);
    uint32_t inode= ext2_filename_to_inode(VMEM_EXT2_RAMIMAGE,path); 
    data[0]=inode;// pos
    data[1]=0; //pos
    
    return data;
}

void ext2_write(void* x)
{
    kpanic("not impl");
}

uint8_t ext2_read(uint32_t* data)
{
    char c;
    ext2_read_inode(VMEM_EXT2_RAMIMAGE,data[0],&c,&data[1],1);
    return c;
}

bool ext2_has(uint32_t* data)
{
    uint32_t save=data[1];
    uint32_t c=ext2_read_inode(VMEM_EXT2_RAMIMAGE,data[0],&c,&data[1],1);
    data[1]=save;
    return (c>0);

}
void ext2_close(void* x)
{
    kbfree(x);
}

fd fd_from_path(char* path)
{
    fd f;
    f.type=FD_TYPE_EXT2_FILE;
    f.data=ext2_init(path);
    f.read=ext2_read;
    f.write=ext2_write;
    f.close=ext2_close;
    f.has=ext2_has;
    return f;
}