diff --git a/interfaces/inner_api/file_access/include/file_access_observer_common.h b/interfaces/inner_api/file_access/include/file_access_observer_common.h index 06e7b6cc1c74bdafa034e4465601fda2099184dc..75a3ed60531777929ef4926c5f4451549414e907 100644 --- a/interfaces/inner_api/file_access/include/file_access_observer_common.h +++ b/interfaces/inner_api/file_access/include/file_access_observer_common.h @@ -25,7 +25,7 @@ namespace OHOS { namespace FileAccessFwk { - +constexpr int64_t MAX_COUNTNUM = 1000; enum NotifyType { NOTIFY_ADD = 0, NOTIFY_DELETE, @@ -44,6 +44,10 @@ public: { notifyType_ = NotifyType(parcel.ReadInt32()); auto count = parcel.ReadInt32(); + if (count > MAX_COUNTNUM) { + HILOG_ERROR("ERROR:Count greater than 1000 ."); + return false; + } for (int i = 0; i < count; i++) { uris_.push_back(parcel.ReadString()); } diff --git a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp index 1c20aa4ed256334e6670937b9f355a2f27ea6bdd..14a84bc14a7f81b7cacfc09de8bd3d8f40cc9af5 100644 --- a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp +++ b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp @@ -590,6 +590,129 @@ int JsFileAccessExtAbility::Move(const Uri &sourceFile, const Uri &targetParent, return ERR_OK; } +static void TranslateCopyResult(CopyResult ©Result) +{ + if (errCodeTable.find(copyResult.errCode) != errCodeTable.end()) { + copyResult.errCode = errCodeTable.at(copyResult.errCode).first; + if (copyResult.errMsg.empty()) { + copyResult.errMsg = errCodeTable.at(copyResult.errCode).second; + } + } +} + +static bool GetResultByJs(NativeEngine &engine, NativeValue *nativeCopyResult, CopyResult &result, const int ©Ret) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "GetResultsByJs"); + NativeObject *obj = ConvertNativeValueTo(nativeCopyResult); + if (obj == nullptr) { + HILOG_ERROR("Convert js object fail."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return false; + } + + bool ret = true; + if (copyRet == COPY_NOEXCEPTION) { + ret = ret && ConvertFromJsValue(engine, obj->GetProperty("sourceUri"), result.sourceUri); + ret = ret && ConvertFromJsValue(engine, obj->GetProperty("destUri"), result.destUri); + } + if ((copyRet == COPY_NOEXCEPTION) || (copyRet == COPY_EXCEPTION)) { + ret = ret && ConvertFromJsValue(engine, obj->GetProperty("errCode"), result.errCode); + } + if (!ret) { + HILOG_ERROR("Convert js value fail."); + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; +} + +static bool ParserGetJsCopyResult(NativeEngine &engine, NativeValue *nativeValue, + std::vector ©Result, int ©Ret) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "ParserGetJsCopyResult"); + NativeObject *obj = ConvertNativeValueTo(nativeValue); + if (obj == nullptr) { + HILOG_ERROR("Convert js object fail."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return false; + } + + bool ret = ConvertFromJsValue(engine, obj->GetProperty("code"), copyRet); + if (!ret) { + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + HILOG_ERROR("Convert js value fail."); + return false; + } + if (copyRet == ERR_OK) { + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return true; + } + + NativeArray *nativeArray = ConvertNativeValueTo(obj->GetProperty("results")); + if (nativeArray == nullptr) { + HILOG_ERROR("nativeArray is nullptr"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return false; + } + + for (uint32_t i = 0; i < nativeArray->GetLength(); i++) { + NativeValue *nativeCopyResult = nativeArray->GetElement(i); + if (nativeCopyResult == nullptr) { + HILOG_ERROR("get native FileInfo fail."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return false; + } + + CopyResult result; + ret = GetResultByJs(engine, nativeCopyResult, result, copyRet); + if (ret) { + TranslateCopyResult(result); + copyResult.push_back(result); + } + } + + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return true; +} + +int JsFileAccessExtAbility::Copy(const Uri &sourceUri, const Uri &destUri, std::vector ©Result, + bool force) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Copy"); + auto argParser = [sourceUri, destUri, force](NativeEngine &engine, NativeValue* argv[], size_t &argc) -> bool { + NativeValue *srcNativeUri = engine.CreateString(sourceUri.ToString().c_str(), + sourceUri.ToString().length()); + NativeValue *dstNativeUri = engine.CreateString(destUri.ToString().c_str(), destUri.ToString().length()); + NativeValue *forceCopy = engine.CreateBoolean(force); + if (srcNativeUri == nullptr || dstNativeUri == nullptr || forceCopy == nullptr) { + HILOG_ERROR("create arguments native js value fail."); + return false; + } + argv[ARGC_ZERO] = srcNativeUri; + argv[ARGC_ONE] = dstNativeUri; + argv[ARGC_TWO] = forceCopy; + argc = ARGC_THREE; + return true; + }; + + int copyRet = COPY_EXCEPTION; + auto retParser = [©Result, ©Ret](NativeEngine &engine, NativeValue *result) -> bool { + return ParserGetJsCopyResult(engine, result, copyResult, copyRet); + }; + + auto errCode = CallJsMethod("copy", jsRuntime_, jsObj_.get(), argParser, retParser); + if (errCode != ERR_OK) { + HILOG_ERROR("CallJsMethod error, code:%{public}d.", errCode); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + CopyResult result { "", "", errCode, ""}; + TranslateCopyResult(result); + copyResult.push_back(result); + return COPY_EXCEPTION; + } + + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return copyRet; +} + int JsFileAccessExtAbility::Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Rename"); diff --git a/interfaces/kits/picker/picker.js b/interfaces/kits/picker/picker.js index 5fa8419f5d79cf791b6029cb2f6aa57340890a21..bdf8cad64f810499350c426baa95817c0294de88 100644 --- a/interfaces/kits/picker/picker.js +++ b/interfaces/kits/picker/picker.js @@ -23,11 +23,13 @@ const PhotoViewMIMETypes = { const ErrCode = { INVALID_ARGS: 13900020, RESULT_ERROR: 13900042, + NAME_TOO_LONG: 13900030, } const ERRCODE_MAP = new Map([ [ErrCode.INVALID_ARGS, 'Invalid argument'], [ErrCode.RESULT_ERROR, 'Unknown error'], + [ErrCode.NAME_TOO_LONG, 'File name too long'], ]); const PHOTO_VIEW_MIME_TYPE_MAP = new Map([ @@ -48,31 +50,58 @@ const ARGS_ZERO = 0; const ARGS_ONE = 1; const ARGS_TWO = 2; +/* +* UTF-8字符编码数值对应的存储长度: +* 0000 - 0x007F (eg: a~z A~Z 0~9) +* 0080 - 0x07FF (eg: 希腊字母) +* 0800 - 0xFFFF (eg: 中文) +* 其他 (eg: 平面符号) +*/ +function strSizeUTF8(str) { + let strLen = str.length; + let bytesLen = 0; + for (let i = 0; i < strLen; i++) { + let charCode = str.charCodeAt(i); + if (charCode <= 0x007f) { + bytesLen++; + } else if (charCode <= 0x07ff) { + bytesLen += 2; + } else if (charCode <= 0xffff) { + bytesLen += 3; + } else { + bytesLen += 4; + } + } + return bytesLen; +} + function checkArguments(args) { + let checkArgumentsResult = undefined; + if (args.length === ARGS_TWO && typeof args[ARGS_ONE] !== 'function') { - return false; + checkArgumentsResult = getErr(ErrCode.INVALID_ARGS); } if (args.length > 0 && typeof args[ARGS_ZERO] === 'object') { let option = args[ARGS_ZERO]; if (option.maxSelectNumber !== undefined) { if (option.maxSelectNumber.toString().indexOf('.') !== -1) { - return false; + checkArgumentsResult = getErr(ErrCode.INVALID_ARGS); } } if (option.newFileNames !== undefined && option.newFileNames.length > 0) { for (let i = 0; i < option.newFileNames.length; i++) { let value = option.newFileNames[i]; - if (value.length > CREATE_FILE_NAME_LENGTH_LIMIT) { + if (strSizeUTF8(value) > CREATE_FILE_NAME_LENGTH_LIMIT) { console.log('[picker] checkArguments Invalid name: ' + value); - return false; + checkArgumentsResult = getErr(ErrCode.NAME_TOO_LONG); } } } } - return true; + return checkArgumentsResult; } function getErr(errCode) { @@ -126,9 +155,10 @@ function getPhotoPickerSelectResult(args) { } async function photoPickerSelect(...args) { - if (!checkArguments(args)) { + let checkArgsResult = checkArguments(args); + if (checkArgsResult !== undefined) { console.log('[picker] Invalid argument'); - throw Error(getErr(ErrCode.INVALID_ARGS)); + throw checkArgsResult; } const config = parsePhotoPickerSelectOption(args); @@ -209,9 +239,10 @@ function getDocumentPickerSelectResult(args) { } async function documentPickerSelect(...args) { - if (!checkArguments(args)) { + let checkArgsResult = checkArguments(args); + if (checkArgsResult !== undefined) { console.log('[picker] Select Invalid argument'); - throw Error(getErr(ErrCode.INVALID_ARGS)); + throw checkArgsResult; } let context = undefined; @@ -311,9 +342,10 @@ function getDocumentPickerSaveResult(args) { } async function documentPickerSave(...args) { - if (!checkArguments(args)) { - console.log('[picker] Save Invalid argument'); - throw Error({code: 13900020, message: 'Invalid argument'}); + let checkArgsResult = checkArguments(args); + if (checkArgsResult !== undefined) { + console.log('[picker] Invalid argument'); + throw checkArgsResult; } let context = undefined; @@ -330,12 +362,12 @@ async function documentPickerSave(...args) { config = parseDocumentPickerSaveOption(args, ACTION.SAVE_ACTION_MODAL); result = await context.requestDialogService(config); } catch (paramError) { - console.log('[picker] Save paramError: ' + JSON.stringify(paramError)); + console.log('[picker] paramError: ' + JSON.stringify(paramError)); try { config = parseDocumentPickerSaveOption(args, ACTION.SAVE_ACTION); result = await context.startAbilityForResult(config, {windowMode: 0}); } catch (error) { - console.log('[picker] Save error: ' + error); + console.log('[picker] error: ' + error); return undefined; } } @@ -361,9 +393,10 @@ async function documentPickerSave(...args) { } async function audioPickerSelect(...args) { - if (!checkArguments(args)) { + let checkArgsResult = checkArguments(args); + if (checkArgsResult !== undefined) { console.log('[picker] Invalid argument'); - throw Error({code: 13900020, message: 'Invalid argument'}); + throw checkArgsResult; } const config = parseDocumentPickerSelectOption(args, action.SELECT_ACTION); diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts index 103f9fec0d6bed7e84f6045ed053af508f9eecdc..26672add05f304dc4e4c265f28caaa4ce8d47042 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts @@ -133,14 +133,14 @@ function getFileInfos(sourceFileUri: string, offset: number, count: number, filt {infos: Fileinfo[], code: number} { let infos : Fileinfo[] = []; let path = getPath(sourceFileUri); - let statPath = fs.statSync(path); - if (!statPath.isDirectory()) { - return { - infos: [], - code: E_GETRESULT, - }; - } try { + let statPath = fs.statSync(path); + if (!statPath.isDirectory()) { + return { + infos: [], + code: E_GETRESULT, + }; + } let options; let listNum = offset + count; if (hasFilter(filter)) { @@ -177,7 +177,7 @@ function getFileInfos(sourceFileUri: string, offset: number, count: number, filt }); } } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'getFileInfos error ' + e.message); + hilog.error(DOMAIN_CODE, TAG, `getFileInfos error: ${e.message},code: ${e.code}`); return { infos: [], code: E_GETRESULT, diff --git a/services/native/file_access_service/include/file_access_service_ipc_interface_code.h b/services/native/file_access_service/include/file_access_service_ipc_interface_code.h new file mode 100644 index 0000000000000000000000000000000000000000..769784b80b743aeb71f5d0df666c353330ec843c --- /dev/null +++ b/services/native/file_access_service/include/file_access_service_ipc_interface_code.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 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 FILE_ACCESS_SERVICE_IPC_INTERFACE_CODE_H +#define FILE_ACCESS_SERVICE_IPC_INTERFACE_CODE_H + +namespace OHOS { +namespace FileAccessFwk { + enum class FileAccessServiceInterfaceCode { + CMD_REGISTER_NOTIFY = 0, + CMD_UNREGISTER_NOTIFY, + CMD_ONCHANGE + }; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_ACCESS_SERVICE_IPC_INTERFACE_CODE_H \ No newline at end of file