From d568b09632452b409d81b9e14689e817632a3e34 Mon Sep 17 00:00:00 2001 From: Liav A Date: Fri, 5 Jan 2024 12:27:19 +0200 Subject: [PATCH] LibCore: Add methods to convert DirectoryEntry types to names We can either convert DirectoryEntry::type to its representative name or to the POSIX DT_* name. --- Userland/Libraries/LibCore/DirectoryEntry.cpp | 50 +++++++++++++++++++ Userland/Libraries/LibCore/DirectoryEntry.h | 3 ++ 2 files changed, 53 insertions(+) diff --git a/Userland/Libraries/LibCore/DirectoryEntry.cpp b/Userland/Libraries/LibCore/DirectoryEntry.cpp index c0f9391683..b51c752e58 100644 --- a/Userland/Libraries/LibCore/DirectoryEntry.cpp +++ b/Userland/Libraries/LibCore/DirectoryEntry.cpp @@ -9,6 +9,56 @@ namespace Core { +StringView DirectoryEntry::posix_name_from_directory_entry_type(Type type) +{ + switch (type) { + case Type::BlockDevice: + return "DT_BLK"sv; + case Type::CharacterDevice: + return "DT_CHR"sv; + case Type::Directory: + return "DT_DIR"sv; + case Type::File: + return "DT_REG"sv; + case Type::NamedPipe: + return "DT_FIFO"sv; + case Type::Socket: + return "DT_SOCK"sv; + case Type::SymbolicLink: + return "DT_LNK"sv; + case Type::Unknown: + return "DT_UNKNOWN"sv; + case Type::Whiteout: + return "DT_WHT"sv; + } + VERIFY_NOT_REACHED(); +} + +StringView DirectoryEntry::representative_name_from_directory_entry_type(Type type) +{ + switch (type) { + case Type::BlockDevice: + return "BlockDevice"sv; + case Type::CharacterDevice: + return "CharacterDevice"sv; + case Type::Directory: + return "Directory"sv; + case Type::File: + return "File"sv; + case Type::NamedPipe: + return "NamedPipe"sv; + case Type::Socket: + return "Socket"sv; + case Type::SymbolicLink: + return "SymbolicLink"sv; + case Type::Unknown: + return "Unknown"sv; + case Type::Whiteout: + return "Whiteout"sv; + } + VERIFY_NOT_REACHED(); +} + DirectoryEntry::Type DirectoryEntry::directory_entry_type_from_stat(mode_t st_mode) { switch (st_mode & S_IFMT) { diff --git a/Userland/Libraries/LibCore/DirectoryEntry.h b/Userland/Libraries/LibCore/DirectoryEntry.h index c2927435ce..c33d4adf75 100644 --- a/Userland/Libraries/LibCore/DirectoryEntry.h +++ b/Userland/Libraries/LibCore/DirectoryEntry.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include namespace Core { @@ -28,6 +29,8 @@ struct DirectoryEntry { ByteString name; ino_t inode_number; + static StringView posix_name_from_directory_entry_type(Type); + static StringView representative_name_from_directory_entry_type(Type); static Type directory_entry_type_from_stat(mode_t st_mode); static DirectoryEntry from_dirent(dirent const&); static DirectoryEntry from_stat(DIR*, dirent const&);