-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmpfs.c
More file actions
143 lines (110 loc) · 3.35 KB
/
Copy pathtmpfs.c
File metadata and controls
143 lines (110 loc) · 3.35 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "tmpfs.h"
#include "console.h"
#include "heap.h"
#include "string.h"
static uint64_t tmpfs_read(fs_node_t *node, uint64_t offset, uint64_t size,
uint8_t *buffer);
static uint64_t tmpfs_write(fs_node_t *node, uint64_t offset, uint64_t size,
uint8_t *buffer);
static struct dirent *tmpfs_readdir(fs_node_t *node, uint32_t index);
static fs_node_t *tmpfs_finddir(fs_node_t *node, char *name);
fs_node_t *tmpfs_create_file(fs_node_t *parent, char *name);
fs_node_t *tmpfs_create_dir(fs_node_t *parent, char *name);
static fs_node_t *create_node(char *name, uint32_t flags) {
fs_node_t *node = (fs_node_t *)malloc(sizeof(fs_node_t));
k_memset(node, 0, sizeof(fs_node_t));
k_strcpy(node->name, name);
node->flags = flags;
node->open = NULL;
node->close = NULL;
node->read = tmpfs_read;
node->write = tmpfs_write;
node->readdir = tmpfs_readdir;
node->finddir = tmpfs_finddir;
node->create = tmpfs_create_file;
node->mkdir = tmpfs_create_dir;
return node;
}
fs_node_t *tmpfs_init(void) { return tmpfs_create_dir(NULL, "root"); }
fs_node_t *tmpfs_create_file(fs_node_t *parent, char *name) {
fs_node_t *node = create_node(name, FS_FILE);
node->ptr = NULL;
node->length = 0;
if (parent) {
node->next = parent->ptr;
parent->ptr = node;
}
return node;
}
fs_node_t *tmpfs_create_dir(fs_node_t *parent, char *name) {
fs_node_t *node = create_node(name, FS_DIRECTORY);
node->ptr = NULL;
if (parent) {
node->next = parent->ptr;
parent->ptr = node;
}
return node;
}
static uint64_t tmpfs_read(fs_node_t *node, uint64_t offset, uint64_t size,
uint8_t *buffer) {
if ((node->flags & 0x7) != FS_FILE)
return 0;
if (offset >= node->length)
return 0;
if (offset + size > node->length)
size = node->length - offset;
char *data = (char *)node->ptr;
if (!data)
return 0;
k_memcpy(buffer, data + offset, size);
return size;
}
static uint64_t tmpfs_write(fs_node_t *node, uint64_t offset, uint64_t size,
uint8_t *buffer) {
if ((node->flags & 0x7) != FS_FILE)
return 0;
size_t new_end = offset + size;
if (new_end > node->length) {
char *new_data = (char *)malloc(new_end + 1);
if (!new_data)
return 0;
k_memset(new_data, 0, new_end + 1);
if (node->ptr) {
k_memcpy(new_data, node->ptr, node->length);
free(node->ptr);
}
node->ptr = (struct fs_node *)new_data;
node->length = new_end;
}
char *data = (char *)node->ptr;
k_memcpy(data + offset, buffer, size);
return size;
}
static struct dirent dir_entry;
static struct dirent *tmpfs_readdir(fs_node_t *node, uint32_t index) {
if ((node->flags & 0x7) != FS_DIRECTORY)
return NULL;
fs_node_t *child = (fs_node_t *)node->ptr;
uint32_t i = 0;
while (child != NULL) {
if (i == index) {
k_strcpy(dir_entry.name, child->name);
dir_entry.inode = 0;
return &dir_entry;
}
child = child->next;
i++;
}
return NULL;
}
static fs_node_t *tmpfs_finddir(fs_node_t *node, char *name) {
if ((node->flags & 0x7) != FS_DIRECTORY)
return NULL;
fs_node_t *child = (fs_node_t *)node->ptr;
while (child != NULL) {
if (k_strcmp(name, child->name) == 0)
return child;
child = child->next;
}
return NULL;
}