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