-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
807 lines (664 loc) · 21.9 KB
/
main.c
File metadata and controls
807 lines (664 loc) · 21.9 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "fs.h"
#define MAX_COMMAND_LEN 255
#define MAX_COMMAND_ARGS 10
#define MAX_DIR_ENTRIES 255
#define COLOR_GREEN "\x1b[92m"
#define COLOR_CYAN "\x1b[96m"
#define COLOR_RESET "\x1b[0m"
#define HANDLE_FS_ERROR(x) do { int result = x; if (result != FS_OK) { print_fs_error(result); return; } } while(0);
#define TMP_FILENAME "tmp"
const char* filename;
fs_disk_operations_t operations;
fs_t fs;
char cmd[MAX_COMMAND_LEN];
char* args[MAX_COMMAND_ARGS];
fs_dir_entry_t entries[MAX_DIR_ENTRIES];
char current_dir[FS_PATH_MAX_LENGTH];
void init(int argc, char** argv);
int loop();
void cleanup();
void cmd_cp(const char* source, const char* destination);
void cmd_mv(const char* source, const char* destination);
void cmd_mkdir(const char* path);
void cmd_touch(const char* path);
void cmd_ln(const char* destination, const char* link_name);
void cmd_rm(const char* path);
void cmd_import(const char* real_source, const char* destination);
void cmd_export(const char* source, const char* real_destination);
void cmd_edit(const char* path);
void cmd_cat(const char* path);
void cmd_ls(const char* path, int show_details, int show_size);
void cmd_cd(const char* path);
void cmd_pwd();
void cmd_exp(const char* path, size_t count);
void cmd_trunc(const char* path, size_t count);
void cmd_fsinfo();
void cmd_help();
size_t parse_input(char* input, char** output, size_t max_outputs);
void print_fs_error(int fs_error_code);
void absolute_path(const char* path, char* result);
int real_init_create(void ** result_state);
int real_init_open(void ** result_state);
int real_read(void* state, void* buffer, size_t position, size_t size);
int real_write(void* state, const void* buffer, size_t position, size_t size);
int real_close(void* state);
int main(int argc, char** argv)
{
init(argc, argv);
while (loop());
cleanup();
return 0;
}
void init(int argc, char** argv)
{
if (argc < 2)
{
puts(COLOR_RESET"Usage: ");
puts("Open existing: ./fs file_name");
puts("Create new: ./fs file_name size_in_bytes");
exit(-1);
}
filename = argv[1];
operations.read = &real_read;
operations.write = &real_write;
operations.close = &real_close;
printf(COLOR_GREEN);
if (argc >= 3)
{
operations.init = &real_init_create;
if (fs_create(&operations, atoi(argv[2]), &fs) != FS_OK)
{
puts("Error occurred while creating file system.");
exit(-1);
}
puts("File system successfully created.");
}
else
{
operations.init = &real_init_open;
if (fs_open(&operations, &fs) != FS_OK)
{
puts("Error occured while opening file system.");
exit(-1);
}
puts("File system successfully opened.");
}
strcpy(current_dir, "/");
puts("Type help to get more information");
}
int loop()
{
printf(COLOR_RESET);
printf(COLOR_CYAN"%s"COLOR_RESET"$ ", current_dir);
fgets(cmd, MAX_COMMAND_LEN, stdin);
size_t args_count = parse_input(cmd, args, MAX_COMMAND_ARGS);
if (args_count == 0) return 1;
printf(COLOR_GREEN);
if (args[0][0] == 0)
{
return 1;
}
else if (strcmp(args[0], "cp") == 0)
{
if (args_count < 3)
{
puts("cp requires 2 arguments");
return 1;
}
cmd_cp(args[1], args[2]);
}
else if (strcmp(args[0], "mv") == 0)
{
if (args_count < 3)
{
puts("mv requires 2 arguments");
return 1;
}
cmd_mv(args[1], args[2]);
}
else if (strcmp(args[0], "mkdir") == 0)
{
if (args_count < 2)
{
puts("mkdir requires 1 argument");
return 1;
}
cmd_mkdir(args[1]);
}
else if (strcmp(args[0], "touch") == 0)
{
if (args_count < 2)
{
puts("touch required 1 argument");
return 1;
}
cmd_touch(args[1]);
}
else if (strcmp(args[0], "ln") == 0)
{
if (args_count < 3)
{
puts("ln required 2 arguments");
return 1;
}
cmd_ln(args[1], args[2]);
}
else if (strcmp(args[0], "rm") == 0)
{
if (args_count < 2)
{
puts("rm required 1 argument");
return 1;
}
cmd_rm(args[1]);
}
else if (strcmp(args[0], "import") == 0)
{
if (args_count < 3)
{
puts("import requires 2 arguments");
return 1;
}
cmd_import(args[1], args[2]);
}
else if (strcmp(args[0], "export") == 0)
{
if (args_count < 3)
{
puts("export requires 2 arguments");
return 1;
}
cmd_export(args[1], args[2]);
}
else if (strcmp(args[0], "edit") == 0)
{
if (args_count < 2)
{
puts("edit requires 1 argument");
return 1;
}
cmd_edit(args[1]);
}
else if (strcmp(args[0], "cat") == 0)
{
if (args_count < 2)
{
puts("cat requires 1 argument");
return 1;
}
cmd_cat(args[1]);
}
else if (strcmp(args[0], "exp") == 0)
{
if (args_count < 3)
{
puts("exp requires 2 arguments");
return 1;
}
cmd_exp(args[1], atoi(args[2]));
}
else if (strcmp(args[0], "trunc") == 0)
{
if (args_count < 3)
{
puts("trunc requires 2 arguments");
return 1;
}
cmd_trunc(args[1], atoi(args[2]));
}
else if (strcmp(args[0], "cd") == 0)
{
if (args_count < 2)
{
puts("cd requires 1 argument");
return 1;
}
cmd_cd(args[1]);
}
else if (strcmp(args[0], "ls") == 0)
{
char* path_arg = NULL;
int details = 0;
int size = 0;
for (size_t i = 1; i < args_count; i++)
{
if (args[i][0] == '-')
{
if (strchr(args[i], 'd') != NULL) details = 1;
if (strchr(args[i], 's') != NULL) size = 1;
}
else
{
if (path_arg != NULL)
{
puts("Too many arguments specified");
}
else
{
path_arg = args[i];
}
}
}
if (path_arg == NULL) path_arg = current_dir;
cmd_ls(path_arg, details, size);
}
else if (strcmp(args[0], "pwd") == 0)
{
cmd_pwd();
}
else if (strcmp(args[0], "fsinfo") == 0)
{
cmd_fsinfo();
}
else if (strcmp(args[0], "help") == 0)
{
cmd_help();
}
else if (strcmp(args[0], "exit") == 0)
{
return 0;
}
else
{
puts("Unknown command. Type help to get more information.");
}
return 1;
}
void cleanup()
{
printf(COLOR_GREEN);
if (fs_close(&fs) != FS_OK)
{
puts("Error occured while closing file system.");
exit(-1);
}
puts("File system successfully closed.");
}
void cmd_cp(const char* source, const char* destination)
{
char src_path[FS_PATH_MAX_LENGTH];
char dst_path[FS_PATH_MAX_LENGTH];
absolute_path(source, src_path);
absolute_path(destination, dst_path);
fs_file_t file;
HANDLE_FS_ERROR(fs_file_open(&fs, src_path, 0, &file));
fs_file_t dst_file;
HANDLE_FS_ERROR(fs_file_open(&fs, dst_path, FS_CREATE, &dst_file));
size_t read;
size_t written;
char buffer[256];
int err = fs_file_read(&fs, &file, buffer, 256, &read);
while (err != FS_EOF)
{
if (err != FS_OK)
{
print_fs_error(err);
return;
}
HANDLE_FS_ERROR(fs_file_write(&fs, &dst_file, buffer, read, &written));
err = fs_file_read(&fs, &file, buffer, 256, &read);
}
HANDLE_FS_ERROR(fs_file_close(&fs, &file));
HANDLE_FS_ERROR(fs_file_close(&fs, &dst_file));
}
void cmd_mv(const char* source, const char* destination)
{
char src_path[FS_PATH_MAX_LENGTH];
char dst_path[FS_PATH_MAX_LENGTH];
absolute_path(source, src_path);
absolute_path(destination, dst_path);
HANDLE_FS_ERROR(fs_entry_info(&fs, src_path, &entries[0]));
HANDLE_FS_ERROR(fs_link(&fs, dst_path, entries[0].node));
HANDLE_FS_ERROR(fs_remove(&fs, src_path));
}
void cmd_mkdir(const char* path)
{
char final_path[FS_PATH_MAX_LENGTH];
absolute_path(path, final_path);
HANDLE_FS_ERROR(fs_mkdir(&fs, final_path));
}
void cmd_touch(const char* path)
{
char final_path[FS_PATH_MAX_LENGTH];
absolute_path(path, final_path);
fs_file_t file;
int error = fs_file_open(&fs, final_path, 0, &file);
if (error == FS_NOT_EXISTS)
{
HANDLE_FS_ERROR(fs_file_open(&fs, final_path, FS_CREATE, &file));
}
else if (error != FS_OK)
{
HANDLE_FS_ERROR(error);
}
HANDLE_FS_ERROR(fs_file_close(&fs, &file));
}
void cmd_ln(const char* destination, const char* link_name)
{
char dst_path[FS_PATH_MAX_LENGTH];
char link[FS_PATH_MAX_LENGTH];
absolute_path(destination, dst_path);
absolute_path(link_name, link);
HANDLE_FS_ERROR(fs_entry_info(&fs, dst_path, &entries[0]));
HANDLE_FS_ERROR(fs_link(&fs, link, entries[0].node));
}
void cmd_rm(const char* path)
{
char final_path[FS_PATH_MAX_LENGTH];
absolute_path(path, final_path);
HANDLE_FS_ERROR(fs_remove(&fs, final_path));
}
void cmd_import(const char* real_source, const char* destination)
{
char dst_path[FS_PATH_MAX_LENGTH];
absolute_path(destination, dst_path);
FILE* real_file = fopen(real_source, "r");
if (real_file == NULL)
{
printf("Cannot open external file %s\n", real_source);
return;
}
fs_file_t file;
HANDLE_FS_ERROR(fs_file_open(&fs, dst_path, FS_CREATE, &file));
char buffer[256];
size_t read;
size_t written;
while (read = fread(buffer, 1, 256, real_file))
{
HANDLE_FS_ERROR(fs_file_write(&fs, &file, buffer, read, &written));
}
fclose(real_file);
HANDLE_FS_ERROR(fs_file_close(&fs, &file));
}
void cmd_export(const char* source, const char* real_destination)
{
char src_path[FS_PATH_MAX_LENGTH];
absolute_path(source, src_path);
fs_file_t file;
HANDLE_FS_ERROR(fs_file_open(&fs, src_path, 0, &file));
FILE* real_file = fopen(real_destination, "w+");
if (real_file == NULL)
{
printf("Cannot open external file %s\n", real_destination);
return;
}
char buffer[256];
size_t read;
int err = fs_file_read(&fs, &file, buffer, 256, &read);
while (err != FS_EOF)
{
fwrite(buffer, 1, read, real_file);
err = fs_file_read(&fs, &file, buffer, 256, &read);
if (err != FS_EOF && err != FS_OK)
{
print_fs_error(err);
return;
}
}
fclose(real_file);
HANDLE_FS_ERROR(fs_file_close(&fs, &file));
}
void cmd_edit(const char* path)
{
char full_path[FS_PATH_MAX_LENGTH];
absolute_path(path, full_path);
fs_dir_entry_t result;
int err = fs_entry_info(&fs, full_path, &result);
if (err == FS_OK)
{
cmd_export(path, TMP_FILENAME);
}
else if (err != FS_NOT_EXISTS)
{
print_fs_error(err);
return;
}
system("vim " TMP_FILENAME);
cmd_import(TMP_FILENAME, path);
remove(TMP_FILENAME);
}
void cmd_cat(const char* path)
{
char full_path[FS_PATH_MAX_LENGTH];
absolute_path(path, full_path);
fs_file_t file;
HANDLE_FS_ERROR(fs_file_open(&fs, full_path, 0, &file));
char buffer[256];
size_t read;
int err = fs_file_read(&fs, &file, buffer, 256, &read);
while (err != FS_EOF)
{
if (err != FS_OK)
{
print_fs_error(err);
return;
}
for (size_t i = 0; i < read; i++) putchar(buffer[i]);
err = fs_file_read(&fs, &file, buffer, 256, &read);
}
HANDLE_FS_ERROR(fs_file_close(&fs, &file));
putchar('\n');
}
void cmd_ls(const char* path, int show_details, int show_size)
{
char full_path[FS_PATH_MAX_LENGTH];
absolute_path(path, full_path);
size_t count;
HANDLE_FS_ERROR(fs_dir_list(&fs, full_path, entries, &count, MAX_DIR_ENTRIES));
for (size_t i = 0; i < count; i++)
{
printf("%-4s ", entries[i].node_type == FS_FILE ? "FILE" : "DIR");
if (show_details)
{
time_t raw_time = (time_t)entries[i].node_modification_time;
struct tm* time = localtime(&raw_time);
char tbuffer[20];
strftime(tbuffer, 20, "%Y-%m-%d %H:%M:%S", time);
printf("0x%08X %2d %s ", entries[i].node, entries[i].node_links_count, tbuffer);
}
printf(" %-27s", entries[i].name);
if (show_size && strcmp(entries[i].name, "..") != 0)
{
uint32_t size;
HANDLE_FS_ERROR(fs_size(&fs, entries[i].node, &size));
printf(" %d B", size);
}
putchar('\n');
}
}
void cmd_cd(const char* path)
{
char tmp_path[255];
strcpy(tmp_path, path);
char* tmp_tokens[10];
size_t tokens_count = 0;
char* token = strtok(tmp_path, "/");
while (token != NULL)
{
tmp_tokens[tokens_count++] = token;
token = strtok(NULL, "/");
}
char tmp_current[255];
strcpy(tmp_current, current_dir);
for (int i = 0; i < tokens_count; i++)
{
token = tmp_tokens[i];
if (strcmp(token, "..") == 0)
{
size_t curr_len = strlen(tmp_current);
if (curr_len > 1)
{
*(tmp_current + curr_len - 1) = 0;
char* last_slash = strrchr(tmp_current, '/');
if (last_slash != NULL)
{
*(last_slash + 1) = 0;
}
}
}
else if (strcmp(token, ".") != 0)
{
size_t curr_len = strlen(tmp_current);
strcpy(tmp_current + curr_len, token);
curr_len = strlen(tmp_current);
*(tmp_current + curr_len) = '/';
*(tmp_current + curr_len + 1) = 0;
HANDLE_FS_ERROR(fs_entry_info(&fs, tmp_current, &entries[0]));
}
}
strcpy(current_dir, tmp_current);
}
void cmd_pwd()
{
puts(current_dir);
}
void cmd_exp(const char* path, size_t count)
{
char full_path[FS_PATH_MAX_LENGTH];
absolute_path(path, full_path);
fs_file_t file;
HANDLE_FS_ERROR(fs_file_open(&fs, full_path, FS_APPEND, &file));
char buffer[256];
memset(buffer, 0xFF, 256);
size_t written;
while (count)
{
size_t c = count;
if (c > 256) c = 256;
HANDLE_FS_ERROR(fs_file_write(&fs, &file, buffer, c, &written));
count -= written;
}
HANDLE_FS_ERROR(fs_file_close(&fs, &file));
}
void cmd_trunc(const char* path, size_t count)
{
char full_path[FS_PATH_MAX_LENGTH];
absolute_path(path, full_path);
fs_file_t file;
HANDLE_FS_ERROR(fs_file_open(&fs, full_path, 0, &file));
HANDLE_FS_ERROR(fs_file_seek(&fs, &file, FS_SEEK_END, count));
HANDLE_FS_ERROR(fs_file_discard(&fs, &file));
HANDLE_FS_ERROR(fs_file_close(&fs, &file));
}
void cmd_fsinfo()
{
fs_info_t info;
HANDLE_FS_ERROR(fs_info(&fs, &info));
printf("Sector size: %d\n", FS_SECTOR_SIZE);
printf("Sectors (total / boot / allocation table): %d / %d / %d\n", info.sectors, 1, info.table_sectors);
printf("Clusters (total / free / node / data): %d / %d / %d / %d\n", info.clusters, info.free_clusters, info.node_clusters, info.data_clusters);
printf("Nodes (used / allocated): %d / %d\n", info.nodes, info.allocated_nodes);
printf("File system size (total / usable): %d B / %d B\n", info.total_size, info.usable_space);
printf("Size (files / directory structures / nodes): %d B / %d B / %d B\n", info.files_size, info.dir_structures_size, info.nodes_size);
printf("Usage: %d / %d B\n", info.used_space, info.usable_space);
}
void cmd_help()
{
puts(COLOR_CYAN"cp source destination"COLOR_GREEN" - Copies file from source to destination.");
puts(COLOR_CYAN"mv source destination"COLOR_GREEN" - Moves file from soruce to destination.");
puts(COLOR_CYAN"mkdir path"COLOR_GREEN" - Creates directory. Allows nested directories.");
puts(COLOR_CYAN"touch path"COLOR_GREEN" - Creates empty file.");
puts(COLOR_CYAN"ln file_path link_name"COLOR_GREEN" - Creates hard link of link_name to file_path.");
puts(COLOR_CYAN"rm path"COLOR_GREEN" - Removes file or directory recursively.");
puts(COLOR_CYAN"import real_source destination"COLOR_GREEN" - Imports external file into file system.");
puts(COLOR_CYAN"export source real_destination"COLOR_GREEN" - Exports file from file system.");
puts(COLOR_CYAN"edit file"COLOR_GREEN" - Enters edit mode for specified file.");
puts(COLOR_CYAN"cat file"COLOR_GREEN" - Prints content of specified file");
puts(COLOR_CYAN"ls [path] [-ds]"COLOR_GREEN" - Lists specified directory. If path not specified then current directory is used. Flag -d - show detailed information (node index, links count, modification time). Flag -s - show size of the files and directories.");
puts(COLOR_CYAN"cd dir"COLOR_GREEN" - Change current directory.");
puts(COLOR_CYAN"pwd"COLOR_GREEN" - Prints path of current directory.");
puts(COLOR_CYAN"exp file bytes"COLOR_GREEN" - Expands file by specified amount of bytes");
puts(COLOR_CYAN"trunc file bytes"COLOR_GREEN" - Truncates file by specified amount of bytes");
puts(COLOR_CYAN"fsinfo"COLOR_GREEN" - Displays info about file system");
puts(COLOR_CYAN"exit"COLOR_GREEN" - Closes file system and exists application.");
puts(COLOR_CYAN"help"COLOR_GREEN" - Displays help.");
}
size_t parse_input(char* input, char** output, size_t max_outputs)
{
size_t len = strlen(input);
if (input[len - 1] == '\n') input[len - 1] = 0;
size_t current = 0;
char* token = strtok(input, " ");
while (token != NULL && current < max_outputs)
{
output[current++] = token;
token = strtok(NULL, " ");
}
return current;
}
void print_fs_error(int fs_error_code)
{
switch (fs_error_code)
{
case FS_OK: puts("Ok"); break;
case FS_DISK_INIT_ERROR: puts("An error occurred while initializing disk"); break;
case FS_DISK_READ_ERROR: puts("An error occured while reading from the disk"); break;
case FS_DISK_WRITE_ERROR: puts("An error occurred while writing to the disk"); break;
case FS_DISK_CLOSE_ERROR: puts("An errror occured while closing the disk"); break;
case FS_FULL: puts("File system is full"); break;
case FS_NOT_A_DIRECTORY: puts("Not a directory"); break;
case FS_WRONG_PATH: puts("Wrong path specified"); break;
case FS_PATH_TOO_LONG: puts("Path is too long"); break;
case FS_NAME_TOO_LONG: puts("Name is too long"); break;
case FS_BUFFER_TOO_SMALL: puts("Buffer is too small to handle result"); break;
case FS_NOT_A_FILE: puts("Not a file"); break;
case FS_NOT_EXISTS: puts("Not exists"); break;
case FS_FILE_CLOSED: puts("File is closed"); break;
case FS_EOF: puts("End of file"); break;
case FS_ALREADY_EXISTS: puts("Already exists"); break;
}
}
void absolute_path(const char* path, char* result)
{
if (path[0] != '/')
{
strcpy(result, current_dir);
size_t len = strlen(result);
strcpy(result + len, path);
}
else
{
strcpy(result, path);
}
}
int real_init_create(void** result_state)
{
FILE* file = fopen(filename, "w+");
if (file == NULL) return FS_DISK_INIT_ERROR;
*result_state = file;
return FS_OK;
}
int real_init_open(void** result_state)
{
FILE* file = fopen(filename, "r+");
if (file == NULL) return FS_DISK_INIT_ERROR;
*result_state = file;
return FS_OK;
}
int real_read(void* state, void* buffer, size_t position, size_t size)
{
FILE* file = (FILE*)state;
fseek(file, position, SEEK_SET);
size_t read = fread(buffer, 1, size, file);
if (read != size) return FS_DISK_READ_ERROR;
return FS_OK;
}
int real_write(void* state, const void* buffer, size_t position, size_t size)
{
FILE* file = (FILE*)state;
fseek(file, position, SEEK_SET);
size_t written = fwrite(buffer, 1, size, file);
if (written != size) return FS_DISK_WRITE_ERROR;
return FS_OK;
}
int real_close(void* state)
{
FILE* file = (FILE*)state;
int result = fclose(file);
if (result != 0) return FS_DISK_CLOSE_ERROR;
return FS_OK;
}