1 /*
2 * Copyright (c) 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 #include "common_func.h"
17
18 #include <memory>
19 #include <vector>
20
21 #include "class_file/file_n_exporter.h"
22 #include "class_stat/stat_n_exporter.h"
23 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM)
24 #include "class_randomaccessfile/randomaccessfile_n_exporter.h"
25 #include "class_stream/stream_n_exporter.h"
26 #include "class_watcher/watcher_n_exporter.h"
27 #endif
28 #include "filemgmt_libhilog.h"
29 #include "properties/prop_n_exporter.h"
30
31 using namespace std;
32
33 namespace OHOS {
34 namespace FileManagement {
35 namespace ModuleFileIO {
Export(napi_env env,napi_value exports)36 static napi_value Export(napi_env env, napi_value exports)
37 {
38 InitOpenMode(env, exports);
39 std::vector<unique_ptr<NExporter>> products;
40 products.emplace_back(make_unique<PropNExporter>(env, exports));
41 products.emplace_back(make_unique<FileNExporter>(env, exports));
42 products.emplace_back(make_unique<StatNExporter>(env, exports));
43 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM)
44 products.emplace_back(make_unique<RandomAccessFileNExporter>(env, exports));
45 products.emplace_back(make_unique<StreamNExporter>(env, exports));
46 products.emplace_back(make_unique<WatcherNExporter>(env, exports));
47 #endif
48 for (auto &&product : products) {
49 #ifdef WIN_PLATFORM
50 string nExporterName = product->GetNExporterName();
51 #else
52 string nExporterName = product->GetClassName();
53 #endif
54 if (!product->Export()) {
55 HILOGE("INNER BUG. Failed to export class %{public}s for module fileio", nExporterName.c_str());
56 return nullptr;
57 } else {
58 HILOGI("Class %{public}s for module fileio has been exported", nExporterName.c_str());
59 }
60 }
61 return exports;
62 }
63
64 static napi_module _module = {
65 .nm_version = 1,
66 .nm_flags = 0,
67 .nm_filename = nullptr,
68 .nm_register_func = Export,
69 .nm_modname = "file.fs",
70 .nm_priv = ((void *)0),
71 .reserved = {0}
72 };
73
RegisterModule(void)74 extern "C" __attribute__((constructor)) void RegisterModule(void)
75 {
76 napi_module_register(&_module);
77 }
78 } // namespace ModuleFileIO
79 } // namespace FileManagement
80 } // namespace OHOS
81