1 /* 2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_FUNC_H 17 #define INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_FUNC_H 18 19 #include "fd_guard.h" 20 #include "n_val.h" 21 #include "uv.h" 22 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) && !defined(CROSS_PLATFORM) 23 #include "iremote_broker.h" 24 #include "file_uri.h" 25 #endif 26 27 namespace OHOS { 28 namespace FileManagement { 29 namespace ModuleFileIO { 30 31 constexpr int RDONLY = UV_FS_O_RDONLY; 32 constexpr int WRONLY = UV_FS_O_WRONLY; 33 constexpr int RDWR = UV_FS_O_RDWR; 34 constexpr int CREATE = UV_FS_O_CREAT; 35 constexpr int TRUNC = UV_FS_O_TRUNC; 36 constexpr int APPEND = UV_FS_O_APPEND; 37 constexpr int NONBLOCK = UV_FS_O_NONBLOCK; 38 constexpr int DIRECTORY = UV_FS_O_DIRECTORY; 39 constexpr int NOFOLLOW = UV_FS_O_NOFOLLOW; 40 constexpr int SYNC = UV_FS_O_SYNC; 41 42 constexpr unsigned int MODE_EXIST = 00; 43 constexpr unsigned int MODE_WRITE = 02; 44 constexpr unsigned int MODE_READ = 04; 45 constexpr unsigned int MODE_READ_WRITE = 06; 46 47 constexpr unsigned int MODE_LOCAL = 00; 48 constexpr unsigned int MODE_LOCATION_LOCAL = 01; 49 constexpr unsigned int MODE_LOCATION_CLOUD = 02; 50 51 constexpr unsigned int USR_READ_ONLY = 00; 52 constexpr unsigned int USR_WRITE_ONLY = 01; 53 constexpr unsigned int USR_RDWR = 02; 54 constexpr unsigned int USR_CREATE = 0100; 55 constexpr unsigned int USR_TRUNC = 01000; 56 constexpr unsigned int USR_APPEND = 02000; 57 constexpr unsigned int USR_NONBLOCK = 04000; 58 constexpr unsigned int USR_DIRECTORY = 0200000; 59 constexpr unsigned int USR_NOFOLLOW = 0400000; 60 constexpr unsigned int USR_SYNC = 04010000; 61 62 const double NS = 1e9; 63 const double MS = 1e3; 64 65 struct FileInfo { 66 bool isPath = false; 67 std::unique_ptr<char[]> path = { nullptr }; 68 std::unique_ptr<DistributedFS::FDGuard> fdg = { nullptr }; 69 }; 70 71 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) && !defined(CROSS_PLATFORM) 72 class FileIoToken : public IRemoteBroker { 73 public: 74 DECLARE_INTERFACE_DESCRIPTOR(u"ohos.fileio.open"); 75 76 FileIoToken() = default; 77 virtual ~FileIoToken() noexcept = default; 78 }; 79 #endif 80 81 void InitAccessModeType(napi_env env, napi_value exports); 82 void InitAccessFlagType(napi_env env, napi_value exports); 83 void InitLocationType(napi_env env, napi_value exports); 84 void InitOpenMode(napi_env env, napi_value exports); 85 void InitWhenceType(napi_env env, napi_value exports); 86 87 struct CommonFunc { 88 static unsigned int ConvertJsFlags(unsigned int &flags); 89 static LibN::NVal InstantiateStat(napi_env env, const uv_stat_t &buf, bool async = false); 90 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) 91 static LibN::NVal InstantiateStat(napi_env env, const uv_stat_t &buf, std::shared_ptr<FileInfo> fileInfo, 92 bool async = false); 93 #endif 94 #ifndef WIN_PLATFORM 95 static LibN::NVal InstantiateFile(napi_env env, int fd, const std::string &pathOrUri, bool isUri); 96 static LibN::NVal InstantiateStream(napi_env env, std::shared_ptr<FILE> fp, bool async = false); 97 #endif 98 static std::tuple<bool, void *, size_t, int64_t> GetReadArg(napi_env env, 99 napi_value readBuf, 100 napi_value option); 101 static std::tuple<bool, std::unique_ptr<char[]>, void *, size_t, int64_t> GetWriteArg(napi_env env, 102 napi_value argWBuf, 103 napi_value argOption); 104 static std::tuple<bool, std::unique_ptr<char[]>, std::unique_ptr<char[]>> GetCopyPathArg(napi_env env, 105 napi_value srcPath, 106 napi_value dstPath); 107 static void fs_req_cleanup(uv_fs_t* req); 108 static std::string GetModeFromFlags(unsigned int flags); 109 static bool CheckPublicDirPath(const std::string &sandboxPath); 110 static std::string Decode(const std::string &uri); 111 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) && !defined(CROSS_PLATFORM) 112 static bool GetAndCheckUserId(Uri* uri, std::string &userId); 113 static bool IsSystemApp(); 114 static uint32_t GetApiCompatibleVersion(); 115 #endif 116 }; 117 } // namespace ModuleFileIO 118 } // namespace FileManagement 119 } // namespace OHOS 120 #endif // INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_FUNC_H