Compare commits

...

5 Commits

Author SHA1 Message Date
0fad179485
apps+libc+libos: Remove _LUNA_SYSTEM_ERROR_EXTENSIONS and reorder headers
All checks were successful
continuous-integration/drone/push Build is passing
libluna/libos headers can now go after errno.h, so there's no reason to keep them separate.
2023-05-02 10:51:53 +02:00
052ae4902e
libluna: Remove EFIXME and make others declare error_string() and error_name()
Closes #26.
2023-05-02 10:51:53 +02:00
7058ec945a
libluna: Use a String for name and handle prefix correctly in TarStream 2023-05-02 10:51:53 +02:00
6982a8c07e
libluna: Make String::from_string_view handle non-null-terminated strings properly 2023-05-02 10:51:52 +02:00
1444cbb3df
libluna: Allow constructing a StringView from a string that might not be null-terminated 2023-05-02 10:51:52 +02:00
25 changed files with 76 additions and 63 deletions

View File

@ -1,6 +1,5 @@
#include <os/ArgumentParser.h>
#include <os/File.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>

View File

@ -1,12 +1,11 @@
#include <errno.h>
#include <fcntl.h>
#include <luna/PathParser.h>
#include <luna/String.h>
#include <luna/Vector.h>
#include <os/Directory.h>
#include <os/File.h>
#include <os/Process.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

View File

