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