修改recent,trash 的 double-free

Signed-off-by: zhangxiaoliang25 <zhangxiaoliang25@huawei.com>
Change-Id: I30d0410057e58e8f3757d9aaddc45fee39196ed6
This commit is contained in:
zhangxiaoliang25 2024-01-18 20:41:11 +08:00
parent 84a1d39ce3
commit bbada8c030
8 changed files with 105 additions and 94 deletions

View File

@ -18,6 +18,7 @@
#include "ability_info.h"
#include "accesstoken_kit.h"
#include "extension_context.h"
#include "file_access_check_util.h"
#include "file_access_ext_stub_impl.h"
#include "file_access_extension_info.h"
#include "file_access_framework_errno.h"
@ -36,11 +37,9 @@
#include "napi_common_util.h"
#include "napi_common_want.h"
#include "napi_remote_object.h"
#include "os_account_manager.h"
#include "parameter.h"
#include "system_ability_definition.h"
#include "user_access_common_utils.h"
#include "user_access_tracer.h"
#include "file_access_check_util.h"
namespace OHOS {
namespace FileAccessFwk {
@ -1187,24 +1186,6 @@ int JsFileAccessExtAbility::ScanFile(const FileInfo &fileInfo, const int64_t off
return ERR_OK;
}
bool GetDeviceType(std::string &deviceType)
{
char deviceTypeChar[PARAM_CONST_VALUE_LEN_MAX];
int32_t ret = GetParameter("const.product.devicetype", "0", deviceTypeChar, PARAM_CONST_VALUE_LEN_MAX);
if (ret < 0) {
HILOG_ERROR("Get deviceType fail. %{public}d", ret);
return false;
}
deviceType = deviceTypeChar;
return true;
}
bool GetUserName(std::string &userName)
{
userName = "default";
return true;
}
void ChangeCurrentDir(RootInfo &rootInfo)
{
// 获取用户名

View File

@ -19,45 +19,11 @@
#include "filemgmt_libn.h"
#include "hilog_wrapper.h"
#include "recent_n_exporter.h"
#include "os_account_manager.h"
#include "parameter.h"
namespace OHOS::FileManagement::Recent {
using namespace std;
using namespace LibN;
static bool GetDeviceType(std::string &deviceType)
{
char deviceTypeChar[PARAM_CONST_VALUE_LEN_MAX];
int32_t ret = GetParameter("const.product.devicetype", "0", deviceTypeChar, PARAM_CONST_VALUE_LEN_MAX);
if (ret < 0) {
HILOG_ERROR("Get deviceType fail. %{public}d", ret);
return false;
}
deviceType = deviceTypeChar;
return true;
}
static bool GetUserName(std::string &userName)
{
userName = "default";
return true;
}
static std::string GetRecentDir()
{
std::string result = "/storage/Users/currentUser/.Recent/";
std::string deviceType;
if (GetDeviceType(deviceType) && deviceType == "2in1") {
std::string userName;
if (GetUserName(userName) && userName != "") {
result = "/storage/Users/" + userName + "/.Recent/";
}
}
HILOG_INFO("GetRecentDir %{public}s", result.c_str());
return result;
}
static napi_value Export(napi_env env, napi_value exports)
{
std::vector<unique_ptr<NExporter>> products;
@ -72,7 +38,6 @@ static napi_value Export(napi_env env, napi_value exports)
}
}
RecentNExporter::recentPath_ = GetRecentDir();
return exports;
}

View File

@ -25,8 +25,7 @@
#include "file_utils.h"
#include "hilog_wrapper.h"
#include "ipc_skeleton.h"
#include "os_account_manager.h"
#include "parameter.h"
#include "user_access_common_utils.h"
namespace OHOS {
namespace FileManagement {
@ -35,6 +34,8 @@ using namespace std;
using namespace LibN;
using namespace AppFileService::ModuleFileUri;
using namespace FileAccessFwk;
static std::mutex recentPathMutex;
static bool CheckPermission(const std::string &permission)
{
Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
@ -314,7 +315,29 @@ string RecentNExporter::GetClassName()
return RecentNExporter::className;
}
RecentNExporter::RecentNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}
void RecentNExporter::InitRecentPath()
{
if (RecentNExporter::recentPath_.empty()) {
std::unique_lock<std::mutex> lock(recentPathMutex);
if (!RecentNExporter::recentPath_.empty()) {
return ;
}
RecentNExporter::recentPath_ = "/storage/Users/currentUser/.Recent/";
std::string deviceType;
if (GetDeviceType(deviceType) && deviceType == "2in1") {
std::string userName;
if (GetUserName(userName) && userName != "") {
RecentNExporter::recentPath_ = "/storage/Users/" + userName + "/.Recent/";
}
}
HILOG_INFO("GetRecentDir %{public}s", RecentNExporter::recentPath_.c_str());
}
}
RecentNExporter::RecentNExporter(napi_env env, napi_value exports) : NExporter(env, exports)
{
InitRecentPath();
}
RecentNExporter::~RecentNExporter() {}
} // namespace Recent

View File

@ -31,6 +31,7 @@ class RecentNExporter final : public LibN::NExporter {
public:
inline static const std::string className = "Recent";
inline static std::string recentPath_ {};
static void InitRecentPath();
bool Export() override;
std::string GetClassName() override;

View File

@ -42,6 +42,9 @@ class FileTrashNExporter final : public NExporter {
public:
inline static const std::string className_ = "trash";
inline static std::string trashPath_ {};
// 初始化回收站路径
static void InitTrashPath();
static napi_value ListFile(napi_env env, napi_callback_info info);
static napi_value Recover(napi_env env, napi_callback_info info);
static napi_value CompletelyDelete(napi_env env, napi_callback_info info);

View File

@ -26,38 +26,6 @@ namespace OHOS {
namespace Trash {
using namespace std;
static bool GetDeviceType(std::string &deviceType)
{
char deviceTypeChar[PARAM_CONST_VALUE_LEN_MAX];
int32_t ret = GetParameter("const.product.devicetype", "0", deviceTypeChar, PARAM_CONST_VALUE_LEN_MAX);
if (ret < 0) {
HILOG_ERROR("Get deviceType fail. %{public}d", ret);
return false;
}
deviceType = deviceTypeChar;
return true;
}
static bool GetUserName(std::string &userName)
{
userName = "default";
return true;
}
static std::string GetTrashDir()
{
std::string result = "/storage/Users/currentUser/.Trash";
std::string deviceType;
if (GetDeviceType(deviceType) && deviceType == "2in1") {
std::string userName;
if (GetUserName(userName) && userName != "") {
result = "/storage/Users/" + userName + "/.Trash";
}
}
HILOG_INFO("GetTrashDir %{public}s", result.c_str());
return result;
}
EXTERN_C_START
static napi_value Export(napi_env env, napi_value exports)
{
@ -73,7 +41,6 @@ static napi_value Export(napi_env env, napi_value exports)
HILOG_INFO("Class %{public}s for module trash has been exported", nExporterName.c_str());
}
}
FileTrashNExporter::trashPath_ = GetTrashDir();
return exports;
}
EXTERN_C_END

View File

@ -16,15 +16,16 @@
#include "file_trash_n_exporter.h"
#include <ctime>
#include <mutex>
#include "access_token.h"
#include "accesstoken_kit.h"
#include "file_access_framework_errno.h"
#include "file_info.h"
#include "file_uri.h"
#include "file_util.h"
#include "ipc_skeleton.h"
#include "rust_file.h"
#include "user_access_common_utils.h"
namespace OHOS {
namespace Trash {
@ -35,6 +36,9 @@ namespace {
using namespace FileManagement::LibN;
using namespace FileManagement;
using namespace std;
using namespace FileAccessFwk;
static std::mutex trashPathMutex;
static bool CheckCallingPermission(const std::string &permission)
{
@ -684,7 +688,29 @@ string FileTrashNExporter::GetClassName()
return FileTrashNExporter::className_;
}
FileTrashNExporter::FileTrashNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}
void FileTrashNExporter::InitTrashPath()
{
if (FileTrashNExporter::trashPath_.empty()) {
std::unique_lock<std::mutex> lock(trashPathMutex);
if (!FileTrashNExporter::trashPath_.empty()) {
return ;
}
FileTrashNExporter::trashPath_ = "/storage/Users/currentUser/.Trash";
std::string deviceType;
if (GetDeviceType(deviceType) && deviceType == "2in1") {
std::string userName;
if (GetUserName(userName) && userName != "") {
FileTrashNExporter::trashPath_ = "/storage/Users/" + userName + "/.Trash";
}
}
HILOG_INFO("GetRecentDir %{public}s", FileTrashNExporter::trashPath_.c_str());
}
}
FileTrashNExporter::FileTrashNExporter(napi_env env, napi_value exports) : NExporter(env, exports)
{
InitTrashPath();
}
FileTrashNExporter::~FileTrashNExporter() {}
} // namespace Trash

View File

@ -0,0 +1,45 @@
/*
* 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 USER_ACCESS_COMMON_UTILS_H
#define USER_ACCESS_COMMON_UTILS_H
#include "os_account_manager.h"
#include "parameter.h"
namespace OHOS {
namespace FileAccessFwk {
static bool GetDeviceType(std::string &deviceType)
{
char deviceTypeChar[PARAM_CONST_VALUE_LEN_MAX];
int32_t ret = GetParameter("const.product.devicetype", "0", deviceTypeChar, PARAM_CONST_VALUE_LEN_MAX);
if (ret < 0) {
HILOG_ERROR("Get deviceType fail. %{public}d", ret);
return false;
}
deviceType = deviceTypeChar;
return true;
}
static bool GetUserName(std::string &userName)
{
userName = "currentUser";
return true;
}
} // FileAccessFwk
} // OHOS
#endif //USER_ACCESS_COMMON_UTILS_H