• 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/napi_helper.cpp"
17 #include "common/native_common.h"
18 #include "napi/native_api.h"
19 #include <cerrno>
20 #include <cstdio>
21 #include <cstring>
22 #include <fcntl.h>
23 #include <sys/sendfile.h>
24 #include <unistd.h>
25 
26 #define TEST_MODE 0666
27 #define PARAM_0 0
28 #define PARAM_1 1
29 #define ONEVAL 1
30 #define MINUSONE (-1)
31 #define ERRON_0 0
32 #define PARAM_0777 0777
33 
Sendfile(napi_env env,napi_callback_info info)34 static napi_value Sendfile(napi_env env, napi_callback_info info)
35 {
36     errno = ERRON_0;
37     size_t argc = PARAM_1;
38     napi_value args[1] = {nullptr};
39     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
40     int valueFirst = PARAM_0;
41     napi_get_value_int32(env, args[0], &valueFirst);
42     off_t off = PARAM_0;
43     napi_value result = nullptr;
44     if (valueFirst == PARAM_0) {
45         int fromfd = open("/data/storage/el2/base/files/test1.txt", O_RDONLY);
46         int tofd = open("/data/storage/el2/base/files/test2.txt", O_WRONLY | O_CREAT, TEST_MODE);
47         int senval = sendfile(tofd, fromfd, &off, ONEVAL);
48         napi_create_int32(env, senval, &result);
49         close(fromfd);
50         close(tofd);
51     } else {
52         int fromfd = MINUSONE;
53         int tofd = MINUSONE;
54         int senval = sendfile(tofd, fromfd, &off, ONEVAL);
55         napi_create_int32(env, senval, &result);
56     }
57     return result;
58 }
59 
Sendfile64(napi_env env,napi_callback_info info)60 static napi_value Sendfile64(napi_env env, napi_callback_info info)
61 {
62     char fromFile[] = "/data/storage/el2/base/files/test_send_file1.txt";
63     char toFile[] = "/data/storage/el2/base/files/test_send_file2.txt";
64     char text[] = "test sendfile64";
65     int len = strlen(text);
66     size_t argc = PARAM_1;
67     napi_value args[1] = {nullptr};
68     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
69     int param;
70     napi_get_value_int32(env, args[0], &param);
71 
72     size_t value;
73     if (param == PARAM_0) {
74         int fromfd = open(fromFile, O_CREAT | O_RDWR, PARAM_0777);
75         int tofd = open(toFile, O_CREAT | O_RDWR, PARAM_0777);
76         write(fromfd, text, len);
77         lseek(fromfd, PARAM_0, SEEK_SET);
78         value = sendfile64(tofd, fromfd, nullptr, len);
79         close(fromfd);
80         close(tofd);
81         remove(fromFile);
82         remove(toFile);
83     } else {
84         int fromfd = MINUSONE;
85         int tofd = MINUSONE;
86         value = sendfile64(tofd, fromfd, nullptr, len);
87     }
88 
89     int ret;
90     if (value == len) {
91         ret = ONEVAL;
92     } else {
93         ret = MINUSONE;
94     }
95     napi_value result = nullptr;
96     napi_create_int32(env, ret, &result);
97     return result;
98 }
99 
100 EXTERN_C_START
Init(napi_env env,napi_value exports)101 static napi_value Init(napi_env env, napi_value exports)
102 {
103     napi_property_descriptor desc[] = {
104         {"sendfile", nullptr, Sendfile, nullptr, nullptr, nullptr, napi_default, nullptr},
105         {"sendfile64", nullptr, Sendfile64, nullptr, nullptr, nullptr, napi_default, nullptr},
106     };
107     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
108     return exports;
109 }
110 
111 EXTERN_C_END
112 
113 static napi_module demoModule = {
114     .nm_version = 1,
115     .nm_flags = 0,
116     .nm_filename = nullptr,
117     .nm_register_func = Init,
118     .nm_modname = "sendfile",
119     .nm_priv = ((void *)0),
120     .reserved = {0},
121 };
122 
RegisterEntryModule(void)123 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
124