mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 12:35:00 +00:00
mount: Port to LibMain
This commit is contained in:
parent
46c4988568
commit
9d48406312
2 changed files with 29 additions and 40 deletions
|
@ -135,6 +135,7 @@ target_link_libraries(mkfifo LibMain)
|
||||||
target_link_libraries(mknod LibMain)
|
target_link_libraries(mknod LibMain)
|
||||||
target_link_libraries(mktemp LibMain)
|
target_link_libraries(mktemp LibMain)
|
||||||
target_link_libraries(mv LibMain)
|
target_link_libraries(mv LibMain)
|
||||||
|
target_link_libraries(mount LibMain)
|
||||||
target_link_libraries(nc LibMain)
|
target_link_libraries(nc LibMain)
|
||||||
target_link_libraries(netstat LibMain)
|
target_link_libraries(netstat LibMain)
|
||||||
target_link_libraries(notify LibGUI)
|
target_link_libraries(notify LibGUI)
|
||||||
|
|
|
@ -4,13 +4,13 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Assertions.h>
|
|
||||||
#include <AK/JsonArray.h>
|
#include <AK/JsonArray.h>
|
||||||
#include <AK/JsonObject.h>
|
#include <AK/JsonObject.h>
|
||||||
#include <AK/JsonValue.h>
|
#include <AK/JsonValue.h>
|
||||||
#include <AK/Optional.h>
|
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
|
#include <LibCore/System.h>
|
||||||
|
#include <LibMain/Main.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -50,28 +50,24 @@ static int get_source_fd(const char* source)
|
||||||
{
|
{
|
||||||
if (is_source_none(source))
|
if (is_source_none(source))
|
||||||
return -1;
|
return -1;
|
||||||
int fd = open(source, O_RDWR);
|
auto fd_or_error = Core::System::open(source, O_RDWR);
|
||||||
if (fd < 0)
|
if (fd_or_error.is_error())
|
||||||
fd = open(source, O_RDONLY);
|
fd_or_error = Core::System::open(source, O_RDONLY);
|
||||||
if (fd < 0) {
|
if (fd_or_error.is_error()) {
|
||||||
int saved_errno = errno;
|
int saved_errno = errno;
|
||||||
auto message = String::formatted("Failed to open: {}\n", source);
|
auto message = String::formatted("Failed to open: {}\n", source);
|
||||||
errno = saved_errno;
|
errno = saved_errno;
|
||||||
perror(message.characters());
|
perror(message.characters());
|
||||||
}
|
}
|
||||||
return fd;
|
return fd_or_error.release_value();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool mount_all()
|
static ErrorOr<void> mount_all()
|
||||||
{
|
{
|
||||||
// Mount all filesystems listed in /etc/fstab.
|
// Mount all filesystems listed in /etc/fstab.
|
||||||
dbgln("Mounting all filesystems...");
|
dbgln("Mounting all filesystems...");
|
||||||
|
|
||||||
auto fstab = Core::File::construct("/etc/fstab");
|
auto fstab = TRY(Core::File::open("/etc/fstab", Core::OpenMode::ReadOnly));
|
||||||
if (!fstab->open(Core::OpenMode::ReadOnly)) {
|
|
||||||
warnln("Failed to open {}: {}", fstab->name(), fstab->error_string());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool all_ok = true;
|
bool all_ok = true;
|
||||||
while (fstab->can_read_line()) {
|
while (fstab->can_read_line()) {
|
||||||
|
@ -103,33 +99,27 @@ static bool mount_all()
|
||||||
|
|
||||||
dbgln("Mounting {} ({}) on {}", filename, fstype, mountpoint);
|
dbgln("Mounting {} ({}) on {}", filename, fstype, mountpoint);
|
||||||
|
|
||||||
int rc = mount(fd, mountpoint, fstype, flags);
|
auto error_or_void = Core::System::mount(fd, mountpoint, fstype, flags);
|
||||||
if (rc != 0) {
|
if (error_or_void.is_error()) {
|
||||||
warnln("Failed to mount {} (FD: {}) ({}) on {}: {}", filename, fd, fstype, mountpoint, strerror(errno));
|
warnln("Failed to mount {} (FD: {}) ({}) on {}: {}", filename, fd, fstype, mountpoint, strerror(errno));
|
||||||
all_ok = false;
|
all_ok = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return all_ok;
|
if (all_ok)
|
||||||
|
return {};
|
||||||
|
else
|
||||||
|
return Error::from_string_literal("One or more errors occurred. Please verify earlier output.");
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool print_mounts()
|
static ErrorOr<void> print_mounts()
|
||||||
{
|
{
|
||||||
// Output info about currently mounted filesystems.
|
// Output info about currently mounted filesystems.
|
||||||
auto df = Core::File::construct("/proc/df");
|
auto df = TRY(Core::File::open("/proc/df", Core::OpenMode::ReadOnly));
|
||||||
if (!df->open(Core::OpenMode::ReadOnly)) {
|
|
||||||
warnln("Failed to open {}: {}", df->name(), df->error_string());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto content = df->read_all();
|
auto content = df->read_all();
|
||||||
auto json_or_error = JsonValue::from_string(content);
|
auto json = TRY(JsonValue::from_string(content));
|
||||||
if (json_or_error.is_error()) {
|
|
||||||
warnln("Failed to decode JSON: {}", json_or_error.error());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
auto json = json_or_error.release_value();
|
|
||||||
|
|
||||||
json.as_array().for_each([](auto& value) {
|
json.as_array().for_each([](auto& value) {
|
||||||
auto& fs_object = value.as_object();
|
auto& fs_object = value.as_object();
|
||||||
|
@ -158,10 +148,10 @@ static bool print_mounts()
|
||||||
outln(")");
|
outln(")");
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
const char* source = nullptr;
|
const char* source = nullptr;
|
||||||
const char* mountpoint = nullptr;
|
const char* mountpoint = nullptr;
|
||||||
|
@ -175,14 +165,13 @@ int main(int argc, char** argv)
|
||||||
args_parser.add_option(fs_type, "File system type", nullptr, 't', "fstype");
|
args_parser.add_option(fs_type, "File system type", nullptr, 't', "fstype");
|
||||||
args_parser.add_option(options, "Mount options", nullptr, 'o', "options");
|
args_parser.add_option(options, "Mount options", nullptr, 'o', "options");
|
||||||
args_parser.add_option(should_mount_all, "Mount all file systems listed in /etc/fstab", nullptr, 'a');
|
args_parser.add_option(should_mount_all, "Mount all file systems listed in /etc/fstab", nullptr, 'a');
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
if (should_mount_all) {
|
if (should_mount_all)
|
||||||
return mount_all() ? 0 : 1;
|
TRY(mount_all());
|
||||||
}
|
|
||||||
|
|
||||||
if (!source && !mountpoint)
|
if (!source && !mountpoint)
|
||||||
return print_mounts() ? 0 : 1;
|
TRY(print_mounts());
|
||||||
|
|
||||||
if (source && mountpoint) {
|
if (source && mountpoint) {
|
||||||
if (!fs_type)
|
if (!fs_type)
|
||||||
|
@ -191,13 +180,12 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
int fd = get_source_fd(source);
|
int fd = get_source_fd(source);
|
||||||
|
|
||||||
if (mount(fd, mountpoint, fs_type, flags) < 0) {
|
TRY(Core::System::mount(fd, mountpoint, fs_type, flags));
|
||||||
perror("mount");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
args_parser.print_usage(stderr, argv[0]);
|
args_parser.print_usage(stderr, arguments.argv[0]);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue