LibFileSystem+Everywhere: Return ByteString from read_link()

This commit is contained in:
Sam Atkins 2024-01-15 17:24:00 +00:00 committed by Sam Atkins
parent cac66aeb53
commit 8d80841e9c
Notes: sideshowbarker 2024-07-17 05:00:08 +09:00
11 changed files with 34 additions and 43 deletions

View File

@ -81,9 +81,7 @@ TEST_CASE(test_change_file_location)
ftruncate(fd, 0);
EXPECT(fchmod(fd, 06755) != -1);
auto suid_path_string = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
auto suid_path = suid_path_string.to_byte_string();
auto suid_path = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
EXPECT(suid_path.characters());
auto new_path = ByteString::formatted("{}.renamed", suid_path);

View File

@ -218,7 +218,7 @@ TEST_CASE(unlink_symlink)
}
auto target = TRY_OR_FAIL(FileSystem::read_link("/tmp/linky"sv));
EXPECT_EQ(target.bytes_as_string_view(), "/proc/2/foo"sv);
EXPECT_EQ(target, "/proc/2/foo"sv);
rc = unlink("/tmp/linky");
EXPECT(rc >= 0);

View File

@ -86,8 +86,7 @@ TEST_CASE(test_mkstemp_unique_filename)
auto fd = mkstemp(path);
EXPECT_NE(fd, -1);
auto temp_path_string = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
auto temp_path = temp_path_string.to_byte_string();
auto temp_path = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
EXPECT(temp_path.characters());
close(fd);
@ -105,8 +104,7 @@ TEST_CASE(test_mkstemp_unique_filename)
auto fd = mkstemp(path);
EXPECT(fd != -1);
auto path2_string = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
auto path2 = path2_string.to_byte_string();
auto path2 = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
EXPECT(path2.characters());
close(fd);
@ -128,8 +126,7 @@ TEST_CASE(test_mkstemps_unique_filename)
auto fd = mkstemps(path, 6);
EXPECT_NE(fd, -1);
auto temp_path_string = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
auto temp_path = temp_path_string.to_byte_string();
auto temp_path = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
EXPECT(temp_path.characters());
close(fd);
@ -151,8 +148,7 @@ TEST_CASE(test_mkstemps_unique_filename)
auto fd = mkstemps(path, 6);
EXPECT(fd != -1);
auto path2_string = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
auto path2 = path2_string.to_byte_string();
auto path2 = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
EXPECT(path2.characters());
close(fd);

View File

@ -58,8 +58,7 @@ TEST_CASE(test_interp_header_tiny_p_filesz)
int nwritten = write(fd, buffer, sizeof(buffer));
EXPECT(nwritten);
auto elf_path_string = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
auto elf_path = elf_path_string.to_byte_string();
auto elf_path = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
EXPECT(elf_path.characters());
int rc = execl(elf_path.characters(), "test-elf", nullptr);
@ -113,8 +112,7 @@ TEST_CASE(test_interp_header_p_filesz_larger_than_p_memsz)
int nwritten = write(fd, buffer, sizeof(buffer));
EXPECT(nwritten);
auto elf_path_string = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
auto elf_path = elf_path_string.to_byte_string();
auto elf_path = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
EXPECT(elf_path.characters());
int rc = execl(elf_path.characters(), "test-elf", nullptr);
@ -172,8 +170,7 @@ TEST_CASE(test_interp_header_p_filesz_plus_p_offset_overflow_p_memsz)
int nwritten = write(fd, buffer, sizeof(buffer));
EXPECT(nwritten);
auto elf_path_string = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
auto elf_path = elf_path_string.to_byte_string();
auto elf_path = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
EXPECT(elf_path.characters());
int rc = execl(elf_path.characters(), "test-elf", nullptr);
@ -228,8 +225,7 @@ TEST_CASE(test_load_header_p_memsz_zero)
int nwritten = write(fd, buffer, sizeof(buffer));
EXPECT(nwritten);
auto elf_path_string = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
auto elf_path = elf_path_string.to_byte_string();
auto elf_path = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
EXPECT(elf_path.characters());
int rc = execl(elf_path.characters(), "test-elf", nullptr);
@ -284,8 +280,7 @@ TEST_CASE(test_load_header_p_memsz_not_equal_to_p_align)
int nwritten = write(fd, buffer, sizeof(buffer));
EXPECT(nwritten);
auto elf_path_string = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
auto elf_path = elf_path_string.to_byte_string();
auto elf_path = TRY_OR_FAIL(FileSystem::read_link(ByteString::formatted("/proc/{}/fd/{}", getpid(), fd)));
EXPECT(elf_path.characters());
int rc = execl(elf_path.characters(), "test-elf", nullptr);

