Signed-off-by: yang-jingbo1985 <yangjingbo10@huawei.com>
This commit is contained in:
yang-jingbo1985 2023-11-29 11:22:32 +08:00
parent 0394449f47
commit c0b59a9b3b
16 changed files with 377 additions and 0 deletions

View File

@ -217,6 +217,7 @@ napi_value FileAccessHelperInit(napi_env env, napi_value exports)
DECLARE_NAPI_FUNCTION("move", NAPI_Move),
DECLARE_NAPI_FUNCTION("query", NAPI_Query),
DECLARE_NAPI_FUNCTION("copy", NAPI_Copy),
DECLARE_NAPI_FUNCTION("copyFile", NAPI_CopyFile),
DECLARE_NAPI_FUNCTION("rename", NAPI_Rename),
DECLARE_NAPI_FUNCTION("getRoots", NAPI_GetRoots),
DECLARE_NAPI_FUNCTION("access", NAPI_Access),
@ -683,6 +684,32 @@ static std::tuple<bool, std::string, std::string, bool> GetCopyArguments(napi_en
return std::make_tuple(true, srcPathStr, destPathStr, force);
}
static std::tuple<bool, std::string, std::string, std::string> GetCopyFileArguments(napi_env env,
NFuncArg &funcArg)
{
bool retStatus = false;
std::unique_ptr<char[]> srcPath = nullptr;
std::unique_ptr<char[]> destPath = nullptr;
std::tie(retStatus, srcPath, destPath) = GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]);
if (!retStatus) {
HILOG_ERROR("Get first or second argument error");
return std::make_tuple(false, "", "", "");
}
std::string srcPathStr(srcPath.get());
std::string destPathStr(destPath.get());
bool succFileName = false;
std::unique_ptr<char[]> fileName = nullptr;
std::tie(succFileName, fileName, std::ignore) = NVal(env, funcArg[NARG_POS::THIRD]).ToUTF8String();
if (!succFileName) {
HILOG_ERROR("Get third argument error");
NError(EINVAL).ThrowErr(env);
return {false, std::move(srcPathStr), std::move(destPathStr), ""};
}
std::string fileNameStr(fileName.get());
return std::make_tuple(true, srcPathStr, destPathStr, fileNameStr);
}
static napi_value AddNAsyncWork(napi_env env, std::string procedureName, NFuncArg &funcArg, CallbackExec cbExec,
CallbackComplete cbComplete)
{
@ -749,6 +776,61 @@ napi_value NAPI_Copy(napi_env env, napi_callback_info info)
return AddNAsyncWork(env, "copy", funcArg, cbExec, cbComplete);
}
static napi_value AddCopyFileNAsyncWork(napi_env env, NFuncArg &funcArg, CallbackExec cbExec,
CallbackComplete cbComplete)
{
const std::string procedureName = "copyFile";
NVal thisVar(env, funcArg.GetThisVar());
if (funcArg.GetArgc() == NARG_CNT::THREE) {
return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
}
NVal cb(env, funcArg[NARG_POS::FOURTH]);
if (!cb.TypeIs(napi_function)) {
NError(EINVAL).ThrowErr(env);
return nullptr;
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
}
napi_value NAPI_CopyFile(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::THREE, NARG_CNT::FOUR)) {
return NapiFileInfoExporter::ThrowError(env, EINVAL);
}
bool retStatus = false;
std::string srcFileName = "";
std::string destPath = "";
std::string fileName = "";
std::tie(retStatus, srcFileName, destPath, fileName) = GetCopyFileArguments(env, funcArg);
if (!retStatus) {
return NapiFileInfoExporter::ThrowError(env, EINVAL);
}
FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar());
if (fileAccessHelper == nullptr) {
return nullptr;
}
auto result = std::make_shared<string>();
auto cbExec = [srcFileName, destPath, fileName, result, fileAccessHelper]() -> NError {
OHOS::Uri srcUri(srcFileName);
OHOS::Uri destUri(destPath);
OHOS::Uri newFileUri("");
int ret = fileAccessHelper->CopyFile(srcUri, destUri, fileName, newFileUri);
*result = newFileUri.ToString();
return NError(ret);
};
auto cbComplete = [result](napi_env env, NError err) -> NVal {
if (err) {
return { env, err.GetNapiErr(env) };
}
return NVal::CreateUTF8String(env, *result);
};
return AddCopyFileNAsyncWork(env, funcArg, cbExec, cbComplete);
}
napi_value NAPI_Rename(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);

View File

