Compare commits

...

4 Commits

15 changed files with 122 additions and 4 deletions

View File

@ -37,6 +37,7 @@ namespace Kernel
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
virtual BAN::ErrorOr<void> chmod_impl(mode_t) override;
private:
uint32_t fs_block_of_data_block_index(uint32_t data_block_index);

View File

@ -99,6 +99,7 @@ namespace Kernel
BAN::ErrorOr<size_t> read(off_t, BAN::ByteSpan buffer);
BAN::ErrorOr<size_t> write(off_t, BAN::ConstByteSpan buffer);
BAN::ErrorOr<void> truncate(size_t);
BAN::ErrorOr<void> chmod(mode_t);
bool has_data() const;
protected:
@ -115,6 +116,7 @@ namespace Kernel
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<void> truncate_impl(size_t) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<void> chmod_impl(mode_t) { return BAN::Error::from_errno(ENOTSUP); }
virtual bool has_data_impl() const { dwarnln("nonblock not supported"); return true; }
private:

View File

@ -56,6 +56,8 @@ namespace Kernel
ASSERT((inode_info.mode & Inode::Mode::TYPE_MASK) == 0);
}
virtual BAN::ErrorOr<void> chmod_impl(mode_t) override;
protected:
RamFileSystem& m_fs;
FullInodeInfo m_inode_info;

View File

@ -97,6 +97,8 @@ namespace Kernel
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_chmod(const char*, mode_t);
BAN::ErrorOr<long> sys_pipe(int fildes[2]);
BAN::ErrorOr<long> sys_dup(int fildes);
BAN::ErrorOr<long> sys_dup2(int fildes, int fildes2);

View File

@ -242,6 +242,16 @@ namespace Kernel
return {};
}
BAN::ErrorOr<void> Ext2Inode::chmod_impl(mode_t mode)
{
ASSERT((mode & Inode::Mode::TYPE_MASK) == 0);
if (m_inode.mode == mode)
return {};
m_inode.mode = (m_inode.mode & Inode::Mode::TYPE_MASK) | mode;
TRY(sync());
return {};
}
BAN::ErrorOr<void> Ext2Inode::list_next_inodes_impl(off_t offset, DirectoryEntryList* list, size_t list_size)
{
ASSERT(mode().ifdir());

View File

@ -129,6 +129,14 @@ namespace Kernel
return truncate_impl(size);
}
BAN::ErrorOr<void> Inode::chmod(mode_t mode)
{
ASSERT((mode & Inode::Mode::TYPE_MASK) == 0);
LockGuard _(m_lock);
Thread::TerminateBlocker blocker(Thread::current());
return chmod_impl(mode);
}
bool Inode::has_data() const
{
LockGuard _(m_lock);

View File

@ -26,6 +26,14 @@ namespace Kernel
this->rdev = 0;
}
BAN::ErrorOr<void> RamInode::chmod_impl(mode_t mode)
{
ASSERT((mode & Inode::Mode::TYPE_MASK) == 0);
m_inode_info.mode = (m_inode_info.mode & Inode::Mode::TYPE_MASK) | mode;
return {};
}
/*
RAM FILE INODE
@ -54,7 +62,7 @@ namespace Kernel
if (offset >= size())
return 0;
size_t to_copy = BAN::Math::min<size_t>(m_inode_info.size - offset, buffer.size());
memcpy(buffer.data(), m_data.data(), to_copy);
memcpy(buffer.data(), m_data.data() + offset, to_copy);
return to_copy;
}
@ -193,9 +201,9 @@ namespace Kernel
{
BAN::RefPtr<RamInode> inode;
if (Mode(mode).ifreg())
inode = TRY(RamFileInode::create(m_fs, mode, uid, gid));
inode = TRY(RamFileInode::create(m_fs, mode & ~Inode::Mode::TYPE_MASK, uid, gid));
else if (Mode(mode).ifdir())
inode = TRY(RamDirectoryInode::create(m_fs, ino(), mode, uid, gid));
inode = TRY(RamDirectoryInode::create(m_fs, ino(), mode & ~Inode::Mode::TYPE_MASK, uid, gid));
else
ASSERT_NOT_REACHED();

View File

@ -733,6 +733,21 @@ namespace Kernel
return TRY(m_open_file_descriptors.write(fd, BAN::ByteSpan((uint8_t*)buffer, count)));
}
BAN::ErrorOr<long> Process::sys_chmod(const char* path, mode_t mode)
{
if (mode & S_IFMASK)
return BAN::Error::from_errno(EINVAL);
LockGuard _(m_lock);
validate_string_access(path);
auto absolute_path = TRY(absolute_path_of(path));
auto file = TRY(VirtualFileSystem::get().file_from_absolute_path(m_credentials, absolute_path, O_WRONLY));
TRY(file.inode->chmod(mode));
return 0;
}
BAN::ErrorOr<long> Process::sys_pipe(int fildes[2])
{
LockGuard _(m_lock);

View File

@ -199,6 +199,9 @@ namespace Kernel
case SYS_POWEROFF:
ret = Process::current().sys_poweroff((int)arg1);
break;
case SYS_CHMOD:
ret = Process::current().sys_chmod((const char*)arg1, (mode_t)arg2);
break;
default:
dwarnln("Unknown syscall {}", syscall);
break;

View File

@ -55,6 +55,7 @@ __BEGIN_DECLS
#define SYS_MUNMAP 52
#define SYS_TTY_CTRL 53
#define SYS_POWEROFF 54
#define SYS_CHMOD 55
__END_DECLS

View File

@ -4,6 +4,11 @@
#include <sys/syscall.h>
#include <unistd.h>
int chmod(const char* path, mode_t mode)
{
return syscall(SYS_CHMOD, path, mode);
}
int fstat(int fildes, struct stat* buf)
{
return syscall(SYS_FSTAT, fildes, buf);

View File

@ -5,6 +5,7 @@ project(userspace CXX)
set(USERSPACE_PROJECTS
cat
cat-mmap
chmod
cp
dd
echo

View File

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

43
userspace/chmod/main.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
void usage(const char* argv0, int ret)
{
FILE* out = (ret == 0) ? stdout : stderr;
fprintf(out, "usage: %s MODE FILE...\n", argv0);
fprintf(out, " Change the mode of each FILE to MODE.\n");
exit(ret);
}
int main(int argc, char** argv)
{
if (argc <= 2)
usage(argv[0], 1);
int base = (argv[1][0] == '0') ? 8 : 10;
mode_t mode = 0;
for (const char* ptr = argv[1]; *ptr; ptr++)
{
if (!isdigit(*ptr))
{
fprintf(stderr, "Invalid MODE %s\n", argv[1]);
usage(argv[0], 1);
}
mode = (mode * base) + (*ptr - '0');
}
int ret = 0;
for (int i = 2; i < argc; i++)
{
if (chmod(argv[i], mode) == -1)
{
perror("chmod");
ret = 1;
}
}
return ret;
}

View File

@ -69,7 +69,7 @@ bool copy_file(const BAN::String& source, BAN::String destination)
perror("write");
ret = false;
}
if (nwrite == 0)
if (nwrite <= 0)
break;
written += nwrite;
}