SayoriOS 0.3.3

tempfs.c
1
10 #include <io/ports.h>
11 #include <fs/fsm.h>
12 #include <fs/tempfs.h>
13 #include <lib/php/pathinfo.h>
14 #include <drv/disk/dpm.h>
15 #include "lib/string.h"
16 #include "mem/vmm.h"
17 #include "../../include/portability.h"
18
19 int fs_tempfs_func_getCountAllBlocks(size_t all_disk){
20 size_t all_free_disk = (all_disk) - (sizeof(TEMPFS_BOOT)) - 1;
21 if (all_free_disk <= 0){
22 return 0;
23 }
24 size_t all_blocks = (all_free_disk / sizeof(TEMPFS_ENTITY)) - 1;
25 return (all_blocks <= 0?0:all_blocks);
26 }
27
28 int fs_tempfs_func_checkSign(uint16_t sign1, uint16_t sign2){
29 return (sign1 + sign2 == 0x975f?1:0);
30 }
31
32 void fs_tempfs_func_fixPackage(char* src, int count){
33 for (int i = count; i < 9;i++){
34 src[i] = 0;
35 }
36 }
37
38 int fs_tempfs_tcache_update(const char Disk){
39 tfs_log("[>] TCache update...\n");
40
41 TEMPFS_Cache* __TCache__ = dpm_metadata_read(Disk);
42
43 // dpm_metadata_write
44 if (__TCache__ == 0){
45 __TCache__ = malloc(sizeof(TEMPFS_Cache));
46 if (__TCache__ == NULL){
47 return 1;
48 }
49 }
50 memset(__TCache__, 0, sizeof(TEMPFS_Cache));
51 if (__TCache__->Boot != 0){
52 tfs_log(" |--- Free boot (0x%x)...\n", __TCache__->Boot);
53 free(__TCache__->Boot);
54 }
55
56 if (__TCache__->Files != 0){
57 tfs_log(" |--- Free files (0x%x)...\n", __TCache__->Files);
58 for (uint32_t i = 0; i < __TCache__->CountFiles; i++) {
59 free(&__TCache__->Files[i]);
60 }
61 free(__TCache__->Files);
62 }
63 tfs_log(" |--- Malloc Boot...\n");
64 __TCache__->Boot = malloc(sizeof(TEMPFS_BOOT));
65 if (__TCache__->Boot == NULL){
66 return 1;
67 }
68 tfs_log(" | |--- Addr Boot: 0x%x\n", __TCache__->Boot);
69 tfs_log(" | |--- Zero Boot...\n");
70 memset(__TCache__->Boot, 0, sizeof(TEMPFS_BOOT));
71 tfs_log(" | |--- Read Boot...\n");
72 int read = dpm_read(Disk, 0, 0, sizeof(TEMPFS_BOOT), __TCache__->Boot);
73 if (read != sizeof(TEMPFS_BOOT)){
74 tfs_log(" | |--- Read: %d\n", read);
75 return 2;
76 }
77
78 if (fs_tempfs_func_checkSign(__TCache__->Boot->Sign1, __TCache__->Boot->Sign2) != 1){
79 tfs_log(" | |--- Error check signature\n");
80 return 3;
81 }
82
83 if (__TCache__->Boot->CountFiles > 0){
84 __TCache__->Files = malloc(sizeof(TEMPFS_ENTITY) * __TCache__->Boot->CountFiles);
85 if (__TCache__->Files == NULL){
86 return 1;
87 }
88 memset(__TCache__->Files, 0, sizeof(TEMPFS_ENTITY) * __TCache__->Boot->CountFiles);
89 size_t offset = 512;
90 size_t inx = 0;
91 tfs_log(" |--- [>] Enter while\n");
92 while(1){
93 if (inx + 1 > __TCache__->Boot->CountFiles) break;
94 tfs_log(" | |--- [>] %d | %d\n", inx + 1, __TCache__->Boot->CountFiles);
95 int eread = dpm_read(Disk, 0, offset, sizeof(TEMPFS_ENTITY), &__TCache__->Files[inx]);
96 tfs_log(" | | |--- [>] Disk read\n");
97 tfs_log(" | | | |--- Offset: %d\n", offset);
98 tfs_log(" | | | |--- Size: %d\n", sizeof(TEMPFS_ENTITY));
99 offset += sizeof(TEMPFS_ENTITY);
100 if (eread != sizeof(TEMPFS_ENTITY)){
101 tfs_log(" | | |--- Failed to load enough bytes for data.!\n");
102 break;
103 }
104 if (__TCache__->Files[inx].Status != TEMPFS_ENTITY_STATUS_READY){
105 tfs_log(" | |--- No data.!\n");
106 continue;
107 }
108
109 tfs_log(" | | |--- [>] File info\n");
110 tfs_log(" | | | |--- Name: %s\n", __TCache__->Files[inx].Name);
111 tfs_log(" | | | |--- Path: %s\n", __TCache__->Files[inx].Path);
112 tfs_log(" | | | |--- Size: %d\n", __TCache__->Files[inx].Size);
113 tfs_log(" | | | |--- Type: %s\n", (__TCache__->Files[inx].Type == TEMPFS_ENTITY_TYPE_FOLDER?"Folder":"File"));
114 //tfs_log(" | | | |--- Date: %s\n", Files[count].LastTime);
115 tfs_log(" | | | |--- CHMOD: 0x%x\n", __TCache__->Files[inx].CHMOD);
116 tfs_log(" | | |--- Next!\n");
117 inx++;
118 }
119 }
120 __TCache__->CountFiles = __TCache__->Boot->CountFiles;
121 __TCache__->BlocksAll = fs_tempfs_func_getCountAllBlocks(TMF_GETDISKSIZE(Disk));
122 __TCache__->FreeAll = (__TCache__->BlocksAll - __TCache__->Boot->CountBlocks - __TCache__->Boot->CountFiles);
123 __TCache__->Status = 1;
124
125 dpm_metadata_write(Disk, (uint32_t)__TCache__);
126 return 0x7246;
127 }
128
129
130 TEMPFS_PACKAGE* fs_tempfs_func_readPackage(const char Disk, size_t Address){
131 TEMPFS_PACKAGE* pack = malloc(sizeof(TEMPFS_PACKAGE));
132 if (pack == NULL){
133 return NULL;
134 }
135 memset(pack, 0, sizeof(TEMPFS_PACKAGE));
136
137 /* size_t read = */dpm_read(Disk,0,Address, sizeof(TEMPFS_PACKAGE), pack);
138 return pack;
139 }
140
141 int fs_tempfs_func_writePackage(const char Disk, size_t Address, TEMPFS_PACKAGE* pack){
142 int write = dpm_write(Disk, 0, Address, sizeof(TEMPFS_PACKAGE), pack);
143 return (write == sizeof(TEMPFS_PACKAGE)?1:0);
144 }
145
146 size_t fs_tempfs_func_getIndexEntity(const char Disk, char* Path){
147 TEMPFS_Cache* __TCache__ = dpm_metadata_read(Disk);
148 if (__TCache__ == 0 || __TCache__->Status != 1 || __TCache__->Boot->CountFiles <= 0){
149 return -1;
150 }
151 char* dir = pathinfo(Path, PATHINFO_DIRNAME);
152 char* basename = pathinfo(Path, PATHINFO_BASENAME);
153 size_t offset = 512;
154 size_t inx = 0;
155 size_t ginx = 0;
156 tfs_log(" |--- [>] Enter while\n");
157 while(1){
158 if (inx + 1 > __TCache__->Boot->CountFiles) break;
159 tfs_log(" | |--- [>] %d | %d\n", inx + 1, __TCache__->Boot->CountFiles);
160 TEMPFS_ENTITY tmp = {0};
161 int eread = dpm_read(Disk, 0, offset, sizeof(TEMPFS_ENTITY), &tmp);
162 tfs_log(" | | |--- [>] Disk read\n");
163 tfs_log(" | | | |--- Offset: %d\n", offset);
164 tfs_log(" | | | |--- Size: %d\n", sizeof(TEMPFS_ENTITY));
165 offset += sizeof(TEMPFS_ENTITY);
166 if (eread != sizeof(TEMPFS_ENTITY)){
167 tfs_log(" | | |--- Failed to load enough bytes for data.!\n");
168 break;
169 }
170 if (tmp.Status != TEMPFS_ENTITY_STATUS_READY){
171 tfs_log(" | |--- No data.!\n");
172 ginx++;
173 continue;
174 }
175 int is_in = strcmp(tmp.Path, dir);
176 int is_file = strcmp(tmp.Name, basename);
177
178 if (is_in == 0 && is_file == 0){
179 free(dir);
180 free(basename);
181 return ginx;
182 }
183 ginx++;
184 inx++;
185 }
186 free(dir);
187 free(basename);
188 return -1;
189 }
190
191 TEMPFS_ENTITY* fs_tempfs_func_readEntity(const char Disk, char* Path){
192 TEMPFS_ENTITY* entity = malloc(sizeof(TEMPFS_ENTITY));
193 if (entity == NULL) return NULL;
194 memset(entity, 0, sizeof(TEMPFS_ENTITY));
195 TEMPFS_Cache* __TCache__ = dpm_metadata_read(Disk);
196 if (__TCache__ == 0 || __TCache__->Status != 1 || __TCache__->CountFiles == 0){
197 return entity;
198 }
199
200 char* dir = pathinfo(Path, PATHINFO_DIRNAME);
201 char* basename = pathinfo(Path, PATHINFO_BASENAME);
202 tfs_log("basename: %s | dir: %s\n",basename, dir);
203 for(size_t cid = 0; cid < __TCache__->CountFiles; cid++){
204 if (__TCache__->Files[cid].Status != 1){
205 continue;
206 }
207 int is_in = strcmp(__TCache__->Files[cid].Path, dir);
208 int is_file = strcmp(__TCache__->Files[cid].Name, basename);
209
210 tfs_log(" |- name: %s | path: %s\n",__TCache__->Files[cid].Name, __TCache__->Files[cid].Path);
211 if (is_in == 0 && is_file == 0){
212 tfs_log("{^^^^ SELECTED}\n");
213 memcpy(entity, &__TCache__->Files[cid], sizeof(TEMPFS_ENTITY));
214 return entity;
215 }
216 }
217 free(dir);
218 free(basename);
219 return entity;
220 }
221
222 int fs_tempfs_func_writeEntity(const char Disk, int Index, TEMPFS_ENTITY* entity){
223 int write = dpm_write(Disk, 0, 512 + (Index*sizeof(TEMPFS_ENTITY)), sizeof(TEMPFS_ENTITY), entity);
224 return (write == sizeof(TEMPFS_ENTITY)?1:0);
225 }
226
227
228 int fs_tempfs_func_updateBoot(const char Disk, TEMPFS_BOOT* boot){
229 int write = dpm_write(Disk, 0, 0, sizeof(TEMPFS_BOOT), boot);
230 return (write == sizeof(TEMPFS_BOOT)?1:0);
231 }
232
233 TEMPFS_BOOT* fs_tempfs_func_getBootInfo(const char Disk){
234 TEMPFS_Cache* __TCache__ = dpm_metadata_read(Disk);
235 if (__TCache__ == 0) return NULL;
236 return __TCache__->Boot;
237 }
238
239 size_t fs_tempfs_func_getCountAllFreeEntity(const char Disk){
240 TEMPFS_Cache* __TCache__ = dpm_metadata_read(Disk);
241 if (__TCache__ == 0) return 0;
242 return __TCache__->FreeAll;
243 }
244
245 int fs_tempfs_func_findFreePackage(const char Disk, int Skip){
246 TEMPFS_Cache* __TCache__ = dpm_metadata_read(Disk);
247 if (__TCache__ == 0 || __TCache__->Status != 1){
248 return -1;
249 }
250 tfs_log("[>] Find free package...\n");
251
252 TEMPFS_BOOT* boot = fs_tempfs_func_getBootInfo(Disk);
253 if (boot == NULL || fs_tempfs_func_checkSign(boot->Sign1, boot->Sign2) != 1) {
254 tfs_log(" |--- [ERR] Signature error\n");
255 return -1;
256 }
257
258
259 int allcount = fs_tempfs_func_getCountAllBlocks(boot->EndDisk);
260 if (allcount <= 0){
261 tfs_log(" |--- [ERR] No blocks\n");
262 return -1;
263 }
264
265 size_t adr = 0;
266 for (int i = 0; i < allcount; i++){
267 adr = (boot->EndDisk - sizeof(TEMPFS_PACKAGE) - (i * sizeof(TEMPFS_PACKAGE)));
268 TEMPFS_PACKAGE* pack = fs_tempfs_func_readPackage(Disk, adr);
269
270 tfs_log("[%d] Test PackAge\n", i);
271
272 tfs_log(" |--- Addr | %x\n",adr);
273 tfs_log(" |--- Data | %s\n",pack->Data);
274 tfs_log(" |--- Status | %d\n",pack->Status);
275 tfs_log(" |--- Length | %d\n",pack->Length);
276 tfs_log(" |--- Next | %x\n\n",pack->Next);
277
278 if (pack->Status != TEMPFS_ENTITY_STATUS_READY && Skip !=0){
279 Skip--;
280 continue;
281 }
282
283 if (pack->Status != TEMPFS_ENTITY_STATUS_READY){
284 free(pack);
285 return adr;
286 }
287 free(pack);
288 }
289
290 return -1;
291 }
292
293 int fs_tempfs_func_findFreeInfoBlock(const char Disk){
294 TEMPFS_Cache* __TCache__ = dpm_metadata_read(Disk);
295 if (__TCache__ == 0) return 0;
296 if (__TCache__->Status != 1){
297 return 0;
298 }
299 size_t offset = 512;
300 size_t cmx = 0;
301 TEMPFS_ENTITY* tmp = malloc(sizeof(TEMPFS_ENTITY));
302 while(1){
303 memset(tmp, 0, sizeof(TEMPFS_ENTITY));
304 int read = dpm_read(Disk, 0, offset, sizeof(TEMPFS_ENTITY), tmp);
305 if (read != sizeof(TEMPFS_ENTITY)) {
306 free(tmp);
307 return -1;
308 }
309 if (tmp->Status == 0 || tmp->Type == 0){
310 free(tmp);
311 return cmx;
312 }
313 cmx++;
314 offset += sizeof(TEMPFS_ENTITY);
315 }
316 free(tmp);
317 }
318
319
320 int fs_tempfs_func_findDIR(const char Disk, const char* Path){
321 TEMPFS_Cache* __TCache__ = dpm_metadata_read(Disk);
322 if (__TCache__ == 0 || __TCache__->Status != 1 || __TCache__->CountFiles == 0){
323 printf("[>] %d == 0 || %d != 1 || %d == 0", __TCache__, __TCache__->Status, __TCache__->CountFiles);
324 return 0;
325 }
326 int ret = 0x00;
327 char* dir = pathinfo(Path, PATHINFO_DIRNAME);
328 char* basename = pathinfo(Path, PATHINFO_BASENAME);
329 for(size_t cid = 0; cid < __TCache__->CountFiles; cid++){
330 if (__TCache__->Files[cid].Status != 1){
331 continue;
332 }
333 if (__TCache__->Files[cid].Type != 0x02){
334 continue;
335 }
336 int is_in = strcmp(__TCache__->Files[cid].Path, dir);
337 int is_ph = strcmp(__TCache__->Files[cid].Name, basename);
338 if (is_in == 0){
339 ret |= TEMPFS_DIR_INFO_ROOT;
340 }
341 if (is_ph == 0){
342 ret |= TEMPFS_DIR_INFO_EXITS;
343 }
344 if ((ret & TEMPFS_DIR_INFO_ROOT) && (ret & TEMPFS_DIR_INFO_EXITS)){
345 free(dir);
346 return ret;
347 }
348 }
349 free(dir);
350 return ret;
351 }
352
353 size_t fs_tempfs_func_findFilesOnDIR(const char Disk, const char* Path){
354 TEMPFS_Cache* __TCache__ = dpm_metadata_read(Disk);
355 if (__TCache__ == 0 || __TCache__->Status != 1 || __TCache__->CountFiles == 0){
356 return 0;
357 }
358 size_t ret = 0;
359 for(size_t cid = 0; cid < __TCache__->CountFiles; cid++){
360 if (__TCache__->Files[cid].Status != 1){
361 continue;
362 }
363 int is_in = strcmp(__TCache__->Files[cid].Path, Path);
364 if (is_in == 0){
365 ret++;
366 }
367 }
368 return ret;
369 }
370
371 int fs_tempfs_func_findFILE(const char Disk, const char* Path){
372 TEMPFS_Cache* __TCache__ = dpm_metadata_read(Disk);
373 if (__TCache__ == 0 || __TCache__->Status != 1 || __TCache__->CountFiles == 0){
374 return 0;
375 }
376
377 char* dir = pathinfo(Path, PATHINFO_DIRNAME);
378 char* basename = pathinfo(Path, PATHINFO_BASENAME);
379 for(size_t cid = 0; cid < __TCache__->CountFiles; cid++){
380 if (__TCache__->Files[cid].Status != 1){
381 continue;
382 }
383 if (__TCache__->Files[cid].Type != 0x01){
384 continue;
385 }
386 int is_in = fs_tempfs_func_findDIR(Disk, dir);
387 int is_file = strcmp(__TCache__->Files[cid].Name, basename);
388 //int is_in = strcmp(__TCache__->Files[cid].Path, dir);
389 if (is_in == 0x03 && is_file == 0){
390 free(dir);
391 free(basename);
392 return 1;
393 }
394 }
395 free(dir);
396 free(basename);
397 return 0;
398 }
399
400 int fs_tempfs_func_clearBlocks(const char Disk, size_t Addr){
401 tfs_log("[!] Starting delete package: 0x%x\n", Addr);
402 if (Addr == 0){
403 tfs_log("[E] ERROR POINT\n");
404 return 1;
405 }
406 TEMPFS_PACKAGE* pkg_free = calloc(1, sizeof(TEMPFS_PACKAGE));
407 size_t ADRNOW = Addr;
408 while(1){
409 tfs_log(" |---[>] Starting get package: 0x%x\n", ADRNOW);
410 TEMPFS_PACKAGE* pack = fs_tempfs_func_readPackage(Disk, ADRNOW);
411 if (pack == NULL){
412 tfs_log(" |--- NULL\n");
413 free(pkg_free);
414 return 0;
415 }
416 if (pack->Status == TEMPFS_ENTITY_STATUS_PKG_READY){
417 tfs_log(" |--- Delete PKG to Address: 0x%x\n", ADRNOW);
418 fs_tempfs_func_writePackage(Disk, ADRNOW, pkg_free);
419 }
420 if (pack->Next == -1){
421 free(pack);
422 break;
423 }
424 tfs_log(" |--- Next Address: 0x%x\n", pack->Next);
425 ADRNOW = pack->Next;
426 free(pack);
427 }
428
429 //fs_tempfs_func_writePackage(Disk, pkg_addr[i], pkg_free);
430 tfs_log("[+] complete delete package\n");
431 free(pkg_free);
432 return 1;
433 }
434
435
436 void fs_tempfs_func_cacheUpdate(const char Disk){
437 fs_tempfs_tcache_update(Disk);
438 }
439
440 size_t fs_tempfs_read(const char Disk, const char* Path, size_t Offset, size_t Size, void* Buffer){
441 TEMPFS_ENTITY* ent = fs_tempfs_func_readEntity(Disk, Path);
442 if (ent == NULL || ent->Status != 1 || ent->Point == 0){
443 return 0;
444 }
445
446 char* Buf = (char*)malloc(Size);
447 if (Buf == NULL){
448 return 0;
449 }
450 memset(Buf, 0, Size);
451
452 size_t total_bytes_read = 0;
453
454 // Определяем пакет, с которого необходимо начать чтение на основе смещения
455 TEMPFS_PACKAGE* pack = fs_tempfs_func_readPackage(Disk, ent->Point);
456 while(pack != NULL && Offset > 0){
457 // Пропускаем пакеты, пока смещение не уменьшится до 0
458 size_t bytes_to_skip = (Offset < pack->Length) ? Offset : pack->Length;
459 Offset -= bytes_to_skip;
460 pack = (pack->Next != -1) ? fs_tempfs_func_readPackage(Disk, pack->Next) : NULL;
461 }
462
463 // Начинаем считывать данные с учетом смещения и количества
464 while(pack != NULL && total_bytes_read < Size){
465 size_t bytes_to_read = (Size - total_bytes_read < pack->Length) ? (Size - total_bytes_read) : pack->Length;
466 memcpy(&Buf[total_bytes_read], pack->Data, bytes_to_read);
467 total_bytes_read += bytes_to_read;
468
469 pack = (pack->Next != -1) ? fs_tempfs_func_readPackage(Disk, pack->Next) : NULL;
470 }
471
472 // Копируем результат в указанный буфер
473 memcpy(Buffer, Buf, total_bytes_read);
474
475 // Освобождаем буфер
476 free(Buf);
477
478 return total_bytes_read;
479 }
480
481 size_t fs_tempfs_write(const char Disk, const char* Path, size_t Offset, size_t Size, void* Buffer){
482 tfs_log("File write...\n");
483 TEMPFS_ENTITY* ent = fs_tempfs_func_readEntity(Disk, Path);
484 if (ent == NULL || ent->Status != TEMPFS_ENTITY_STATUS_READY) return 0;
485
486 tfs_log("File next...\n");
487 size_t src_size = ent->Size < Size + Offset ? Size + Offset : ent->Size;
488
489 char* PREBUF = calloc(src_size, 1);
490 size_t fre = fs_tempfs_read(Disk, Path, 0, src_size, PREBUF);
491
492 memcpy(PREBUF + Offset, Buffer, Size);
493
494 tfs_log("PREBUF: \n%s\n", PREBUF);
495
496 size_t src_seek = 0;
497 size_t read = 0;
498 char src_buf[500] = {0}; // Буфер для одного пакета
499
500 TEMPFS_PACKAGE* pkg_free = calloc(1, sizeof(TEMPFS_PACKAGE));
501 if (pkg_free == NULL){
502 tfs_log("KMALLOC ERROR\n");
503 return 0;
504 }
505
506 // Определяем количество пакетов, которые нужно записать
507 size_t countPack = (src_size / 500) + 1; // Рассчитываем количество пакетов
508
509 // Отчищаем старые блоки, если уже есть точка входа
510 if (fs_tempfs_func_clearBlocks(Disk, ent->Point) == 0) {
511 tfs_log("BLOCK CLEAR ERROR\n");
512 return 0;
513 }
514 uint32_t* pkg_addr = malloc(sizeof(uint32_t) * countPack);
515
516 for (size_t i = 0; i < countPack; i++) {
517 // Находим свободный пакет для записи данных
518 pkg_addr[i] = fs_tempfs_func_findFreePackage(Disk,i);
519 if (pkg_addr[i] == -1) {
520 tfs_log("NO FREE PACKAGE!!!\n");
521 return 0;
522 }
523 }
524
525 for (size_t i = 0; i < countPack; i++) {
526 // Определяем размер данных для записи в текущем пакете
527 size_t bytes_to_write = (src_size - src_seek >= 500) ? 500 : src_size - src_seek;
528
529 // Копируем данные из буфера в текущий пакет
530 memcpy(src_buf, (char*)PREBUF + src_seek, bytes_to_write);
531 src_seek += bytes_to_write;
532
533 // Записываем пакет в файловую систему
534 pkg_free->Length = bytes_to_write;
535 pkg_free->Next = (i < countPack - 1) ? fs_tempfs_func_findFreePackage(Disk, i + 1) : -1;
536 pkg_free->Status = TEMPFS_ENTITY_STATUS_PKG_READY;
537 memcpy(pkg_free->Data, src_buf, 500);
538 fs_tempfs_func_writePackage(Disk, pkg_addr[i], pkg_free);
539
540 // Сбрасываем буфер
541 memset(src_buf, 0, 500);
542 }
543
544 tfs_log("Write complete!\n");
545
546 // После записи данных обновляем информацию о файле
547 size_t indexFile = fs_tempfs_func_getIndexEntity(Disk, Path);
548 ent->Size = src_seek;
549 ent->Point = pkg_addr[0];
550 int went = fs_tempfs_func_writeEntity(Disk, indexFile, ent);
551 free(ent);
552 tfs_log("[>] Write entity (%d) to disk...\n", indexFile);
553
554
555 // Обновляем информацию о загрузочном секторе
556 TEMPFS_BOOT* boot = fs_tempfs_func_getBootInfo(Disk);
557 if (boot == NULL || fs_tempfs_func_checkSign(boot->Sign1, boot->Sign2) != 1) {
558 tfs_log(" |--- [ERR] TempFS signature did not match OR error reading TempFS boot sector\n");
559 return 0;
560 }
561
562 // Увеличиваем общее количество блоков
563 boot->CountBlocks += countPack;
564 tfs_log("[>] Boot update data...\n");
566 int boot_write = fs_tempfs_func_updateBoot(Disk, boot);
567 if (boot_write != 1){
568 tfs_log(" |-- [ERR] An error occurred while writing the TempFS boot partition\n");
569 return 0;
570 }
571 fs_tempfs_func_cacheUpdate(Disk);
572
573 return src_seek;
574 }
575
576 FSM_FILE fs_tempfs_info(const char Disk, const char* Path){
577 // fs_tempfs_func_readEntity
578 FSM_FILE file = {0};
579 TEMPFS_ENTITY* entity = fs_tempfs_func_readEntity(Disk, Path);
580 if (entity == NULL || entity->Status != 1) return file;
581 memcpy(file.Name, entity->Name, strlen(entity->Name));
582 memcpy(file.Path, entity->Path, strlen(entity->Path));
583
584 file.CHMOD = entity->CHMOD;
585 file.Mode = entity->CHMOD;
586 file.Ready = 1;
587 file.Size = entity->Size;
588 file.Type = (entity->Type == TEMPFS_ENTITY_TYPE_FOLDER?5:0);
589
590 free(entity);
591 return file;
592 }
593
594 FSM_DIR* fs_tempfs_dir(const char Disk, const char* Path){
595 FSM_DIR* Dir = malloc(sizeof(FSM_DIR));
596 memset(Dir, 0, sizeof(FSM_DIR));
597 tfs_log("[>] Get DIR: %s\n", Path);
598 TEMPFS_BOOT* boot = fs_tempfs_func_getBootInfo(Disk);
599 if (boot == NULL || fs_tempfs_func_checkSign(boot->Sign1, boot->Sign2) != 1) {
600 tfs_log(" |--- Error sign 0x%x %d %d\n",boot , boot->Sign1, boot->Sign2);
601 return Dir;
602 }
603 FSM_FILE *Files = malloc(sizeof(FSM_FILE) * boot->CountFiles);
604 if (Files == NULL) {
605 tfs_log(" |--- Error malloc\n");
606 return Dir;
607 }
608 size_t offset = 512;
609 size_t count = 0;
610 size_t CF = 0, CD = 0;
611 size_t CZ = 0;
612 tfs_log(" |--- [>] Enter while\n");
613 while(1){
614 if (CZ + 1 > boot->CountFiles){
615 break;
616 }
617 tfs_log(" | |--- [>] %d > %d\n", CZ + 1, boot->CountFiles);
618 TEMPFS_ENTITY* entity = malloc(sizeof(TEMPFS_ENTITY));
619 if (entity == NULL) {
620 break;
621 }
622 int eread = dpm_read(Disk,0,offset, sizeof(TEMPFS_ENTITY), entity);
623 tfs_log(" | | |--- [>] Disk read\n");
624 tfs_log(" | | | |--- Offset: %d\n", offset);
625 tfs_log(" | | | |--- Size: %d\n", sizeof(TEMPFS_ENTITY));
626 offset += sizeof(TEMPFS_ENTITY);
627 if (eread != sizeof(TEMPFS_ENTITY)){
628 tfs_log(" | | |--- Failed to load enough bytes for data.!\n");
629 free(entity);
630 break;
631 }
632 if (entity->Status == TEMPFS_ENTITY_STATUS_ERROR ||
633 entity->Type == TEMPFS_ENTITY_TYPE_UNKNOWN){
634 tfs_log(" | |--- No data.!\n");
635 free(entity);
636 continue;
637 }
638 tfs_log(" | |--- Name: %s\n", entity->Path);
639 tfs_log(" | |--- Path: %s\n", entity->Name);
640 tfs_log(" | |--- (%d == 0 && %d == 0) || %d\n",
641 strcmp(entity->Path, "/") == 0,
642 strcmp(entity->Name, "/") == 0,
643 strcmp(entity->Path, Path) != 0
644 );
645 if ((strcmp(entity->Path, "/") == 0 && strcmp(entity->Name, "/") == 0) || strcmp(entity->Path, Path) != 0){
646 free(entity);
647 CZ++;
648 continue;
649 }
650 memset(&Files[count], 0, sizeof(FSM_FILE));
651 Files[count].Ready = 1;
652 Files[count].CHMOD = entity->CHMOD;
653 Files[count].Size = entity->Size;
654 memcpy(Files[count].Name, entity->Name, strlen(entity->Name));
655 memcpy(Files[count].Path, entity->Path, strlen(entity->Path));
656
657 if (entity->Type == TEMPFS_ENTITY_TYPE_FOLDER){
658 CD++;
659 Files[count].Type = 5;
660 } else {
661 CF++;
662 Files[count].Type = 0;
663 }
664 tfs_log(" | | |--- [>] File info\n");
665 tfs_log(" | | | |--- Name: %s\n", Files[count].Name);
666 tfs_log(" | | | |--- Path: %s\n", Files[count].Path);
667 tfs_log(" | | | |--- Size: %d\n", Files[count].Size);
668 tfs_log(" | | | |--- Type: %s\n", (Files[count].Type == 5?"Folder":"File"));
669 //tfs_log(" | | | |--- Date: %s\n", Files[count].LastTime);
670 tfs_log(" | | | |--- CHMOD: 0x%x\n", Files[count].CHMOD);
671
672 count++;
673 CZ++;
674 free(entity);
675 tfs_log(" | | |--- Next!\n");
676 }
677
678 Dir->Ready = 1;
679 Dir->Count = count;
680 Dir->CountFiles = CF;
681 Dir->CountDir = CD;
682 Dir->CountOther = 0;
683 Dir->Files = Files;
684
685 return Dir;
686 }
687
688 int fs_tempfs_create(const char Disk,const char* Path,int Mode){
689 tfs_log("[>] Creating a new entity\n");
690 size_t lenp = strlen(Path);
691 if (lenp <= 0 || lenp >= (Mode == 0?128:256)) return 0;
692 TEMPFS_ENTITY* entity = malloc(sizeof(TEMPFS_ENTITY));
693 memset(entity, 0, sizeof(TEMPFS_ENTITY));
694 int find_dir = fs_tempfs_func_findDIR(Disk, Path);
695 tfs_log(" |--- Folder search result: 0x%x\n", find_dir);
696 if (Mode == 0) {
697 tfs_log(" |--- Creating a file\n");
698
699 int find_file = fs_tempfs_func_findFILE(Disk, Path);
700 tfs_log(" |--- File search result: %d\n", find_file);
701 if (find_file == 1){
702 free(entity);
703 return 0;
704 }
705 tfs_log(" |--- Filling in metadata\n");
706 char* dir = pathinfo(Path, PATHINFO_DIRNAME);
707 char* basename = pathinfo(Path, PATHINFO_BASENAME);
708 memcpy(entity->Name, basename, strlen(basename));
709 memcpy(entity->Path, dir, strlen(dir));
710 entity->CHMOD |= TEMPFS_CHMOD_READ | TEMPFS_CHMOD_WRITE;
711 // entity->Date = 0;
712 entity->Size = 0;
713 entity->Status = TEMPFS_ENTITY_STATUS_READY;
714 entity->Type = TEMPFS_ENTITY_TYPE_FILE;
715 tfs_log(" |--- Next step\n");
716 } else {
717 tfs_log(" |--- Creating a folder\n");
718 tfs_log(" |--- %d | %d\n", (find_dir & TEMPFS_DIR_INFO_EXITS), !(find_dir & TEMPFS_DIR_INFO_ROOT));
719 if ((find_dir & TEMPFS_DIR_INFO_EXITS) || !(find_dir & TEMPFS_DIR_INFO_ROOT)){
720 free(entity);
721 return 0;
722 }
723 char* dir = pathinfo(Path, PATHINFO_DIRNAME);
724 char* basename = pathinfo(Path, PATHINFO_BASENAME);
725 memcpy(entity->Name, basename, strlen(basename));
726 memcpy(entity->Path, dir, strlen(dir));
727 entity->CHMOD |= TEMPFS_CHMOD_READ | TEMPFS_CHMOD_WRITE;
728 // entity->Date = 0;
729 entity->Size = 0;
730 entity->Status = TEMPFS_ENTITY_STATUS_READY;
731 entity->Type = TEMPFS_ENTITY_TYPE_FOLDER;
732 tfs_log(" |--- Next step\n");
733 }
734 tfs_log(" |--- Searching for a free block to record an entity\n");
735 int inx = fs_tempfs_func_findFreeInfoBlock(Disk);
736 if (inx == -1){
737 tfs_log(" |--- Couldn't find a free entity\n");
738 free(entity);
739 return 0;
740 }
741
742
743 tfs_log(" |--- Writing data to the device\n");
744 int wr_entity = fs_tempfs_func_writeEntity(Disk, inx, entity);
745 free(entity);
746 if (wr_entity != 1){
747 tfs_log(" |-- [WARN] There was a problem when writed data on the 0x%x section\n", 512 + (inx * sizeof(TEMPFS_ENTITY)));
748 return 0;
749 }
750 tfs_log(" |--- Updating boot sector\n");
751 TEMPFS_BOOT* boot = fs_tempfs_func_getBootInfo(Disk);
752 if (boot == NULL || fs_tempfs_func_checkSign(boot->Sign1, boot->Sign2) != 1) {
753 tfs_log(" |--- [ERR] TempFS signature did not match OR error reading TempFS boot sector\n");
754 return 0;
755 }
756 boot->CountFiles++;
758 int boot_write = fs_tempfs_func_updateBoot(Disk, boot);
759 if (boot_write != 1){
760 tfs_log(" |-- [ERR] An error occurred while writing the TempFS boot partition\n");
761 return 0;
762 }
763 tfs_log(" |--- Updating the cache\n");
764 fs_tempfs_func_cacheUpdate(Disk);
765 return 1;
766 }
767
768 int fs_tempfs_delete(const char Disk,const char* Path,int Mode){
769 TEMPFS_BOOT* boot = fs_tempfs_func_getBootInfo(Disk);
770 if (boot == NULL || fs_tempfs_func_checkSign(boot->Sign1, boot->Sign2) != 1) {
771 tfs_log(" |--- [ERR] TempFS signature did not match OR error reading TempFS boot sector\n");
772 return 0;
773 }
774
775 size_t indexFile = fs_tempfs_func_getIndexEntity(Disk, Path);
776 if (indexFile == -1){
777 tfs_log("Element %s no found to delete...\n", Path);
778 return 0;
779 }
780 TEMPFS_ENTITY* ent = malloc(sizeof(TEMPFS_ENTITY));
781 if (ent == NULL){
782 tfs_log("No malloc free entity...\n");
783 return 0;
784 }
785 memset(ent, 0, sizeof(TEMPFS_ENTITY));
786 TEMPFS_ENTITY* elem = fs_tempfs_func_readEntity(Disk, Path);
787 if (elem == NULL){
788 free(ent);
789 return 0;
790 }
791 if ((elem->CHMOD & TEMPFS_CHMOD_SYS) || !(elem->CHMOD & TEMPFS_CHMOD_WRITE)){
792 tfs_log("You don't have enough rights to delete this item!\n%s%s",((elem->CHMOD & TEMPFS_CHMOD_SYS)?"Reason: the file is a system file\n":""),(!(elem->CHMOD & TEMPFS_CHMOD_WRITE)?"Reason: no recording rights\n":""));
793 return 0;
794 }
795
796 if (Mode == 0) {
797 tfs_log(" |--- Delete a file\n");
798 size_t deleteblocks = fs_tempfs_func_clearBlocks(Disk, elem->Point);
799 tfs_log(" |--- Delete blocks: %d\n", deleteblocks);
800 boot->CountBlocks -= deleteblocks;
801 } else {
802 tfs_log(" |--- Delete a folder\n");
803 size_t foundElems = fs_tempfs_func_findFilesOnDIR(Disk, Path);
804 if (foundElems != 0){
805 tfs_log(" |--- To delete a folder, you need to delete %d more items inside it.\n", foundElems);
806 free(ent);
807 free(elem);
808 return 0;
809 }
810 }
811 int wr_entity = fs_tempfs_func_writeEntity(Disk, indexFile, ent);
812 tfs_log(" |--- Delete metadata file (%d)...", indexFile);
813 free(ent);
814 free(elem);
815
816 boot->CountFiles--;
818 int boot_write = fs_tempfs_func_updateBoot(Disk, boot);
819 if (boot_write != 1){
820 tfs_log(" |-- [ERR] An error occurred while writing the TempFS boot partition\n");
821 return 0;
822 }
823 tfs_log(" |--- Updating the cache\n");
824
825 fs_tempfs_func_cacheUpdate(Disk);
826 return 1;
827 }
828
829 void fs_tempfs_label(const char Disk, char* Label){
830 TEMPFS_BOOT* boot = fs_tempfs_func_getBootInfo(Disk);
831 if (boot == NULL || fs_tempfs_func_checkSign(boot->Sign1, boot->Sign2) != 1) {
832 tfs_log(" |--- [ERR] TempFS signature did not match OR error reading TempFS boot sector\n");
833 return;
834 }
835 memcpy(Label,boot->Label,strlen(boot->Label));
836 }
837
838 int fs_tempfs_detect(const char Disk){
839 tfs_log("\n[>] Attempt to check the boot sector\n");
840 int ret = fs_tempfs_tcache_update(Disk);
841 if (ret != 0x7246){
842 tfs_log(" |--- [>] Return code: 0x%x\n", ret);
843 return 0;
844 }
845 return 1;
846 }
847
848 void fs_tempfs_format(const char Disk){
849 tfs_log("\n[>] Formatting for TempFS has started...\n");
851 TEMPFS_BOOT* boot = malloc(sizeof(TEMPFS_BOOT));
852 TEMPFS_ENTITY* tmp = malloc(sizeof(TEMPFS_ENTITY));
854 memset(boot, 0, sizeof(TEMPFS_BOOT));
855 memset(tmp, 0, sizeof(TEMPFS_ENTITY));
856
858 boot->Sign1 = 0x7246;
859 boot->Sign2 = 0x2519;
860 memcpy(boot->Label,"New disk",strlen("New disk"));
861 boot->EndDisk = TMF_GETDISKSIZE(Disk);
862 boot->CountBlocks = 0;
863 boot->CountFiles = 1;
864
866 int write = fs_tempfs_func_updateBoot(Disk, boot);
867 if (write != 1){
868 tfs_log(" |-- [ERR] An error occurred while writing the TempFS boot partition\n");
869 return;
870 }
871
873 size_t all_free_disk = (boot->EndDisk) - (sizeof(TEMPFS_BOOT)) - 1;
874 if (all_free_disk <= 0){
875 tfs_log(" |-- [ERR] The file system requires a minimum of 1024 bytes of memory\n");
876 tfs_log(" |-- %d = (%d - %d - 1)\n", all_free_disk, boot->EndDisk, (sizeof(TEMPFS_BOOT)));
877 tfs_log(" |-- INTERRUPTED!!");
878 return;
879 }
881 size_t all_blocks = (all_free_disk / sizeof(TEMPFS_ENTITY)) - 1;
882 if (all_blocks <= 0){
883 tfs_log(" |-- [WARN] There are no free blocks left for file system elements!\n");
884 all_blocks = 0;
885 }
886
888 for (size_t abx = 0; abx < all_blocks; abx++){
889 // tfs_log(" |-- [>] [%d | %d] Clearing the hard drive of old data\n",abx + 1,all_blocks);
890 int wr_entity = fs_tempfs_func_writeEntity(Disk, abx, tmp);
891 if (wr_entity != 1){
892 tfs_log(" |-- [WARN] There was a problem when erasing data on the 0x%x section\n", 512 + (abx * sizeof(TEMPFS_ENTITY)));
893 }
894 }
895
897 tmp->CHMOD |= TEMPFS_CHMOD_READ | TEMPFS_CHMOD_WRITE | TEMPFS_CHMOD_SYS;
898 tmp->Status = TEMPFS_ENTITY_STATUS_READY;
899 tmp->Type = TEMPFS_ENTITY_TYPE_FOLDER;
900 memcpy(tmp->Name, "/", strlen("/"));
901 memcpy(tmp->Path, "/", strlen("/"));
902 memcpy(tmp->Owner, "root", strlen("root"));
903
904 int root_write = fs_tempfs_func_writeEntity(Disk, 0, tmp);
905 if (root_write != 1){
906 tfs_log(" |-- [WARN] Failed to write the root directory\n");
907 }
908 tfs_log(" |-- Disk formatting is complete!\n");
909 tfs_log(" |-- Label: %s\n", boot->Label);
910 tfs_log(" |-- Free space: %d\n", all_free_disk);
911 tfs_log(" |-- Free blocks: %d\n", all_blocks);
912
914 free(tmp);
915 free(boot);
916 }
dpm_write
size_t dpm_write(char Letter, uint64_t high_offset, uint64_t low_offset, size_t Size, char *Buffer)
[DPM] Запись данных на диск
Definition: dpm.c:123
dpm_read
size_t dpm_read(char Letter, uint64_t high_offset, uint64_t low_offset, size_t Size, void *Buffer)
[DPM] Считывание данных с диска
Definition: dpm.c:82
is_file
bool is_file(const char *Path)
[FileIO] Проверяет существует ли сущность и является ли она файлом
Definition: fileio.c:24
strlen
size_t strlen(const char *str)
Возращает длину строки
Definition: string.c:88
strcmp
int strcmp(const char *s1, const char *s2)
Сравнение строк
Definition: string.c:253
memset
void * memset(void *ptr, char value, size_t num)
Заполнение массива указанными символами
Definition: string.c:203
memcpy
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.
Definition: string.c:173
TEMPFS_BOOT
Definition: tempfs.h:39
TEMPFS_BOOT::Sign1
uint16_t Sign1
Сигнатура 1.
Definition: tempfs.h:40
TEMPFS_BOOT::Sign2
uint16_t Sign2
Сигнатура 2.
Definition: tempfs.h:41
TEMPFS_BOOT::CountFiles
uint32_t CountFiles
Количество файлов
Definition: tempfs.h:43
TEMPFS_BOOT::Label
char Label[32]
Метка диска
Definition: tempfs.h:42
TEMPFS_BOOT::EndDisk
uint32_t EndDisk
Точка конца диска
Definition: tempfs.h:44
TEMPFS_BOOT::CountBlocks
uint32_t CountBlocks
Количество используемых блоков
Definition: tempfs.h:45
TEMPFS_Cache
Definition: tempfs.h:57
TEMPFS_Cache::Boot
TEMPFS_BOOT * Boot
Ссылка на Boot.
Definition: tempfs.h:59
TEMPFS_Cache::FreeAll
size_t FreeAll
Свободное количество блоков
Definition: tempfs.h:63
TEMPFS_Cache::Status
uint8_t Status
Статус
Definition: tempfs.h:58
TEMPFS_Cache::Files
TEMPFS_ENTITY * Files
Ссылка на Файловые поинты
Definition: tempfs.h:61
TEMPFS_Cache::BlocksAll
size_t BlocksAll
Максимальное количество блоков информации
Definition: tempfs.h:62
TEMPFS_Cache::CountFiles
size_t CountFiles
Количество файлов
Definition: tempfs.h:60
TEMPFS_ENTITY
Definition: tempfs.h:27
TEMPFS_ENTITY::Path
char Path[412]
Путь файла | папки
Definition: tempfs.h:30
TEMPFS_ENTITY::Size
uint32_t Size
Размер файла в байтах (только для файлов)
Definition: tempfs.h:31
TEMPFS_ENTITY::CHMOD
uint8_t CHMOD
Права доступа
Definition: tempfs.h:36
TEMPFS_ENTITY::Owner
char Owner[16]
Владелец файла | папки
Definition: tempfs.h:34
TEMPFS_ENTITY::Status
uint8_t Status
Статус
Definition: tempfs.h:28
TEMPFS_ENTITY::Name
char Name[64]
Имя файла | папки
Definition: tempfs.h:29
TEMPFS_ENTITY::Point
uint32_t Point
Точка входа в файл | папки
Definition: tempfs.h:32
TEMPFS_ENTITY::Type
uint8_t Type
Тип файл или папки
Definition: tempfs.h:35
TEMPFS_PACKAGE
Definition: tempfs.h:49
TEMPFS_PACKAGE::Next
uint32_t Next
Следующий пакет данных
Definition: tempfs.h:52
TEMPFS_PACKAGE::Status
uint8_t Status
Статус пакета
Definition: tempfs.h:50
TEMPFS_PACKAGE::Length
uint16_t Length
Длина пакета
Definition: tempfs.h:51
TEMPFS_PACKAGE::Data
char Data[500]
Пакет данных
Definition: tempfs.h:53