• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "prop_n_exporter.h"
17 
18 #include <cstring>
19 #include <ctime>
20 #include <iostream>
21 #include <memory>
22 #include <sstream>
23 #include <unistd.h>
24 
25 #include "common_func.h"
26 #include "class_file/file_entity.h"
27 #include "class_file/file_n_exporter.h"
28 #include "close.h"
29 #include "copy_file.h"
30 #include "create_stream.h"
31 #include "fdatasync.h"
32 #include "fdopen_stream.h"
33 #include "filemgmt_libn.h"
34 #include "fsync.h"
35 #include "js_native_api.h"
36 #include "js_native_api_types.h"
37 #include "listfile.h"
38 #include "lstat.h"
39 #include "mkdtemp.h"
40 #include "move.h"
41 #include "open.h"
42 #include "read_text.h"
43 #include "rename.h"
44 #include "rmdirent.h"
45 #include "stat.h"
46 #include "symlink.h"
47 #include "truncate.h"
48 
49 namespace OHOS {
50 namespace FileManagement {
51 namespace ModuleFileIO {
52 using namespace std;
53 using namespace OHOS::FileManagement::LibN;
54 
AccessSync(napi_env env,napi_callback_info info)55 napi_value PropNExporter::AccessSync(napi_env env, napi_callback_info info)
56 {
57     NFuncArg funcArg(env, info);
58     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
59         HILOGE("Number of arguments unmatched");
60         NError(EINVAL).ThrowErr(env);
61         return nullptr;
62     }
63 
64     auto [succ, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
65     if (!succ) {
66         HILOGE("Invalid path from JS first argument");
67         NError(EINVAL).ThrowErr(env);
68         return nullptr;
69     }
70 
71     bool isAccess = false;
72     std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> access_req = {
73         new uv_fs_t, CommonFunc::fs_req_cleanup };
74     if (!access_req) {
75         HILOGE("Failed to request heap memory.");
76         NError(ENOMEM).ThrowErr(env);
77         return nullptr;
78     }
79     int ret = uv_fs_access(nullptr, access_req.get(), path.get(), 0, nullptr);
80     if (ret < 0 && errno != ENOENT) {
81         HILOGE("Failed to access file by path");
82         NError(errno).ThrowErr(env);
83         return nullptr;
84     }
85     if (ret == 0) {
86         isAccess = true;
87     }
88     return NVal::CreateBool(env, isAccess).val_;
89 }
90 
Access(napi_env env,napi_callback_info info)91 napi_value PropNExporter::Access(napi_env env, napi_callback_info info)
92 {
93     NFuncArg funcArg(env, info);
94     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
95         HILOGE("Number of arguments unmatched");
96         NError(EINVAL).ThrowErr(env);
97         return nullptr;
98     }
99 
100     auto [succ, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
101     if (!succ) {
102         HILOGE("Invalid path from JS first argument");
103         NError(EINVAL).ThrowErr(env);
104         return nullptr;
105     }
106 
107     auto result = make_shared<AsyncAccessArg>();
108     if (!result) {
109         HILOGE("Failed to request heap memory.");
110         NError(ENOMEM).ThrowErr(env);
111         return nullptr;
112     }
113     auto cbExec = [path = string(tmp.get()), result]() -> NError {
114         std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> access_req = {
115             new uv_fs_t, CommonFunc::fs_req_cleanup };
116         if (!access_req) {
117             HILOGE("Failed to request heap memory.");
118             return NError(ENOMEM);
119         }
120         int ret = uv_fs_access(nullptr, access_req.get(), path.c_str(), 0, nullptr);
121         if (ret == 0) {
122             result->isAccess = true;
123         }
124         return (ret < 0 && errno != ENOENT) ? NError(errno) : NError(ERRNO_NOERR);
125     };
126 
127     auto cbComplete = [result](napi_env env, NError err) -> NVal {
128         if (err) {
129             return { env, err.GetNapiErr(env) };
130         }
131         return NVal::CreateBool(env, result->isAccess);
132     };
133 
134     NVal thisVar(env, funcArg.GetThisVar());
135     if (funcArg.GetArgc() == NARG_CNT::ONE) {
136         return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_ACCESS_NAME, cbExec, cbComplete).val_;
137     } else {
138         NVal cb(env, funcArg[NARG_POS::SECOND]);
139         return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_ACCESS_NAME, cbExec, cbComplete).val_;
140     }
141 }
142 
Unlink(napi_env env,napi_callback_info info)143 napi_value PropNExporter::Unlink(napi_env env, napi_callback_info info)
144 {
145     NFuncArg funcArg(env, info);
146     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
147         HILOGE("Number of Arguments Unmatched");
148         NError(EINVAL).ThrowErr(env);
149         return nullptr;
150     }
151 
152     auto [succ, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
153     if (!succ) {
154         HILOGE("Invalid path from JS first argument");
155         NError(EINVAL).ThrowErr(env);
156         return nullptr;
157     }
158 
159     auto cbExec = [path = string(tmp.get())]() -> NError {
160         std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> unlink_req = {
161             new uv_fs_t, CommonFunc::fs_req_cleanup };
162         if (!unlink_req) {
163             HILOGE("Failed to request heap memory.");
164             return NError(ENOMEM);
165         }
166         int ret = uv_fs_unlink(nullptr, unlink_req.get(), path.c_str(), nullptr);
167         if (ret < 0) {
168             HILOGE("Failed to unlink with path");
169             return NError(errno);
170         }
171         return NError(ERRNO_NOERR);
172     };
173 
174     auto cbCompl = [](napi_env env, NError err) -> NVal {
175         if (err) {
176             return { env, err.GetNapiErr(env) };
177         }
178         return { NVal::CreateUndefined(env) };
179     };
180 
181     NVal thisVar(env, funcArg.GetThisVar());
182     if (funcArg.GetArgc() == NARG_CNT::ONE) {
183         return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_UNLINK_NAME, cbExec, cbCompl).val_;
184     } else {
185         NVal cb(env, funcArg[NARG_POS::SECOND]);
186         return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_UNLINK_NAME, cbExec, cbCompl).val_;
187     }
188 }
189 
UnlinkSync(napi_env env,napi_callback_info info)190 napi_value PropNExporter::UnlinkSync(napi_env env, napi_callback_info info)
191 {
192     NFuncArg funcArg(env, info);
193     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
194         HILOGE("Number of arguments unmatched");
195         NError(EINVAL).ThrowErr(env);
196         return nullptr;
197     }
198 
199     auto [succ, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
200     if (!succ) {
201         HILOGE("Invalid path from JS first argument");
202         NError(EINVAL).ThrowErr(env);
203         return nullptr;
204     }
205 
206     std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> unlink_req = {
207         new uv_fs_t, CommonFunc::fs_req_cleanup };
208     if (!unlink_req) {
209         HILOGE("Failed to request heap memory.");
210         NError(ENOMEM).ThrowErr(env);
211         return nullptr;
212     }
213     int ret = uv_fs_unlink(nullptr, unlink_req.get(), path.get(), nullptr);
214     if (ret < 0) {
215         HILOGE("Failed to unlink with path");
216         NError(errno).ThrowErr(env);
217         return nullptr;
218     }
219 
220     return NVal::CreateUndefined(env).val_;
221 }
222 
Mkdir(napi_env env,napi_callback_info info)223 napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info)
224 {
225     NFuncArg funcArg(env, info);
226     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
227         HILOGE("Number of arguments unmatched");
228         NError(EINVAL).ThrowErr(env);
229         return nullptr;
230     }
231 
232     auto [succ, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
233     if (!succ) {
234         HILOGE("Invalid path from JS first argument");
235         NError(EINVAL).ThrowErr(env);
236         return nullptr;
237     }
238 
239     auto cbExec = [path = string(tmp.get())]() -> NError {
240         std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> mkdir_req = {
241             new uv_fs_t, CommonFunc::fs_req_cleanup };
242         if (!mkdir_req) {
243             HILOGE("Failed to request heap memory.");
244             return NError(ENOMEM);
245         }
246         int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.c_str(), DIR_DEFAULT_PERM, nullptr);
247         if (ret < 0) {
248             HILOGE("Failed to create directory");
249             return NError(errno);
250         }
251         return NError(ERRNO_NOERR);
252     };
253 
254     auto cbCompl = [](napi_env env, NError err) -> NVal {
255         if (err) {
256             return { env, err.GetNapiErr(env) };
257         }
258         return { NVal::CreateUndefined(env) };
259     };
260 
261     NVal thisVar(env, funcArg.GetThisVar());
262     if (funcArg.GetArgc() == NARG_CNT::ONE) {
263         return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_MKDIR_NAME, cbExec, cbCompl).val_;
264     } else {
265         NVal cb(env, funcArg[NARG_POS::SECOND]);
266         return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_MKDIR_NAME, cbExec, cbCompl).val_;
267     }
268 }
269 
MkdirSync(napi_env env,napi_callback_info info)270 napi_value PropNExporter::MkdirSync(napi_env env, napi_callback_info info)
271 {
272     NFuncArg funcArg(env, info);
273     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
274         HILOGE("Number of arguments unmatched");
275         NError(EINVAL).ThrowErr(env);
276         return nullptr;
277     }
278 
279     auto [succ, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
280     if (!succ) {
281         HILOGE("Invalid path from JS first argument");
282         NError(EINVAL).ThrowErr(env);
283         return nullptr;
284     }
285 
286     std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> mkdir_req = {
287         new uv_fs_t, CommonFunc::fs_req_cleanup };
288     if (!mkdir_req) {
289         HILOGE("Failed to request heap memory.");
290         NError(ENOMEM).ThrowErr(env);
291         return nullptr;
292     }
293     int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.get(), DIR_DEFAULT_PERM, nullptr);
294     if (ret < 0) {
295         HILOGE("Failed to create directory");
296         NError(errno).ThrowErr(env);
297         return nullptr;
298     }
299 
300     return NVal::CreateUndefined(env).val_;
301 }
302 
ReadSync(napi_env env,napi_callback_info info)303 napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info)
304 {
305     NFuncArg funcArg(env, info);
306 
307     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
308         HILOGE("Number of arguments unmatched");
309         NError(EINVAL).ThrowErr(env);
310         return nullptr;
311     }
312 
313     bool succ = false;
314     int fd = 0;
315     tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
316     if (!succ || fd < 0) {
317         HILOGE("Invalid fd from JS first argument");
318         NError(EINVAL).ThrowErr(env);
319         return nullptr;
320     }
321 
322     void *buf = nullptr;
323     size_t len = 0;
324     bool hasOffset = false;
325     int64_t offset = 0;
326     tie(succ, buf, len, hasOffset, offset) =
327         CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]);
328     if (!succ) {
329         HILOGE("Failed to resolve buf and options");
330         NError(EINVAL).ThrowErr(env);
331         return nullptr;
332     }
333     if (!hasOffset) {
334         offset = -1;
335     }
336 
337     uv_buf_t buffer = uv_buf_init(static_cast<char *>(buf), static_cast<unsigned int>(len));
338     std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> read_req = {
339         new uv_fs_t, CommonFunc::fs_req_cleanup };
340     if (!read_req) {
341         HILOGE("Failed to request heap memory.");
342         NError(ENOMEM).ThrowErr(env);
343         return nullptr;
344     }
345     int ret = uv_fs_read(nullptr, read_req.get(), fd, &buffer, 1, offset, nullptr);
346     if (ret < 0) {
347         HILOGE("Failed to read file for %{public}d", ret);
348         NError(errno).ThrowErr(env);
349         return nullptr;
350     }
351 
352     return NVal::CreateInt64(env, static_cast<int64_t>(ret)).val_;
353 }
354 
ReadExec(shared_ptr<AsyncIOReadArg> arg,char * buf,size_t len,int32_t fd,int64_t offset)355 static NError ReadExec(shared_ptr<AsyncIOReadArg> arg, char *buf, size_t len, int32_t fd, int64_t offset)
356 {
357     uv_buf_t buffer = uv_buf_init(buf, len);
358     std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> read_req = {
359         new uv_fs_t, CommonFunc::fs_req_cleanup };
360     if (!read_req) {
361         HILOGE("Failed to request heap memory.");
362         return NError(ENOMEM);
363     }
364     int ret = uv_fs_read(nullptr, read_req.get(), fd, &buffer, 1, offset, nullptr);
365     if (ret < 0) {
366         HILOGE("Failed to read file for %{public}d", ret);
367         return NError(errno);
368     }
369     arg->lenRead = ret;
370     return NError(ERRNO_NOERR);
371 }
372 
Read(napi_env env,napi_callback_info info)373 napi_value PropNExporter::Read(napi_env env, napi_callback_info info)
374 {
375     NFuncArg funcArg(env, info);
376     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) {
377         HILOGE("Number of arguments unmatched");
378         NError(EINVAL).ThrowErr(env);
379         return nullptr;
380     }
381 
382     bool succ = false;
383     void *buf = nullptr;
384     size_t len = 0;
385     int32_t fd = 0;
386     bool hasOffset = false;
387     int64_t offset = 0;
388     tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
389     if (!succ || fd < 0) {
390         HILOGE("Invalid fd from JS first argument");
391         NError(EINVAL).ThrowErr(env);
392         return nullptr;
393     }
394 
395     tie(succ, buf, len, hasOffset, offset) =
396         CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]);
397     if (!succ) {
398         HILOGE("Failed to resolve buf and options");
399         NError(EINVAL).ThrowErr(env);
400         return nullptr;
401     }
402     if (!hasOffset) {
403         offset = -1;
404     }
405 
406     auto arg = make_shared<AsyncIOReadArg>(NVal(env, funcArg[NARG_POS::SECOND]));
407     if (!arg) {
408         HILOGE("Failed to request heap memory.");
409         NError(ENOMEM).ThrowErr(env);
410         return nullptr;
411     }
412 
413     auto cbExec = [arg, buf, len, fd, offset]() -> NError {
414         return ReadExec(arg, static_cast<char *>(buf), len, fd, offset);
415     };
416 
417     auto cbCompl = [arg](napi_env env, NError err) -> NVal {
418         if (err) {
419             return { env, err.GetNapiErr(env) };
420         }
421         return { NVal::CreateInt64(env, static_cast<int64_t>(arg->lenRead)) };
422     };
423 
424     NVal thisVar(env, funcArg.GetThisVar());
425     bool hasOp = false;
426     if (funcArg.GetArgc() == NARG_CNT::THREE) {
427         NVal op = NVal(env, funcArg[NARG_POS::THIRD]);
428         if (op.HasProp("offset") || op.HasProp("length") || !op.TypeIs(napi_function)) {
429             hasOp = true;
430         }
431     }
432     if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == NARG_CNT::THREE && hasOp)) {
433         return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_READ_NAME, cbExec, cbCompl).val_;
434     } else {
435         int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH);
436         NVal cb(env, funcArg[cbIdx]);
437         return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_READ_NAME, cbExec, cbCompl).val_;
438     }
439 }
440 
WriteExec(shared_ptr<AsyncIOWrtieArg> arg,char * buf,size_t len,int32_t fd,int64_t offset)441 static NError WriteExec(shared_ptr<AsyncIOWrtieArg> arg, char *buf, size_t len, int32_t fd, int64_t offset)
442 {
443     uv_buf_t buffer = uv_buf_init(buf, static_cast<unsigned int>(len));
444     std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> write_req = {
445         new uv_fs_t, CommonFunc::fs_req_cleanup };
446     if (!write_req) {
447         HILOGE("Failed to request heap memory.");
448         return NError(ENOMEM);
449     }
450     int ret = uv_fs_write(nullptr, write_req.get(), fd, &buffer, 1, offset, nullptr);
451     if (ret < 0) {
452         HILOGE("Failed to write file for %{public}d", ret);
453         return NError(errno);
454     }
455     arg->actLen = ret;
456     return NError(ERRNO_NOERR);
457 }
458 
Write(napi_env env,napi_callback_info info)459 napi_value PropNExporter::Write(napi_env env, napi_callback_info info)
460 {
461     NFuncArg funcArg(env, info);
462     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) {
463         HILOGE("Number of arguments unmatched");
464         NError(EINVAL).ThrowErr(env);
465         return nullptr;
466     }
467 
468     bool succ = false;
469     int32_t fd = 0;
470     tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
471     if (!succ || fd < 0) {
472         HILOGE("Invalid fd from JS first argument");
473         NError(EINVAL).ThrowErr(env);
474         return nullptr;
475     }
476 
477     unique_ptr<char[]> bufGuard = nullptr;
478     void *buf = nullptr;
479     size_t len = 0;
480     int64_t offset = 0;
481     bool hasOffset = false;
482     tie(succ, bufGuard, buf, len, hasOffset, offset) =
483         CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]);
484     if (!succ) {
485         HILOGE("Failed to resolve buf and options");
486         NError(EINVAL).ThrowErr(env);
487         return nullptr;
488     }
489     if (!hasOffset) {
490         offset = -1;
491     }
492 
493     auto arg = make_shared<AsyncIOWrtieArg>(move(bufGuard));
494     if (!arg) {
495         HILOGE("Failed to request heap memory.");
496         NError(ENOMEM).ThrowErr(env);
497         return nullptr;
498     }
499 
500     auto cbExec = [arg, buf, len, fd, offset]() -> NError {
501         return WriteExec(arg, static_cast<char *>(buf), len, fd, offset);
502     };
503 
504     auto cbCompl = [arg](napi_env env, NError err) -> NVal {
505         if (err) {
506             return { env, err.GetNapiErr(env) };
507         } else {
508             return { NVal::CreateInt64(env, static_cast<int64_t>(arg->actLen)) };
509         }
510     };
511 
512     NVal thisVar(env, funcArg.GetThisVar());
513     bool hasOp = false;
514     if (funcArg.GetArgc() == NARG_CNT::THREE) {
515         NVal op = NVal(env, funcArg[NARG_POS::THIRD]);
516         if (op.HasProp("offset") || op.HasProp("length") || op.HasProp("encoding") || !op.TypeIs(napi_function)) {
517             hasOp = true;
518         }
519     }
520 
521     if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == NARG_CNT::THREE && hasOp)) {
522         return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_WRITE_NAME, cbExec, cbCompl).val_;
523     } else {
524         int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH);
525         NVal cb(env, funcArg[cbIdx]);
526         return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_WRITE_NAME, cbExec, cbCompl).val_;
527     }
528 }
529 
WriteSync(napi_env env,napi_callback_info info)530 napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info)
531 {
532     NFuncArg funcArg(env, info);
533     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
534         HILOGE("Number of arguments unmatched");
535         NError(EINVAL).ThrowErr(env);
536         return nullptr;
537     }
538 
539     bool succ = false;
540     int32_t fd = 0;
541     tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
542     if (!succ || fd < 0) {
543         HILOGE("Invalid fd from JS first argument");
544         NError(EINVAL).ThrowErr(env);
545         return nullptr;
546     }
547 
548     void *buf = nullptr;
549     size_t len = 0;
550     int64_t offset = 0;
551     unique_ptr<char[]> bufGuard = nullptr;
552     bool hasOffset = false;
553     tie(succ, bufGuard, buf, len, hasOffset, offset) =
554         CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]);
555     if (!succ) {
556         HILOGE("Failed to resolve buf and options");
557         return nullptr;
558     }
559     if (!hasOffset) {
560         offset = -1;
561     }
562 
563     uv_buf_t buffer = uv_buf_init(static_cast<char *>(buf), static_cast<unsigned int>(len));
564     std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> write_req = {
565         new uv_fs_t, CommonFunc::fs_req_cleanup };
566     if (!write_req) {
567         HILOGE("Failed to request heap memory.");
568         NError(ENOMEM).ThrowErr(env);
569         return nullptr;
570     }
571     int ret = uv_fs_write(nullptr, write_req.get(), fd, &buffer, 1, offset, nullptr);
572     if (ret < 0) {
573         HILOGE("Failed to write file for %{public}d", ret);
574         NError(errno).ThrowErr(env);
575         return nullptr;
576     }
577 
578     return NVal::CreateInt64(env, static_cast<int64_t>(ret)).val_;
579 }
580 
Export()581 bool PropNExporter::Export()
582 {
583     return exports_.AddProp({
584         NVal::DeclareNapiFunction("access", Access),
585         NVal::DeclareNapiFunction("accessSync", AccessSync),
586         NVal::DeclareNapiFunction("close", Close::Async),
587         NVal::DeclareNapiFunction("closeSync", Close::Sync),
588         NVal::DeclareNapiFunction("copyFile", CopyFile::Async),
589         NVal::DeclareNapiFunction("copyFileSync", CopyFile::Sync),
590         NVal::DeclareNapiFunction("createStream", CreateStream::Async),
591         NVal::DeclareNapiFunction("createStreamSync", CreateStream::Sync),
592         NVal::DeclareNapiFunction("fdatasync", Fdatasync::Async),
593         NVal::DeclareNapiFunction("fdatasyncSync", Fdatasync::Sync),
594         NVal::DeclareNapiFunction("fdopenStream", FdopenStream::Async),
595         NVal::DeclareNapiFunction("fdopenStreamSync", FdopenStream::Sync),
596         NVal::DeclareNapiFunction("fsync", Fsync::Async),
597         NVal::DeclareNapiFunction("fsyncSync", Fsync::Sync),
598         NVal::DeclareNapiFunction("listFile", ListFile::Async),
599         NVal::DeclareNapiFunction("listFileSync", ListFile::Sync),
600         NVal::DeclareNapiFunction("lstat", Lstat::Async),
601         NVal::DeclareNapiFunction("lstatSync", Lstat::Sync),
602         NVal::DeclareNapiFunction("mkdir", Mkdir),
603         NVal::DeclareNapiFunction("mkdirSync", MkdirSync),
604         NVal::DeclareNapiFunction("mkdtemp", Mkdtemp::Async),
605         NVal::DeclareNapiFunction("mkdtempSync", Mkdtemp::Sync),
606         NVal::DeclareNapiFunction("moveFile", Move::Async),
607         NVal::DeclareNapiFunction("moveFileSync", Move::Sync),
608         NVal::DeclareNapiFunction("open", Open::Async),
609         NVal::DeclareNapiFunction("openSync", Open::Sync),
610         NVal::DeclareNapiFunction("read", Read),
611         NVal::DeclareNapiFunction("readSync", ReadSync),
612         NVal::DeclareNapiFunction("readText", ReadText::Async),
613         NVal::DeclareNapiFunction("readTextSync", ReadText::Sync),
614         NVal::DeclareNapiFunction("rename", Rename::Async),
615         NVal::DeclareNapiFunction("renameSync", Rename::Sync),
616         NVal::DeclareNapiFunction("rmdir", Rmdirent::Async),
617         NVal::DeclareNapiFunction("rmdirSync", Rmdirent::Sync),
618         NVal::DeclareNapiFunction("stat", Stat::Async),
619         NVal::DeclareNapiFunction("statSync", Stat::Sync),
620         NVal::DeclareNapiFunction("symlink", Symlink::Async),
621         NVal::DeclareNapiFunction("symlinkSync", Symlink::Sync),
622         NVal::DeclareNapiFunction("truncate", Truncate::Async),
623         NVal::DeclareNapiFunction("truncateSync", Truncate::Sync),
624         NVal::DeclareNapiFunction("unlink", Unlink),
625         NVal::DeclareNapiFunction("unlinkSync", UnlinkSync),
626         NVal::DeclareNapiFunction("write", Write),
627         NVal::DeclareNapiFunction("writeSync", WriteSync),
628     });
629 }
630 
GetClassName()631 string PropNExporter::GetClassName()
632 {
633     return PropNExporter::className_;
634 }
635 
PropNExporter(napi_env env,napi_value exports)636 PropNExporter::PropNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}
637 
~PropNExporter()638 PropNExporter::~PropNExporter() {}
639 } // namespace ModuleFileIO
640 } // namespace FileManagement
641 } // namespace OHOS