@ -31,6 +31,7 @@ namespace FileAccessFwk {
napi_value NAPI_Move(napi_env env, napi_callback_info info);
napi_value NAPI_Query(napi_env env, napi_callback_info info);
napi_value NAPI_Copy(napi_env env, napi_callback_info info);
napi_value NAPI_CopyFile(napi_env env, napi_callback_info info);
napi_value NAPI_Rename(napi_env env, napi_callback_info info);
napi_value NAPI_GetRoots(napi_env env, napi_callback_info info);
napi_value NAPI_Access(napi_env env, napi_callback_info info);

View File

@ -49,6 +49,7 @@ public:
virtual int Delete(const Uri &sourceFile);
virtual int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile);
virtual int Copy(const Uri &sourceUri, const Uri &destUri, std::vector<Result> &copyResult, bool force = false);
virtual int CopyFile(const Uri &sourceUri, const Uri &destUri, const std::string &fileName, Uri &newFileUri);
virtual int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile);
virtual int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount,
const FileFilter &filter, std::vector<FileInfo> &fileInfoVec);

View File

@ -45,6 +45,8 @@ public:
virtual int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) override;
virtual int Copy(const Uri &sourceUri, const Uri &destUri, std::vector<Result> &copyResult,
bool force = false) override;
virtual int CopyFile(const Uri &sourceUri, const Uri &destUri, const std::string &fileName,
Uri &newFileUri) override;
virtual int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) override;
virtual int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount,
const FileFilter &filter, std::vector<FileInfo> &fileInfoVec) override;

View File

@ -41,6 +41,7 @@ private:
ErrCode CmdDelete(MessageParcel &data, MessageParcel &reply);
ErrCode CmdMove(MessageParcel &data, MessageParcel &reply);
ErrCode CmdCopy(MessageParcel &data, MessageParcel &reply);
ErrCode CmdCopyFile(MessageParcel &data, MessageParcel &reply);
ErrCode CmdRename(MessageParcel &data, MessageParcel &reply);
ErrCode CmdListFile(MessageParcel &data, MessageParcel &reply);
ErrCode CmdScanFile(MessageParcel &data, MessageParcel &reply);

View File

@ -43,6 +43,8 @@ public:
int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) override;
int Copy(const Uri &sourceUri, const Uri &destUri, std::vector<Result> &copyResult,
bool force = false) override;
int CopyFile(const Uri &sourceUri, const Uri &destUri, const std::string &fileName,
Uri &newFileUri) override;
int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) override;
int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount,
const FileFilter &filter, std::vector<FileInfo> &fileInfoVec) override;

View File

@ -77,6 +77,7 @@ public:
int Delete(Uri &selectFile);
int Move(Uri &sourceFile, Uri &targetParent, Uri &newFile);
int Copy(Uri &sourceUri, Uri &destUri, std::vector<Result> &copyResult, bool force = false);
int CopyFile(Uri &sourceUri, Uri &destUri, const std::string &fileName, Uri &newFileUri);
int Rename(Uri &sourceFile, const std::string &displayName, Uri &newFile);
int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, const FileFilter &filter,
std::vector<FileInfo> &fileInfoVec);
@ -110,6 +111,7 @@ private:
std::shared_ptr<ConnectInfo> GetConnectInfo(const std::string &bundleName);
int CopyOperation(Uri &sourceUri, Uri &destUri, std::vector<Result> &copyResult, bool force = false);
int CopyFileOperation(Uri &sourceUri, Uri &destUri, const std::string &fileName, Uri &newFileUri);
int IsDirectory(Uri &uri, bool &isDir);
sptr<IRemoteObject> token_ = nullptr;

View File

@ -53,6 +53,7 @@ public:
CMD_GET_THUMBNAIL,
CMD_GET_FILEINFO_FROM_URI,
CMD_GET_FILEINFO_FROM_RELATIVE_PATH,
CMD_COPY_FILE,
CMD_MOVE_ITEM,
CMD_MOVE_FILE
};
@ -64,6 +65,8 @@ public:
virtual int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) = 0;
virtual int Copy(const Uri &sourceUri, const Uri &destUri, std::vector<Result> &copyResult,
bool force = false) = 0;
virtual int CopyFile(const Uri &sourceUri, const Uri &destUri, const std::string &fileName,
Uri &newFileUri) = 0;
virtual int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) = 0;
virtual int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount,
const FileFilter &filter, std::vector<FileInfo> &fileInfoVec) = 0;

View File

