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