浏览代码

LibCore: Add `MappedFile::map_from_file()`

This method relies on `map_from_fd_and_close()` but takes a `File`
instead of a fd.
Lucas CHOLLET 2 年之前
父节点
当前提交
5b6e93f96a
共有 2 个文件被更改,包括 8 次插入1 次删除
  1. 6 0
      Userland/Libraries/LibCore/MappedFile.cpp
  2. 2 1
      Userland/Libraries/LibCore/MappedFile.h

+ 6 - 0
Userland/Libraries/LibCore/MappedFile.cpp

@@ -6,6 +6,7 @@
 
 #include <AK/DeprecatedString.h>
 #include <AK/ScopeGuard.h>
+#include <LibCore/File.h>
 #include <LibCore/MappedFile.h>
 #include <LibCore/System.h>
 #include <fcntl.h>
@@ -20,6 +21,11 @@ ErrorOr<NonnullRefPtr<MappedFile>> MappedFile::map(StringView path)
     return map_from_fd_and_close(fd, path);
 }
 
+ErrorOr<NonnullRefPtr<MappedFile>> MappedFile::map_from_file(NonnullOwnPtr<Core::File> stream, StringView path)
+{
+    return map_from_fd_and_close(stream->leak_fd(Badge<MappedFile> {}), path);
+}
+
 ErrorOr<NonnullRefPtr<MappedFile>> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] StringView path)
 {
     TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC));

+ 2 - 1
Userland/Libraries/LibCore/MappedFile.h

@@ -10,7 +10,7 @@
 #include <AK/Noncopyable.h>
 #include <AK/NonnullRefPtr.h>
 #include <AK/RefCounted.h>
-#include <AK/Result.h>
+#include <LibCore/Forward.h>
 
 namespace Core {
 
@@ -20,6 +20,7 @@ class MappedFile : public RefCounted<MappedFile> {
 
 public:
     static ErrorOr<NonnullRefPtr<MappedFile>> map(StringView path);
+    static ErrorOr<NonnullRefPtr<MappedFile>> map_from_file(NonnullOwnPtr<Core::File>, StringView path);
     static ErrorOr<NonnullRefPtr<MappedFile>> map_from_fd_and_close(int fd, StringView path);
     ~MappedFile();