diff --git a/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp index 345365037b35e7d5068d9a74c9836de3c9e92107..c7db1223aef54b23b679a6ae6491cdfabca543a4 100644 --- a/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp @@ -217,7 +217,7 @@ bool Mkdirs(string path) return true; } -bool Rmdirs(string path) +bool Rmdirs(const string &path) { DIR *pDir; struct dirent *ptr = nullptr; @@ -264,7 +264,7 @@ void MkdirExec(napi_env env, void *data) path = UriToAbsolute(path); if (asyncCallbackInfo->recursive && Mkdirs(path)) { asyncCallbackInfo->result = SUCCESS; - } else if (mkdir((char *)path.c_str(), DIR_FAULT_PERM) != FAILED) { + } else if (mkdir(path.c_str(), DIR_FAULT_PERM) != FAILED) { asyncCallbackInfo->result = SUCCESS; } } @@ -297,7 +297,7 @@ void RmdirExec(napi_env env, void *data) if (statPath == COMMON_NUM::ZERO) { if (asyncCallbackInfo->recursive && Rmdirs(path)) { asyncCallbackInfo->result = SUCCESS; - } else if (remove((char *)path.c_str()) != FAILED) { + } else if (remove(path.c_str()) != FAILED) { asyncCallbackInfo->result = SUCCESS; } } else if (statPath == ENOENT) { @@ -331,7 +331,7 @@ void GetExec(napi_env env, void *data) asyncCallbackInfo->errorType = FILE_IO_ERROR; struct stat buf; int statPath = GetRealPath(path); - if (statPath == COMMON_NUM::ZERO && stat((char *)path.c_str(), &buf) == COMMON_NUM::ZERO) { + if (statPath == COMMON_NUM::ZERO && stat(path.c_str(), &buf) == COMMON_NUM::ZERO) { asyncCallbackInfo->length = buf.st_size; asyncCallbackInfo->lastMT = buf.st_mtime * COMMON_NUM::THOUSAND + (int64_t)((buf.st_mtim).tv_nsec / COMMON_NUM::MILLION); @@ -397,7 +397,7 @@ void ListExec(napi_env env, void *data) int statPath = GetRealPath(path); if (statPath == ENOENT) { asyncCallbackInfo->errorType = FILE_PATH_ERROR; - } else if (statPath != COMMON_NUM::ZERO || stat((char *)path.c_str(), &buf) != COMMON_NUM::ZERO) { + } else if (statPath != COMMON_NUM::ZERO || stat(path.c_str(), &buf) != COMMON_NUM::ZERO) { asyncCallbackInfo->errorType = FILE_IO_ERROR; } else if ((buf.st_mode & S_IFMT) == S_IFREG) { asyncCallbackInfo->result = SUCCESS; @@ -471,14 +471,14 @@ int FileCopy(const string& srcPath, const string& dstPath) if (GetRealPath(src) == 0) { if (GetRealPath(dest) == ENOENT) { FDGuard sfd; - sfd.SetFD(open((char *)src.c_str(), O_RDONLY)); + sfd.SetFD(open(src.c_str(), O_RDONLY)); struct stat attrSrc; - if (stat((char *)src.c_str(), &attrSrc) == FAILED) { + if (stat(src.c_str(), &attrSrc) == FAILED) { return FILE_IO_ERROR; } dest = UriToAbsolute(dest); FDGuard ofd; - ofd.SetFD(open((char *)dest.c_str(), O_WRONLY | O_CREAT, attrSrc.st_mode)); + ofd.SetFD(open(dest.c_str(), O_WRONLY | O_CREAT, attrSrc.st_mode)); if (sfd.GetFD() == FAILED || ofd.GetFD() == FAILED) { return FILE_IO_ERROR; } @@ -486,7 +486,7 @@ int FileCopy(const string& srcPath, const string& dstPath) if (sendfile(ofd.GetFD(), sfd.GetFD(), nullptr, attrSrc.st_size) != FAILED) { ret = SUCCESS; } else { - remove((char *)dest.c_str()); + remove(dest.c_str()); } } else if (GetRealPath(dest) == 0) { return (dest == src) ? SUCCESS : FILE_IO_ERROR; @@ -504,7 +504,7 @@ int DirCopy(const string& srcPath, const string& dstPath) } if (GetRealPath(dest) == ENOENT) { struct stat attrSrc; - if (stat((char *)src.c_str(), &attrSrc) == FAILED || !S_ISDIR(attrSrc.st_mode)) { + if (stat(src.c_str(), &attrSrc) == FAILED || !S_ISDIR(attrSrc.st_mode)) { return FILE_IO_ERROR; } dest = UriToAbsolute(dest); @@ -530,7 +530,7 @@ void CopyExec(napi_env env, void *data) } struct stat statbf; - if (stat((char *)path.c_str(), &statbf) == FAILED) { + if (stat(path.c_str(), &statbf) == FAILED) { asyncCallbackInfo->errorType = FILE_IO_ERROR; return; } @@ -588,7 +588,7 @@ int DirMove(const string& srcPath, const string& dstPath) } struct stat attrSrc; - if (stat((char *)src.c_str(), &attrSrc) == FAILED || !S_ISDIR(attrSrc.st_mode)) { + if (stat(src.c_str(), &attrSrc) == FAILED || !S_ISDIR(attrSrc.st_mode)) { return FILE_IO_ERROR; } dest = UriToAbsolute(dest); @@ -639,7 +639,7 @@ void MoveExec(napi_env env, void *data) } struct stat statbf; - if (stat((char *)path.c_str(), &statbf) == FAILED) { + if (stat(path.c_str(), &statbf) == FAILED) { asyncCallbackInfo->errorType = FILE_IO_ERROR; return; } @@ -648,7 +648,7 @@ void MoveExec(napi_env env, void *data) int retval = FileCopy(path, pathDst); if (retval == SUCCESS) { asyncCallbackInfo->result = SUCCESS; - remove((char *)path.c_str()); + remove(path.c_str()); } else { asyncCallbackInfo->errorType = retval; } @@ -689,7 +689,7 @@ void DeleteExec(napi_env env, void *data) int statPath = GetRealPath(path); if (statPath == ENOENT) { asyncCallbackInfo->errorType = FILE_PATH_ERROR; - } else if (statPath == COMMON_NUM::ZERO && remove((char *)path.c_str()) != FAILED) { + } else if (statPath == COMMON_NUM::ZERO && remove(path.c_str()) != FAILED) { asyncCallbackInfo->result = SUCCESS; } else { asyncCallbackInfo->errorType = FILE_IO_ERROR; @@ -843,7 +843,7 @@ void ReadTextExec(napi_env env, void *data) FDGuard fdg; fdg.SetFD(open(path.c_str(), O_RDONLY)); struct stat buf; - int result = stat((char *)path.c_str(), &buf); + int result = stat(path.c_str(), &buf); if (fdg.GetFD() != FAILED && result != FAILED) { auto buffer = std::make_unique(buf.st_size + 1); if (buffer == nullptr) { @@ -891,7 +891,7 @@ void ReadArrayBufferExec(napi_env env, void *data) FDGuard fdg; fdg.SetFD(open(path.c_str(), O_RDONLY)); struct stat buf; - int result = stat((char *)path.c_str(), &buf); + int result = stat(path.c_str(), &buf); if (fdg.GetFD() != FAILED && result != FAILED) { int32_t begin = (buf.st_size < asyncCallbackInfo->position) ? buf.st_size : asyncCallbackInfo->position; int32_t len = diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 0736d7b45d324f07718761bb953a232b67972e66..56e8c569579cd320c179112fc99301cfdbc54a2e 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -38,8 +38,8 @@ constexpr int SYNC = 04010000; struct FileInfo { bool isPath = false; - std::unique_ptr path; - DistributedFS::FDGuard fdg; + std::unique_ptr path = { nullptr }; + std::unique_ptr fdg = { nullptr }; }; void InitOpenMode(napi_env env, napi_value exports); diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp index 1219d709abc93e976ccfcee97f673380fba13ec9..bfe815431d4a29bef5698a563560fd7f519061eb 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -38,7 +38,7 @@ static NError IsAllPath(FileInfo& srcFile, FileInfo& destFile) new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!copyfile_req) { HILOGE("Failed to request heap memory."); - return NError(ERRNO_NOERR); + return NError(ENOMEM); } int ret = uv_fs_copyfile(nullptr, copyfile_req.get(), srcFile.path.get(), destFile.path.get(), UV_FS_COPYFILE_FICLONE, nullptr); @@ -49,14 +49,31 @@ static NError IsAllPath(FileInfo& srcFile, FileInfo& destFile) return NError(ERRNO_NOERR); } -static NError SendFileCore(FileInfo& srcFile, FileInfo& destFile) +static NError SendFileCore(FileInfo& srcFdg, FileInfo& destFdg, struct stat& statbf) +{ + std::unique_ptr sendfile_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!sendfile_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + int ret = uv_fs_sendfile(nullptr, sendfile_req.get(), destFdg.fdg->GetFD(), srcFdg.fdg->GetFD(), 0, + statbf.st_size, nullptr); + if (ret < 0) { + HILOGE("Failed to sendfile by ret : %{public}d", ret); + return NError(errno); + } + return NError(ERRNO_NOERR); +} + +static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) { if (srcFile.isPath) { std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { HILOGE("Failed to request heap memory."); - return NError(ERRNO_NOERR); + return NError(ENOMEM); } int ret = uv_fs_open(nullptr, open_req.get(), srcFile.path.get(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); @@ -64,12 +81,16 @@ static NError SendFileCore(FileInfo& srcFile, FileInfo& destFile) HILOGE("Failed to open srcFile with ret: %{public}d", ret); return NError(errno); } - srcFile.fdg.SetFD(ret, true); + srcFile.fdg = make_unique(ret, true); + if (!srcFile.fdg) { + HILOGE("Failed to request heap memory for src file descriptor."); + return NError(ENOMEM); + } } struct stat statbf; - if (fstat(srcFile.fdg.GetFD(), &statbf) < 0) { - HILOGE("Failed to get stat of file by fd: %{public}d", srcFile.fdg.GetFD()); + if (fstat(srcFile.fdg->GetFD(), &statbf) < 0) { + HILOGE("Failed to get stat of file by fd: %{public}d", srcFile.fdg->GetFD()); return NError(errno); } @@ -78,30 +99,20 @@ static NError SendFileCore(FileInfo& srcFile, FileInfo& destFile) new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { HILOGE("Failed to request heap memory."); - return NError(ERRNO_NOERR); + return NError(ENOMEM); } int ret = uv_fs_open(nullptr, open_req.get(), destFile.path.get(), O_RDWR | O_CREAT, statbf.st_mode, nullptr); if (ret < 0) { HILOGE("Failed to open destFile with ret: %{public}d", ret); return NError(errno); } - destFile.fdg.SetFD(ret, true); - } - - std::unique_ptr sendfile_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!sendfile_req) { - HILOGE("Failed to request heap memory."); - return NError(ERRNO_NOERR); - } - int ret = uv_fs_sendfile(nullptr, sendfile_req.get(), destFile.fdg.GetFD(), srcFile.fdg.GetFD(), 0, - statbf.st_size, nullptr); - if (ret < 0) { - HILOGE("Failed to sendfile by ret : %{public}d", ret); - return NError(errno); + destFile.fdg = make_unique(ret, true); + if (!destFile.fdg) { + HILOGE("Failed to request heap memory for dest file descriptor."); + return NError(ENOMEM); + } } - - return NError(ERRNO_NOERR); + return SendFileCore(srcFile, destFile, statbf); } static tuple ParseJsModeAndProm(napi_env env, const NFuncArg& funcArg) @@ -132,15 +143,16 @@ static tuple ParseJsOperand(napi_env env, NVal pathOrFdFromJsArg { auto [isPath, path, ignore] = pathOrFdFromJsArg.ToUTF8String(); if (isPath) { - return { true, FileInfo{ true, move(path), {} } }; + return { true, FileInfo { true, move(path), {} } }; } auto [isFd, fd] = pathOrFdFromJsArg.ToInt32(); if (isFd) { - return { true, FileInfo{ false, {}, { fd, false } }}; + auto fdg = make_unique(fd, false); + return { true, FileInfo { false, {}, move(fdg) } }; } - return { false, FileInfo{ false, {}, {} } }; + return { false, FileInfo { false, {}, {} } }; }; napi_value CopyFile::Sync(napi_env env, napi_callback_info info) @@ -174,7 +186,7 @@ napi_value CopyFile::Sync(napi_env env, napi_callback_info info) return nullptr; } } else { - auto err = SendFileCore(src, dest); + auto err = OpenFile(src, dest); if (err) { err.ThrowErr(env); return nullptr; @@ -211,7 +223,7 @@ napi_value CopyFile::Async(napi_env env, napi_callback_info info) if (para->src_.isPath && para->dest_.isPath) { return IsAllPath(para->src_, para->dest_); } - return SendFileCore(para->src_, para->dest_); + return OpenFile(para->src_, para->dest_); }; auto cbCompl = [](napi_env env, NError err) -> NVal { diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index 374f41bcd188a32304872573273a56930fdfd7ca..29773d02896797b7123aa850f178c090609c2caa 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -39,7 +39,13 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs NError(EINVAL).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } - return { true, FileInfo { false, {}, { fd, false } } }; + auto fdg = make_unique(fd, false); + if (!fdg) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return { false, FileInfo { false, {}, {} } }; + } + return { true, FileInfo { false, {}, move(fdg) } }; } HILOGE("Invalid parameter"); NError(EINVAL).ThrowErr(env); @@ -67,7 +73,7 @@ napi_value Stat::Sync(napi_env env, napi_callback_info info) return nullptr; } } else { - if (fstat(fileInfo.fdg.GetFD(), &buf) < 0) { + if (fstat(fileInfo.fdg->GetFD(), &buf) < 0) { HILOGE("Failed to stat file with fd"); NError(errno).ThrowErr(env); return nullptr; @@ -103,7 +109,7 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) return NError(errno); } } else { - if (fstat(fileInfo->fdg.GetFD(), &arg->stat_) < 0) { + if (fstat(fileInfo->fdg->GetFD(), &arg->stat_) < 0) { HILOGE("Failed to stat file with fd"); return NError(errno); } diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index 1c41d1d5621462927ba9cd7c061acd4bccefe6b3..bb3f49d6a5aec1f26acb824fcb1104e2fa8b7030 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -38,7 +38,13 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs NError(EINVAL).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } - return { true, FileInfo { false, {}, { fd, false } } }; + auto fdg = make_unique(fd, false); + if (!fdg) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return { false, FileInfo { false, {}, {} } }; + } + return { true, FileInfo { false, {}, move(fdg) } }; }; static NError TruncateCore(napi_env env, FileInfo &fileInfo, int truncateLen) @@ -73,7 +79,7 @@ static NError TruncateCore(napi_env env, FileInfo &fileInfo, int truncateLen) HILOGE("Failed to request heap memory."); return NError(ENOMEM); } - int ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), fileInfo.fdg.GetFD(), truncateLen, nullptr); + int ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), fileInfo.fdg->GetFD(), truncateLen, nullptr); if (ret < 0) { HILOGE("Failed to truncate file by fd"); return NError(EINVAL);