new: FAT32删除文件的功能 (#73)

* new: 将sys_rmdir更改为sys_unlink,.且完成删除文件操作的vfs部分

* new: fat32删除文件

*bugfix: 解决创建文件时的bug
This commit is contained in:
login
2022-11-02 15:42:44 +08:00
committed by GitHub
parent 8e3f5674f8
commit 31b7b49d8c
11 changed files with 424 additions and 151 deletions

View File

@ -1,18 +1,17 @@
#include "cmd.h"
#include <libc/string.h>
#include <libc/stdio.h>
#include <libc/stddef.h>
#include <libsystem/syscall.h>
#include <libc/string.h>
#include <libc/errno.h>
#include <libc/unistd.h>
#include <libc/stdlib.h>
#include <libc/fcntl.h>
#include <libc/dirent.h>
#include <libc/sys/wait.h>
#include <libc/sys/stat.h>
#include "cmd_help.h"
#include "cmd_test.h"
#include <libc/dirent.h>
#include <libc/errno.h>
#include <libc/fcntl.h>
#include <libc/stddef.h>
#include <libc/stdio.h>
#include <libc/stdlib.h>
#include <libc/string.h>
#include <libc/sys/stat.h>
#include <libc/sys/wait.h>
#include <libc/unistd.h>
#include <libsystem/syscall.h>
// 当前工作目录在main_loop中初始化
char *shell_current_path = NULL;
@ -20,22 +19,11 @@ char *shell_current_path = NULL;
* @brief shell 内建函数的主命令与处理函数的映射表
*
*/
struct built_in_cmd_t shell_cmds[] =
{
{"cd", shell_cmd_cd},
{"cat", shell_cmd_cat},
{"exec", shell_cmd_exec},
{"ls", shell_cmd_ls},
{"mkdir", shell_cmd_mkdir},
{"pwd", shell_cmd_pwd},
{"rm", shell_cmd_rm},
{"rmdir", shell_cmd_rmdir},
{"reboot", shell_cmd_reboot},
{"touch", shell_cmd_touch},
{"about", shell_cmd_about},
{"free", shell_cmd_free},
{"help", shell_help},
{"pipe", shell_pipe_test},
struct built_in_cmd_t shell_cmds[] = {
{"cd", shell_cmd_cd}, {"cat", shell_cmd_cat}, {"exec", shell_cmd_exec}, {"ls", shell_cmd_ls},
{"mkdir", shell_cmd_mkdir}, {"pwd", shell_cmd_pwd}, {"rm", shell_cmd_rm}, {"rmdir", shell_cmd_rmdir},
{"reboot", shell_cmd_reboot}, {"touch", shell_cmd_touch}, {"about", shell_cmd_about}, {"free", shell_cmd_free},
{"help", shell_help}, {"pipe", shell_pipe_test},
};
// 总共的内建命令数量
@ -210,7 +198,6 @@ int shell_cmd_cd(int argc, char **argv)
new_path[current_dir_len] = '/';
strcat(new_path, argv[1] + dest_offset);
int x = chdir(new_path);
if (x == 0) // 成功切换目录
{
free(shell_current_path);
@ -221,6 +208,7 @@ int shell_cmd_cd(int argc, char **argv)
}
else
{
free(new_path);
printf("ERROR: Cannot switch to directory: %s\n", new_path);
goto fail;
}
@ -326,6 +314,7 @@ int shell_cmd_cat(int argc, char **argv)
close(fd);
free(buf);
free(file_path);
if (argv != NULL)
free(argv);
return 0;
@ -342,10 +331,11 @@ int shell_cmd_touch(int argc, char **argv)
{
int path_len = 0;
char *file_path;
bool alloc_full_path=false;
if (argv[1][0] == '/')
file_path = argv[1];
else
file_path = get_target_filepath(argv[1], &path_len);
{file_path = get_target_filepath(argv[1], &path_len);alloc_full_path=true;}
// 打开文件
int fd = open(file_path, O_CREAT);
@ -361,19 +351,11 @@ int shell_cmd_touch(int argc, char **argv)
close(fd);
if (argv != NULL)
free(argv);
if(alloc_full_path)
free(file_path);
return 0;
}
/**
* @brief 删除命令
*
* @param argc
* @param argv
* @return int
*/
// todo:
int shell_cmd_rm(int argc, char **argv) {return 0;}
/**
* @brief 创建文件夹的命令
*
@ -384,19 +366,22 @@ int shell_cmd_rm(int argc, char **argv) {return 0;}
int shell_cmd_mkdir(int argc, char **argv)
{
int result_path_len = -1;
const char *full_path = NULL;
char *full_path = NULL;
bool alloc_full_path = false;
if (argv[1][0] == '/')
full_path = argv[1];
else
{
full_path = get_target_filepath(argv[1], &result_path_len);
alloc_full_path = true;
}
// printf("mkdir: full_path = %s\n", full_path);
int retval = mkdir(full_path, 0);
if (argv != NULL)
free(argv);
if (alloc_full_path)
free(full_path);
return retval;
}
@ -407,20 +392,60 @@ int shell_cmd_mkdir(int argc, char **argv)
* @param argv
* @return int
*/
// todo:
int shell_cmd_rmdir(int argc, char **argv)
{
const char *full_path = NULL;
char *full_path = NULL;
int result_path_len = -1;
bool alloc_full_path = false;
if (argv[1][0] == '/')
full_path = argv[1];
else
{
full_path = get_target_filepath(argv[1], &result_path_len);
alloc_full_path = true;
}
int retval = rmdir(full_path);
if (retval != 0)
printf("Failed to remove %s, retval=%d\n", full_path, retval);
// printf("rmdir: path=%s, retval=%d\n", full_path, retval);
if (argv != NULL)
free(argv);
if (alloc_full_path)
free(full_path);
return retval;
}
/**
* @brief 删除文件的命令
*
* @param argc
* @param argv
* @return int
*/
int shell_cmd_rm(int argc, char **argv)
{
char *full_path = NULL;
int result_path_len = -1;
int retval = 0;
bool alloc_full_path = false;
if (argv[1][0] == '/')
full_path = argv[1];
else
{
full_path = get_target_filepath(argv[1], &result_path_len);
alloc_full_path = true;
}
retval = rm(full_path);
// printf("rmdir: path=%s, retval=%d\n", full_path, retval);
if (retval != 0)
printf("Failed to remove %s, retval=%d\n", full_path, retval);
if (alloc_full_path)
free(full_path);
if (argv != NULL)
free(argv);
return retval;
}
@ -446,9 +471,9 @@ int shell_cmd_exec(int argc, char **argv)
// printf("before execv, path=%s, argc=%d\n", file_path, argc);
execv(file_path, argv);
free(argv);
while (1)
;
exit(0);
free(file_path);
exit(-1);
}
else
{
@ -510,11 +535,13 @@ int shell_cmd_free(int argc, char **argv)
printf("Mem:\t");
if (argc == 1) // 按照kb显示
{
printf("%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t\n", mst.total >> 10, mst.used >> 10, mst.free >> 10, mst.shared >> 10, mst.cache_used >> 10, mst.available >> 10);
printf("%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t\n", mst.total >> 10, mst.used >> 10, mst.free >> 10, mst.shared >> 10,
mst.cache_used >> 10, mst.available >> 10);
}
else // 按照MB显示
{
printf("%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t\n", mst.total >> 20, mst.used >> 20, mst.free >> 20, mst.shared >> 20, mst.cache_used >> 20, mst.available >> 20);
printf("%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t\n", mst.total >> 20, mst.used >> 20, mst.free >> 20, mst.shared >> 20,
mst.cache_used >> 20, mst.available >> 20);
}
done:;

View File

@ -161,6 +161,17 @@ int rmdir(const char *path)
return syscall_invoke(SYS_UNLINK_AT, 0, (uint64_t)path, AT_REMOVEDIR, 0, 0, 0, 0, 0);
}
/**
* @brief 删除文件
*
* @param path 绝对路径
* @return int
*/
int rm(const char * path)
{
return syscall_invoke(SYS_UNLINK_AT, 0, (uint64_t)path, 0, 0, 0, 0, 0, 0);
}
/**
* @brief 交换n字节
* @param src 源地址

View File

@ -106,6 +106,8 @@ extern int usleep(useconds_t usec);
*/
int rmdir(const char *path);
int rm(const char * path);
/**
* @brief 交换n字节
* @param src 源地址