• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 int RDONLY = UV_FS_O_RDONLY;
28 constexpr int WRONLY = UV_FS_O_WRONLY;
29 constexpr int RDWR = UV_FS_O_RDWR;
30 constexpr int CREATE = UV_FS_O_CREAT;
31 constexpr int TRUNC = UV_FS_O_TRUNC;
32 constexpr int APPEND = UV_FS_O_APPEND;
33 constexpr int NONBLOCK = UV_FS_O_NONBLOCK;
34 constexpr int DIRECTORY = UV_FS_O_DIRECTORY;
35 constexpr int NOFOLLOW = UV_FS_O_NOFOLLOW;
36 constexpr int SYNC = UV_FS_O_SYNC;
37 
38 constexpr unsigned int USR_READ_ONLY = 00;
39 constexpr unsigned int USR_WRITE_ONLY = 01;
40 constexpr unsigned int USR_RDWR = 02;
41 constexpr unsigned int USR_CREATE = 0100;
42 constexpr unsigned int USR_TRUNC = 01000;
43 constexpr unsigned int USR_APPEND = 02000;
44 constexpr unsigned int USR_NONBLOCK = 04000;
45 constexpr unsigned int USR_DIRECTORY = 0200000;
46 constexpr unsigned int USR_NOFOLLOW = 0400000;
47 constexpr unsigned int USR_SYNC = 04010000;
48 
49 struct FileInfo {
50     bool isPath = false;
51     std::unique_ptr<char[]> path = { nullptr };
52     std::unique_ptr<DistributedFS::FDGuard> fdg = { nullptr };
53 };
54 
55 void InitOpenMode(napi_env env, napi_value exports);
56 
57 struct CommonFunc {
58     static unsigned int ConvertJsFlags(unsigned int &flags);
59     static LibN::NVal InstantiateStat(napi_env env, const uv_stat_t &buf);
60 #ifndef WIN_PLATFORM
61     static LibN::NVal InstantiateFile(napi_env env, int fd, std::string pathOrUri, bool isUri);
62     static LibN::NVal InstantiateStream(napi_env env, std::unique_ptr<FILE, decltype(&fclose)> fp);
63 #endif
64     static std::tuple<bool, void *, size_t, int64_t> GetReadArg(napi_env env,
65                                                                        napi_value readBuf,
66                                                                        napi_value option);
67     static std::tuple<bool, std::unique_ptr<char[]>, void *, size_t, int64_t> GetWriteArg(napi_env env,
68                                                                                                  napi_value argWBuf,
69                                                                                                  napi_value argOption);
70     static std::tuple<bool, std::unique_ptr<char[]>, std::unique_ptr<char[]>> GetCopyPathArg(napi_env env,
71                                                                                              napi_value srcPath,
72                                                                                              napi_value dstPath);
73     static void fs_req_cleanup(uv_fs_t* req);
74     static std::string GetModeFromFlags(unsigned int flags);
75     static bool CheckPublicDirPath(const std::string &path);
76     static std::string Decode(const std::string &uri);
77 };
78 } // namespace ModuleFileIO
79 } // namespace FileManagement
80 } // namespace OHOS
81 #endif // INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_FUNC_H