From 023aef053c96c92c9ea15d067f7d2cb7150585d6 Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Sat, 20 Jan 2018 14:55:54 -0500
Subject: [PATCH] loader: Refactor to also pass filepath into IdentifyType.

---
 src/core/loader/elf.cpp    |  2 +-
 src/core/loader/elf.h      |  5 +++--
 src/core/loader/loader.cpp | 10 +++++-----
 src/core/loader/loader.h   |  3 ++-
 src/core/loader/nro.cpp    |  2 +-
 src/core/loader/nro.h      |  6 +++---
 src/core/loader/nso.cpp    |  3 +--
 src/core/loader/nso.h      |  7 +++----
 8 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp
index 9ba913dbeb..a41d0b94a4 100644
--- a/src/core/loader/elf.cpp
+++ b/src/core/loader/elf.cpp
@@ -364,7 +364,7 @@ SectionID ElfReader::GetSectionByName(const char* name, int firstSection) const
 
 namespace Loader {
 
-FileType AppLoader_ELF::IdentifyType(FileUtil::IOFile& file) {
+FileType AppLoader_ELF::IdentifyType(FileUtil::IOFile& file, const std::string&) {
     static constexpr u16 ELF_MACHINE_ARM{0x28};
 
     u32 magic = 0;
diff --git a/src/core/loader/elf.h b/src/core/loader/elf.h
index 113da59179..a5158338f7 100644
--- a/src/core/loader/elf.h
+++ b/src/core/loader/elf.h
@@ -22,12 +22,13 @@ public:
     /**
      * Returns the type of the file
      * @param file FileUtil::IOFile open file
+     * @param filepath Path of the file that we are opening.
      * @return FileType found, or FileType::Error if this loader doesn't know it
      */
-    static FileType IdentifyType(FileUtil::IOFile& file);
+    static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath);
 
     FileType GetFileType() override {
-        return IdentifyType(file);
+        return IdentifyType(file, filename);
     }
 
     ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp
index 92defd381f..2ecccdd4f4 100644
--- a/src/core/loader/loader.cpp
+++ b/src/core/loader/loader.cpp
@@ -21,11 +21,11 @@ const std::initializer_list<Kernel::AddressMapping> default_address_mappings = {
     {0x1F000000, 0x600000, false}, // entire VRAM
 };
 
-FileType IdentifyFile(FileUtil::IOFile& file) {
+FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath) {
     FileType type;
 
 #define CHECK_TYPE(loader)                                                                         \
-    type = AppLoader_##loader::IdentifyType(file);                                                 \
+    type = AppLoader_##loader::IdentifyType(file, filepath);                                       \
     if (FileType::Error != type)                                                                   \
         return type;
 
@@ -45,13 +45,13 @@ FileType IdentifyFile(const std::string& file_name) {
         return FileType::Unknown;
     }
 
-    return IdentifyFile(file);
+    return IdentifyFile(file, file_name);
 }
 
 FileType GuessFromExtension(const std::string& extension_) {
     std::string extension = Common::ToLower(extension_);
 
-    if (extension == ".elf" || extension == ".axf")
+    if (extension == ".elf")
         return FileType::ELF;
     else if (extension == ".nro")
         return FileType::NRO;
@@ -117,7 +117,7 @@ std::unique_ptr<AppLoader> GetLoader(const std::string& filename) {
     std::string filename_filename, filename_extension;
     Common::SplitPath(filename, nullptr, &filename_filename, &filename_extension);
 
-    FileType type = IdentifyFile(file);
+    FileType type = IdentifyFile(file, filename);
     FileType filename_type = GuessFromExtension(filename_extension);
 
     if (type != filename_type) {
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index dd6bb4e64e..f7828b7ad5 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -37,9 +37,10 @@ enum class FileType {
 /**
  * Identifies the type of a bootable file based on the magic value in its header.
  * @param file open file
+ * @param filepath Path of the file that we are opening.
  * @return FileType of file
  */
-FileType IdentifyFile(FileUtil::IOFile& file);
+FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath);
 
 /**
  * Identifies the type of a bootable file based on the magic value in its header.
diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp
index 6864a19263..ac730f8a31 100644
--- a/src/core/loader/nro.cpp
+++ b/src/core/loader/nro.cpp
@@ -45,7 +45,7 @@ struct ModHeader {
 };
 static_assert(sizeof(ModHeader) == 0x1c, "ModHeader has incorrect size.");
 
-FileType AppLoader_NRO::IdentifyType(FileUtil::IOFile& file) {
+FileType AppLoader_NRO::IdentifyType(FileUtil::IOFile& file, const std::string&) {
     // Read NSO header
     NroHeader nro_header{};
     file.Seek(0, SEEK_SET);
diff --git a/src/core/loader/nro.h b/src/core/loader/nro.h
index e20fa15558..ae4c84fb7c 100644
--- a/src/core/loader/nro.h
+++ b/src/core/loader/nro.h
@@ -4,7 +4,6 @@
 
 #pragma once
 
-#include <map>
 #include <string>
 #include "common/common_types.h"
 #include "common/file_util.h"
@@ -23,12 +22,13 @@ public:
     /**
      * Returns the type of the file
      * @param file FileUtil::IOFile open file
+     * @param filepath Path of the file that we are opening.
      * @return FileType found, or FileType::Error if this loader doesn't know it
      */
-    static FileType IdentifyType(FileUtil::IOFile& file);
+    static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath);
 
     FileType GetFileType() override {
-        return IdentifyType(file);
+        return IdentifyType(file, filepath);
     }
 
     ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;
diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp
index 77b4f93eb8..c80f2a1004 100644
--- a/src/core/loader/nso.cpp
+++ b/src/core/loader/nso.cpp
@@ -4,7 +4,6 @@
 
 #include <vector>
 #include <lz4.h>
-
 #include "common/common_funcs.h"
 #include "common/logging/log.h"
 #include "common/swap.h"
@@ -47,7 +46,7 @@ struct ModHeader {
 };
 static_assert(sizeof(ModHeader) == 0x1c, "ModHeader has incorrect size.");
 
-FileType AppLoader_NSO::IdentifyType(FileUtil::IOFile& file) {
+FileType AppLoader_NSO::IdentifyType(FileUtil::IOFile& file, const std::string&) {
     u32 magic = 0;
     file.Seek(0, SEEK_SET);
     if (1 != file.ReadArray<u32>(&magic, 1)) {
diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h
index 44d6dbbd9c..f6a2b214f2 100644
--- a/src/core/loader/nso.h
+++ b/src/core/loader/nso.h
@@ -4,7 +4,6 @@
 
 #pragma once
 
-#include <map>
 #include <string>
 #include "common/common_types.h"
 #include "common/file_util.h"
@@ -23,12 +22,13 @@ public:
     /**
      * Returns the type of the file
      * @param file FileUtil::IOFile open file
+     * @param filepath Path of the file that we are opening.
      * @return FileType found, or FileType::Error if this loader doesn't know it
      */
-    static FileType IdentifyType(FileUtil::IOFile& file);
+    static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath);
 
     FileType GetFileType() override {
-        return IdentifyType(file);
+        return IdentifyType(file, filepath);
     }
 
     static VAddr LoadModule(const std::string& path, VAddr load_base);
@@ -36,7 +36,6 @@ public:
     ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;
 
 private:
-
     std::string filepath;
 };