@ -72,6 +72,8 @@ public:
int Delete(const Uri &sourceFile) override;
int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) override;
int Copy(const Uri &sourceUri, const Uri &destUri, std::vector<Result> &copyResult, bool force = false) override;
int CopyFile(const Uri &sourceUri, const Uri &destUri, const std::string &fileName,
Uri &newFileUri) override;
int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) override;
int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, const FileFilter &filter,
std::vector<FileInfo> &fileInfoVec) override;

View File

@ -97,6 +97,13 @@ int FileAccessExtAbility::Copy(const Uri &sourceUri, const Uri &destUri, std::ve
return EPERM;
}
int FileAccessExtAbility::CopyFile(const Uri &sourceUri, const Uri &destUri, const std::string &fileName,
Uri &newFileUri)
{
HILOG_ERROR("FileAccessExtAbility::Copy file Undefined operation");
return EPERM;
}
int FileAccessExtAbility::Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile)
{
HILOG_ERROR("FileAccessExtAbility::Rename Undefined operation");

View File

@ -306,6 +306,26 @@ static int WriteCopyFuncArguments(OHOS::MessageParcel &data, const Uri &sourceUr
return ERR_OK;
}
static int WriteCopyFileFuncArguments(OHOS::MessageParcel &data, const Uri &sourceUri, const Uri &destUri,
const std::string &fileName)
{
UserAccessTracer trace;
trace.Start("WriteCopyFuncArguments");
if (!data.WriteString(sourceUri.ToString())) {
HILOG_ERROR("fail to WriteParcelable sourceUri");
return E_IPCS;
}
if (!data.WriteString(destUri.ToString())) {
HILOG_ERROR("fail to WriteParcelable destUri");
return E_IPCS;
}
if (!data.WriteString(fileName)) {
HILOG_ERROR("fail to WriteString fileName");
return E_IPCS;
}
return ERR_OK;
}
static int ReadCopyFuncResults(OHOS::MessageParcel &reply, std::vector<Result> &copyResult)
{
UserAccessTracer trace;
@ -376,6 +396,51 @@ int FileAccessExtProxy::Copy(const Uri &sourceUri, const Uri &destUri, std::vect
return ret;
}
int FileAccessExtProxy::CopyFile(const Uri &sourceUri, const Uri &destUri, const std::string &fileName, Uri &newFileUri)
{
UserAccessTracer trace;
trace.Start("CopyFile");
MessageParcel data;
if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
HILOG_ERROR("WriteInterfaceToken failed");
return E_IPCS;
}
int ret = WriteCopyFileFuncArguments(data, sourceUri, destUri, fileName);
if (ret != ERR_OK) {
HILOG_ERROR("Write copy file function arguments error, code: %{public}d", ret);
return ret;
}
MessageParcel reply;
MessageOption option;
int err = Remote()->SendRequest(CMD_COPY_FILE, data, reply, option);
if (err != ERR_OK) {
HILOG_ERROR("fail to SendRequest, err: %{public}d", err);
return err;
}
if (!reply.ReadInt32(ret)) {
HILOG_ERROR("fail to ReadInt32 ret");
return E_IPCS;
}
if (ret != ERR_OK) {
HILOG_ERROR("Copy file error, code:%{public}d", ret);
return ret;
}
std::string tempUri = "";
if (!reply.ReadString(tempUri)) {
HILOG_ERROR("fail to ReadString copyResult");
return E_IPCS;
}
if (tempUri.empty()) {
HILOG_ERROR("get uri is empty");
return E_GETRESULT;
}
newFileUri = Uri(tempUri);
return ERR_OK;
}
int FileAccessExtProxy::Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile)
{
UserAccessTracer trace;

View File

@ -60,6 +60,7 @@ FileAccessExtStub::FileAccessExtStub()
stubFuncMap_[CMD_GET_THUMBNAIL] = &FileAccessExtStub::CmdGetThumbnail;
stubFuncMap_[CMD_GET_FILEINFO_FROM_URI] = &FileAccessExtStub::CmdGetFileInfoFromUri;
stubFuncMap_[CMD_GET_FILEINFO_FROM_RELATIVE_PATH] = &FileAccessExtStub::CmdGetFileInfoFromRelativePath;
stubFuncMap_[CMD_COPY_FILE] = &FileAccessExtStub::CmdCopyFile;
stubFuncMap_[CMD_MOVE_ITEM] = &FileAccessExtStub::CmdMoveItem;
stubFuncMap_[CMD_MOVE_FILE] = &FileAccessExtStub::CmdMoveFile;
}
@ -329,6 +330,44 @@ ErrCode FileAccessExtStub::CmdCopy(MessageParcel &data, MessageParcel &reply)
return ERR_OK;
}
ErrCode FileAccessExtStub::CmdCopyFile(MessageParcel &data, MessageParcel &reply)
{
UserAccessTracer trace;
trace.Start("CmdCopyFile");
std::string sourceUri = "";
if (!data.ReadString(sourceUri)) {
HILOG_ERROR("Parameter Copy file fail to ReadParcelable sourceUri");
return E_IPCS;
}
std::string destUri = "";
if (!data.ReadString(destUri)) {
HILOG_ERROR("Parameter Copy file fail to ReadParcelable destUri");
return E_IPCS;
}
std::string fileName = "";
if (!data.ReadString(fileName)) {
HILOG_ERROR("Parameter Copy file fail to ReadString fileName");
return E_IPCS;
}
OHOS::Uri newFileUri("");
Uri source(sourceUri);
Uri dest(destUri);
int ret = CopyFile(source, dest, fileName, newFileUri);
if (!reply.WriteInt32(ret)) {
HILOG_ERROR("Parameter Copy file fail to WriteInt32 ret");
return E_IPCS;
}
if (!reply.WriteString(newFileUri.ToString())) {
HILOG_ERROR("Parameter Copy file fail to WriteString newFileUri");
return E_IPCS;
}
return ERR_OK;
}
ErrCode FileAccessExtStub::CmdRename(MessageParcel &data, MessageParcel &reply)
{
UserAccessTracer trace;

View File

@ -106,6 +106,19 @@ int FileAccessExtStubImpl::Copy(const Uri &sourceUri, const Uri &destUri, std::v
return ret;
}
int FileAccessExtStubImpl::CopyFile(const Uri &sourceUri, const Uri &destUri, const std::string &fileName,
Uri &newFileUri)
{
UserAccessTracer trace;
trace.Start("CopyFile");
if (extension_ == nullptr) {
HILOG_ERROR("Copy file get extension failed.");
return E_IPCS;
}
int ret = extension_->CopyFile(sourceUri, destUri, fileName, newFileUri);
return ret;
}
int FileAccessExtStubImpl::Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile)
{
UserAccessTracer trace;

View File

@ -687,6 +687,24 @@ int FileAccessHelper::CopyOperation(Uri &sourceUri, Uri &destUri, std::vector<Re
return ret;
}
int FileAccessHelper::CopyFileOperation(Uri &sourceUri, Uri &destUri, const std::string &fileName, Uri &newFileUri)
{
UserAccessTracer trace;
trace.Start("CopyFileOperation");
sptr<IFileAccessExtBase> proxy = GetProxyByUri(sourceUri);
if (proxy == nullptr) {
HILOG_ERROR("Failed with invalid fileAccessExtProxy");
return E_IPCS;
}
int ret = proxy->CopyFile(sourceUri, destUri, fileName, newFileUri);
if (ret != ERR_OK) {
HILOG_ERROR("Copy file error, code:%{public}d", ret);
return ret;
}
return ret;
}
int FileAccessHelper::Copy(Uri &sourceUri, Uri &destUri, std::vector<Result> &copyResult, bool force)
{
UserAccessTracer trace;
@ -717,6 +735,34 @@ int FileAccessHelper::Copy(Uri &sourceUri, Uri &destUri, std::vector<Result> &co
return ret;
}
int FileAccessHelper::CopyFile(Uri &sourceUri, Uri &destUri, const std::string &fileName, Uri &newFileUri)
{
UserAccessTracer trace;
trace.Start("CopyFile");
if (!IsSystemApp()) {
HILOG_ERROR("Copy check IsSystemAppByFullTokenID failed");
return E_PERMISSION_SYS;
}
if (!CheckUri(sourceUri) || !CheckUri(destUri)) {
HILOG_ERROR("Uri format check error");
return E_URIS;
}
bool isDir = false;
int ret = IsDirectory(destUri, isDir);
if (ret != ERR_OK) {
HILOG_ERROR("Get uri type error");
return E_GETRESULT;
}
if (!isDir) {
HILOG_ERROR("Destination uri is not directory");
return E_URIS;
}
ret = CopyFileOperation(sourceUri, destUri, fileName, newFileUri);
return ret;
}
int FileAccessHelper::Rename(Uri &sourceFile, const std::string &displayName, Uri &newFile)
{
UserAccessTracer trace;

View File

@ -682,6 +682,65 @@ int JsFileAccessExtAbility::Copy(const Uri &sourceUri, const Uri &destUri, std::
return copyRet;
}
int JsFileAccessExtAbility::CopyFile(const Uri &sourceUri, const Uri &destUri, const std::string &fileName,
Uri &newFileUri)
{
UserAccessTracer trace;
trace.Start("CopyFile");
auto value = std::make_shared<Value<std::string>>();
if (value == nullptr) {
HILOG_ERROR("Move value is nullptr.");
return E_GETRESULT;
}
auto argParser = [sourceUri, destUri, fileName](napi_env &env, napi_value *argv, size_t &argc) -> bool {
napi_value srcNativeUri = nullptr;
napi_create_string_utf8(env, sourceUri.ToString().c_str(), sourceUri.ToString().length(), &srcNativeUri);
napi_value dstNativeUri = nullptr;
napi_create_string_utf8(env, destUri.ToString().c_str(), destUri.ToString().length(), &dstNativeUri);
napi_value fileNativeName = nullptr;
napi_create_string_utf8(env, fileName.c_str(), fileName.length(), &fileNativeName);
if (srcNativeUri == nullptr || dstNativeUri == nullptr || fileNativeName == nullptr) {
HILOG_ERROR("create arguments native js value fail.");
return false;
}
argv[ARGC_ZERO] = srcNativeUri;
argv[ARGC_ONE] = dstNativeUri;
argv[ARGC_TWO] = fileNativeName;
argc = ARGC_THREE;
return true;
};
auto retParser = [value](napi_env &env, napi_value result) -> bool {
if (GetUriAndCodeFromJs(env, result, value) != napi_ok) {
HILOG_ERROR("Convert js object fail.");
return false;
}
return true;
};
auto errCode = CallJsMethod("copyFileByFileName", jsRuntime_, jsObj_.get(), argParser, retParser);
if (errCode != ERR_OK) {
HILOG_ERROR("CallJsMethod error, code:%{public}d.", errCode);
return errCode;
}
if (value->code != ERR_OK) {
HILOG_ERROR("fileio fail.");
return value->code;
}
if ((value->data).empty()) {
HILOG_ERROR("call copyFileByFileName with return empty.");
return E_GETRESULT;
}
newFileUri = Uri(value->data);
return ERR_OK;
}
int JsFileAccessExtAbility::Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile)
{
UserAccessTracer trace;

View File

@ -534,6 +534,58 @@ export default class FileExtAbility extends Extension {
}
}
copyFileByFileName(sourceFileUri, targetParentUri, fileName): {string, number } {
sourceFileUri = decodeUri(sourceFileUri);
targetParentUri = decodeUri(targetParentUri);
if (!checkUri(sourceFileUri) || !checkUri(targetParentUri)) {
return uriReturnObject('', E_URIS);
}
let displayName = this.getFileName(sourceFileUri);
let newFileUri = this.genNewFileUri(targetParentUri, displayName);
let newFilePath = getPath(newFileUri);
newFileUri = encodePathOfUri(newFileUri);
if (newFileUri === '') {
return uriReturnObject('', E_URIS);
}
try {
let sourceFilePath = getPath(sourceFileUri);
let isAccess = fs.accessSync(sourceFilePath);
if (!isAccess) {
hilog.error(DOMAIN_CODE, TAG, 'sourceFilePath is not exist');
return uriReturnObject('', E_GETRESULT);
}
let stat = fs.statSync(sourceFilePath);
if (!stat || stat.isDirectory()) {
hilog.error(DOMAIN_CODE, TAG, 'sourceFilePath is not file');
return uriReturnObject('', E_URIS);
}
let statNew = fs.statSync(getPath(targetParentUri));
if (!statNew || !statNew.isDirectory()) {
hilog.error(DOMAIN_CODE, TAG, 'targetParentUri is not directory');
return uriReturnObject('', E_GETRESULT);
}
let isAccessNewFilePath = fs.accessSync(newFilePath);
if (isAccessNewFilePath) {
newFileUri = this.genNewFileUri(targetParentUri, fileName);
newFilePath = getPath(newFileUri);
if (fs.accessSync(newFilePath)) {
hilog.error(DOMAIN_CODE, TAG, 'fileName is exist');
return uriReturnObject('', E_EXIST);
}
}
newFileUri = encodePathOfUri(newFileUri);
fs.copyFileSync(sourceFilePath, newFilePath);
return uriReturnObject(newFileUri, ERR_OK);
} catch (e) {
hilog.error(DOMAIN_CODE, TAG, 'copyFile error :' + e.message);
return uriReturnObject('', e.code);
}
}
access(sourceFileUri): {boolean, number} {
sourceFileUri = decodeUri(sourceFileUri);
if (sourceFileUri === '') {