Compare commits

..

No commits in common. "1733fc810d987bfd26336020b3ccc224c813a89c" and "60c6e764a4e687e8b718baa5efcd3b615f579b85" have entirely different histories.

7 changed files with 45 additions and 172 deletions

View File

@ -1,49 +1,23 @@
#include <luna/NumberParsing.h>
#include <luna/PathParser.h>
#include <os/ArgumentParser.h> #include <os/ArgumentParser.h>
#include <os/FileSystem.h> #include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
Result<void> mkdir_recursively(StringView path, mode_t mode) int main(int argc, char** argv)
{
begin:
auto rc = os::FileSystem::create_directory(path, mode);
if (!rc.has_error()) return {};
if (rc.error() == EEXIST) return {};
if (rc.error() == ENOENT)
{
PathParser parser = TRY(PathParser::create(path.chars()));
auto parent = TRY(parser.dirname());
TRY(mkdir_recursively(parent.view(), mode));
goto begin;
}
return rc.release_error();
}
Result<int> luna_main(int argc, char** argv)
{ {
StringView path; StringView path;
StringView mode_string; StringView mode_string;
bool recursive;
os::ArgumentParser parser; os::ArgumentParser parser;
parser.add_positional_argument(path, "path"_sv, true); parser.add_positional_argument(path, "path"_sv, true);
parser.add_positional_argument(mode_string, "mode"_sv, "755"_sv); parser.add_positional_argument(mode_string, "mode"_sv, "755"_sv);
parser.add_switch_argument(recursive, 'p', "parents"_sv);
parser.parse(argc, argv); parser.parse(argc, argv);
mode_t mode = (mode_t)parse_unsigned_integer(mode_string.chars(), nullptr, 8); mode_t mode = (mode_t)strtoul(mode_string.chars(), NULL, 8);
if (recursive) if (mkdir(path.chars(), mode) < 0)
{ {
TRY(mkdir_recursively(path, mode)); perror("mkdir");
return 0; return 1;
} }
TRY(os::FileSystem::create_directory(path, mode));
return 0;
} }

View File

@ -1,5 +1,5 @@
#include <os/ArgumentParser.h> #include <os/ArgumentParser.h>
#include <os/FileSystem.h> #include <stdio.h>
Result<int> luna_main(int argc, char** argv) Result<int> luna_main(int argc, char** argv)
{ {
@ -9,7 +9,11 @@ Result<int> luna_main(int argc, char** argv)
parser.add_positional_argument(path, "path"_sv, true); parser.add_positional_argument(path, "path"_sv, true);
parser.parse(argc, argv); parser.parse(argc, argv);
TRY(os::FileSystem::remove(path)); if (remove(path.chars()) < 0)
{
perror("rm");
return 1;
}
return 0; return 0;
} }

View File

