1 /*
2 * Copyright (c) 2024 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 "napi/native_api.h"
17 #include "native_common.h"
18 #include "oh_fileio.h"
19 #include "error_code.h"
20 #include <cstdlib>
21 #include <js_native_api_types.h>
22 #include <tuple>
23 #include <unistd.h>
24 #include <cstring.h>
25
GetFileLocation(napi_env env,napi_callback_info info)26 static napi_value GetFileLocation(napi_env env, napi_callback_info info)
27 {
28 FileIO_FileLocation location;
29 char uri[] = "file://com.acts.storage.environmentndk/data/storage/el2/base\
30 /haps/com.acts.storage.environmentndk/files/fileio_location.txt";
31 FileManagement_ErrCode ret = OH_FileIO_GetFileLocation(uri, strlen(uri), &location);
32 napi_value result = nullptr;
33 napi_create_int32(env, ret, &result);
34 return result;
35 }
36
GetFileLocation_noexist(napi_env env,napi_callback_info info)37 static napi_value GetFileLocation_noexist(napi_env env, napi_callback_info info)
38 {
39 FileIO_FileLocation location;
40 char uri[] = "file://docs/storage/Users/currentUser/fileio_location1.txt";
41 FileManagement_ErrCode ret = OH_FileIO_GetFileLocation(uri, strlen(uri), &location);
42 napi_value result = nullptr;
43 napi_create_int32(env, ret, &result);
44 return result;
45 }
46
47 EXTERN_C_START
Init(napi_env env,napi_value exports)48 static napi_value Init(napi_env env, napi_value exports)
49 {
50 napi_property_descriptor desc[] = {
51 {"GetFileLocation", nullptr, GetFileLocation, nullptr, nullptr, nullptr, napi_default, nullptr},
52 {"GetFileLocation_noexist", nullptr, GetFileLocation_noexist, nullptr, nullptr, nullptr, napi_default, nullptr},
53 };
54
55 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
56 return exports;
57 }
58 EXTERN_C_END
59
60 static napi_module demoModule = {
61 .nm_version = 1,
62 .nm_flags = 0,
63 .nm_filename = nullptr,
64 .nm_register_func = Init,
65 .nm_modname = "libfileio",
66 .nm_priv = ((void *)0),
67 .reserved = {0},
68 };
69
RegisterModule(void)70 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }