• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "open_dir.h"
17 
18 #include <memory>
19 #include <string>
20 #include <tuple>
21 
22 #include "class_dir/dir_entity.h"
23 #include "class_dir/dir_n_exporter.h"
24 #include "n_async_work_callback.h"
25 #include "n_async_work_promise.h"
26 #include "n_class.h"
27 #include "n_func_arg.h"
28 #include "n_val.h"
29 #include "uni_error.h"
30 
31 namespace OHOS {
32 namespace DistributedFS {
33 namespace ModuleFileIO {
34 using namespace std;
35 
Sync(napi_env env,napi_callback_info info)36 napi_value OpenDir::Sync(napi_env env, napi_callback_info info)
37 {
38     NFuncArg funcArg(env, info);
39     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
40         return nullptr;
41     }
42 
43     auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
44     if (!resGetFirstArg) {
45         UniError(EINVAL).ThrowErr(env, "Invalid path");
46         return nullptr;
47     }
48 
49     std::unique_ptr<DIR, std::function<void(DIR *)>> dir = { opendir(path.get()), closedir };
50     if (dir == nullptr) {
51         UniError(errno).ThrowErr(env);
52         return nullptr;
53     }
54 
55     napi_value objDir = NClass::InstantiateClass(env, DirNExporter::className_, {});
56     if (objDir == nullptr) {
57         UniError(EINVAL).ThrowErr(env, "Cannot instantiate class DirSync");
58         return nullptr;
59     }
60 
61     auto dirEntity = NClass::GetEntityOf<DirEntity>(env, objDir);
62     if (dirEntity == nullptr) {
63         UniError(EINVAL).ThrowErr(env, "Cannot get the entity of objDir");
64         return nullptr;
65     }
66 
67     dirEntity->dir_.swap(dir);
68     return objDir;
69 }
70 
71 struct OpenDirArgs {
72     NRef dirPtr_;
73     DIR *dir = nullptr;
OpenDirArgsOHOS::DistributedFS::ModuleFileIO::OpenDirArgs74     explicit OpenDirArgs(NVal dirPtr) : dirPtr_(dirPtr) {}
75     ~OpenDirArgs() = default;
76 };
77 
Async(napi_env env,napi_callback_info info)78 napi_value OpenDir::Async(napi_env env, napi_callback_info info)
79 {
80     NFuncArg funcArg(env, info);
81     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
82         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
83         return nullptr;
84     }
85 
86     auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
87     if (!resGetFirstArg) {
88         UniError(EINVAL).ThrowErr(env, "Invalid path");
89         return nullptr;
90     }
91 
92     string path = tmp.get();
93     auto arg = make_shared<OpenDirArgs>(NVal(env, funcArg.GetThisVar()));
94     auto cbExec = [arg, path](napi_env env) -> UniError {
95         DIR *dir = nullptr;
96         dir = opendir(path.c_str());
97         if (dir == nullptr) {
98             return UniError(errno);
99         }
100         arg->dir = dir;
101         return UniError(ERRNO_NOERR);
102     };
103 
104     auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
105         if (err) {
106             return { env, err.GetNapiErr(env) };
107         }
108         std::unique_ptr<DIR, std::function<void(DIR *)>> dir = { arg->dir, closedir };
109         if (!dir) {
110             return { env, UniError(errno).GetNapiErr(env) };
111         }
112 
113         napi_value objDir = NClass::InstantiateClass(env, DirNExporter::className_, {});
114         if (!objDir) {
115             return { env, UniError(EINVAL).GetNapiErr(env) };
116         }
117 
118         auto dirEntity = NClass::GetEntityOf<DirEntity>(env, objDir);
119         if (!dirEntity) {
120             return { env, UniError(EINVAL).GetNapiErr(env) };
121         }
122         dirEntity->dir_.swap(dir);
123         return { env, objDir };
124     };
125 
126     NVal thisVar(env, funcArg.GetThisVar());
127     if (funcArg.GetArgc() == NARG_CNT::ONE) {
128         return NAsyncWorkPromise(env, thisVar).Schedule("fileIOOpenDir", cbExec, cbCompl).val_;
129     } else {
130         NVal cb(env, funcArg[NARG_POS::SECOND]);
131         return NAsyncWorkCallback(env, thisVar, cb).Schedule("fileIOOpenDir", cbExec, cbCompl).val_;
132     }
133 }
134 } // namespace ModuleFileIO
135 } // namespace DistributedFS
136 } // namespace OHOS