/ src / common / android_storage.h
android_storage.h
 1  // Copyright 2023 Citra Emulator Project
 2  // Licensed under GPLv2 or any later version
 3  // Refer to the license.txt file included.
 4  
 5  #pragma once
 6  
 7  #ifdef ANDROID
 8  #include <string>
 9  #include <vector>
10  #include <fcntl.h>
11  #include <jni.h>
12  
13  #define ANDROID_STORAGE_FUNCTIONS(V)                                                               \
14      V(CreateFile, bool, (const std::string& directory, const std::string& filename), create_file,  \
15        "createFile", "(Ljava/lang/String;Ljava/lang/String;)Z")                                     \
16      V(CreateDir, bool, (const std::string& directory, const std::string& filename), create_dir,    \
17        "createDir", "(Ljava/lang/String;Ljava/lang/String;)Z")                                      \
18      V(OpenContentUri, int, (const std::string& filepath, AndroidOpenMode openmode),                \
19        open_content_uri, "openContentUri", "(Ljava/lang/String;Ljava/lang/String;)I")               \
20      V(GetFilesName, std::vector<std::string>, (const std::string& filepath), get_files_name,       \
21        "getFilesName", "(Ljava/lang/String;)[Ljava/lang/String;")                                   \
22      V(CopyFile, bool,                                                                              \
23        (const std::string& source, const std::string& destination_path,                             \
24         const std::string& destination_filename),                                                   \
25        copy_file, "copyFile", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z")          \
26      V(RenameFile, bool, (const std::string& source, const std::string& filename), rename_file,     \
27        "renameFile", "(Ljava/lang/String;Ljava/lang/String;)Z")
28  #define ANDROID_SINGLE_PATH_DETERMINE_FUNCTIONS(V)                                                 \
29      V(IsDirectory, bool, is_directory, CallStaticBooleanMethod, "isDirectory",                     \
30        "(Ljava/lang/String;)Z")                                                                     \
31      V(FileExists, bool, file_exists, CallStaticBooleanMethod, "fileExists",                        \
32        "(Ljava/lang/String;)Z")                                                                     \
33      V(GetSize, std::uint64_t, get_size, CallStaticLongMethod, "getSize", "(Ljava/lang/String;)J")  \
34      V(DeleteDocument, bool, delete_document, CallStaticBooleanMethod, "deleteDocument",            \
35        "(Ljava/lang/String;)Z")
36  namespace AndroidStorage {
37  static JavaVM* g_jvm = nullptr;
38  static jclass native_library = nullptr;
39  #define FR(FunctionName, ReturnValue, JMethodID, Caller, JMethodName, Signature) F(JMethodID)
40  #define FS(FunctionName, ReturnValue, Parameters, JMethodID, JMethodName, Signature) F(JMethodID)
41  #define F(JMethodID) static jmethodID JMethodID = nullptr;
42  ANDROID_SINGLE_PATH_DETERMINE_FUNCTIONS(FR)
43  ANDROID_STORAGE_FUNCTIONS(FS)
44  #undef F
45  #undef FS
46  #undef FR
47  // Reference:
48  // https://developer.android.com/reference/android/os/ParcelFileDescriptor#parseMode(java.lang.String)
49  enum class AndroidOpenMode {
50      READ = O_RDONLY,                        // "r"
51      WRITE = O_WRONLY,                       // "w"
52      READ_WRITE = O_RDWR,                    // "rw"
53      WRITE_APPEND = O_WRONLY | O_APPEND,     // "wa"
54      WRITE_TRUNCATE = O_WRONLY | O_TRUNC,    // "wt
55      READ_WRITE_APPEND = O_RDWR | O_APPEND,  // "rwa"
56      READ_WRITE_TRUNCATE = O_RDWR | O_TRUNC, // "rwt"
57      NEVER = EINVAL,
58  };
59  
60  inline AndroidOpenMode operator|(AndroidOpenMode a, int b) {
61      return static_cast<AndroidOpenMode>(static_cast<int>(a) | b);
62  }
63  
64  AndroidOpenMode ParseOpenmode(const std::string_view openmode);
65  
66  void InitJNI(JNIEnv* env, jclass clazz);
67  
68  void CleanupJNI();
69  
70  #define FS(FunctionName, ReturnValue, Parameters, JMethodID, JMethodName, Signature)               \
71      F(FunctionName, Parameters, ReturnValue)
72  #define F(FunctionName, Parameters, ReturnValue) ReturnValue FunctionName Parameters;
73  ANDROID_STORAGE_FUNCTIONS(FS)
74  #undef F
75  #undef FS
76  
77  #define FR(FunctionName, ReturnValue, JMethodID, Caller, JMethodName, Signature)                   \
78      F(FunctionName, ReturnValue)
79  #define F(FunctionName, ReturnValue) ReturnValue FunctionName(const std::string& filepath);
80  ANDROID_SINGLE_PATH_DETERMINE_FUNCTIONS(FR)
81  #undef F
82  #undef FR
83  } // namespace AndroidStorage
84  #endif