Kernel/LibC/Userspace: Implement mkdir and creat

Touch now uses creat insteadd of open with O_CREAT flag
This commit is contained in:
Bananymous 2023-10-25 19:45:18 +03:00
parent 1f794e4ac0
commit 8bb47aee02
11 changed files with 83 additions and 10 deletions

View File

@ -23,7 +23,7 @@ add_subdirectory(LibELF)
add_subdirectory(userspace) add_subdirectory(userspace)
add_custom_target(sysroot add_custom_target(sysroot
COMMAND mkdir -p ${BANAN_SYSROOT} COMMAND ${CMAKE_COMMAND} -E make_directory ${BANAN_SYSROOT}
COMMAND cd ${BANAN_SYSROOT} && sudo tar xf ${BANAN_BASE_SYSROOT} COMMAND cd ${BANAN_SYSROOT} && sudo tar xf ${BANAN_BASE_SYSROOT}
USES_TERMINAL USES_TERMINAL
) )

View File

@ -96,6 +96,8 @@ namespace Kernel
BAN::ErrorOr<long> sys_close(int fd); BAN::ErrorOr<long> sys_close(int fd);
BAN::ErrorOr<long> sys_read(int fd, void* buffer, size_t count); BAN::ErrorOr<long> sys_read(int fd, void* buffer, size_t count);
BAN::ErrorOr<long> sys_write(int fd, const void* buffer, size_t count); BAN::ErrorOr<long> sys_write(int fd, const void* buffer, size_t count);
BAN::ErrorOr<long> sys_create(const char*, mode_t);
BAN::ErrorOr<long> sys_create_dir(const char*, mode_t);
BAN::ErrorOr<long> sys_chmod(const char*, mode_t); BAN::ErrorOr<long> sys_chmod(const char*, mode_t);

View File

@ -746,6 +746,22 @@ namespace Kernel
return TRY(m_open_file_descriptors.write(fd, BAN::ByteSpan((uint8_t*)buffer, count))); return TRY(m_open_file_descriptors.write(fd, BAN::ByteSpan((uint8_t*)buffer, count)));
} }
BAN::ErrorOr<long> Process::sys_create(const char* path, mode_t mode)
{
LockGuard _(m_lock);
validate_string_access(path);
TRY(create_file_or_dir(path, mode));
return 0;
}
BAN::ErrorOr<long> Process::sys_create_dir(const char* path, mode_t mode)
{
LockGuard _(m_lock);
validate_string_access(path);
TRY(create_file_or_dir(path, Inode::Mode::IFDIR | mode));
return 0;
}
BAN::ErrorOr<long> Process::sys_chmod(const char* path, mode_t mode) BAN::ErrorOr<long> Process::sys_chmod(const char* path, mode_t mode)
{ {
if (mode & S_IFMASK) if (mode & S_IFMASK)

View File

@ -202,6 +202,12 @@ namespace Kernel
case SYS_CHMOD: case SYS_CHMOD:
ret = Process::current().sys_chmod((const char*)arg1, (mode_t)arg2); ret = Process::current().sys_chmod((const char*)arg1, (mode_t)arg2);
break; break;
case SYS_CREATE:
ret = Process::current().sys_create((const char*)arg1, (mode_t)arg2);
break;
case SYS_CREATE_DIR:
ret = Process::current().sys_create_dir((const char*)arg1, (mode_t)arg2);
break;
default: default:
dwarnln("Unknown syscall {}", syscall); dwarnln("Unknown syscall {}", syscall);
break; break;

View File

@ -4,6 +4,11 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
int creat(const char* path, mode_t mode)
{
return syscall(SYS_CREATE, path, S_IFREG | mode);
}
int open(const char* path, int oflag, ...) int open(const char* path, int oflag, ...)
{ {
va_list args; va_list args;

View File

@ -56,6 +56,8 @@ __BEGIN_DECLS
#define SYS_TTY_CTRL 53 #define SYS_TTY_CTRL 53
#define SYS_POWEROFF 54 #define SYS_POWEROFF 54
#define SYS_CHMOD 55 #define SYS_CHMOD 55
#define SYS_CREATE 56 // creat, mkfifo
#define SYS_CREATE_DIR 57 // mkdir
__END_DECLS __END_DECLS

View File

@ -28,3 +28,8 @@ int stat(const char* __restrict path, struct stat* __restrict buf)
{ {
return syscall(SYS_STAT, path, buf, 0); return syscall(SYS_STAT, path, buf, 0);
} }
int mkdir(const char* path, mode_t mode)
{
return syscall(SYS_CREATE_DIR, path, mode);
}

View File

@ -13,6 +13,7 @@ set(USERSPACE_PROJECTS
init init
ls ls
meminfo meminfo
mkdir
mmap-shared-test mmap-shared-test
poweroff poweroff
Shell Shell

View File

@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.26)
project(mkdir CXX)
set(SOURCES
main.cpp
)
add_executable(mkdir ${SOURCES})
target_compile_options(mkdir PUBLIC -O2 -g)
target_link_libraries(mkdir PUBLIC libc)
add_custom_target(mkdir-install
COMMAND sudo cp ${CMAKE_CURRENT_BINARY_DIR}/mkdir ${BANAN_BIN}/
DEPENDS mkdir
USES_TERMINAL
)

23
userspace/mkdir/main.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char** argv)
{
if (argc <= 1)
{
fprintf(stderr, "Missing operand\n");
return 1;
}
int ret = 0;
for (int i = 1; i < argc; i++)
{
if (mkdir(argv[i], 0755) == -1)
{
perror("mkdir");
ret = 1;
}
}
return ret;
}

View File

@ -4,18 +4,14 @@
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
int ret = 0;
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
int fd = open(argv[i], O_WRONLY | O_CREAT, 0644); if (creat(argv[i], 0644) == -1 && errno != EEXIST)
if (fd == -1)
{ {
if (errno != EEXISTS) perror(argv[i]);
perror(argv[i]); ret = 1;
}
else
{
close(fd);
} }
} }
return 0; return ret;
} }