diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index ff16921a08348dd2286a5baf9bd805a918457ac6..db15be981931241d0f5ac10b2d413f6a15712108 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -122,15 +122,16 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) NError(-1).ThrowErr(env); return nullptr; } - std::unique_ptr open_req = { new uv_fs_t, uv_fs_req_cleanup }; - int ret = uv_fs_open(uv_default_loop(), open_req.get(), path.get(), mode, S_IRUSR | - S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, NULL); + uv_fs_t open_req; + int ret = uv_fs_open(nullptr, &open_req, path.get(), mode, S_IRUSR | + S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); + uv_fs_req_cleanup(&open_req); if (ret < 0) { HILOGE("Failed to open file for libuv error %{public}d", ret); NError(errno).ThrowErr(env); return nullptr; } - auto file = InstantiateFile(env, open_req.get()->result, path.get(), false).val_; + auto file = InstantiateFile(env, ret, path.get(), false).val_; return file; } @@ -173,14 +174,15 @@ napi_value Open::Async(napi_env env, napi_callback_info info) HILOGE("Failed to open file by Datashare"); return NError(-1); } - std::unique_ptr open_req = { new uv_fs_t, uv_fs_req_cleanup }; - int ret = uv_fs_open(uv_default_loop(), open_req.get(), path.c_str(), mode, S_IRUSR | - S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, NULL); + uv_fs_t open_req; + int ret = uv_fs_open(nullptr, &open_req, path.c_str(), mode, S_IRUSR | + S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); + uv_fs_req_cleanup(&open_req); if (ret < 0) { HILOGE("Failed to open file for libuv error %{public}d", ret); return NError(errno); } - arg->fd = open_req.get()->result; + arg->fd = ret; arg->path = path; arg->uri = ""; return NError(ERRNO_NOERR); diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 978af11322b46ed2170677312d7272bf6632f874..1d7bef93792dd4c412b281186b520c3817a85099 100755 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -49,16 +49,22 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) return nullptr; } - auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + bool succ = false; + int fd = 0; + tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { HILOGE("Invalid fd"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto [res, buf, len, hasPos, pos] = + void *buf = nullptr; + int64_t len = 0; + bool hasPos = false; + int64_t pos = 0; + tie(succ, buf, len, hasPos, pos) = CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); - if (!res) { + if (!succ) { HILOGE("Failed to resolve buf and options"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -68,14 +74,13 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) uv_buf_t buffer = uv_buf_init(static_cast(buf), len); uv_fs_t read_req; int ret = uv_fs_read(nullptr, &read_req, fd, &buffer, 1, pos, nullptr); + uv_fs_req_cleanup(&read_req); if (ret < 0) { HILOGE("Failed to read file for %{public}d", ret); NError(errno).ThrowErr(env); return nullptr; } - actLen = read_req.result; - uv_fs_req_cleanup(&read_req); - + actLen = ret; return NVal::CreateInt64(env, actLen).val_; } @@ -88,12 +93,12 @@ static NError ReadExec(shared_ptr arg, void *buf, size_t len, in uv_buf_t buffer = uv_buf_init(static_cast(buf), len); uv_fs_t read_req; int ret = uv_fs_read(nullptr, &read_req, fd, &buffer, 1, static_cast(position), nullptr); + uv_fs_req_cleanup(&read_req); if (ret < 0) { HILOGE("Failed to read file for %{public}d", ret); return NError(errno); } - arg->lenRead = read_req.result; - uv_fs_req_cleanup(&read_req); + arg->lenRead = ret; return NError(ERRNO_NOERR); } @@ -106,16 +111,22 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) return nullptr; } - auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + bool succ = false; + void *buf = nullptr; + int64_t len = 0; + int fd = 0; + bool hasPos = false; + int64_t pos = 0; + tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { HILOGE("Invalid fd"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto [res, buf, len, hasPos, pos] = + tie(succ, buf, len, hasPos, pos) = CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); - if (!res) { + if (!succ) { HILOGE("Failed to resolve buf and options"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -157,12 +168,12 @@ NError PropNExporter::WriteExec(shared_ptr arg, void *buf, size uv_buf_t buffer = uv_buf_init(static_cast(buf), len); uv_fs_t write_req; int ret = uv_fs_write(nullptr, &write_req, fd, &buffer, 1, static_cast(position), nullptr); + uv_fs_req_cleanup(&write_req); if (ret < 0) { HILOGE("Failed to write file for %{public}d", ret); return NError(errno); } - arg->actLen = write_req.result; - uv_fs_req_cleanup(&write_req); + arg->actLen = ret; return NError(ERRNO_NOERR); } @@ -175,16 +186,23 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) return nullptr; } - auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + bool succ = false; + int fd; + tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { HILOGE("Invalid fd"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto [res, bufGuard, buf, len, hasPos, position] = + unique_ptr bufGuard; + void *buf = nullptr; + size_t len = 0; + size_t position = 0; + bool hasPos = false; + tie(succ, bufGuard, buf, len, hasPos, position) = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); - if (!res) { + if (!succ) { HILOGE("Failed to resolve buf and options"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -238,16 +256,23 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) return nullptr; } - auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + bool succ = false; + int fd; + tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { HILOGE("Invalid fd"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto [res, bufGuard, buf, len, hasPos, position] = + void *buf = nullptr; + size_t len = 0; + size_t position = 0; + unique_ptr bufGuard; + bool hasPos = false; + tie(succ, bufGuard, buf, len, hasPos, position) = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); - if (!res) { + if (!succ) { HILOGE("Failed to resolve buf and options"); return nullptr; } @@ -256,14 +281,14 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) uv_buf_t buffer = uv_buf_init(static_cast(buf), len); uv_fs_t write_req; int ret = uv_fs_write(nullptr, &write_req, fd, &buffer, 1, static_cast(position), nullptr); + + uv_fs_req_cleanup(&write_req); if (ret < 0) { HILOGE("Failed to write file for %{public}d", ret); NError(errno).ThrowErr(env); return nullptr; } - writeLen = write_req.result; - uv_fs_req_cleanup(&write_req); - + writeLen = ret; return NVal::CreateInt64(env, writeLen).val_; } diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index 0c0e9db88b19213b96c1ec9e78643484cd60fe3a..490e1c8bdf75ddd9e6967289203205757575beab 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -65,23 +65,27 @@ napi_value Truncate::Sync(napi_env env, napi_callback_info info) } } if (fileInfo.isPath) { - std::unique_ptr open_req = { new uv_fs_t, uv_fs_req_cleanup }; - int ret = uv_fs_open(nullptr, open_req.get(), fileInfo.path.get(), O_RDWR, + uv_fs_t open_req; + uv_fs_t ftruncate_req; + int ret = uv_fs_open(nullptr, &open_req, fileInfo.path.get(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); + uv_fs_req_cleanup(&open_req); if (ret < 0) { NError(errno).ThrowErr(env); return nullptr; } - std::unique_ptr ftruncate_req = { new uv_fs_t, uv_fs_req_cleanup }; - ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), open_req.get()->result, truncateLen, nullptr); + + ret = uv_fs_ftruncate(nullptr, &ftruncate_req, ret, truncateLen, nullptr); + uv_fs_req_cleanup(&ftruncate_req); if (ret < 0) { HILOGE("Failed to truncate file by path"); NError(errno).ThrowErr(env); return nullptr; } } else { - std::unique_ptr ftruncate_req = { new uv_fs_t, uv_fs_req_cleanup }; - int ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), fileInfo.fdg.GetFD(), truncateLen, nullptr); + uv_fs_t ftruncate_req; + int ret = uv_fs_ftruncate(nullptr, &ftruncate_req, fileInfo.fdg.GetFD(), truncateLen, nullptr); + uv_fs_req_cleanup(&ftruncate_req); if (ret < 0) { HILOGE("Failed to truncate file by fd"); NError(EINVAL).ThrowErr(env); @@ -113,23 +117,25 @@ napi_value Truncate::Async(napi_env env, napi_callback_info info) } } auto cbExec = [fileInfo = make_shared(move(fileInfo)), truncateLen, env = env]() -> NError { - using uv_fs_unique_ptr_type = std::unique_ptr; if (fileInfo->isPath) { - uv_fs_unique_ptr_type open_req = { new uv_fs_t, uv_fs_req_cleanup }; - int ret = uv_fs_open(uv_default_loop(), open_req.get(), fileInfo->path.get(), O_RDWR, + uv_fs_t open_req; + uv_fs_t ftruncate_req; + int ret = uv_fs_open(nullptr, &open_req, fileInfo->path.get(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); + uv_fs_req_cleanup(&open_req); if (ret < 0) { return NError(errno); } - uv_fs_unique_ptr_type ftruncate_req = { new uv_fs_t, uv_fs_req_cleanup }; - ret = uv_fs_ftruncate(uv_default_loop(), ftruncate_req.get(), open_req.get()->result, truncateLen, nullptr); + ret = uv_fs_ftruncate(nullptr, &ftruncate_req, ret, truncateLen, nullptr); + uv_fs_req_cleanup(&ftruncate_req); if (ret < 0) { HILOGE("Failed to truncate file by path"); return NError(errno); } } else { - uv_fs_unique_ptr_type ftruncate_req = { new uv_fs_t, uv_fs_req_cleanup }; - int ret = uv_fs_ftruncate(uv_default_loop(), ftruncate_req.get(), fileInfo->fdg.GetFD(), truncateLen, nullptr); + uv_fs_t ftruncate_req; + int ret = uv_fs_ftruncate(nullptr, &ftruncate_req, fileInfo->fdg.GetFD(), truncateLen, nullptr); + uv_fs_req_cleanup(&ftruncate_req); if (ret < 0) { HILOGE("Failed to truncate file by fd"); return NError(errno);