@ -1,8 +1,5 @@
#include <luna/String.h>
#include <luna/Vector.h> #include <luna/Vector.h>
#include <os/ArgumentParser.h> #include <os/ArgumentParser.h>
#include <os/File.h>
#include <os/FileSystem.h>
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
@ -12,11 +9,9 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
using os::File; static Result<Vector<char*>> split_command_into_argv(const char* cmd)
static Result<Vector<char*>> split_command_into_argv(StringView cmd)
{ {
char* str = strdup(cmd.chars()); char* str = strdup(cmd);
Vector<char*> result; Vector<char*> result;
@ -36,7 +31,7 @@ static Result<Vector<char*>> split_command_into_argv(StringView cmd)
return result; return result;
} }
[[noreturn]] static void execute_command(StringView command) [[noreturn]] static void execute_command(const char* command)
{ {
Vector<char*> argv; Vector<char*> argv;
bool ok = split_command_into_argv(command).try_move_value_or_error(argv, errno); bool ok = split_command_into_argv(command).try_move_value_or_error(argv, errno);
@ -54,55 +49,48 @@ static Result<Vector<char*>> split_command_into_argv(StringView cmd)
Result<int> luna_main(int argc, char** argv) Result<int> luna_main(int argc, char** argv)
{ {
StringView path; StringView file;
StringView command; StringView command;
bool interactive { false };
SharedPtr<File> input_file; FILE* f;
os::ArgumentParser parser; os::ArgumentParser parser;
parser.add_positional_argument(path, "path"_sv, "-"_sv); parser.add_positional_argument(file, "file"_sv, "-"_sv);
parser.add_value_argument(command, 'c', "command"_sv, true); parser.add_value_argument(command, 'c', "command"_sv, true);
parser.parse(argc, argv); parser.parse(argc, argv);
if (!command.is_empty()) execute_command(command); if (!command.is_empty()) { execute_command(command.chars()); }
if (path == "-") if (file == "-") f = stdin;
{
input_file = File::standard_input();
interactive = true;
}
else else
input_file = TRY(File::open(path, File::ReadOnly)); {
f = fopen(file.chars(), "r");
if (!f)
{
perror("fopen");
return 1;
}
}
while (1) while (1)
{ {
if (interactive) if (file == "-")
{ {
auto cwd = TRY(os::FileSystem::working_directory()); char* cwd = getcwd(NULL, 0);
printf("sh %s%c ", cwd.chars(), getuid() == 0 ? '#' : '$'); if (!cwd)
{
perror("getcwd");
return 1;
}
printf("sh %s%c ", cwd, getuid() == 0 ? '#' : '$');
free(cwd);
} }
auto cmd = TRY(input_file->read_line()); char cmd[4096];
if (cmd.is_empty()) break; char* rc = fgets(cmd, sizeof(cmd), f);
if (!rc) return 0;
if (strspn(cmd.chars(), " \n") == cmd.length()) continue; if (strspn(cmd, " \n") == strlen(cmd)) continue;
if (!strncmp(cmd.chars(), "cd", 2))
{
auto args = TRY(split_command_into_argv(cmd.view()));
check("cd"_sv == args[0]);
if (args.size() == 2)
{
auto home = TRY(os::FileSystem::home_directory());
if (chdir(home.chars()) < 0) perror("cd");
continue;
}
if (chdir(args[1]) < 0) perror("cd");
continue;
}
pid_t child = fork(); pid_t child = fork();
if (child < 0) if (child < 0)
@ -111,7 +99,7 @@ Result<int> luna_main(int argc, char** argv)
return 1; return 1;
} }
if (child == 0) { execute_command(cmd.view()); } if (child == 0) { execute_command(cmd); }
if (waitpid(child, NULL, 0) < 0) if (waitpid(child, NULL, 0) < 0)
{ {
@ -119,6 +107,4 @@ Result<int> luna_main(int argc, char** argv)
return 1; return 1;
} }
} }
return 0;
} }

View File

@ -116,7 +116,7 @@ Result<u64> sys_chown(Registers*, SyscallArgs args)
Credentials& auth = Scheduler::current()->auth; Credentials& auth = Scheduler::current()->auth;
auto inode = TRY(VFS::resolve_path(path.chars(), auth, Scheduler::current()->current_directory)); auto inode = TRY(VFS::resolve_path(path.chars(), auth));
if (auth.euid != 0) return err(EPERM); if (auth.euid != 0) return err(EPERM);

View File

@ -6,7 +6,6 @@ set(SOURCES
${HEADERS} ${HEADERS}
src/ArgumentParser.cpp src/ArgumentParser.cpp
src/File.cpp src/File.cpp
src/FileSystem.cpp
src/Main.cpp src/Main.cpp
) )

View File

@ -1,21 +0,0 @@
#pragma once
#include <luna/Result.h>
#include <luna/StringView.h>
#include <sys/types.h>
namespace os
{
namespace FileSystem
{
bool exists(StringView path);
bool is_directory(StringView path);
Result<void> create_directory(StringView path, mode_t mode);
Result<void> remove(StringView path);
Result<String> working_directory();
Result<String> home_directory();
}
}

View File

@ -1,69 +0,0 @@
#include <luna/Ignore.h>
#include <luna/String.h>
#include <os/FileSystem.h>
#include <errno.h>
#include <pwd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>
namespace os::FileSystem
{
bool exists(StringView path)
{
struct stat st;
if (stat(path.chars(), &st) < 0) return false;
return true;
}
bool is_directory(StringView path)
{
struct stat st;
if (stat(path.chars(), &st) < 0) return false;
return S_ISDIR(st.st_mode);
}
Result<void> create_directory(StringView path, mode_t mode)
{
long rc = syscall(SYS_mkdir, path.chars(), mode);
int _ignore = TRY(Result<int>::from_syscall(rc));
ignore(_ignore);
return {};
}
Result<void> remove(StringView path)
{
long rc = syscall(SYS_unlink, path.chars(), 0);
int _ignore = TRY(Result<int>::from_syscall(rc));
ignore(_ignore);
return {};
}
Result<String> working_directory()
{
char* ptr = getcwd(NULL, 0);
if (!ptr) return err(errno);
return String { ptr };
}
Result<String> home_directory()
{
char* home = getenv("HOME");
if (home) return String::from_cstring(home);
struct passwd* pw = getpwuid(getuid());
if (!pw) return err(ENOENT);
return String::from_cstring(pw->pw_dir);
}
}