Merge branch 'master' of gitee.com:openharmony/filemanagement_app_file_service into master

Signed-off-by: weishaoxiong <weishaoxiong2@huawei.com>
This commit is contained in:
weishaoxiong 2024-02-26 01:56:22 +00:00 committed by Gitee
commit 99a754edb1
20 changed files with 488 additions and 50 deletions

View File

@ -60,6 +60,7 @@
"//foundation/filemanagement/app_file_service/interfaces/kits/js:fileshare",
"//foundation/filemanagement/app_file_service/interfaces/kits/js:fileuri",
"//foundation/filemanagement/app_file_service/interfaces/kits/js:backup",
"//foundation/filemanagement/app_file_service/interfaces/kits/ndk/fileuri/src:ohfileuri",
"//foundation/filemanagement/app_file_service/interfaces/kits/ndk/fileshare/src:ohfileshare"
],
"service_group": [
@ -97,6 +98,15 @@
"header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri/include"
}
},
{
"name": "//foundation/filemanagement/app_file_service/interfaces/kits/ndk/fileuri/src:ohfileuri",
"header": {
"header_files": [
"oh_file_uri.h"
],
"header_base": "//foundation/filemanagement/app_file_service/interfaces/kits/ndk/fileuri/include"
}
},
{
"name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native:remote_file_share_native",
"header": {

View File

@ -89,6 +89,7 @@ ohos_shared_library("fileuri_native") {
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"hilog:libhilog",
"init:libbegetutil",
"ipc:ipc_core",
"samgr:samgr_proxy",
]

View File

@ -384,7 +384,8 @@ int32_t FileShare::CreateShareFile(const vector<string> &uriList,
int32_t FileShare::DeleteShareFile(uint32_t tokenId, const vector<string> &uriList)
{
lock_guard<mutex> lock(mapMutex_);
string bundleName, currentUid;
string bundleName;
string currentUid;
int32_t ret = GetTargetInfo(tokenId, bundleName, currentUid);
if (ret != 0) {
LOGE("Failed to delete share file %{public}d", -EINVAL);

View File

@ -29,7 +29,9 @@ public:
std::string GetRealPath();
std::string ToString();
std::string GetFullDirectoryUri();
bool IsRemoteUri();
bool CheckUriFormat(const std::string &uri);
explicit FileUri(const std::string &uriOrPath);
~FileUri() = default;

View File

@ -24,6 +24,7 @@
#include "common_func.h"
#include "log.h"
#include "sandbox_helper.h"
#include "parameter.h"
using namespace std;
namespace OHOS {
@ -36,6 +37,20 @@ const std::string FILE_SCHEME_PREFIX = "file://";
const std::string FILE_MANAGER_AUTHORITY = "docs";
const std::string MEDIA_AUTHORITY = "media";
const std::string NETWORK_PARA = "?networkid=";
const std::string BACKFLASH = "/";
const char *g_fullMountEnableParameter = "const.filemanager.full_mount.enable";
static bool CheckFileManagerFullMountEnable()
{
char value[] = "false";
int retSystem = GetParameter(g_fullMountEnableParameter, "false", value, sizeof(value));
if (retSystem > 0 && !strcmp(value, "true")) {
LOGD("The full mount enable parameter is true");
return true;
}
LOGD("The full mount enable parameter is false");
return false;
}
string FileUri::GetName()
{
string sandboxPath = SandboxHelper::Decode(uri_.GetPath());
@ -70,7 +85,7 @@ string FileUri::GetRealPath()
string bundleName = uri_.GetAuthority();
if (bundleName == FILE_MANAGER_AUTHORITY &&
uri_.ToString().find(NETWORK_PARA) == string::npos &&
access(realPath.c_str(), F_OK) == 0) {
(access(realPath.c_str(), F_OK) == 0 || CheckFileManagerFullMountEnable())) {
return realPath;
}
@ -109,6 +124,26 @@ string FileUri::GetFullDirectoryUri()
return "";
}
bool FileUri::IsRemoteUri()
{
size_t pos = uri_.ToString().find(NETWORK_PARA);
if (pos != string::npos && pos > 0 && pos < uri_.ToString().size() - NETWORK_PARA.size()) {
if (uri_.ToString().substr(pos + NETWORK_PARA.size()).find(BACKFLASH) == string::npos) {
return true;
}
}
return false;
}
bool FileUri::CheckUriFormat(const std::string &uri)
{
if (uri.find(FILE_SCHEME_PREFIX) != 0) {
LOGE("URI is missing file://");
return false;
}
return true;
}
FileUri::FileUri(const string &uriOrPath): uri_(
(uriOrPath.find(FILE_SCHEME_PREFIX) == 0) ? uriOrPath : CommonFunc::GetUriFromPath(uriOrPath)
)

View File

@ -20,13 +20,14 @@
#include <string>
#include <tuple>
#include "b_resources/b_constants.h"
#include "filemgmt_libn.h"
namespace OHOS::FileManagement::Backup {
struct IncrementalBackupTime {
explicit IncrementalBackupTime(const LibN::NVal &data)
{
LibN::NVal name = data.GetProp("bundleName");
LibN::NVal name = data.GetProp(BConstants::BUNDLE_NAME);
if (name.val_ != nullptr) {
auto [succ, str, ignore] = name.ToUTF8String();
if (succ) {
@ -34,7 +35,7 @@ struct IncrementalBackupTime {
}
}
LibN::NVal time = data.GetProp("lastIncrementalTime");
LibN::NVal time = data.GetProp(BConstants::LAST_INCREMENTAL_TIME);
if (time.val_ != nullptr) {
auto [succ, tm] = time.ToInt64();
if (succ) {
@ -49,7 +50,7 @@ struct IncrementalBackupTime {
struct FileManifestData {
explicit FileManifestData(const LibN::NVal &data)
{
LibN::NVal fd = data.GetProp("manifestFd");
LibN::NVal fd = data.GetProp(BConstants::MANIFEST_FD);
if (fd.val_ != nullptr) {
auto [succ, tmp] = fd.ToInt32();
if (succ) {
@ -63,7 +64,7 @@ struct FileManifestData {
struct BackupParams {
explicit BackupParams(const LibN::NVal &data)
{
LibN::NVal para = data.GetProp("parameters");
LibN::NVal para = data.GetProp(BConstants::PARAMETERS);
if (para.val_ != nullptr) {
auto [succ, str, ignore] = para.ToUTF8String();
if (succ) {
@ -77,7 +78,7 @@ struct BackupParams {
struct BackupPriority {
explicit BackupPriority(const LibN::NVal &data)
{
LibN::NVal pr = data.GetProp("priority");
LibN::NVal pr = data.GetProp(BConstants::PRIORITY);
if (pr.val_ != nullptr) {
auto [succ, tmp] = pr.ToInt32();
if (succ) {

View File

@ -16,6 +16,7 @@
#include "b_error/b_error.h"
#include "b_incremental_data.h"
#include "b_resources/b_constants.h"
#include "filemgmt_libhilog.h"
#include "filemgmt_libn.h"
#include "incremental_backup_data.h"
@ -46,7 +47,7 @@ static napi_value AsyncCallback(napi_env env, const NFuncArg& funcArg)
return {env, err.GetNapiErr(env)};
}
NVal obj = NVal::CreateObject(env);
obj.AddProp({NVal::DeclareNapiProperty("fd", NVal::CreateInt32(env, fd->Release()).val_)});
obj.AddProp({NVal::DeclareNapiProperty(BConstants::FD.c_str(), NVal::CreateInt32(env, fd->Release()).val_)});
return {obj};
};
@ -63,7 +64,7 @@ static napi_value AsyncCallback(napi_env env, const NFuncArg& funcArg)
static bool CheckDataList(const LibN::NVal &data)
{
LibN::NVal name = data.GetProp("bundleName");
LibN::NVal name = data.GetProp(BConstants::BUNDLE_NAME);
if (name.val_ == nullptr) {
return false;
}
@ -72,7 +73,7 @@ static bool CheckDataList(const LibN::NVal &data)
return false;
}
LibN::NVal time = data.GetProp("lastIncrementalTime");
LibN::NVal time = data.GetProp(BConstants::LAST_INCREMENTAL_TIME);
if (time.val_ == nullptr) {
return false;
}
@ -148,7 +149,7 @@ static napi_value AsyncDataList(napi_env env, const NFuncArg& funcArg)
return {env, err.GetNapiErr(env)};
}
NVal obj = NVal::CreateObject(env);
obj.AddProp({NVal::DeclareNapiProperty("fd", NVal::CreateInt32(env, fd->Release()).val_)});
obj.AddProp({NVal::DeclareNapiProperty(BConstants::FD.c_str(), NVal::CreateInt32(env, fd->Release()).val_)});
return {obj};
};

View File

@ -58,9 +58,10 @@ static void OnFileReady(weak_ptr<GeneralCallbacks> pCallbacks, const BFileInfo &
return {env, err.GetNapiErr(env)};
}
NVal obj = NVal::CreateObject(env);
obj.AddProp({NVal::DeclareNapiProperty("bundleName", NVal::CreateUTF8String(env, bundleName).val_),
NVal::DeclareNapiProperty("uri", NVal::CreateUTF8String(env, fileName).val_),
NVal::DeclareNapiProperty("fd", NVal::CreateInt32(env, fd->Release()).val_)});
obj.AddProp({
NVal::DeclareNapiProperty(BConstants::BUNDLE_NAME.c_str(), NVal::CreateUTF8String(env, bundleName).val_),
NVal::DeclareNapiProperty(BConstants::URI.c_str(), NVal::CreateUTF8String(env, fileName).val_),
NVal::DeclareNapiProperty(BConstants::FD.c_str(), NVal::CreateInt32(env, fd->Release()).val_)});
return {obj};
};

View File

@ -62,10 +62,12 @@ static void OnFileReady(weak_ptr<GeneralCallbacks> pCallbacks, const BFileInfo &
return {env, err.GetNapiErr(env)};
}
NVal obj = NVal::CreateObject(env);
obj.AddProp({NVal::DeclareNapiProperty("bundleName", NVal::CreateUTF8String(env, bundleName).val_),
NVal::DeclareNapiProperty("uri", NVal::CreateUTF8String(env, fileName).val_),
NVal::DeclareNapiProperty("fd", NVal::CreateInt32(env, fd->Release()).val_),
NVal::DeclareNapiProperty("manifestFd", NVal::CreateInt32(env, manifestFd->Release()).val_)});
obj.AddProp({
NVal::DeclareNapiProperty(BConstants::BUNDLE_NAME.c_str(), NVal::CreateUTF8String(env, bundleName).val_),
NVal::DeclareNapiProperty(BConstants::URI.c_str(), NVal::CreateUTF8String(env, fileName).val_),
NVal::DeclareNapiProperty(BConstants::FD.c_str(), NVal::CreateInt32(env, fd->Release()).val_),
NVal::DeclareNapiProperty(BConstants::MANIFEST_FD.c_str(),
NVal::CreateInt32(env, manifestFd->Release()).val_)});
return {obj};
};
@ -250,7 +252,7 @@ napi_value SessionIncrementalBackupNExporter::Constructor(napi_env env, napi_cal
static bool CheckDataList(const LibN::NVal &data)
{
LibN::NVal name = data.GetProp("bundleName");
LibN::NVal name = data.GetProp(BConstants::BUNDLE_NAME);
if (name.val_ == nullptr) {
return false;
}
@ -259,7 +261,7 @@ static bool CheckDataList(const LibN::NVal &data)
return false;
}
LibN::NVal time = data.GetProp("lastIncrementalTime");
LibN::NVal time = data.GetProp(BConstants::LAST_INCREMENTAL_TIME);
if (time.val_ == nullptr) {
return false;
}

View File

@ -61,9 +61,10 @@ static void OnFileReadyWhole(weak_ptr<GeneralCallbacks> pCallbacks, const BFileI
return {env, err.GetNapiErr(env)};
}
NVal obj = NVal::CreateObject(env);
obj.AddProp({NVal::DeclareNapiProperty("bundleName", NVal::CreateUTF8String(env, bundleName).val_),
NVal::DeclareNapiProperty("uri", NVal::CreateUTF8String(env, fileName).val_),
NVal::DeclareNapiProperty("fd", NVal::CreateInt32(env, fd->Release()).val_)});
obj.AddProp({
NVal::DeclareNapiProperty(BConstants::BUNDLE_NAME.c_str(), NVal::CreateUTF8String(env, bundleName).val_),
NVal::DeclareNapiProperty(BConstants::URI.c_str(), NVal::CreateUTF8String(env, fileName).val_),
NVal::DeclareNapiProperty(BConstants::FD.c_str(), NVal::CreateInt32(env, fd->Release()).val_)});
return {obj};
};
@ -98,10 +99,12 @@ static void OnFileReadySheet(weak_ptr<GeneralCallbacks> pCallbacks,
return {env, err.GetNapiErr(env)};
}
NVal obj = NVal::CreateObject(env);
obj.AddProp({NVal::DeclareNapiProperty("bundleName", NVal::CreateUTF8String(env, bundleName).val_),
NVal::DeclareNapiProperty("uri", NVal::CreateUTF8String(env, fileName).val_),
NVal::DeclareNapiProperty("fd", NVal::CreateInt32(env, fd->Release()).val_),
NVal::DeclareNapiProperty("manifestFd", NVal::CreateInt32(env, manifestFd->Release()).val_)});
obj.AddProp({
NVal::DeclareNapiProperty(BConstants::BUNDLE_NAME.c_str(), NVal::CreateUTF8String(env, bundleName).val_),
NVal::DeclareNapiProperty(BConstants::URI.c_str(), NVal::CreateUTF8String(env, fileName).val_),
NVal::DeclareNapiProperty(BConstants::FD.c_str(), NVal::CreateInt32(env, fd->Release()).val_),
NVal::DeclareNapiProperty(BConstants::MANIFEST_FD.c_str(),
NVal::CreateInt32(env, manifestFd->Release()).val_)});
return {obj};
};
@ -360,14 +363,14 @@ static std::tuple<bool, std::unique_ptr<char[]>, std::unique_ptr<char[]>> ParseF
{
bool succ = false;
std::unique_ptr<char[]> bundleName = nullptr;
tie(succ, bundleName, ignore) = fileMeta.GetProp("bundleName").ToUTF8String();
tie(succ, bundleName, ignore) = fileMeta.GetProp(BConstants::BUNDLE_NAME).ToUTF8String();
if (!succ) {
HILOGE("First argument is not have property bundle name.");
return { false, nullptr, nullptr };
}
std::unique_ptr<char[]> fileName = nullptr;
tie(succ, fileName, ignore) = fileMeta.GetProp("uri").ToUTF8String();
tie(succ, fileName, ignore) = fileMeta.GetProp(BConstants::URI).ToUTF8String();
if (!succ) {
HILOGE("First argument is not have property file name.");
return { false, nullptr, nullptr };

View File

@ -21,6 +21,7 @@
#include "file_utils.h"
#include "log.h"
#include "uri.h"
#include "sandbox_helper.h"
using namespace std;
namespace OHOS {
@ -28,7 +29,7 @@ namespace AppFileService {
namespace ModuleFileUri {
using namespace FileManagement;
using namespace FileManagement::LibN;
const std::string MEDIA_AUTHORITY = "media";
napi_value FileUriNExporter::Constructor(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
@ -113,6 +114,24 @@ napi_value FileUriNExporter::GetFullDirectoryUri(napi_env env, napi_callback_inf
return NVal::CreateUTF8String(env, uri).val_;
}
napi_value FileUriNExporter::IsRemoteUri(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
LOGE("Number of arguments unmatched");
NError(E_PARAMS).ThrowErr(env);
return nullptr;
}
auto fileuriEntity = NClass::GetEntityOf<FileUriEntity>(env, funcArg.GetThisVar());
if (!fileuriEntity) {
LOGE("Failed to get file entity");
NError(EINVAL).ThrowErr(env);
return nullptr;
}
bool isRemoteUri = fileuriEntity->fileUri_.IsRemoteUri();
return NVal::CreateBool(env, isRemoteUri).val_;
}
napi_value FileUriNExporter::GetFileUriPath(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
@ -127,7 +146,12 @@ napi_value FileUriNExporter::GetFileUriPath(napi_env env, napi_callback_info inf
NError(EINVAL).ThrowErr(env);
return nullptr;
}
return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetPath()).val_;
string sandboxPath = SandboxHelper::Decode(fileuriEntity->fileUri_.uri_.GetPath());
string bundleName = fileuriEntity->fileUri_.uri_.GetAuthority();
if (bundleName == MEDIA_AUTHORITY) {
return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetPath()).val_;
}
return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetRealPath()).val_;
}
static std::string Split(const std::string &path, Uri &uri)
@ -447,6 +471,7 @@ bool FileUriNExporter::Export()
NVal::DeclareNapiGetter("name", GetFileUriName),
NVal::DeclareNapiGetter("path", GetFileUriPath),
NVal::DeclareNapiFunction("getFullDirectoryUri", GetFullDirectoryUri),
NVal::DeclareNapiFunction("isRemoteUri", IsRemoteUri),
NVal::DeclareNapiFunction("normalize", Normalize),
NVal::DeclareNapiFunction("equals", Equals),
NVal::DeclareNapiFunction("equalsTo", EqualsTo),

View File

@ -32,6 +32,7 @@ public:
static napi_value GetFileUriName(napi_env env, napi_callback_info info);
static napi_value GetFileUriPath(napi_env env, napi_callback_info info);
static napi_value GetFullDirectoryUri(napi_env env, napi_callback_info info);
static napi_value IsRemoteUri(napi_env env, napi_callback_info info);
static napi_value Normalize(napi_env env, napi_callback_info info);
static napi_value Equals(napi_env env, napi_callback_info info);
static napi_value EqualsTo(napi_env env, napi_callback_info info);

View File

@ -0,0 +1,28 @@
# 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.
import("//build/ohos.gni")
import("//build/ohos/ndk/ndk.gni")
ohos_ndk_headers("oh_file_uri_header") {
dest_dir = "$ndk_headers_out_dir/filemanagement/file_uri/"
sources = [ "./include/oh_file_uri.h" ]
}
ohos_ndk_library("libohfileuri") {
output_name = "ohfileuri"
output_extension = "so"
system_capability = "SystemCapability.FileManagement.AppFileService"
ndk_description_file = "./liboh_file_uri.ndk.json"
min_compact_version = "12"
system_capability_headers = [ "filemanagement/file_uri/oh_file_uri.h" ]
}

View File

@ -0,0 +1,82 @@
/*
* 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 FILE_MANAGEMENT_FILEIO_ERROR_CODE_H
#define FILE_MANAGEMENT_FILEIO_ERROR_CODE_H
/**
* @addtogroup FileIO
* @{
*
* @brief Provide the definition of the error codes.
* @since 12
*/
/**
* @file error_code.h
*
* @brief Declare the error codes of file management module.
* @syscap SystemCapability.FileManagement.File.FileIO
* @since 12
*/
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief error codes of file management
* @since 12
*/
typedef enum FileManagement_ErrCode {
/**
* operation completed successfully.
*/
ERR_OK = 0,
/**
* permission verification failed.
*/
ERR_PERMISSION_ERROR = 201,
/**
* parameter error.
*/
ERR_PARAMS = 401,
/**
* device not supported.
*/
ERR_DEVICE_NOT_SUPPORTED = 801,
/**
* operation not permitted.
*/
ERR_EPERM = 13900001,
/**
* no such file or directory.
*/
ERR_ENOENT = 13900002,
/**
* out of memory.
*/
ERR_ENOMEM = 139000011,
/**
* unknown error.
*/
ERR_UNKNOWN = 13900042
} FileManagement_ErrCode;
#ifdef __cplusplus
}
#endif
#endif // FILE_MANAGEMENT_FILEIO_ERROR_CODE_H

View File

@ -0,0 +1,83 @@
/*
* 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 FILE_MANAGEMENT_OH_FILE_URI_H
#define FILE_MANAGEMENT_OH_FILE_URI_H
/**
* @file oh_file_uri.h
*
* @brief uri verification and conversion
* @library libohfileuri.so
* @syscap SystemCapability.FileManagement.AppFileService
* @since 12
*/
#include "error_code.h"
#include "stdbool.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Get uri From path.
*
* @param path Input a pointer to the path string.
* @param length The length of the input path.
* @param result Output a pointer to a uri string. Please use free() to clear the resource.
* @return Returns the status code of the execution.
* @syscap SystemCapability.FileManagement.AppFileService
* @since 12
*/
FileManagement_ErrCode OH_FileUri_GetUriFromPath(const char *path, unsigned int length, char **result);
/**
* @brief Get path From uri.
*
* @param uri Input a pointer to the uri string.
* @param length The length of the input uri.
* @param result Output a pointer to a path string. Please use free() to clear the resource.
* @return Returns the status code of the execution.
* @syscap SystemCapability.FileManagement.AppFileService
* @since 12
*/
FileManagement_ErrCode OH_FileUri_GetPathFromUri(const char *uri, unsigned int length, char **result);
/**
* @brief Gets the uri of the path or directory where the uri is located.
*
* @param uri Input a pointer to the uri string.
* @param length The length of the input uri.
* @param result Output a pointer to a uri string. Please use free() to clear the resource.
* @return Returns the status code of the execution.
* @syscap SystemCapability.FileManagement.AppFileService
* @since 12
*/
FileManagement_ErrCode OH_FileUri_GetFullDirectoryUri(const char *uri, unsigned int length, char **result);
/**
* @brief Check that the incoming uri is valid
*
* @param uri Input a pointer to the uri string.
* @param length The length of the input uri.
* @return Returns true: Valid incoming uri, false: Invalid incoming uri.
* @syscap SystemCapability.FileManagement.AppFileService
* @since 12
*/
bool OH_FileUri_IsValidUri(const char *uri, unsigned int length);
#ifdef __cplusplus
};
#endif
#endif // FILE_MANAGEMENT_OH_FILE_URI_H

View File

@ -0,0 +1,17 @@
[
{ "first_introduced": "12",
"name":"OH_FileUri_GetUriFromPath"
},
{
"first_introduced": "12",
"name":"OH_FileUri_GetPathFromUri"
},
{
"first_introduced": "12",
"name":"OH_FileUri_GetFullDirectoryUri"
},
{
"first_introduced": "12",
"name":"OH_FileUri_IsValidUri"
}
]

View File

@ -0,0 +1,47 @@
# 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.
import("//build/ohos.gni")
import("//foundation/filemanagement/app_file_service/app_file_service.gni")
ohos_shared_library("ohfileuri") {
branch_protector_ret = "pac_ret"
sanitize = {
integer_overflow = true
ubsan = true
boundary_sanitize = true
cfi = true
cfi_cross_dso = true
debug = false
}
include_dirs = [
"../include",
"${app_file_service_path}/interfaces/innerkits/native/file_uri/include",
]
sources = [ "oh_file_uri.cpp" ]
deps =
[ "${app_file_service_path}/interfaces/innerkits/native:fileuri_native" ]
external_deps = [
"ability_base:zuri",
"c_utils:utils",
"file_api:filemgmt_libn",
"hilog:libhilog",
]
output_extension = "so"
relative_install_dir = "ndk"
part_name = "app_file_service"
subsystem_name = "filemanagement"
}

View File

@ -0,0 +1,89 @@
/*
* 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.
*/
#include "oh_file_uri.h"
#include <cstring>
#include "file_uri.h"
#include "log.h"
#include "securec.h"
using namespace std;
static FileManagement_ErrCode GetValue(const char *resultStr, char **result)
{
int count = strlen(resultStr);
if (count == 0) {
return ERR_UNKNOWN;
}
*result = static_cast<char *>(malloc(sizeof(char) * (count + 1)));
if (*result == nullptr) {
LOGE("malloc is feiled!");
return ERR_ENOMEM;
}
int ret = strncpy_s(*result, count + 1, resultStr, count);
if (ret != 0) {
LOGE("strncpy_s is feiled!");
free(*result);
*result = nullptr;
return ERR_ENOMEM;
}
return ERR_OK;
}
FileManagement_ErrCode OH_FileUri_GetUriFromPath(const char *path, unsigned int length, char **result)
{
if (path == nullptr || strlen(path) != length || result == nullptr) {
return ERR_PARAMS;
}
std::string pathStr(path, length);
OHOS::AppFileService::ModuleFileUri::FileUri fileUri(pathStr);
const char *resultUri = fileUri.ToString().c_str();
return GetValue(resultUri, result);
}
FileManagement_ErrCode OH_FileUri_GetPathFromUri(const char *uri, unsigned int length, char **result)
{
if (uri == nullptr || strlen(uri) != length || result == nullptr) {
return ERR_PARAMS;
}
std::string uriStr(uri, length);
OHOS::AppFileService::ModuleFileUri::FileUri fileUri(uriStr);
const char *resultPath = fileUri.GetRealPath().c_str();
return GetValue(resultPath, result);
}
FileManagement_ErrCode OH_FileUri_GetFullDirectoryUri(const char *uri, unsigned int length, char **result)
{
if (uri == nullptr || strlen(uri) != length || result == nullptr) {
return ERR_PARAMS;
}
std::string uriStr(uri, length);
OHOS::AppFileService::ModuleFileUri::FileUri fileUri(uriStr);
const char *resultUri = fileUri.GetFullDirectoryUri().c_str();
if (strlen(resultUri) == 0) {
return ERR_ENOENT;
}
return GetValue(resultUri, result);
}
bool OH_FileUri_IsValidUri(const char *uri, unsigned int length)
{
if (uri == nullptr || strlen(uri) != length) {
return ERR_PARAMS;
}
std::string uriStr(uri, length);
OHOS::AppFileService::ModuleFileUri::FileUri fileUri(uriStr);
return fileUri.CheckUriFormat(uriStr);
}

View File

@ -32,19 +32,19 @@ namespace ModuleRemoteFileShare {
namespace {
constexpr int HMDFS_CID_SIZE = 64;
constexpr unsigned HMDFS_IOC = 0xf2;
const char* SHARE_PATH = "/data/storage/el2/distributedfiles/.share";
const char* g_sharePATH = "/data/storage/el2/distributedfiles/.share";
}
#define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct hmdfs_share_control)
#define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct HmdfsShareControl)
struct hmdfs_share_control {
int src_fd;
struct HmdfsShareControl {
int srcFd;
char cid[HMDFS_CID_SIZE];
};
bool ShareFilePathIoctlFdAndCidFuzzTest(const uint8_t* data, size_t size)
{
struct hmdfs_share_control sc;
struct HmdfsShareControl sc;
int32_t ret = 0;
int32_t dirFd;
@ -52,15 +52,15 @@ bool ShareFilePathIoctlFdAndCidFuzzTest(const uint8_t* data, size_t size)
return false;
}
if (access(SHARE_PATH, F_OK) != 0) {
ret = mkdir(SHARE_PATH, S_IRWXU | S_IRWXG | S_IXOTH);
if (access(g_sharePATH, F_OK) != 0) {
ret = mkdir(g_sharePATH, S_IRWXU | S_IRWXG | S_IXOTH);
if (ret < 0) {
return false;
}
}
char realPath[PATH_MAX] = {0};
if (!realpath(SHARE_PATH, realPath)) {
if (!realpath(g_sharePATH, realPath)) {
return false;
}
dirFd = open(realPath, O_RDONLY);
@ -69,7 +69,7 @@ bool ShareFilePathIoctlFdAndCidFuzzTest(const uint8_t* data, size_t size)
}
const char* cid = reinterpret_cast<const char*>(data);
sc.src_fd = size;
sc.srcFd = size;
if (memcpy_s(sc.cid, HMDFS_CID_SIZE, cid, size) != 0) {
close(dirFd);
return false;
@ -86,7 +86,7 @@ bool ShareFilePathIoctlFdAndCidFuzzTest(const uint8_t* data, size_t size)
bool ShareFilePathIoctlCidFuzzTest(const uint8_t* data, size_t size)
{
struct hmdfs_share_control sc;
struct HmdfsShareControl sc;
int32_t ret = 0;
int32_t dirFd;
int32_t srcFd;
@ -95,15 +95,15 @@ bool ShareFilePathIoctlCidFuzzTest(const uint8_t* data, size_t size)
return false;
}
if (access(SHARE_PATH, F_OK) != 0) {
ret = mkdir(SHARE_PATH, S_IRWXU | S_IRWXG | S_IXOTH);
if (access(g_sharePATH, F_OK) != 0) {
ret = mkdir(g_sharePATH, S_IRWXU | S_IRWXG | S_IXOTH);
if (ret < 0) {
return false;
}
}
char realPath[PATH_MAX] = {0};
if (!realpath(SHARE_PATH, realPath)) {
if (!realpath(g_sharePATH, realPath)) {
return false;
}
dirFd = open(realPath, O_RDONLY);
@ -117,7 +117,7 @@ bool ShareFilePathIoctlCidFuzzTest(const uint8_t* data, size_t size)
close(dirFd);
return false;
}
sc.src_fd = size;
sc.srcFd = size;
const char* cid = reinterpret_cast<const char*>(data);
if (memcpy_s(sc.cid, HMDFS_CID_SIZE, cid, size) != 0) {
close(dirFd);
@ -138,7 +138,7 @@ bool ShareFilePathIoctlCidFuzzTest(const uint8_t* data, size_t size)
bool ShareFilePathIoctlFdFuzzTest(const uint8_t* data, size_t size)
{
struct hmdfs_share_control sc;
struct HmdfsShareControl sc;
int32_t ret = 0;
int32_t dirFd;
@ -146,15 +146,15 @@ bool ShareFilePathIoctlFdFuzzTest(const uint8_t* data, size_t size)
return false;
}
if (access(SHARE_PATH, F_OK) != 0) {
ret = mkdir(SHARE_PATH, S_IRWXU | S_IRWXG | S_IXOTH);
if (access(g_sharePATH, F_OK) != 0) {
ret = mkdir(g_sharePATH, S_IRWXU | S_IRWXG | S_IXOTH);
if (ret < 0) {
return false;
}
}
char realPath[PATH_MAX] = {0};
if (!realpath(SHARE_PATH, realPath)) {
if (!realpath(g_sharePATH, realPath)) {
return false;
}
@ -168,7 +168,7 @@ bool ShareFilePathIoctlFdFuzzTest(const uint8_t* data, size_t size)
close(dirFd);
return false;
}
sc.src_fd = size;
sc.srcFd = size;
ret = ioctl(dirFd, HMDFS_IOC_SET_SHARE_PATH, &sc);
if (ret < 0) {

View File

@ -164,6 +164,15 @@ static inline std::array<std::string_view, PATHES_TO_BACKUP_SIZE> PATHES_TO_BACK
"data/storage/el2/base/haps/*/files/", "data/storage/el2/base/haps/*/preferences/",
"data/storage/el2/distributedfiles/",
};
// napi 层代码引用到的常量对应js接口声明(@ohos.file.backup.d.ts)中的属性字段
static inline std::string BUNDLE_NAME = "bundleName";
static inline std::string URI = "uri";
static inline std::string FD = "fd";
static inline std::string MANIFEST_FD = "manifestFd";
static inline std::string LAST_INCREMENTAL_TIME = "lastIncrementalTime";
static inline std::string PARAMETERS = "parameters";
static inline std::string PRIORITY = "priority";
} // namespace OHOS::FileManagement::Backup::BConstants
#endif // OHOS_FILEMGMT_BACKUP_B_CONSTANTS_H