SayoriOS 0.3.3

nvfs.c
1
11 #include <io/ports.h>
12 #include <drv/disk/dpm.h>
13 #include <fs/nvfs.h>
14
15 #include "../lib/libstring/include/string.h"
16 #include "mem/vmm.h"
17 #include "lib/php/explode.h"
18 #include "fs/fsm.h"
19 #include "lib/php/str_replace.h"
20
21 bool nvfs_debug = false;
22
23 NVFS_DECINFO* nvfs_decode(const char* Name) {
24 NVFS_DECINFO* info = kcalloc(sizeof(NVFS_DECINFO), 1);
25
26 info->DriverFS = -1;
27
28 qemu_log("Decoding name: %s (%x)", Name, Name);
29
30 // Is path header valid?
31 bool is_valid_delim = struntil(Name, ':') == 1 && (struntil(Name, '\\') == 2 || struntil(Name, '/') == 2);
32
33 if (!is_valid_delim) {
34 goto end;
35 }
36
37 // Disk is always first letter of the path.
38 info->Disk = Name[0];
39
40 qemu_log("Disk: %d", info->Disk);
41
42
43 // Now cut a rest of path with trailing \ (or /)
44 substr(info->Path, Name, 2, strlen(Name + 2));
45
46 qemu_log("Supposed path: %s", info->Path);
47
48
49 // Get disk info.
50 DPM_Disk disk = dpm_info(info->Disk);
51
52 if (disk.Ready != 1) {
53 goto end;
54 }
55
56 info->Online = 1;
57 memcpy(info->FileSystem, disk.FileSystem, sizeof(disk.FileSystem));
58
59 info->DriverFS = fsm_getIDbyName(info->FileSystem);
60
61 if (info->DriverFS == -1) {
62 goto end;
63 }
64
65 int fgm = fsm_getMode(info->DriverFS);
66
67 if (fgm == 0){
68 char_replace(0x2F,0x5C,info->Path);
69 } else {
70 char_replace(0x5C,0x2F,info->Path);
71 }
72
73 info->Ready = 1;
74
75 end:
76
77 return info;
78 }
79
80 size_t nvfs_read(const char* Name, size_t Offset, size_t Count, void* Buffer){
81 if(nvfs_debug) {
82 qemu_log("Name=%s", Name);
83 }
84
85 NVFS_DECINFO* vinfo = nvfs_decode(Name);
86 size_t res = 0;
87
88 if (vinfo->Ready == 0) {
89 goto end;
90 }
91
92 res = fsm_read(vinfo->DriverFS, vinfo->Disk, vinfo->Path, Offset, Count, Buffer);
93
94 end:
95 kfree(vinfo);
96
97 return res;
98 }
99
100 int nvfs_create(const char* Name, int Mode){
101 NVFS_DECINFO* vinfo = nvfs_decode(Name);
102 size_t res = 0;
103
104 if (vinfo->Ready == 0) {
105 goto end;
106 }
107
108 res = fsm_create(vinfo->DriverFS, vinfo->Disk, vinfo->Path, Mode);
109
110 end:
111 kfree(vinfo);
112 return res;
113 }
114
115 int nvfs_delete(const char* Name, int Mode){
116 NVFS_DECINFO* vinfo = nvfs_decode(Name);
117 size_t res = 0;
118
119 if (vinfo->Ready == 0) {
120 goto end;
121 }
122
123 res = fsm_delete(vinfo->DriverFS, vinfo->Disk, vinfo->Path, Mode);
124
125 end:
126
127 kfree(vinfo);
128
129 return res;
130 }
131
132 size_t nvfs_write(const char* Name, size_t Offset, size_t Count, const void *Buffer){
133 NVFS_DECINFO* vinfo = nvfs_decode(Name);
134 size_t res = 0;
135
136 if (vinfo->Ready == 0) {
137 goto end;
138 }
139
140 res = fsm_write(vinfo->DriverFS, vinfo->Disk, vinfo->Path, Offset, Count, Buffer);
141
142 end:
143
144 kfree(vinfo);
145
146 return res;
147 }
148
149 FSM_FILE nvfs_info(const char* Name){
150 NVFS_DECINFO* vinfo = nvfs_decode(Name); // no memleak
151 if (nvfs_debug) {
152 qemu_log("NVFS INFO:\n"
153 "Ready: %d\n"
154 "Disk: [%d] %c\n"
155 "Path: [%d] %s\n"
156 "Disk Online: %d\n"
157 "Disk file system: [%d] %s\n"
158 "Loaded in file system driver: %d",
159 vinfo->Ready,
160 vinfo->Disk,
161 vinfo->Disk,
162 strlen(vinfo->Path),
163 vinfo->Path,
164 vinfo->Online,
165 strlen(vinfo->FileSystem),
166 vinfo->FileSystem,
167 vinfo->DriverFS
168 );
169 }
170
171 FSM_FILE file = {};
172
173 if (vinfo->Ready != 1){
174 goto end;
175 }
176
177 file = fsm_info(vinfo->DriverFS, vinfo->Disk, vinfo->Path);
178 end:
179
180 kfree(vinfo);
181
182 return file;
183 }
184
185 FSM_DIR* nvfs_dir(const char* Name){
186 NVFS_DECINFO* vinfo = nvfs_decode(Name);
187
188 if (vinfo->Ready != 1) {
189 kfree(vinfo);
190 FSM_DIR* dir = kcalloc(sizeof(FSM_DIR), 1);
191 return dir;
192 }
193
194 FSM_DIR* dir = fsm_dir(vinfo->DriverFS, vinfo->Disk, vinfo->Path);
195
196 kfree(vinfo);
197 return dir;
198 }
199
200 /*
201
202 void vnfs_test(){
203
204 FSM_FILE file = nvfs_info("R:\\help next you\\main.c");
205 //qemu_log("Ready: %d",file.Ready);
206 fsm_dump(file);
207
208 char* bf1 = kmalloc(file.Size);
209 int rf1 = nvfs_read("R:\\help next you\\main.c",10,5,bf1);
210
211 qemu_log("Read %d / %d |\n%s\n",rf1,file.Size,bf1);
212
213 FSM_FILE file2 = nvfs_info("b:\\pizdec\\вот это драйвер\\ахуеть.exe");
214
215 fsm_dump(file2);
216 while(1){}
217 }*/
strlen
size_t strlen(const char *str)
Возращает длину строки
Definition: string.c:88
memcpy
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.
Definition: string.c:173
substr
void substr(char *restrict dest, const char *restrict source, int from, int length)
Вырезает и возвращает подстроку из строки
Definition: string.c:492
struntil
size_t struntil(const char *str, const char find)
Возвращает индекс символа в строке
Definition: string.c:124
char_replace
uint32_t char_replace(char search, char replace, char *subject)
Заменяет все вхождения символа поиска на символ замены
Definition: str_replace.c:21