1 /* 2 * Copyright (c) 2022 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 23 namespace OHOS { 24 namespace FileManagement { 25 namespace ModuleFileIO { 26 27 constexpr int64_t INVALID_POSITION = std::numeric_limits<decltype(INVALID_POSITION)>::max(); 28 constexpr int RDONLY = 00; 29 constexpr int WRONLY = 01; 30 constexpr int RDWR = 02; 31 constexpr int CREATE = 0100; 32 constexpr int TRUNC = 01000; 33 constexpr int APPEND = 02000; 34 constexpr int NONBLOCK = 04000; 35 constexpr int DIRECTORY = 0200000; 36 constexpr int NOFOLLOW = 0400000; 37 constexpr int SYNC = 04010000; 38 39 struct FileInfo { 40 bool isPath = false; 41 std::unique_ptr<char[]> path = { nullptr }; 42 std::unique_ptr<DistributedFS::FDGuard> fdg = { nullptr }; 43 }; 44 45 void InitOpenMode(napi_env env, napi_value exports); 46 47 struct CommonFunc { 48 static unsigned int ConvertJsFlags(unsigned int &flags); 49 static LibN::NVal InstantiateStat(napi_env env, struct stat &buf); 50 static LibN::NVal InstantiateStream(napi_env env, std::unique_ptr<FILE, decltype(&fclose)> fp); 51 static std::tuple<bool, void *, size_t, bool, int64_t> GetReadArg(napi_env env, 52 napi_value readBuf, 53 napi_value option); 54 static std::tuple<bool, std::unique_ptr<char[]>, void *, size_t, bool, int64_t> GetWriteArg(napi_env env, 55 napi_value argWBuf, 56 napi_value argOption); 57 static std::tuple<bool, std::unique_ptr<char[]>, std::unique_ptr<char[]>> GetCopyPathArg(napi_env env, 58 napi_value srcPath, 59 napi_value dstPath); 60 static void fs_req_cleanup(uv_fs_t* req); 61 static std::string GetModeFromFlags(unsigned int flags); 62 }; 63 } // namespace ModuleFileIO 64 } // namespace FileManagement 65 } // namespace OHOS 66 #endif // INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_FUNC_H