Skip to content

Commit cfac706

Browse files
Fixed a lot of displaying issues, fixed mount doing multiple times.
1 parent 1db5684 commit cfac706

File tree

7 files changed

+104
-37
lines changed

7 files changed

+104
-37
lines changed

disk.img

0 Bytes
Binary file not shown.

source/includes/filesystems/vfs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,6 @@ int vfs_unlink(const char* path);
117117
*/
118118
const char* vfs_getcwd();
119119

120+
const char* vfs_basename(const char* path);
121+
120122
#endif // VFS_H

source/kernel/C/ahci.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,23 @@ general_partition_t* search_general_partition(cstring partition_name) {
171171

172172
mount_entry_t* add_mount(const char* mount_point, const char* part_name, partition_fs_type_t type, void* fs_ptr)
173173
{
174-
if (mounted_partition_count >= MAX_PARTITIONS) return NULL;
174+
if (!mount_point || !part_name) return NULL;
175+
176+
for (int i = 0; i < mounted_partition_count; i++) {
177+
if (strcmp(mounted_partitions[i].mount_point, mount_point) == 0) {
178+
printf("mount: mount point '%s' already in use", mount_point);
179+
return NULL;
180+
}
181+
if (strcmp(mounted_partitions[i].part_name, part_name) == 0) {
182+
printf("mount: partition '%s' already mounted", part_name);
183+
return NULL;
184+
}
185+
}
186+
187+
if (mounted_partition_count >= MAX_PARTITIONS) {
188+
printf("mount: maximum number of mounts reached");
189+
return NULL;
190+
}
175191

176192
mount_entry_t* new_mount = kmalloc(sizeof(mount_entry_t));
177193
if(!new_mount) return NULL;
@@ -202,11 +218,11 @@ mount_entry_t* find_mount_by_point(const char* mount_point)
202218
void list_all_mounts()
203219
{
204220
if(mounted_partition_count == 0){
205-
printf("No mounted partitions.");
221+
printf("no mounted partitions.");
206222
return;
207223
}
208224

209-
printf(yellow_color "Mounted partitions :" reset_color);
225+
printf(yellow_color "mounted partitions :" reset_color);
210226
for(int i = 0; i < mounted_partition_count; i++){
211227
mount_entry_t* mnt = &mounted_partitions[i];
212228
printf(" [%d] %s -> %s (FS Type: %d)", i, mnt->part_name, mnt->mount_point, mnt->type);

source/kernel/C/filesystems/fat16.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ int fat16_mount(int portno, uint32_t partition_lba, fat16_fs_t* fs) {
7575
fs->fat_start = partition_lba + bs->reserved_sectors;
7676
fs->root_dir_start = fs->fat_start + (bs->num_fats * bs->sectors_per_fat);
7777
fs->data_start = fs->root_dir_start + fs->root_dir_sectors;
78-
79-
printf("mount: successfully mounted FAT16 partition");
78+
8079
return FAT_OK;
8180
}
8281

@@ -372,14 +371,14 @@ void fat16_format_name(const char* input, char out[11]) {
372371

373372
// name
374373
while (input[i] && input[i] != '.' && j < 8)
375-
out[j++] = toupper(input[i++]);
374+
out[j++] = (input[i++]);
376375

377376
// extension
378377
if (input[i] == '.') {
379378
i++;
380379
j = 8;
381380
while (input[i] && j < 11)
382-
out[j++] = toupper(input[i++]);
381+
out[j++] = (input[i++]);
383382
}
384383
}
385384

source/kernel/C/filesystems/vfs.c

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ static int vfs_resolve_mount(const char* path, vfs_mount_res_t* out) {
5656

5757
if (!best) {
5858
if(path)
59-
printf("no mount matches path: %s", path);
59+
printf("resolve_mount: no mount matches path: %s", path);
6060
else
61-
printf("no mount matches the given path.");
61+
printf("resolve_mount: no mount matches the given path.");
6262
return -2;
6363
}
6464

@@ -153,42 +153,53 @@ void vfs_close(vfs_file_t* file) {
153153
fat16_close(&file->f);
154154
}
155155

156-
int vfs_ls(const char* path) {
156+
int vfs_ls(const char* path)
157+
{
157158
if (!path) {
158-
printf("ls: invalid path passed");
159+
printf("ls: invalid path");
159160
return -1;
160161
}
161162

163+
char norm[256];
164+
vfs_normalize_path(path, norm);
165+
162166
vfs_mount_res_t res;
163-
if (vfs_resolve_mount(path, &res) != 0)
167+
if (vfs_resolve_mount(norm, &res) != 0)
164168
return -1;
165169

166-
if (res.mnt->type == FS_FAT16){
170+
int listed_anything = 0;
171+
172+
for (int i = 0; i < mounted_partition_count; i++) {
173+
mount_entry_t* m = &mounted_partitions[i];
174+
175+
if (vfs_is_direct_child_mount(norm, m)) {
176+
const char* name = vfs_basename(m->mount_point);
177+
printfnoln(yellow_color "%s " reset_color, name);
178+
}
179+
}
180+
181+
if (res.mnt->type == FS_FAT16) {
167182
fat16_fs_t* fs = (fat16_fs_t*)res.mnt->fs;
168183

169184
if (*res.rel_path == '\0') {
170185
fat16_list_root(fs);
171-
return 0;
172-
}
186+
} else {
187+
fat16_dir_entry_t e;
188+
if (fat16_find_path(fs, res.rel_path, &e) != 0) {
189+
printf("ls: path not found");
190+
return -3;
191+
}
173192

174-
fat16_dir_entry_t e;
175-
if (fat16_find_path(fs, res.rel_path, &e) != 0) {
176-
printf("ls: path not found");
177-
return -3;
178-
}
193+
if (!(e.attr & 0x10)) {
194+
printf("ls: not a directory");
195+
return -4;
196+
}
179197

180-
if (!(e.attr & 0x10)) {
181-
printf("ls: not a directory");
182-
return -4;
198+
fat16_list_dir_cluster(fs, e.first_cluster);
183199
}
184-
185-
fat16_list_dir_cluster(fs, e.first_cluster);
186-
187-
return 0;
188200
}
189-
190-
printf("ls: unknown filesystem");
191-
return -2;
201+
202+
return 0;
192203
}
193204

194205
int vfs_open(const char* path, vfs_file_t* out) {
@@ -440,4 +451,33 @@ int vfs_mv(const char* src, const char* dst)
440451

441452
const char* vfs_getcwd(void) {
442453
return vfs_cwd;
454+
}
455+
456+
int vfs_is_direct_child_mount(const char* parent, mount_entry_t* m) {
457+
if (!strcmp(parent, "/")) {
458+
if (m->mount_point[0] != '/')
459+
return 0;
460+
/* Only one slash allowed */
461+
const char* rest = m->mount_point + 1;
462+
return strchr(rest, '/') == NULL;
463+
}
464+
465+
size_t plen = strlen(parent);
466+
if (strncmp(m->mount_point, parent, plen) != 0)
467+
return 0;
468+
469+
if (m->mount_point[plen] != '/')
470+
return 0;
471+
472+
return strchr(m->mount_point + plen + 1, '/') == NULL;
473+
}
474+
475+
const char* vfs_basename(const char* path) {
476+
const char* last = path;
477+
while (*path) {
478+
if (*path == '/')
479+
last = path + 1;
480+
path++;
481+
}
482+
return last;
443483
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ int cmd_mount(int argc, char** argv)
4242
printf("mount: memory allocation failed.");
4343
return 1;
4444
}
45+
mount_entry_t* new_mount = add_mount(mount_point, device, partition->fs_type, fs_struct);
46+
if (!new_mount)
47+
return 1;
48+
4549
ret = fat16_mount(partition->ahci_port, partition->lba_start, (fat16_fs_t*)fs_struct);
4650
break;
4751

@@ -55,12 +59,6 @@ int cmd_mount(int argc, char** argv)
5559
return 1;
5660
}
5761

58-
mount_entry_t* new_mount = add_mount(mount_point, device, partition->fs_type, fs_struct);
59-
if (!new_mount) {
60-
printf("mount: add_mount failed.");
61-
return 1;
62-
}
63-
64-
printf("Mounted %s at %s", device, mount_point);
62+
printf("mount: mounted %s at %s", device, mount_point);
6563
return 0;
6664
}

source/kernel/C/strings.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,15 @@ char* strrchr(const char* s, int c) {
530530
if (c == 0) return (char*)s; // null terminator
531531
return last;
532532
}
533+
534+
/* Find first occurrence of character c in string s */
535+
char* strchr(const char* s, int c) {
536+
while (*s) {
537+
if (*s == (char)c)
538+
return (char*)s; // cast away const
539+
s++;
540+
}
541+
if (c == '\0') // special case: searching for null terminator
542+
return (char*)s;
543+
return NULL;
544+
}

0 commit comments

Comments
 (0)