@ -1,9 +1,8 @@
#include <fcntl.h>
#include <os/ArgumentParser.h>
#include <os/Directory.h>
#include <os/File.h>
#include <os/FileSystem.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
@ -39,11 +38,13 @@ Result<int> luna_main(int argc, char** argv)
files = TRY(dir->list(filter));
}
else
else if (os::FileSystem::exists(pathname))
{
auto str = TRY(String::from_string_view(pathname));
TRY(files.try_append(move(str)));
}
else
return err(ENOENT);
if (!long_list)
{

View File

@ -1,11 +1,10 @@
#include <errno.h>
#include <luna/String.h>
#include <luna/Vector.h>
#include <os/ArgumentParser.h>
#include <os/File.h>
#include <os/FileSystem.h>
#include <os/Process.h>
#include <errno.h>
#include <pwd.h>
#include <stddef.h>
#include <stdio.h>

View File

@ -37,13 +37,13 @@ Result<void> InitRD::populate_vfs()
{
if (entry.type == TarStream::EntryType::RegularFile)
{
auto file = TRY(VFS::create_file(entry.name, Credentials {}));
auto file = TRY(VFS::create_file(entry.name.chars(), Credentials {}));
file->write(entry.data(), 0, entry.size);
file->chmod(entry.mode & (mode_t)~S_IFMT);
}
else if (entry.type == TarStream::EntryType::Directory)
{
TRY(vfs_create_dir_if_not_exists(entry.name, entry.mode));
TRY(vfs_create_dir_if_not_exists(entry.name.chars(), entry.mode));
}
}

View File

@ -1,7 +1,6 @@
#include <errno.h>
#include <luna/ScopeGuard.h>
#include <luna/Vector.h>
#include <errno.h>
#include <stdlib.h>
extern "C" char** environ;

View File

@ -1,4 +1,3 @@
#define _LUNA_SYSTEM_ERROR_EXTENSIONS
#include <bits/errno-return.h>
#include <fcntl.h>
#include <luna/Format.h>

View File

@ -1,4 +1,3 @@
#define _LUNA_SYSTEM_ERROR_EXTENSIONS
#include <errno.h>
#include <limits.h>
#include <luna/Heap.h>

View File

@ -1,4 +1,3 @@
#define _LUNA_SYSTEM_ERROR_EXTENSIONS
#include <luna/SystemError.h>
#include <string.h>
@ -7,6 +6,8 @@ extern "C"
// All other string functions are defined in luna/CString.cpp, so the same definitions can be used by both kernel
// and userspace.
extern "C++" const char* error_string(int);
char* strerror(int errnum)
{
return const_cast<char*>(error_string(errnum));

View File

@ -1,4 +1,3 @@
#define _LUNA_SYSTEM_ERROR_EXTENSIONS
#include <bits/errno-return.h>
#include <luna/Heap.h>
#include <sys/mman.h>

View File

@ -1,4 +1,3 @@
#define _LUNA_SYSTEM_ERROR_EXTENSIONS
#include <bits/errno-return.h>
#include <luna/Check.h>
#include <luna/Format.h>

View File

@ -1,8 +1,7 @@
#include <luna/StringBuilder.h>
#include <luna/Vector.h>
#include <bits/errno-return.h>
#include <fcntl.h>
#include <luna/StringBuilder.h>
#include <luna/Vector.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>

View File

@ -1,5 +1,4 @@
#pragma once
#define _LUNA_SYSTEM_ERROR_EXTENSIONS
#include <luna/Check.h>
#include <luna/Move.h>
#include <luna/Option.h>
@ -7,6 +6,8 @@
#include <luna/SystemError.h>
#include <luna/Types.h>
extern const char* error_string(int error);
struct Error
{
Error(int err)

View File

@ -9,35 +9,53 @@ class String
typedef const char* ConstIterator;
public:
/* Constructs a String, which will take ownership of the given c-string. To create a String that makes its own
* internal copy of the passed string, use String::from_cstring(). */
String(char* c_str);
/* Constructs a String with precalculated length, which will take ownership of the given c-string. To create a
* String that makes its own internal copy of the passed string, use String::from_cstring(). */
String(char* c_str, usize length);
/* Constructs an empty String. */
String();
String(String&&);
/* Implicit copying is disabled for Strings, as copy constructors cannot use value-based error handling. To copy a
* string, please use the clone() method. */
String(const String&) = delete;
String& operator=(String&&);
/* Implicit copying is disabled for Strings, as copy constructors cannot use value-based error handling. To copy a
* string, please use the clone() method. */
String& operator=(const String&) = delete;
~String();
/* Creates a copy of this String and returns it. */
Result<String> clone() const;
/* Creates a copy of a specific segment of this String and returns it. */
Result<String> substring(usize begin, usize size) const;
/* Splits a String with a delimiter. */
Result<Vector<String>> split(StringView delim) const
{
return view().split(delim);
}
/* Splits a String into two parts with a delimiter. */
Result<Vector<String>> split_once(char delim) const
{
return view().split_once(delim);
}
/* Creates a single String consisting of a list of strings separated by a delimiter. */
static Result<String> join(const Vector<String>& vec, StringView delim);
/* Removes all trailing characters contained in delim. */
void trim(StringView delim);
static Result<String> format(const String& fmt, ...);
@ -45,10 +63,7 @@ class String
static Result<String> vformat(StringView fmt, va_list ap);
static Result<String> from_cstring(const char* str);
static Result<String> from_string_view(StringView str)
{
return from_cstring(str.chars());
}
static Result<String> from_string_view(StringView str);
const char* chars() const
{

View File

@ -43,6 +43,8 @@ class StringView
Result<Vector<String>> split(StringView delim) const;
Result<Vector<String>> split_once(char delim) const;
static StringView from_fixed_size_cstring(const char* string, usize max);
Result<usize> to_uint() const;
Iterator begin() const

View File

@ -53,14 +53,3 @@
#define EISCONN 106 // Transport endpoint is already connected
#define ETIMEDOUT 110 // Connection timed out
#define EALREADY 114 // Operation already in progress
#if defined(IN_MOON) || defined(USE_FREESTANDING) || defined(_LUNA_SYSTEM_ERROR_EXTENSIONS) || defined(_LUNA_SOURCE)
// This one is Luna-specific.
#define EFIXME 342 // Functionality not yet implemented
#ifndef _LUNA_SOURCE
const char* error_string(int error);
const char* error_name(int error);
#endif
#endif

View File

@ -1,5 +1,6 @@
#pragma once
#include <luna/Result.h>
#include <luna/String.h>
#include <luna/Types.h>
#include <sys/types.h>
@ -9,12 +10,13 @@ class TarStream
enum class EntryType
{
RegularFile,
Directory
Directory,
Unimplemented,
};
struct Entry
{
char name[257];
String name;
usize size;
mode_t mode;
EntryType type;

View File

@ -145,19 +145,23 @@ Result<String> String::vformat(StringView fmt, va_list ap)
Result<String> String::from_cstring(const char* str)
{
usize len = strlen(str);
if (len < sizeof(m_inline_storage))
return from_string_view(StringView { str });
}
Result<String> String::from_string_view(StringView str)
{
if (str.length() < sizeof(m_inline_storage))
{
String result;
result.m_inline = true;
result.m_length = len;
strncpy(result.m_inline_storage, str, sizeof(m_inline_storage));
result.m_length = str.length();
strncpy(result.m_inline_storage, str.chars(), sizeof(m_inline_storage));
return result;
}
char* const dup = strdup(str);
char* const dup = strndup(str.chars(), str.length());
if (!dup) return err(ENOMEM);
return String { dup };
return String { dup, str.length() };
}
Result<String> String::join(const Vector<String>& vec, StringView delim)

View File

@ -120,5 +120,10 @@ Result<usize> StringView::to_uint() const
Result<String> StringView::to_string()
{
return String::from_cstring(m_string);
return String::from_string_view(*this);
}
StringView StringView::from_fixed_size_cstring(const char* string, usize max)
{
return { string, strnlen(string, max) };
}

View File

@ -1,4 +1,3 @@
#define _LUNA_SYSTEM_ERROR_EXTENSIONS
#include <luna/SystemError.h>
const char* error_string(int error)
@ -57,7 +56,6 @@ const char* error_string(int error)
case EISCONN: return "Transport endpoint is already connected";
case ETIMEDOUT: return "Connection timed out";
case EALREADY: return "Operation already in progress";
case EFIXME: return "Functionality not yet implemented";
default: return "Unknown error";
}
}

View File

@ -1,8 +1,8 @@
#define _LUNA_SYSTEM_ERROR_EXTENSIONS
#include <luna/Alignment.h>
#include <luna/Alloc.h>
#include <luna/CString.h>
#include <luna/NumberParsing.h>
#include <luna/StringBuilder.h>
#include <luna/TarStream.h>
TarStream::TarStream(void* base, usize size) : m_base(base), m_pos(base), m_size(size)
@ -52,11 +52,19 @@ Result<TarStream::Entry> TarStream::parse_header(const TarStream::TarHeader* hdr
case '\0':
case '0': entry.type = EntryType::RegularFile; break;
case '5': entry.type = EntryType::Directory; break;
default: return err(EFIXME);
default: entry.type = EntryType::Unimplemented; break;
}
if (!strlen(hdr->prefix)) { strlcpy(entry.name, hdr->name, 101); }
else { return err(EFIXME); }
if (!strlen(hdr->prefix))
entry.name = TRY(String::from_string_view(StringView::from_fixed_size_cstring(hdr->name, sizeof(hdr->name))));
else
{
StringBuilder sb;
TRY(sb.add(StringView::from_fixed_size_cstring(hdr->prefix, sizeof(hdr->prefix))));
TRY(sb.add('/'));
TRY(sb.add(StringView::from_fixed_size_cstring(hdr->name, sizeof(hdr->name))));
entry.name = TRY(sb.string());
}
if (entry.size)
{

View File

@ -1,8 +1,7 @@
#include <luna/CString.h>
#include <os/Directory.h>
#include <errno.h>
#include <fcntl.h>
#include <luna/CString.h>
#include <os/Directory.h>
#include <sys/syscall.h>
#include <unistd.h>

View File

@ -1,10 +1,9 @@
#include <luna/String.h>
#include <os/Directory.h>
#include <os/FileSystem.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <luna/String.h>
#include <os/Directory.h>
#include <os/FileSystem.h>
#include <pwd.h>
#include <stdlib.h>
#include <sys/stat.h>

View File

@ -1,6 +1,5 @@
#include <os/Main.h>
#include <errno.h>
#include <os/Main.h>
#include <stdio.h>
int g_argc;

View File

@ -1,6 +1,5 @@
#include <os/Process.h>
#include <errno.h>
#include <os/Process.h>
#include <sys/syscall.h>
#include <unistd.h>