1 /*
2 * Copyright (C) 2022 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 <memory>
17 #include <vector>
18
19 #include "filemgmt_libn.h"
20 #include "file_extension_info_napi.h"
21 #include "hilog_wrapper.h"
22 #include "napi_fileaccess_helper.h"
23 #include "napi_file_info_exporter.h"
24 #include "napi_file_iterator_exporter.h"
25 #include "napi_root_info_exporter.h"
26 #include "napi_root_iterator_exporter.h"
27
28 namespace OHOS {
29 namespace FileAccessFwk {
30 using namespace FileManagement::LibN;
31
32 EXTERN_C_START
33 /*
34 * The module initialization.
35 */
Init(napi_env env,napi_value exports)36 static napi_value Init(napi_env env, napi_value exports)
37 {
38 FileAccessHelperInit(env, exports);
39 InitDeviceFlag(env, exports);
40 InitDocumentFlag(env, exports);
41 InitNotifyType(env, exports);
42 InitDeviceType(env, exports);
43 InitFileInfo(env, exports);
44 InitRootInfo(env, exports);
45 InitOpenFlags(env, exports);
46
47 std::vector<std::unique_ptr<NExporter>> products;
48 products.emplace_back(std::make_unique<NapiRootIteratorExporter>(env, exports));
49 products.emplace_back(std::make_unique<NapiRootInfoExporter>(env, exports));
50 products.emplace_back(std::make_unique<NapiFileIteratorExporter>(env, exports));
51 products.emplace_back(std::make_unique<NapiFileInfoExporter>(env, exports));
52 for (auto &&product : products) {
53 if (!product->Export()) {
54 HILOG_ERROR("INNER BUG. Failed to export class %{public}s", product->GetClassName().c_str());
55 return nullptr;
56 } else {
57 HILOG_ERROR("Class %{public}s has been exported", product->GetClassName().c_str());
58 }
59 }
60
61 return exports;
62 }
63 EXTERN_C_END
64
65 /*
66 * The module definition.
67 */
68 static napi_module _module = {
69 .nm_version = 1,
70 .nm_flags = 0,
71 .nm_filename = nullptr,
72 .nm_register_func = Init,
73 .nm_modname = "file.fileAccess",
74 .nm_priv = ((void *)0),
75 .reserved = {0}
76 };
77
78 /*
79 * The module registration.
80 */
RegisterModule(void)81 extern "C" __attribute__((constructor)) void RegisterModule(void)
82 {
83 napi_module_register(&_module);
84 }
85 } // namespace FileAccessFwk
86 } // namespace OHOS