kernel+libc: Add mkdir()
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-03-12 15:32:09 +01:00
parent d2049567c8
commit 682d3c753e
Signed by: apio
GPG Key ID: B8A7D06E42258954
8 changed files with 62 additions and 5 deletions

View File

@ -1,6 +1,7 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
@ -14,7 +15,10 @@ int main()
atexit(bye);
printf("Welcome to %s from userspace (pid %d)!\n", "Luna", getpid());
int fd = open("/etc/motd", O_RDONLY);
mkdir("/home", 0);
mkdir("/home/user", 0);
int fd = open("/home/user/notes.txt", O_RDWR | O_CREAT);
if (fd < 0)
{
perror("open");
@ -25,7 +29,7 @@ int main()
ssize_t nread = read(fd, buffer, sizeof(buffer));
buffer[nread] = 0;
printf("/etc/motd says: %s", buffer);
printf("/home/user/notes.txt says: %s", buffer);
close(fd);

View File

@ -29,6 +29,7 @@ set(SOURCES
src/sys/open.cpp
src/sys/file.cpp
src/sys/id.cpp
src/sys/mkdir.cpp
src/fs/VFS.cpp
src/fs/tmpfs/FileSystem.cpp
src/InitRD.cpp

18
kernel/src/sys/mkdir.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "Log.h"
#include "fs/VFS.h"
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
Result<u64> sys_mkdir(Registers*, SyscallArgs args)
{
u64 path_address = args[0];
if (!MemoryManager::validate_userspace_string(path_address)) return err(EFAULT);
const char* path = (const char*)path_address;
kinfoln("mkdir: attempting to create %s", path);
TRY(VFS::create_directory(path));
return 0;
}

View File

@ -12,6 +12,7 @@ set(SOURCES
src/atexit.cpp
src/ctype.cpp
src/time.cpp
src/sys/stat.cpp
src/sys/mman.cpp
)

View File

@ -1,7 +1,7 @@
/* sys/mman.h: Memory allocation and deallocation. */
#ifndef _LUNA_MMAN_H
#define _LUNA_MMAN_H
#ifndef _SYS_MMAN_H
#define _SYS_MMAN_H
#include <bits/mmap-flags.h>
#include <sys/types.h>

20
libc/include/sys/stat.h Normal file
View File

@ -0,0 +1,20 @@
/* sys/stat.h: stat() routine and friends. */
#ifndef _SYS_STAT_H
#define _SYS_STAT_H
#include <sys/types.h>
#ifdef __cplusplus
extern "C"
{
#endif
/* Create a directory. */
int mkdir(const char* path, mode_t mode);
#ifdef __cplusplus
}
#endif
#endif

13
libc/src/sys/stat.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <bits/errno-return.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>
extern "C"
{
int mkdir(const char* path, mode_t)
{
long rc = syscall(SYS_mkdir, path);
__errno_return(rc, int);
}
}

View File

@ -2,7 +2,7 @@
#define enumerate_syscalls(_e) \
_e(exit) _e(console_write) _e(clock_gettime) _e(allocate_memory) _e(deallocate_memory) _e(usleep) _e(open) \
_e(close) _e(read) _e(getpid) _e(write) _e(lseek)
_e(close) _e(read) _e(getpid) _e(write) _e(lseek) _e(mkdir)
enum Syscalls
{