diff --git a/frameworks/native/backup_ext/src/ext_backup_context_js.cpp b/frameworks/native/backup_ext/src/ext_backup_context_js.cpp index 57b3fbe531fc6a9052fa55ac66e95020dc4773c8..bafc88c59e22c8d6da3729adbc13edbe18bc1efe 100644 --- a/frameworks/native/backup_ext/src/ext_backup_context_js.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_context_js.cpp @@ -60,7 +60,10 @@ napi_value ExtBackupContextJS::OnGetBackupDir(napi_env env, [[maybe_unused]]Abil return nullptr; } std::string backupDir = context->GetBackupDir(); - napi_create_string_utf8(env, backupDir.c_str(), backupDir.size(), &result); + if (napi_create_string_utf8(env, backupDir.c_str(), backupDir.size(), &result) != napi_ok) { + HILOGE("create napi string failed"); + return nullptr; + } return result; } } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_ext/src/ext_backup_js.cpp b/frameworks/native/backup_ext/src/ext_backup_js.cpp index 2daa22139f8da56e29d9908941015cb0d844afc6..9be9b12b60764b2368e8ab5b48ca7eaf2cac0390 100644 --- a/frameworks/native/backup_ext/src/ext_backup_js.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_js.cpp @@ -250,8 +250,11 @@ static bool CallCatchPromise(AbilityRuntime::JsRuntime &jsRuntime, napi_value re return false; } napi_value ret; - napi_create_function(env, "promiseCatchCallback", strlen("promiseCatchCallback"), PromiseCatchCallback, - callbackInfo, &ret); + if (napi_create_function(env, "promiseCatchCallback", strlen("promiseCatchCallback"), PromiseCatchCallback, + callbackInfo, &ret) != napi_ok) { + HILOGE("napi create function failed"); + return false; + } napi_value argv[1] = {ret}; napi_call_function(env, result, method, 1, argv, nullptr); return true; @@ -276,7 +279,11 @@ static bool CallPromise(AbilityRuntime::JsRuntime &jsRuntime, napi_value result, return false; } napi_value ret; - napi_create_function(env, "promiseCallback", strlen("promiseCallback"), PromiseCallback, callbackInfo, &ret); + if (napi_create_function(env, "promiseCallback", strlen("promiseCallback"), PromiseCallback, callbackInfo, &ret) != + napi_ok) { + HILOGE("napi create function failed"); + return false; + } napi_value argv[1] = {ret}; napi_call_function(env, result, method, 1, argv, nullptr); if (!CallCatchPromise(jsRuntime, result, callbackInfo)) { @@ -306,8 +313,11 @@ static bool CallCatchPromiseEx(AbilityRuntime::JsRuntime &jsRuntime, napi_value return false; } napi_value ret; - napi_create_function(env, "promiseCatchCallbackEx", strlen("promiseCatchCallbackEx"), PromiseCatchCallbackEx, - callbackInfoEx, &ret); + if (napi_create_function(env, "promiseCatchCallbackEx", strlen("promiseCatchCallbackEx"), PromiseCatchCallbackEx, + callbackInfoEx, &ret) != napi_ok) { + HILOGE("napi create function failed"); + return false; + } napi_value argv[1] = {ret}; napi_call_function(env, result, method, 1, argv, nullptr); return true; @@ -332,8 +342,11 @@ static bool CallPromiseEx(AbilityRuntime::JsRuntime &jsRuntime, napi_value resul return false; } napi_value ret; - napi_create_function(env, "promiseCallbackEx", strlen("promiseCallbackEx"), PromiseCallbackEx, callbackInfoEx, - &ret); + if (napi_create_function(env, "promiseCallbackEx", strlen("promiseCallbackEx"), PromiseCallbackEx, callbackInfoEx, + &ret) != napi_ok) { + HILOGE("napi create function failed"); + return false; + } napi_value argv[1] = {ret}; napi_call_function(env, result, method, 1, argv, nullptr); if (!CallCatchPromiseEx(jsRuntime, result, callbackInfoEx)) { @@ -363,8 +376,11 @@ static bool CallPromiseEx(AbilityRuntime::JsRuntime &jsRuntime, napi_value resul return false; } napi_value ret; - napi_create_function(env, "promiseCallbackEx", strlen("promiseCallbackEx"), PromiseCallbackEx, callbackInfoBackup, - &ret); + if (napi_create_function(env, "promiseCallbackEx", strlen("promiseCallbackEx"), PromiseCallbackEx, + callbackInfoBackup, &ret) != napi_ok) { + HILOGE("napi create function failed"); + return false; + } napi_value argv[1] = {ret}; napi_call_function(env, result, method, 1, argv, nullptr); return true; @@ -775,7 +791,6 @@ static int InvokeJsMethod(CallJsParam *param, AbilityRuntime::HandleEscape& hand napi_value value = param->jsObj->GetNapiValue(); if (value == nullptr) { HILOGE("failed to get napi value object."); - napi_close_handle_scope(env, scope); return EINVAL; } napi_status status; @@ -783,7 +798,6 @@ static int InvokeJsMethod(CallJsParam *param, AbilityRuntime::HandleEscape& hand status = napi_get_named_property(env, value, param->funcName.c_str(), &method); if (status != napi_ok || param->retParser == nullptr) { HILOGE("ResultValueParser must not null."); - napi_close_handle_scope(env, scope); return EINVAL; } napi_value result; @@ -791,10 +805,8 @@ static int InvokeJsMethod(CallJsParam *param, AbilityRuntime::HandleEscape& hand napi_call_function(env, value, method, argv.size(), argv.data(), &result); if (!param->retParser(env, handleEscape.Escape(result))) { HILOGE("Parser js result fail."); - napi_close_handle_scope(env, scope); return EINVAL; } - napi_close_handle_scope(env, scope); return ERR_OK; } @@ -827,6 +839,7 @@ static int DoCallJsMethod(CallJsParam *param) } } auto ret = InvokeJsMethod(param, handleEscape, env, scope, argv); + napi_close_handle_scope(env, scope); HILOGI("End execute DoCallJsMethod"); return ret; } @@ -890,7 +903,10 @@ std::function &argv)> ExtBackupJs::Pa napi_value backupExtInfoVal = nullptr; napi_create_object(env, &backupExtInfoVal); HILOGI("backupExtInfo is:%{public}s", GetAnonyString(backupExtInfo).c_str()); - napi_create_string_utf8(env, backupExtInfo.c_str(), backupExtInfo.size(), &backupExtInfoVal); + if (napi_create_string_utf8(env, backupExtInfo.c_str(), backupExtInfo.size(), &backupExtInfoVal) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(backupExtInfoVal); return true; }; @@ -908,7 +924,10 @@ std::function &argv)> ExtBackupJs::Pa napi_create_object(env, &restoreRetValue); napi_set_named_property(env, objValue, "code", AbilityRuntime::CreateJsValue(env, appVersionCode)); napi_set_named_property(env, objValue, "name", AbilityRuntime::CreateJsValue(env, appVersionStr.c_str())); - napi_create_string_utf8(env, restoreExtInfo.c_str(), restoreExtInfo.size(), &restoreRetValue); + if (napi_create_string_utf8(env, restoreExtInfo.c_str(), restoreExtInfo.size(), &restoreRetValue) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(objValue); argv.push_back(restoreRetValue); return true; diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index 162d6760e25fdaeeb4626833bc8ea5a336a4570e..0349d74b4393732208d3dc86cd319cbfcb902eef 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -542,7 +542,7 @@ int TarFile::WriteAll(const vector &buf, size_t len) size_t count = 0; while (count < len) { auto i = fwrite(&buf[0] + count, sizeof(char), len - count, currentTarFile_); - if (i < 1) { + if (ferror(currentTarFile_) || i < 1) { HILOGE("Failed to fwrite tar file, err = %{public}d", errno); return count; } diff --git a/frameworks/native/backup_ext/src/untar_file.cpp b/frameworks/native/backup_ext/src/untar_file.cpp index b78a17d26503dd15632cd2726a3308a9ba42fed0..ccc7d57653efb4f5afef7a9a4ae5147f3868db77 100644 --- a/frameworks/native/backup_ext/src/untar_file.cpp +++ b/frameworks/native/backup_ext/src/untar_file.cpp @@ -458,8 +458,14 @@ ErrFileInfo UntarFile::ParseRegularFile(FileStatInfo &info) if (remainSize < READ_BUFF_SIZE) { readBuffSize = remainSize; } - fread(&destStr[0], sizeof(char), readBuffSize, tarFilePtr_); - fwrite(&destStr[0], sizeof(char), readBuffSize, destFile); + auto readSize = fread(&destStr[0], sizeof(char), readBuffSize, tarFilePtr_); + if (readSize != readBuffSize) { + readBuffSize = readSize; + } + size_t writeSize = 0; + do { + writeSize += fwrite(&destStr[writeSize], sizeof(char), readBuffSize - writeSize, destFile); + } while (writeSize < readBuffSize); remainSize -= readBuffSize; } fclose(destFile); diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index cbcd9fbccfc80d356cd38761fc4b0f7a971241eb..27ba7ed0fc205da554faf59e89e7096914feaaa9 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -39,10 +39,6 @@ struct BackupEntity { static void OnFileReady(weak_ptr pCallbacks, const BFileInfo &fileInfo, UniqueFd fd, int sysErrno) { - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onFileReady has already been released"); @@ -92,10 +88,6 @@ static void OnFileReady(weak_ptr pCallbacks, const BFileInfo & static void onBundleBegin(weak_ptr pCallbacks, ErrCode err, const BundleName name) { HILOGI("Callback onBundleBegin, bundleName=%{public}s, errCode=%{public}d", name.c_str(), err); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onBundleBegin has already been released"); @@ -138,10 +130,6 @@ static void onBundleBegin(weak_ptr pCallbacks, ErrCode err, co static void onBundleEnd(weak_ptr pCallbacks, ErrCode err, const BundleName name) { HILOGI("Callback onBundleEnd, bundleName=%{public}s, errCode=%{public}d", name.c_str(), err); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onBundleEnd has already been released"); @@ -183,10 +171,6 @@ static void onBundleEnd(weak_ptr pCallbacks, ErrCode err, cons static void onAllBundlesEnd(weak_ptr pCallbacks, ErrCode err) { - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onAllBundlesEnd has already been released"); @@ -225,10 +209,6 @@ static void OnResultReport(weak_ptr pCallbacks, const std::str const std::string result) { HILOGI("Callback OnResultReport, bundleName=%{public}s", bundleName.c_str()); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onResultReport has already been released"); @@ -240,10 +220,16 @@ static void OnResultReport(weak_ptr pCallbacks, const std::str } auto cbCompl = [bName {bundleName}, res {result}](napi_env env, vector &argv) -> bool { napi_value napi_bName = nullptr; - napi_create_string_utf8(env, bName.c_str(), bName.size(), &napi_bName); + if (napi_create_string_utf8(env, bName.c_str(), bName.size(), &napi_bName) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(napi_bName); napi_value napi_res = nullptr; - napi_create_string_utf8(env, res.c_str(), res.size(), &napi_res); + if (napi_create_string_utf8(env, res.c_str(), res.size(), &napi_res) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(napi_res); return true; }; @@ -253,10 +239,6 @@ static void OnResultReport(weak_ptr pCallbacks, const std::str static void OnBackupServiceDied(weak_ptr pCallbacks) { HILOGI("Callback OnBackupServiceDied."); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("js callback function onBackupServiceDied has already been released"); @@ -270,7 +252,10 @@ static void OnBackupServiceDied(weak_ptr pCallbacks) auto cbCompl = [](napi_env env, vector &argv) -> bool { argv.push_back(nullptr); napi_value napi_res = nullptr; - napi_get_undefined(env, &napi_res); + if (napi_get_undefined(env, &napi_res) != napi_ok) { + HILOGE("create undefined napi object failed"); + return false; + } argv.push_back(napi_res); return true; }; @@ -280,10 +265,6 @@ static void OnBackupServiceDied(weak_ptr pCallbacks) static void OnProcess(weak_ptr pCallbacks, const BundleName name, const std::string processInfo) { HILOGI("Callback OnProcess, bundleName=%{public}s", name.c_str()); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function OnProcess has already been released"); @@ -295,10 +276,16 @@ static void OnProcess(weak_ptr pCallbacks, const BundleName na } auto cbCompl = [bundleName {name}, process {processInfo}](napi_env env, vector &argv) -> bool { napi_value napi_bName = nullptr; - napi_create_string_utf8(env, bundleName.c_str(), bundleName.size(), &napi_bName); + if (napi_create_string_utf8(env, bundleName.c_str(), bundleName.size(), &napi_bName) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(napi_bName); napi_value napi_process = nullptr; - napi_create_string_utf8(env, process.c_str(), process.size(), &napi_process); + if (napi_create_string_utf8(env, process.c_str(), process.size(), &napi_process) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(napi_process); return true; }; diff --git a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp index d33e0c5e805f51aa4684d2d39e5a9721dc7ec6fb..3dae0a8e36f7b99156cde86d971c2efc660a2d5b 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp @@ -42,10 +42,6 @@ struct BackupEntity { static void OnFileReady(weak_ptr pCallbacks, const BFileInfo &fileInfo, UniqueFd fd, UniqueFd manifestFd, int sysErrno) { - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onFileReady has already been released"); @@ -96,10 +92,6 @@ static void OnFileReady(weak_ptr pCallbacks, const BFileInfo & static void onBundleBegin(weak_ptr pCallbacks, ErrCode err, const BundleName name) { HILOGI("Callback onBundleBegin, bundleName=%{public}s, errCode=%{public}d", name.c_str(), err); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onBundleBegin has already been released"); @@ -142,10 +134,6 @@ static void onBundleBegin(weak_ptr pCallbacks, ErrCode err, co static void onBundleEnd(weak_ptr pCallbacks, ErrCode err, const BundleName name) { HILOGI("Callback onBundleEnd, bundleName=%{public}s, errCode=%{public}d", name.c_str(), err); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onBundleEnd has already been released"); @@ -187,10 +175,6 @@ static void onBundleEnd(weak_ptr pCallbacks, ErrCode err, cons static void onAllBundlesEnd(weak_ptr pCallbacks, ErrCode err) { - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onAllBundlesEnd has already been released"); @@ -229,10 +213,6 @@ static void OnResultReport(weak_ptr pCallbacks, const std::str const std::string result) { HILOGI("Callback OnResultReport, bundleName=%{public}s", bundleName.c_str()); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onResultReport has already been released"); @@ -244,10 +224,16 @@ static void OnResultReport(weak_ptr pCallbacks, const std::str } auto cbCompl = [bName {bundleName}, res {result}](napi_env env, vector &argv) -> bool { napi_value napi_bName = nullptr; - napi_create_string_utf8(env, bName.c_str(), bName.size(), &napi_bName); + if (napi_create_string_utf8(env, bName.c_str(), bName.size(), &napi_bName) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(napi_bName); napi_value napi_res = nullptr; - napi_create_string_utf8(env, res.c_str(), res.size(), &napi_res); + if (napi_create_string_utf8(env, res.c_str(), res.size(), &napi_res) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(napi_res); return true; }; @@ -257,10 +243,6 @@ static void OnResultReport(weak_ptr pCallbacks, const std::str static void OnBackupServiceDied(weak_ptr pCallbacks) { HILOGI("Callback OnBackupServiceDied."); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("js callback function onBackupServiceDied has already been released"); @@ -274,7 +256,10 @@ static void OnBackupServiceDied(weak_ptr pCallbacks) auto cbCompl = [](napi_env env, vector &argv) -> bool { argv.push_back(nullptr); napi_value napi_res = nullptr; - napi_get_undefined(env, &napi_res); + if (napi_get_undefined(env, &napi_res) != napi_ok) { + HILOGE("create undefined napi object failed"); + return false; + } argv.push_back(napi_res); return true; }; @@ -284,10 +269,6 @@ static void OnBackupServiceDied(weak_ptr pCallbacks) static void OnProcess(weak_ptr pCallbacks, const BundleName name, const std::string processInfo) { HILOGI("Callback OnProcess, bundleName=%{public}s", name.c_str()); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function OnProcess has already been released"); @@ -299,10 +280,16 @@ static void OnProcess(weak_ptr pCallbacks, const BundleName na } auto cbCompl = [bundleName {name}, process {processInfo}](napi_env env, vector &argv) -> bool { napi_value napi_bName = nullptr; - napi_create_string_utf8(env, bundleName.c_str(), bundleName.size(), &napi_bName); + if (napi_create_string_utf8(env, bundleName.c_str(), bundleName.size(), &napi_bName) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(napi_bName); napi_value napi_process = nullptr; - napi_create_string_utf8(env, process.c_str(), process.size(), &napi_process); + if (napi_create_string_utf8(env, process.c_str(), process.size(), &napi_process) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(napi_process); return true; }; diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index 6257637efce94b1c4743174f9bd3eb5cee7c4880..9133593b4cb1efeca5cd4ee4e91453dfcc156483 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -44,10 +44,6 @@ struct RestoreEntity { static void OnFileReadySheet(weak_ptr pCallbacks, const BFileInfo &fileInfo, UniqueFd fd, UniqueFd manifestFd, int32_t sysErrno) { - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onFileReady has already been released"); @@ -98,10 +94,6 @@ static void OnFileReadySheet(weak_ptr pCallbacks, const BFileI static void onBundleBegin(weak_ptr pCallbacks, ErrCode err, const BundleName name) { HILOGI("Callback onBundleBegin, bundleName=%{public}s, errCode=%{public}d", name.c_str(), err); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onBundleBegin has already been released"); @@ -144,10 +136,6 @@ static void onBundleBegin(weak_ptr pCallbacks, ErrCode err, co static void onBundleEnd(weak_ptr pCallbacks, ErrCode err, const BundleName name) { HILOGI("Callback onBundleEnd, bundleName=%{public}s, errCode=%{public}d", name.c_str(), err); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onBundleEnd has already been released"); @@ -189,10 +177,6 @@ static void onBundleEnd(weak_ptr pCallbacks, ErrCode err, cons static void onAllBundlesEnd(weak_ptr pCallbacks, ErrCode err) { - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onAllBundlesEnd has already been released"); @@ -230,10 +214,6 @@ static void onAllBundlesEnd(weak_ptr pCallbacks, ErrCode err) static void OnBackupServiceDied(weak_ptr pCallbacks) { HILOGI("Callback OnBackupServiceDied."); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("js callback function onBackupServiceDied has already been released"); @@ -247,7 +227,10 @@ static void OnBackupServiceDied(weak_ptr pCallbacks) auto cbCompl = [](napi_env env, vector &argv) -> bool { argv.push_back(nullptr); napi_value napi_res = nullptr; - napi_get_undefined(env, &napi_res); + if (napi_get_undefined(env, &napi_res) != napi_ok) { + HILOGE("create undefined napi object failed"); + return false; + } argv.push_back(napi_res); return true; }; @@ -258,10 +241,6 @@ static void OnResultReport(weak_ptr pCallbacks, const std::str const std::string result) { HILOGI("Callback OnResultReport, bundleName=%{public}s", bundleName.c_str()); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function onResultReport has already been released"); @@ -273,10 +252,16 @@ static void OnResultReport(weak_ptr pCallbacks, const std::str } auto cbCompl = [bName {bundleName}, res {result}](napi_env env, vector &argv) -> bool { napi_value napi_bName = nullptr; - napi_create_string_utf8(env, bName.c_str(), bName.size(), &napi_bName); + if (napi_create_string_utf8(env, bName.c_str(), bName.size(), &napi_bName) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(napi_bName); napi_value napi_res = nullptr; - napi_create_string_utf8(env, res.c_str(), res.size(), &napi_res); + if (napi_create_string_utf8(env, res.c_str(), res.size(), &napi_res) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(napi_res); return true; }; @@ -286,10 +271,6 @@ static void OnResultReport(weak_ptr pCallbacks, const std::str static void OnProcess(weak_ptr pCallbacks, const BundleName name, const std::string processInfo) { HILOGI("Callback OnProcess, bundleName=%{public}s", name.c_str()); - if (pCallbacks.expired()) { - HILOGI("callbacks is unbound"); - return; - } auto callbacks = pCallbacks.lock(); if (!callbacks) { HILOGI("callback function OnProcess has already been released"); @@ -301,10 +282,16 @@ static void OnProcess(weak_ptr pCallbacks, const BundleName na } auto cbCompl = [bundleName {name}, process {processInfo}](napi_env env, vector &argv) -> bool { napi_value napi_bName = nullptr; - napi_create_string_utf8(env, bundleName.c_str(), bundleName.size(), &napi_bName); + if (napi_create_string_utf8(env, bundleName.c_str(), bundleName.size(), &napi_bName) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(napi_bName); napi_value napi_process = nullptr; - napi_create_string_utf8(env, process.c_str(), process.size(), &napi_process); + if (napi_create_string_utf8(env, process.c_str(), process.size(), &napi_process) != napi_ok) { + HILOGE("create napi string failed"); + return false; + } argv.push_back(napi_process); return true; }; diff --git a/tests/mock/library_func_mock/library_func_define.h b/tests/mock/library_func_mock/library_func_define.h new file mode 100644 index 0000000000000000000000000000000000000000..c86f0582364d3a281894e98db4b7762aa043e902 --- /dev/null +++ b/tests/mock/library_func_mock/library_func_define.h @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef FILEMANAGEMENT_APP_FILE_SERVICE_LIBRARY_FUNC_DEFINE_H +#define FILEMANAGEMENT_APP_FILE_SERVICE_LIBRARY_FUNC_DEFINE_H + +#define fseeko Fseeko +#define ftello Ftello +#define access Access +#define mkdir Mkdir +#define fread Fread +#define fwrite Fwrite +#define realpath Realpath +#define fopen Fopen +#define fclose Fclose +#define chmod Chmod +#define utime Utime +#define stat(pathname, statbuf) Stat(pathname, statbuf) + +#endif // FILEMANAGEMENT_APP_FILE_SERVICE_LIBRARY_FUNC_DEFINE_H \ No newline at end of file diff --git a/tests/mock/library_func_mock/library_func_mock.cpp b/tests/mock/library_func_mock/library_func_mock.cpp index 5ff83cc4637f9d0047eda7d092856f2cba7c3c50..8779d92dd68de475b48af1cb2f45ddc5ee463998 100644 --- a/tests/mock/library_func_mock/library_func_mock.cpp +++ b/tests/mock/library_func_mock/library_func_mock.cpp @@ -16,34 +16,62 @@ #include "library_func_mock.h" using namespace OHOS::AppFileService; -int fseeko(FILE *stream, off_t offset, int whence) +int Fseeko(FILE *stream, off_t offset, int whence) { - if (LibraryFunc::libraryFunc_ == nullptr) { - return -1; - } return LibraryFunc::libraryFunc_->fseeko(stream, offset, whence); } -off_t ftello(FILE *stream) +off_t Ftello(FILE *stream) { - if (LibraryFunc::libraryFunc_ == nullptr) { - return -1; - } return LibraryFunc::libraryFunc_->ftello(stream); } -int access(const char *pathname, int mode) +int Access(const char *pathname, int mode) { - if (LibraryFunc::libraryFunc_ == nullptr) { - return -1; - } return LibraryFunc::libraryFunc_->access(pathname, mode); } -int mkdir(const char *pathname, mode_t mode) +int Mkdir(const char *pathname, mode_t mode) { - if (LibraryFunc::libraryFunc_ == nullptr) { - return -1; - } return LibraryFunc::libraryFunc_->mkdir(pathname, mode); +} + +size_t Fread(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + return LibraryFunc::libraryFunc_->fread(ptr, size, nmemb, stream); +} + +size_t Fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + return LibraryFunc::libraryFunc_->fwrite(ptr, size, nmemb, stream); +} + +char *Realpath(const char *path, char *resolved_path) +{ + return LibraryFunc::libraryFunc_->realpath(path, resolved_path); +} + +FILE* Fopen(const char *pathname, const char *mode) +{ + return LibraryFunc::libraryFunc_->fopen(pathname, mode); +} + +int Fclose(FILE *stream) +{ + return LibraryFunc::libraryFunc_->fclose(stream); +} + +int Chmod(const char *pathname, mode_t mode) +{ + return LibraryFunc::libraryFunc_->chmod(pathname, mode); +} + +int Stat(const char *pathname, struct stat *statbuf) +{ + return LibraryFunc::libraryFunc_->stat(pathname, statbuf); +} + +int Utime(const char *filename, const struct utimbuf *times) +{ + return LibraryFunc::libraryFunc_->utime(filename, times); } \ No newline at end of file diff --git a/tests/mock/library_func_mock/library_func_mock.h b/tests/mock/library_func_mock/library_func_mock.h index a6cc5a1e2f2e4d552dab537a2980c43e639a7fd8..61e6edf124f928442382b55b54133bcd7179293e 100644 --- a/tests/mock/library_func_mock/library_func_mock.h +++ b/tests/mock/library_func_mock/library_func_mock.h @@ -20,31 +20,60 @@ #include #include -#include +#include #include #include +#include + +int Fseeko(FILE*, off_t, int); +off_t Ftello(FILE*); +int Access(const char*, int); +int Mkdir(const char*, mode_t); +size_t Fread(void*, size_t, size_t, FILE*); +size_t Fwrite(const void*, size_t, size_t, FILE*); +char* Realpath(const char*, char*); +FILE* Fopen(const char*, const char*); +int Fclose(FILE*); +int Chmod(const char*, mode_t); +int Stat(const char*, struct stat*); +int Utime(const char*, const struct utimbuf*); namespace OHOS { namespace AppFileService { class LibraryFunc { public: virtual ~LibraryFunc() = default; - virtual int fseeko(FILE *stream, off_t offset, int whence) = 0; - virtual off_t ftello(FILE *stream) = 0; - virtual int access(const char *pathname, int mode) = 0; - virtual int mkdir(const char *pathname, mode_t mode) = 0; + virtual int fseeko(FILE*, off_t, int) = 0; + virtual off_t ftello(FILE*) = 0; + virtual int access(const char*, int) = 0; + virtual int mkdir(const char*, mode_t) = 0; + virtual size_t fread(void*, size_t, size_t, FILE*) = 0; + virtual size_t fwrite(const void*, size_t, size_t, FILE*) = 0; + virtual char* realpath(const char*, char*) = 0; + virtual FILE* fopen(const char*, const char*) = 0; + virtual int fclose(FILE*) = 0; + virtual int chmod(const char*, mode_t) = 0; + virtual int stat(const char*, struct stat*) = 0; + virtual int utime(const char*, const struct utimbuf*) = 0; public: static inline std::shared_ptr libraryFunc_ = nullptr; }; class LibraryFuncMock : public LibraryFunc { public: - MOCK_METHOD3(fseeko, int(FILE *stream, off_t offset, int whence)); - MOCK_METHOD1(ftello, off_t(FILE *stream)); - MOCK_METHOD2(access, int(const char *pathname, int mode)); - MOCK_METHOD2(mkdir, int(const char *pathname, mode_t mode)); + MOCK_METHOD(int, fseeko, (FILE*, off_t, int)); + MOCK_METHOD(off_t, ftello, (FILE*)); + MOCK_METHOD(int, access, (const char*, int)); + MOCK_METHOD(int, mkdir, (const char*, mode_t)); + MOCK_METHOD(size_t, fread, (void*, size_t, size_t, FILE*)); + MOCK_METHOD(size_t, fwrite, (const void*, size_t, size_t, FILE*)); + MOCK_METHOD(char*, realpath, (const char*, char*)); + MOCK_METHOD(FILE*, fopen, (const char*, const char*)); + MOCK_METHOD(int, fclose, (FILE*)); + MOCK_METHOD(int, chmod, (const char*, mode_t)); + MOCK_METHOD(int, stat, (const char*, struct stat*)); + MOCK_METHOD(int, utime, (const char*, const struct utimbuf*)); }; } // namespace AppFileService } // namespace OHOS - -#endif // FILEMANAGEMENT_APP_FILE_SERVICE_LIBRARY_FUNC_MOCK_H +#endif // FILEMANAGEMENT_APP_FILE_SERVICE_LIBRARY_FUNC_MOCK_H \ No newline at end of file diff --git a/tests/mock/library_func_mock/library_func_undef.h b/tests/mock/library_func_mock/library_func_undef.h new file mode 100644 index 0000000000000000000000000000000000000000..e2f0f9ae95d01a911485873511573dc28df96958 --- /dev/null +++ b/tests/mock/library_func_mock/library_func_undef.h @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef FILEMANAGEMENT_APP_FILE_SERVICE_LIBRARY_FUNC_UNDEF_H +#define FILEMANAGEMENT_APP_FILE_SERVICE_LIBRARY_FUNC_UNDEF_H + +#define Fseeko fseeko +#define Ftello ftello +#define Access access +#define Mkdir mkdir +#define Fread fread +#define Fwrite fwrite +#define Realpath realpath +#define Fopen fopen +#define Fclose fclose +#define Chmod chmod +#define Utime utime +#define Stat(pathname, statbuf) stat(pathname, statbuf) + +#endif // FILEMANAGEMENT_APP_FILE_SERVICE_LIBRARY_FUNC_UNDEF_H \ No newline at end of file diff --git a/tests/unittests/backup_ext/untar_file_sup_test.cpp b/tests/unittests/backup_ext/untar_file_sup_test.cpp index 1b0df748339d5c39f7c0a7a2d226de83f7f3f26c..780ad61fd9d08b94626d19631d7faa006504b7e0 100644 --- a/tests/unittests/backup_ext/untar_file_sup_test.cpp +++ b/tests/unittests/backup_ext/untar_file_sup_test.cpp @@ -16,12 +16,22 @@ #include #include +#include + +#include "b_anony/b_anony.h" #include "b_error/b_error.h" +#include "b_filesystem/b_dir.h" +#include "directory_ex.h" #include "file_ex.h" +#include "filemgmt_libhilog.h" #include "library_func_mock.h" +#include "securec.h" #include "test_manager.h" -#include "untar_file.cpp" +#include "untar_file.h" +#include "library_func_define.h" +#include "untar_file.cpp" +#include "library_func_undef.h" namespace OHOS::FileManagement::Backup { using namespace std; @@ -175,6 +185,43 @@ HWTEST_F(UntarFileSupTest, SUB_Untar_File_ForceCreateDirectoryWithMode_0100, tes GTEST_LOG_(INFO) << "UntarFileSupTest-end SUB_Untar_File_ParseOctalStr_0100"; } +/** + * @tc.number: SUB_Untar_File_ReadLongName_0100 + * @tc.name: SUB_Untar_File_ReadLongName_0100 + * @tc.desc: 测试 ReadLongName 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(UntarFileSupTest, SUB_Untar_File_ReadLongName_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UntarFileSupTest-begin SUB_Untar_File_ReadLongName_0100"; + FileStatInfo info; + info.fullPath = "test"; + UntarFile::GetInstance().tarFileSize_ = PATH_MAX_LEN + 1; + auto [ret, fileInfo] = UntarFile::GetInstance().ReadLongName(info); + EXPECT_EQ(ret, -1); + + UntarFile::GetInstance().tarFileSize_ = PATH_MAX_LEN; + EXPECT_CALL(*funcMock, fread(_, _, _, _)).WillOnce(Return(1)); + tie(ret, fileInfo) = UntarFile::GetInstance().ReadLongName(info); + EXPECT_EQ(ret, -1); + + UntarFile::GetInstance().tarFileSize_ = PATH_MAX_LEN; + EXPECT_CALL(*funcMock, fread(_, _, _, _)).WillOnce(Return(PATH_MAX_LEN)); + EXPECT_CALL(*funcMock, fseeko(_, _, _)).WillOnce(Return(1)); + tie(ret, fileInfo) = UntarFile::GetInstance().ReadLongName(info); + EXPECT_EQ(ret, -1); + + UntarFile::GetInstance().tarFileSize_ = PATH_MAX_LEN; + EXPECT_CALL(*funcMock, fread(_, _, _, _)).WillOnce(Return(PATH_MAX_LEN)); + EXPECT_CALL(*funcMock, fseeko(_, _, _)).WillOnce(Return(0)); + tie(ret, fileInfo) = UntarFile::GetInstance().ReadLongName(info); + EXPECT_EQ(ret, 0); + GTEST_LOG_(INFO) << "UntarFileSupTest-end SUB_Untar_File_ParseOctalStr_0100"; +} + /** * @tc.number: SUB_Untar_File_CreateDir_0100 * @tc.name: SUB_Untar_File_CreateDir_0100 @@ -321,4 +368,155 @@ HWTEST_F(UntarFileSupTest, SUB_Untar_File_CheckIfTarBlockValid_0100, testing::ex } GTEST_LOG_(INFO) << "UntarFileSupTest-end SUB_Untar_File_CheckIfTarBlockValid_0100"; } + +/** + * @tc.number: SUB_Untar_File_ParseRegularFile_0100 + * @tc.name: SUB_Untar_File_ParseRegularFile_0100 + * @tc.desc: 测试 ParseRegularFile 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(UntarFileSupTest, SUB_Untar_File_ParseRegularFile_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UntarFileSupTest-begin SUB_Untar_File_ParseRegularFile_0100"; + try { + char c = '\0'; + FileStatInfo info; + info.fullPath = "/test.txt"; + EXPECT_CALL(*funcMock, realpath(_, _)).WillOnce(Return(&c)); + EXPECT_CALL(*funcMock, fopen(_, _)).WillOnce(Return(nullptr)); + EXPECT_CALL(*funcMock, fseeko(_, _, _)).WillOnce(Return(0)); + auto ret = UntarFile::GetInstance().ParseRegularFile(info); + EXPECT_EQ(ret[info.fullPath].size(), 1); + + UntarFile::GetInstance().tarFileSize_ = READ_BUFF_SIZE + READ_BUFF_SIZE - 1; + EXPECT_CALL(*funcMock, realpath(_, _)).WillOnce(Return(&c)); + EXPECT_CALL(*funcMock, fopen(_, _)).WillOnce(Return(reinterpret_cast(&c))); + EXPECT_CALL(*funcMock, fread(_, _, _, _)) + .WillOnce(Return(READ_BUFF_SIZE)) + .WillOnce(Return(READ_BUFF_SIZE >> 1)) + .WillOnce(Return((READ_BUFF_SIZE >> 1) - 1)); + EXPECT_CALL(*funcMock, fwrite(_, _, _, _)) + .WillOnce(Return(READ_BUFF_SIZE)) + .WillOnce(Return(READ_BUFF_SIZE >> 1)) + .WillOnce(Return((READ_BUFF_SIZE >> 1) - 1)); + EXPECT_CALL(*funcMock, fclose(_)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, chmod(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, stat(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, fseeko(_, _, _)).WillOnce(Return(0)); + ret = UntarFile::GetInstance().ParseRegularFile(info); + EXPECT_EQ(ret[info.fullPath].size(), 0); + + UntarFile::GetInstance().tarFileSize_ = 0; + EXPECT_CALL(*funcMock, realpath(_, _)).WillOnce(Return(&c)); + EXPECT_CALL(*funcMock, fopen(_, _)).WillOnce(Return(reinterpret_cast(&c))); + EXPECT_CALL(*funcMock, fclose(_)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, chmod(_, _)).WillOnce(Return(1)); + EXPECT_CALL(*funcMock, stat(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, fseeko(_, _, _)).WillOnce(Return(0)); + ret = UntarFile::GetInstance().ParseRegularFile(info); + EXPECT_EQ(ret[info.fullPath].size(), 1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UntarFileSupTest-an exception occurred by ParseRegularFile."; + } + GTEST_LOG_(INFO) << "UntarFileSupTest-end SUB_Untar_File_ParseRegularFile_0100"; +} + +/** + * @tc.number: SUB_Untar_File_ParseRegularFile_0200 + * @tc.name: SUB_Untar_File_ParseRegularFile_0200 + * @tc.desc: 测试 ParseRegularFile 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(UntarFileSupTest, SUB_Untar_File_ParseRegularFile_0200, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UntarFileSupTest-begin SUB_Untar_File_ParseRegularFile_0200"; + try { + char c = '\0'; + FileStatInfo info; + info.fullPath = "/test.txt"; + UntarFile::GetInstance().tarFileSize_ = 0; + EXPECT_CALL(*funcMock, realpath(_, _)).WillOnce(Return(&c)); + EXPECT_CALL(*funcMock, fopen(_, _)).WillOnce(Return(reinterpret_cast(&c))); + EXPECT_CALL(*funcMock, fclose(_)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, chmod(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, stat(_, _)).WillOnce(Return(1)); + EXPECT_CALL(*funcMock, fseeko(_, _, _)).WillOnce(Return(0)); + auto ret = UntarFile::GetInstance().ParseRegularFile(info); + EXPECT_EQ(ret[info.fullPath].size(), 1); + + info.mtime = 1; + EXPECT_CALL(*funcMock, realpath(_, _)).WillOnce(Return(&c)); + EXPECT_CALL(*funcMock, fopen(_, _)).WillOnce(Return(reinterpret_cast(&c))); + EXPECT_CALL(*funcMock, fclose(_)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, chmod(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, stat(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, utime(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, fseeko(_, _, _)).WillOnce(Return(0)); + ret = UntarFile::GetInstance().ParseRegularFile(info); + EXPECT_EQ(ret[info.fullPath].size(), 0); + + EXPECT_CALL(*funcMock, realpath(_, _)).WillOnce(Return(&c)); + EXPECT_CALL(*funcMock, fopen(_, _)).WillOnce(Return(reinterpret_cast(&c))); + EXPECT_CALL(*funcMock, fclose(_)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, chmod(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, stat(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, utime(_, _)).WillOnce(Return(1)); + EXPECT_CALL(*funcMock, fseeko(_, _, _)).WillOnce(Return(0)); + ret = UntarFile::GetInstance().ParseRegularFile(info); + EXPECT_EQ(ret[info.fullPath].size(), 1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UntarFileSupTest-an exception occurred by ParseRegularFile."; + } + GTEST_LOG_(INFO) << "UntarFileSupTest-end SUB_Untar_File_ParseRegularFile_0200"; +} + +/** + * @tc.number: SUB_Untar_File_UnPacket_0100 + * @tc.name: SUB_Untar_File_UnPacket_0100 + * @tc.desc: 测试 UnPacket 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(UntarFileSupTest, SUB_Untar_File_UnPacket_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UntarFileSupTest-begin SUB_Untar_File_UnPacket_0100"; + try { + string tarFile; + string rootPath; + errno = EPERM; + EXPECT_CALL(*funcMock, fopen(_, _)).WillOnce(Return(nullptr)); + auto [ret, info, err] = UntarFile::GetInstance().UnPacket(tarFile, rootPath); + EXPECT_EQ(ret, EPERM); + + char c = '\0'; + EXPECT_CALL(*funcMock, fopen(_, _)).WillOnce(Return(reinterpret_cast(&c))); + EXPECT_CALL(*funcMock, fseeko(_, _, _)).WillOnce(Return(0)).WillOnce(Return(1)); + EXPECT_CALL(*funcMock, ftello(_)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, fclose(_)).WillOnce(Return(0)); + tie(ret, info, err) = UntarFile::GetInstance().UnPacket(tarFile, rootPath); + EXPECT_EQ(ret, 0); + + EXPECT_CALL(*funcMock, fopen(_, _)).WillOnce(Return(reinterpret_cast(&c))); + EXPECT_CALL(*funcMock, fseeko(_, _, _)).WillOnce(Return(0)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, ftello(_)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, fread(_, _, _, _)).WillOnce(Return(0)); + EXPECT_CALL(*funcMock, fclose(_)).WillOnce(Return(0)); + tie(ret, info, err) = UntarFile::GetInstance().UnPacket(tarFile, rootPath); + EXPECT_EQ(ret, 0); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UntarFileSupTest-an exception occurred by UnPacket."; + } + GTEST_LOG_(INFO) << "UntarFileSupTest-end SUB_Untar_File_UnPacket_0100"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/utils/src/b_hiaudit/zip_util.cpp b/utils/src/b_hiaudit/zip_util.cpp index 8e24685f14d81137accbe5bdbc6c25accb589051..41994b82afac645a86baa82535551cb9560e75b5 100644 --- a/utils/src/b_hiaudit/zip_util.cpp +++ b/utils/src/b_hiaudit/zip_util.cpp @@ -53,17 +53,24 @@ int ZipUtil::AddFileInZip(zipFile &zipfile, const std::string &srcFile, int keep return ERROR_GET_HANDLE; } std::string srcFileName = GetDestFilePath(srcFile, destFileName, keepParentPathStatus); - zipOpenNewFileInZip(zipfile, srcFileName.c_str(), &zipInfo, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, - Z_DEFAULT_COMPRESSION); + int errcode = zipOpenNewFileInZip(zipfile, srcFileName.c_str(), &zipInfo, nullptr, 0, nullptr, 0, nullptr, + Z_DEFLATED, Z_DEFAULT_COMPRESSION); + if (errcode != Z_OK) { + HILOGE("zipOpenNewFileInZip failed:%{public}s, errno: %{public}d.", srcFile.c_str(), errcode); + return errcode; + } - int errcode = 0; char buf[READ_MORE_LENGTH] = {0}; while (!feof(srcFp)) { size_t numBytes = fread(buf, 1, sizeof(buf), srcFp); if (numBytes == 0) { break; } - zipWriteInFileInZip(zipfile, buf, static_cast(numBytes)); + errcode = zipWriteInFileInZip(zipfile, buf, static_cast(numBytes)); + if (errcode != Z_OK) { + HILOGE("zipOpenNewFileInZip failed:%{public}s, errno: %{public}d.", srcFile.c_str(), errcode); + break; + } if (ferror(srcFp)) { HILOGE("zip file failed:%{public}s, errno: %{public}d.", srcFile.c_str(), errno); errcode = errno;