summaryrefslogtreecommitdiff
path: root/Userland/mount.cpp
diff options
context:
space:
mode:
authorLiav A <liavalb@gmail.com>2020-03-26 02:51:16 +0200
committerAndreas Kling <kling@serenityos.org>2020-04-02 12:03:08 +0200
commita60ea79a41845767ce40f225de20da7c99534ad1 (patch)
tree771b082c27a3cf8060fb23a8548be83ec8837677 /Userland/mount.cpp
parent6b59311d4bdc1447e085573f9bd2c42819e264dd (diff)
downloadserenity-a60ea79a41845767ce40f225de20da7c99534ad1.zip
Kernel & Userland: Allow to mount image files formatted with Ext2FS
Diffstat (limited to 'Userland/mount.cpp')
-rw-r--r--Userland/mount.cpp38
1 files changed, 33 insertions, 5 deletions
diff --git a/Userland/mount.cpp b/Userland/mount.cpp
index 314588a197..195d744056 100644
--- a/Userland/mount.cpp
+++ b/Userland/mount.cpp
@@ -27,8 +27,10 @@
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
+#include <AK/Optional.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
+#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@@ -54,6 +56,27 @@ int parse_options(const StringView& options)
return flags;
}
+bool is_source_none(const char* source)
+{
+ return !strcmp("none", source);
+}
+
+int get_source_fd(const char* source)
+{
+ if (is_source_none(source))
+ return -1;
+ int fd = open(source, O_RDWR);
+ if (fd < 0)
+ fd = open(source, O_RDONLY);
+ if (fd < 0) {
+ int saved_errno = errno;
+ auto message = String::format("Failed to open: %s\n", source);
+ errno = saved_errno;
+ perror(message.characters());
+ }
+ return fd;
+}
+
bool mount_all()
{
// Mount all filesystems listed in /etc/fstab.
@@ -86,7 +109,6 @@ bool mount_all()
continue;
}
- const char* devname = parts[0].characters();
const char* mountpoint = parts[1].characters();
const char* fstype = parts[2].characters();
int flags = parts.size() >= 4 ? parse_options(parts[3]) : 0;
@@ -96,11 +118,15 @@ bool mount_all()
continue;
}
- dbg() << "Mounting " << devname << "(" << fstype << ")"
+ const char* filename = parts[0].characters();
+
+ int fd = get_source_fd(filename);
+
+ dbg() << "Mounting " << filename << "(" << fstype << ")"
<< " on " << mountpoint;
- int rc = mount(devname, mountpoint, fstype, flags);
+ int rc = mount(fd, mountpoint, fstype, flags);
if (rc != 0) {
- fprintf(stderr, "Failed to mount %s (%s) on %s: %s\n", devname, fstype, mountpoint, strerror(errno));
+ fprintf(stderr, "Failed to mount %s (FD: %d) (%s) on %s: %s\n", filename, fd, fstype, mountpoint, strerror(errno));
all_ok = false;
continue;
}
@@ -179,7 +205,9 @@ int main(int argc, char** argv)
fs_type = "ext2";
int flags = options ? parse_options(options) : 0;
- if (mount(source, mountpoint, fs_type, flags) < 0) {
+ int fd = get_source_fd(source);
+
+ if (mount(fd, mountpoint, fs_type, flags) < 0) {
perror("mount");
return 1;
}