Skip to content

Commit 8b2c206

Browse files
Cleaned up code added VFS open flags and >. next up >> will come.
1 parent 3a8984c commit 8b2c206

File tree

9 files changed

+221
-61
lines changed

9 files changed

+221
-61
lines changed

disk.img

0 Bytes
Binary file not shown.

source/includes/filesystems/vfs.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@
1818
typedef struct {
1919
mount_entry_t* mnt;
2020
fat16_file_t f;
21+
int flags;
2122
} vfs_file_t;
2223

2324
typedef struct {
2425
mount_entry_t* mnt;
2526
const char* rel_path;
2627
} vfs_mount_res_t;
2728

29+
/* Open flags */
30+
#define VFS_O_RDONLY 0x0001
31+
#define VFS_O_WRONLY 0x0002
32+
#define VFS_O_RDWR 0x0003
33+
34+
#define VFS_O_CREAT 0x0100
35+
#define VFS_O_TRUNC 0x0200
36+
#define VFS_O_APPEND 0x0400
37+
2838

2939
// Current working directory
3040
extern char vfs_cwd[256];
@@ -35,7 +45,7 @@ extern char vfs_cwd[256];
3545
* @param out_file Pointer to vfs_file_t to receive file handle
3646
* @return 0 on success, negative on error
3747
*/
38-
int vfs_open(const char* path, vfs_file_t* out_file);
48+
int vfs_open(const char* path, int flags, vfs_file_t* out_file);
3949

4050
/**
4151
* @brief Read from an open file

source/includes/sh_util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <filesystems/fwrfs.h>
2626
#include <fdlfcn.h>
2727
#include <commands/commands.h>
28+
#include <stream.h>
2829

2930
#define BUFFER_SIZE 128
3031
#define MAX_COMMAND_LINE 1024
@@ -46,6 +47,14 @@ typedef struct
4647
size_t count;
4748
} command_list;
4849

50+
typedef struct {
51+
int redirect_stdout;
52+
int redirect_stderr;
53+
int append;
54+
const char* filename;
55+
} redir_t;
56+
57+
4958
/**
5059
* @brief Wrapper to store properly the function commands list.
5160
*

source/kernel/C/filesystems/fat16.c

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -475,17 +475,19 @@ int fat16_read(fat16_file_t* f, uint8_t* out, uint32_t size) {
475475
return read;
476476
}
477477

478-
int fat16_write(fat16_file_t* f, const uint8_t* data, uint32_t size) {
478+
int fat16_write(fat16_file_t* f, const uint8_t* data, uint32_t size)
479+
{
479480
uint32_t written = 0;
480481
uint8_t sector[512];
481482

482483
uint32_t cluster_size =
483484
f->fs->bs.sectors_per_cluster * 512;
484485

485-
/* ---------- allocate first cluster if empty ---------- */
486+
/* ---------- allocate first cluster ---------- */
486487
if (f->entry.first_cluster == 0) {
487488
uint16_t c = fat16_allocate_cluster(f->fs);
488-
if (!c) return FAT_OK;
489+
if (!c)
490+
return written;
489491

490492
fat16_write_fat_entry(f->fs, c, FAT16_EOC);
491493
f->entry.first_cluster = c;
@@ -502,12 +504,16 @@ int fat16_write(fat16_file_t* f, const uint8_t* data, uint32_t size) {
502504
/* ---------- walk / extend FAT chain ---------- */
503505
for (uint32_t i = 0; i < cluster_index; i++) {
504506
uint16_t next = fat16_read_fat_fs(f->fs, cluster);
507+
505508
if (next >= FAT16_EOC) {
506509
next = fat16_allocate_cluster(f->fs);
507-
if (!next) return written;
510+
if (!next)
511+
return written;
512+
508513
fat16_write_fat_entry(f->fs, cluster, next);
509514
fat16_write_fat_entry(f->fs, next, FAT16_EOC);
510515
}
516+
511517
cluster = next;
512518
}
513519

@@ -528,8 +534,7 @@ int fat16_write(fat16_file_t* f, const uint8_t* data, uint32_t size) {
528534
written += to_copy;
529535
}
530536

531-
if (f->pos > f->entry.filesize)
532-
f->entry.filesize = f->pos;
537+
f->entry.filesize = f->pos;
533538

534539
/* ---------- update directory entry ---------- */
535540
if (f->parent_cluster == 0)
@@ -554,14 +559,16 @@ uint16_t fat16_find_free_cluster(fat16_fs_t* fs) {
554559
uint16_t* fat = (uint16_t*)sector;
555560
for (int i = 0; i < 256; i++) {
556561
uint16_t cluster = s * 256 + i;
557-
if (cluster < 2) continue;
558562

559-
if (fat[i] == 0x0000) {
563+
if (cluster < 2)
564+
continue;
565+
566+
if (fat[i] == 0x0000)
560567
return cluster;
561-
}
562568
}
563569
}
564-
return FAT_OK; // no space
570+
571+
return 0; // ✅ NO SPACE
565572
}
566573

567574
void fat16_write_fat_entry(fat16_fs_t* fs, uint16_t cluster, uint16_t value) {
@@ -579,17 +586,16 @@ void fat16_write_fat_entry(fat16_fs_t* fs, uint16_t cluster, uint16_t value) {
579586

580587
uint16_t fat16_allocate_cluster(fat16_fs_t* fs) {
581588
uint16_t cluster = fat16_find_free_cluster(fs);
582-
if (!cluster)
583-
return FAT_OK;
589+
if (cluster == 0)
590+
return 0;
584591

585592
fat16_write_fat_entry(fs, cluster, FAT16_EOC);
586593

587594
uint8_t zero[512] = {0};
588595
uint32_t lba = fat16_cluster_lba(fs, cluster);
589596

590-
for (uint32_t s = 0; s < fs->bs.sectors_per_cluster; s++) {
597+
for (uint32_t s = 0; s < fs->bs.sectors_per_cluster; s++)
591598
ahci_write_sector(fs->portno, lba + s, zero, 1);
592-
}
593599

594600
return cluster;
595601
}
@@ -835,24 +841,27 @@ int fat16_unlink(fat16_fs_t* fs, uint16_t parent_cluster, const char* name) {
835841
return fat16_delete_entry_in_cluster(fs, parent_cluster, fatname);
836842
}
837843

838-
int fat16_truncate(fat16_file_t* f, uint32_t new_size) {
839-
if (new_size >= f->entry.filesize)
840-
return FAT_OK;
841-
844+
int fat16_truncate(fat16_file_t* f, uint32_t new_size)
845+
{
842846
uint32_t cluster_size =
843847
f->fs->bs.sectors_per_cluster * 512;
844848

845-
/* ----- truncate to zero ----- */
849+
/* ---------- truncate to zero ---------- */
846850
if (new_size == 0) {
847851
if (f->entry.first_cluster >= 2)
848852
fat16_free_chain(f->fs, f->entry.first_cluster);
849853

850854
f->entry.first_cluster = 0;
851855
f->entry.filesize = 0;
852-
853-
goto update;
856+
f->pos = 0;
857+
f->cluster = 0;
858+
return FAT_OK;
854859
}
855860

861+
/* ---------- no-op grow ---------- */
862+
if (new_size >= f->entry.filesize)
863+
return FAT_OK;
864+
856865
uint32_t keep_clusters =
857866
(new_size + cluster_size - 1) / cluster_size;
858867

@@ -871,16 +880,13 @@ int fat16_truncate(fat16_file_t* f, uint32_t new_size) {
871880
fat16_free_chain(f->fs, cluster);
872881

873882
f->entry.filesize = new_size;
874-
875-
update:
876-
if (f->parent_cluster == 0)
877-
fat16_update_root_entry(f->fs, &f->entry);
878-
else
879-
fat16_update_dir_entry(f->fs, f->parent_cluster, &f->entry);
883+
if (f->pos > new_size)
884+
f->pos = new_size;
880885

881886
return FAT_OK;
882887
}
883888

889+
884890
int fat16_mkdir(fat16_fs_t* fs, uint16_t parent_cluster, const char* name) {
885891
if (fat16_is_reserved_name(name)) {
886892
printf("refusing to create reserved name '%s'", name);

source/kernel/C/filesystems/vfs.c

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,32 +115,39 @@ static void vfs_normalize_path(const char* in, char* out) {
115115
out[oi] = '\0';
116116
}
117117

118-
int vfs_read(vfs_file_t* file, uint8_t* buf, uint32_t size) {
119-
if (!file || !file->mnt || !buf){
120-
eprintf("read: invalid parameters passed");
118+
int vfs_read(vfs_file_t* file, uint8_t* buf, uint32_t size)
119+
{
120+
if (!file || !buf)
121121
return -1;
122-
}
123122

124-
if (file->mnt->type == FS_FAT16)
125-
return fat16_read(&file->f, buf, size);
123+
if (!(file->flags & VFS_O_RDONLY) &&
124+
!(file->flags & VFS_O_RDWR)) {
125+
eprintf("read: file not opened for reading");
126+
return -2;
127+
}
126128

127-
eprintf("read: unknown filesystem");
128-
return -2;
129+
return fat16_read(&file->f, buf, size);
129130
}
130131

131-
int vfs_write(vfs_file_t* file, const uint8_t* buf, uint32_t size) {
132-
if (!file || !file->mnt || !buf){
133-
eprintf("write: invalid parameters passed");
132+
133+
int vfs_write(vfs_file_t* file, const uint8_t* buf, uint32_t size)
134+
{
135+
if (!file || !buf)
134136
return -1;
137+
138+
if (!(file->flags & VFS_O_WRONLY) &&
139+
!(file->flags & VFS_O_RDWR)) {
140+
eprintf("write: file not opened for writing");
141+
return -2;
135142
}
136143

137-
if (file->mnt->type == FS_FAT16)
138-
return fat16_write(&file->f, buf, size);
144+
// if (file->flags & VFS_O_APPEND)
145+
// file->f.pos = file->f.size;
139146

140-
eprintf("write: unknown filesystem");
141-
return -2;
147+
return fat16_write(&file->f, buf, size);
142148
}
143149

150+
144151
void vfs_close(vfs_file_t* file) {
145152
if (!file || !file->mnt) {
146153
eprintf("close: invalid file pointer");
@@ -206,29 +213,64 @@ int vfs_ls(const char* path)
206213
return 0;
207214
}
208215

209-
int vfs_open(const char* path, vfs_file_t* out) {
216+
int vfs_open(const char* path, int flags, vfs_file_t* out)
217+
{
210218
if (!path || !out) {
211-
eprintf("open: invalid parameters passed");
219+
eprintf("open: invalid parameters");
212220
return -1;
213221
}
214222

215223
char norm[256];
216224
vfs_normalize_path(path, norm);
217225

218226
vfs_mount_res_t res;
219-
if (vfs_resolve_mount(norm, &res) != 0) return -2;
227+
if (vfs_resolve_mount(norm, &res) != 0)
228+
return -2;
220229

221-
if (res.mnt->type == FS_FAT16) {
222-
if (fat16_open((fat16_fs_t*)res.mnt->fs, res.rel_path, &out->f) != 0)
223-
return -4;
230+
if (res.mnt->type != FS_FAT16) {
231+
eprintf("open: unknown filesystem");
232+
return -3;
233+
}
224234

225-
out->mnt = res.mnt;
235+
fat16_fs_t* fs = (fat16_fs_t*)res.mnt->fs;
236+
int ret;
237+
238+
/* ---------- CREATE ---------- */
239+
if (flags & VFS_O_CREAT) {
240+
/* create if missing */
241+
ret = fat16_open(fs, res.rel_path, &out->f);
242+
if (ret != 0) {
243+
/* create new file */
244+
ret = fat16_create_path(fs, res.rel_path,
245+
FAT16_ROOT_CLUSTER,
246+
0x20); /* archive */
247+
if (ret != 0)
248+
return -4;
226249

227-
return 0;
250+
ret = fat16_open(fs, res.rel_path, &out->f);
251+
if (ret != 0)
252+
return -5;
253+
}
254+
} else {
255+
ret = fat16_open(fs, res.rel_path, &out->f);
256+
if (ret != 0)
257+
return -6;
258+
}
259+
260+
/* ---------- TRUNC ---------- */
261+
if (flags & VFS_O_TRUNC) {
262+
fat16_truncate(&out->f, 0);
228263
}
229264

230-
eprintf("open: unknown filesystem");
231-
return -3;
265+
/* ---------- APPEND ---------- */
266+
// if (flags & VFS_O_APPEND) {
267+
// out->f.pos = out->f.size;
268+
// }
269+
270+
out->mnt = res.mnt;
271+
out->flags = flags;
272+
273+
return 0;
232274
}
233275

234276
int vfs_mkdir(const char* path) {

source/kernel/C/kernel.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,10 @@ void main(void) {
249249

250250
void shutdown(){
251251
info("shutdown has been called", __FILE__);
252-
debug_printf("hhdm offset -> 0x%x", hhdm_request.response->offset);
253-
254252
acpi_shutdown_hack(hhdm_request.response->offset, acpi_find_sdt);
255253
}
256254

257255
void reboot(){
258256
info("reboot has been called", __FILE__);
259-
debug_printf("hhdm offset -> 0x%x", hhdm_request.response->offset);
260-
261257
acpi_reboot(hhdm_request.response->offset);
262258
}

source/kernel/C/shell/commands/cat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int cmd_cat(int argc, char** argv)
2929
vfs_file_t file;
3030

3131
/* Open file */
32-
if (vfs_open(argv[i], &file) != 0) {
32+
if (vfs_open(argv[i], VFS_O_RDONLY, &file) != 0) {
3333
printf("cat: %s: no such file or directory", argv[i]);
3434
continue;
3535
}

0 commit comments

Comments
 (0)