View File

@ -170,9 +170,11 @@ ErrorOr<void> PropertiesWindow::create_general_tab(GUI::TabWidget& tab_widget, b
} else {
auto link_destination = link_destination_or_error.release_value();
auto* link_location = general_tab.find_descendant_of_type_named<GUI::LinkLabel>("link_location");
link_location->set_text(link_destination);
// FIXME: How do we safely display some text that might not be utf8?
auto link_destination_string = TRY(String::from_byte_string(link_destination));
link_location->set_text(link_destination_string);
link_location->on_click = [link_destination] {
auto link_directory = LexicalPath(link_destination.to_byte_string());
auto link_directory = LexicalPath(link_destination);
Desktop::Launcher::open(URL::create_with_file_scheme(link_directory.dirname(), link_directory.basename()));
};
}

View File

@ -173,17 +173,17 @@ bool is_link(int fd)
return S_ISLNK(st.st_mode);
}
static ErrorOr<String> get_duplicate_file_name(StringView path)
static ErrorOr<ByteString> get_duplicate_file_name(StringView path)
{
int duplicate_count = 0;
LexicalPath lexical_path(path);
auto parent_path = LexicalPath::canonicalized_path(lexical_path.dirname());
auto basename = lexical_path.basename();
auto current_name = TRY(String::from_byte_string(LexicalPath::join(parent_path, basename).string()));
auto current_name = LexicalPath::join(parent_path, basename).string();
while (exists(current_name)) {
++duplicate_count;
current_name = TRY(String::from_byte_string(LexicalPath::join(parent_path, TRY(String::formatted("{} ({})", basename, duplicate_count))).string()));
current_name = LexicalPath::join(parent_path, ByteString::formatted("{} ({})", basename, duplicate_count)).string();
}
return current_name;
@ -196,7 +196,7 @@ ErrorOr<void> copy_file(StringView destination_path, StringView source_path, str
if (destination_or_error.error().code() != EISDIR)
return destination_or_error.release_error();
auto destination_dir_path = TRY(String::formatted("{}/{}", destination_path, LexicalPath::basename(source_path)));
auto destination_dir_path = ByteString::formatted("{}/{}", destination_path, LexicalPath::basename(source_path));
destination_or_error = TRY(Core::File::open(destination_dir_path, Core::File::OpenMode::Write, 0666));
}
auto destination = destination_or_error.release_value();
@ -257,10 +257,10 @@ ErrorOr<void> copy_directory(StringView destination_path, StringView source_path
return di.error();
while (di.has_next()) {
auto filename = TRY(String::from_byte_string(di.next_path()));
auto filename = di.next_path();
TRY(copy_file_or_directory(
TRY(String::formatted("{}/{}", destination_path, filename)),
TRY(String::formatted("{}/{}", source_path, filename)),
ByteString::formatted("{}/{}", destination_path, filename),
ByteString::formatted("{}/{}", source_path, filename),
RecursionMode::Allowed, link, AddDuplicateFileMarker::Yes, preserve_mode));
}
@ -290,11 +290,11 @@ ErrorOr<void> copy_directory(StringView destination_path, StringView source_path
ErrorOr<void> copy_file_or_directory(StringView destination_path, StringView source_path, RecursionMode recursion_mode, LinkMode link_mode, AddDuplicateFileMarker add_duplicate_file_marker, PreserveMode preserve_mode)
{
String final_destination_path;
ByteString final_destination_path;
if (add_duplicate_file_marker == AddDuplicateFileMarker::Yes)
final_destination_path = TRY(get_duplicate_file_name(destination_path));
else
final_destination_path = TRY(String::from_utf8(destination_path));
final_destination_path = destination_path;
auto source = TRY(Core::File::open(source_path, Core::File::OpenMode::Read));
@ -383,9 +383,9 @@ bool can_delete_or_move(StringView path)
return user_id == 0 || directory_stat.st_uid == user_id || stat_or_empty(path).st_uid == user_id;
}
ErrorOr<String> read_link(StringView link_path)
ErrorOr<ByteString> read_link(StringView link_path)
{
return TRY(String::from_byte_string(TRY(Core::System::readlink(link_path))));
return Core::System::readlink(link_path);
}
ErrorOr<void> link_file(StringView destination_path, StringView source_path)

View File

@ -7,8 +7,8 @@
#pragma once
#include <AK/ByteString.h>
#include <AK/Error.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibCore/File.h>
#include <sys/stat.h>
@ -74,7 +74,7 @@ ErrorOr<void> remove(StringView path, RecursionMode);
ErrorOr<size_t> size(StringView path);
bool can_delete_or_move(StringView path);
ErrorOr<String> read_link(StringView link_path);
ErrorOr<ByteString> read_link(StringView link_path);
ErrorOr<void> link_file(StringView destination_path, StringView source_path);
bool looks_like_shared_library(StringView path);

View File

@ -259,7 +259,7 @@ Icon FileIconProvider::icon_for_path(StringView path, mode_t mode)
auto raw_symlink_target_or_error = FileSystem::read_link(path);
if (raw_symlink_target_or_error.is_error())
return s_symlink_icon;
auto raw_symlink_target = raw_symlink_target_or_error.release_value().to_byte_string();
auto raw_symlink_target = raw_symlink_target_or_error.release_value();
ByteString target_path;
if (raw_symlink_target.starts_with('/')) {

View File

@ -68,7 +68,7 @@ bool FileSystemModel::Node::fetch_data(ByteString const& full_path, bool is_root
if (sym_link_target_or_error.is_error())
perror("readlink");
else {
symlink_target = sym_link_target_or_error.release_value().to_byte_string();
symlink_target = sym_link_target_or_error.release_value();
if (symlink_target.is_empty())
perror("readlink");
}

View File

@ -309,7 +309,7 @@ void Launcher::for_each_handler_for_path(ByteString const& path, Function<bool(H
return;
}
auto link_target = LexicalPath { link_target_or_error.release_value().to_byte_string() };
auto link_target = LexicalPath { link_target_or_error.release_value() };
LexicalPath absolute_link_target = link_target.is_absolute() ? link_target : LexicalPath::join(LexicalPath::dirname(path), link_target.string());
auto real_path_or_error = FileSystem::real_path(absolute_link_target.string());
if (real_path_or_error.is_error()) {

View File

@ -54,7 +54,7 @@ enum FollowSymlinks {
No
};
static Vector<String> find_matching_executables_in_path(StringView filename, FollowSymlinks follow_symlinks = FollowSymlinks::No)
static Vector<ByteString> find_matching_executables_in_path(StringView filename, FollowSymlinks follow_symlinks = FollowSymlinks::No)
{
// Edge cases in which there are guaranteed no solutions
if (filename.is_empty() || filename.contains('/'))
@ -65,10 +65,10 @@ static Vector<String> find_matching_executables_in_path(StringView filename, Fol
if (path_str != nullptr) // maybe && *path_str
path = { path_str, strlen(path_str) };
Vector<String> executables;
Vector<ByteString> executables;
auto directories = path.split_view(':');
for (auto directory : directories) {
auto file = String::formatted("{}/{}", directory, filename).release_value_but_fixme_should_propagate_errors();
auto file = ByteString::formatted("{}/{}", directory, filename);
if (follow_symlinks == FollowSymlinks::Yes) {
auto path_or_error = FileSystem::read_link(file);