diff --git a/ce/interp.c b/ce/interp.c index 1b40a95..5a5c23b 100644 --- a/ce/interp.c +++ b/ce/interp.c @@ -73,6 +73,30 @@ static void mkdir (struct context* context, char** dir_paths, size_t dirs_count) } } +static void rm (struct context* context, char** paths, size_t count) { + char volume[VOLUME_MAX]; + const char* path; + int ret; + + for (size_t i = 0; i < count; i++) { + const char* path1 = paths[i]; + + if (!path_parse (path1, volume, &path)) { + cprintf (context, "ERROR bad path '%s'\n", path1); + continue; + } + + if ((ret = volume_open (volume)) < 0) { + cprintf (context, "ERROR could not open volume '%s': %s\n", volume, str_status[-ret]); + continue; + } + + remove (path); + + volume_close (); + } +} + static void cat (struct context* context, char** file_paths, size_t files_count) { char volume[VOLUME_MAX]; const char* path; @@ -172,12 +196,13 @@ static void quit1 (struct context* context) { static void help (struct context* context) { cprintf (context, "Available commands:\n"); - cprintf (context, "echo ...\n"); cprintf (context, "help\n"); - cprintf (context, "cat \n"); - cprintf (context, "ls \n"); - cprintf (context, "mkfile \n"); - cprintf (context, "mkdir \n"); + cprintf (context, "echo ...\n"); + cprintf (context, "cat \n"); + cprintf (context, "ls \n"); + cprintf (context, "mkfile \n"); + cprintf (context, "mkdir \n"); + cprintf (context, "rm \n"); cprintf (context, "quit\n"); } @@ -212,8 +237,9 @@ static void execute_cmd (struct ast_cmd* cmd, struct context* context) { mkfile (context, cmd->args, cmd->arg_count); } else if (strcmp (cmd->name, "mkdir") == 0) { mkdir (context, cmd->args, cmd->arg_count); + } else if (strcmp (cmd->name, "rm") == 0) { + rm (context, cmd->args, cmd->arg_count); } else { - /* cprintf (context, "ERROR unknown command '%s'\n", cmd->name); */ char volume[VOLUME_MAX]; const char* path; diff --git a/include/status.h b/include/status.h index df891df..ef603a7 100644 --- a/include/status.h +++ b/include/status.h @@ -22,5 +22,6 @@ #define ST_NOT_DRV 18 #define ST_PARTITION_ERROR 19 #define ST_CREATE_DIR_ERROR 20 +#define ST_REMOVE_ERROR 21 #endif // _M_STATUS_H diff --git a/include/syscall_defs.h b/include/syscall_defs.h index bea4092..0922e7f 100644 --- a/include/syscall_defs.h +++ b/include/syscall_defs.h @@ -28,5 +28,6 @@ #define SYS_WAIT_FOR_PID 25 #define SYS_KILL 26 #define SYS_CREATE_DIR 27 +#define SYS_REMOVE 28 #endif // _M_SYSCALL_DEFS_H diff --git a/kernel/fs/fatfs.c b/kernel/fs/fatfs.c index 83e7782..5fb3cb5 100644 --- a/kernel/fs/fatfs.c +++ b/kernel/fs/fatfs.c @@ -264,5 +264,12 @@ int fatfs_create_dir (struct vfs_volume* volume, const char* path) { struct fatfs_ctx* fatfs_ctx = volume->udata; int r = fl_createdirectory (fatfs_ctx, path); - return r == 0 ? ST_OK : ST_CREATE_DIR_ERROR; + return r == 0 ? ST_OK : -ST_CREATE_DIR_ERROR; +} + +int fatfs_remove (struct vfs_volume* volume, const char* path) { + struct fatfs_ctx* fatfs_ctx = volume->udata; + + int r = fl_remove (fatfs_ctx, path); + return r == 0 ? ST_OK : -ST_REMOVE_ERROR; } diff --git a/kernel/fs/fatfs.h b/kernel/fs/fatfs.h index 47917d2..053b987 100644 --- a/kernel/fs/fatfs.h +++ b/kernel/fs/fatfs.h @@ -31,4 +31,6 @@ int fatfs_create_file (struct vfs_volume* volume, const char* path); int fatfs_create_dir (struct vfs_volume* volume, const char* path); +int fatfs_remove (struct vfs_volume* volume, const char* path); + #endif // _KERNEL_FS_FATFS_H diff --git a/kernel/fs/tarfs.c b/kernel/fs/tarfs.c index 263ea11..119e952 100644 --- a/kernel/fs/tarfs.c +++ b/kernel/fs/tarfs.c @@ -213,3 +213,8 @@ int tarfs_create_dir (struct vfs_volume* volume, const char* path) { (void)volume, (void)path; return ST_OK; } + +int tarfs_remove (struct vfs_volume* volume, const char* path) { + (void)volume, (void)path; + return ST_OK; +} diff --git a/kernel/fs/tarfs.h b/kernel/fs/tarfs.h index 0f04d49..3214832 100644 --- a/kernel/fs/tarfs.h +++ b/kernel/fs/tarfs.h @@ -53,4 +53,6 @@ int tarfs_create_file (struct vfs_volume* volume, const char* path); int tarfs_create_dir (struct vfs_volume* volume, const char* path); +int tarfs_remove (struct vfs_volume* volume, const char* path); + #endif // _KERNEL_FS_TARFS_H diff --git a/kernel/fs/vfs.c b/kernel/fs/vfs.c index 4ffe620..98152f1 100644 --- a/kernel/fs/vfs.c +++ b/kernel/fs/vfs.c @@ -62,6 +62,7 @@ int vfs_create_volume (const char* key, int fs_type, struct device* back_device, volume->driver_ops.read_dir_entry = &tarfs_read_dir_entry; volume->driver_ops.create_file = &tarfs_create_file; volume->driver_ops.create_dir = &tarfs_create_dir; + volume->driver_ops.remove = &tarfs_remove; break; case VFS_FAT16: volume->driver_ops.mount = &fatfs_mount; @@ -72,6 +73,7 @@ int vfs_create_volume (const char* key, int fs_type, struct device* back_device, volume->driver_ops.read_dir_entry = &fatfs_read_dir_entry; volume->driver_ops.create_file = &fatfs_create_file; volume->driver_ops.create_dir = &fatfs_create_dir; + volume->driver_ops.remove = &fatfs_remove; break; case VFS_FAT32: volume->driver_ops.mount = &fatfs_mount; @@ -82,6 +84,7 @@ int vfs_create_volume (const char* key, int fs_type, struct device* back_device, volume->driver_ops.read_dir_entry = &fatfs_read_dir_entry; volume->driver_ops.create_file = &fatfs_create_file; volume->driver_ops.create_dir = &fatfs_create_dir; + volume->driver_ops.remove = &fatfs_remove; break; default: free (volume); @@ -300,6 +303,24 @@ int vfs_create_dir (struct proc* proc, const char* volume_name, const char* path return volume->driver_ops.create_dir (volume, path); } +int vfs_remove (struct proc* proc, const char* volume_name, const char* path) { + struct vfs_volume* volume = vfs_find_volume (volume_name); + + if (volume == NULL) + return -ST_NOT_FOUND; + + spin_lock (&volume->lock); + + if (volume->locked && volume->owner != proc) { + spin_unlock (&volume->lock); + return -ST_PERMISSION_ERROR; + } + + spin_unlock (&volume->lock); + + return volume->driver_ops.remove (volume, path); +} + void vfs_init (void) { memset (&volume_table, 0, sizeof (volume_table)); diff --git a/kernel/fs/vfs.h b/kernel/fs/vfs.h index e0b7866..096d926 100644 --- a/kernel/fs/vfs.h +++ b/kernel/fs/vfs.h @@ -50,6 +50,8 @@ struct vfs_volume { int (*create_file) (struct vfs_volume* volume, const char* path); int (*create_dir) (struct vfs_volume* volume, const char* path); + + int (*remove) (struct vfs_volume* volume, const char* path); } driver_ops; struct device* back_device; void* udata @@ -83,6 +85,8 @@ int vfs_create_file (struct proc* proc, const char* volume_name, const char* pat int vfs_create_dir (struct proc* proc, const char* volume_name, const char* path); +int vfs_remove (struct proc* proc, const char* volume_name, const char* path); + void vfs_init (void); void vfs_translate (size_t fs_block, size_t fs_block_count, size_t fs_block_size, diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index 7663419..0fb7743 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -617,6 +617,30 @@ DEFINE_SYSCALL (sys_create_dir) { return SYSRESULT (ret); } +/* int remove (char* path) */ +DEFINE_SYSCALL (sys_remove) { + uintptr_t uvaddr_path = a1; + + struct limine_hhdm_response* hhdm = limine_hhdm_request.response; + + uintptr_t out_paddr; + + spin_lock (&proc->procgroup->lock); + out_paddr = mm_v2p (&proc->procgroup->pd, uvaddr_path); + spin_unlock (&proc->procgroup->lock); + + if (out_paddr == 0) + return SYSRESULT (-ST_BAD_ADDRESS_SPACE); + + const char* path = (const char*)((uintptr_t)hhdm->offset + out_paddr); + + spin_lock (&proc->lock); + int ret = vfs_remove (proc, proc->cwv, path); + spin_unlock (&proc->lock); + + return SYSRESULT (ret); +} + static syscall_handler_func_t handler_table[] = { [SYS_QUIT] = &sys_quit, [SYS_TEST] = &sys_test, @@ -645,6 +669,7 @@ static syscall_handler_func_t handler_table[] = { [SYS_WAIT_FOR_PID] = &sys_wait_for_pid, [SYS_KILL] = &sys_kill, [SYS_CREATE_DIR] = &sys_create_dir, + [SYS_REMOVE] = &sys_remove, }; syscall_handler_func_t syscall_find_handler (int syscall_num) { diff --git a/libsystem/system.c b/libsystem/system.c index 74a041f..696345f 100644 --- a/libsystem/system.c +++ b/libsystem/system.c @@ -80,3 +80,5 @@ int wait_for_pid (int pid) { return (int)do_syscall (SYS_WAIT_FOR_PID, pid); } int kill (int pid) { return (int)do_syscall (SYS_KILL, pid); } int create_dir (const char* path) { return (int)do_syscall (SYS_CREATE_DIR, path); } + +int remove (const char* path) { return (int)do_syscall (SYS_REMOVE, path); } diff --git a/libsystem/system.h b/libsystem/system.h index 9e307b8..638059e 100644 --- a/libsystem/system.h +++ b/libsystem/system.h @@ -87,4 +87,7 @@ int kill (int pid); /* Create a directory */ int create_dir (const char* path); +/* Remove filesystem entry */ +int remove (const char* path); + #endif // _LIBMSL_M_SYSTEM_H