refactor file oper process

Signed-off-by: panqiangbiao <panqiangbiao@163.com>
This commit is contained in:
panqiangbiao 2021-12-14 21:07:45 +08:00
parent c16d79d2b7
commit 059230b0fb
9 changed files with 128 additions and 134 deletions

View File

@ -17,20 +17,25 @@
#include <vector>
namespace OHOS {
namespace FileManagerService {
enum {
FMS_GET_ROOT,
FMS_MEDIA_START,
FMS_MEDIA_MKDIR,
FMS_MEDIA_LISTFILE,
FMS_MEDIA_CREATEFILE,
FMS_MEDIA_END,
FMS_EXTERNAL_START,
FMS_EXTERNAL_END,
enum FILE_OPER {
GET_ROOT,
MKDIR,
LIST_FILE,
CREATE_FILE
};
enum EQUIPMENT {
INTERNAL,
EXTERNAL
};
const int32_t CODE_MASK = 0xff;
const int32_t EQUIPMENT_SHIFT = 16;
const int32_t SUCCESS = 0;
const int32_t FAIL = -1;
const int32_t E_NOEXIST = -2;
const int32_t E_EMPTYFOLDER = -3;
const int32_t E_NOEXIST = -2; // file not exist
const int32_t E_EMPTYFOLDER = -3; // folder empty
} // namespace FileManagerService
} // namespace OHOS

View File

@ -23,48 +23,6 @@ using namespace std;
namespace OHOS {
namespace FileManagerService {
FileManagerProxy::FileManagerProxy(const sptr<IRemoteObject> &impl)
: IRemoteProxy<IFileManagerService>(impl) { }
IFmsClient *IFmsClient::GetFmsInstance()
{
auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (samgr == nullptr) {
DEBUG_LOG("samgr object is NULL.");
return nullptr;
}
sptr<IRemoteObject> object = samgr->GetSystemAbility(FILE_MANAGER_SERVICE_ID);
if (object == nullptr) {
DEBUG_LOG("FileManager Service object is NULL.");
return nullptr;
}
static FileManagerProxy msProxy(object);
DEBUG_LOG("FileManagerProxy::GetFmsInstance");
return &msProxy;
}
int FileManagerProxy::CreateFile(string name, string path, string &uri)
{
MessageParcel data;
MessageParcel reply;
MessageOption option;
sptr<IRemoteObject> remote = Remote();
data.WriteString(name);
data.WriteString(path);
int err = remote->SendRequest(FMS_MEDIA_CREATEFILE, data, reply, option);
if (err != ERR_NONE) {
ERR_LOG("FileManagerProxy::CreateFile send request fail %{public}d", err);
return err;
}
reply.ReadString(uri);
DEBUG_LOG("FileManagerProxy::CreateFile reply uri %{public}s", uri.c_str());
reply.ReadInt32(err);
DEBUG_LOG("FileManagerProxy::CreateFile reply %{public}d", err);
return err;
}
int GetFileInfo(FileInfo &file, MessageParcel &reply)
{
string path;
@ -84,6 +42,45 @@ int GetFileInfo(FileInfo &file, MessageParcel &reply)
return SUCCESS;
}
FileManagerProxy::FileManagerProxy(const sptr<IRemoteObject> &impl)
: IRemoteProxy<IFileManagerService>(impl) { }
int FileManagerProxy::CreateFile(string name, string path, string &uri)
{
MessageParcel data;
MessageParcel reply;
MessageOption option;
sptr<IRemoteObject> remote = Remote();
data.WriteString(name);
data.WriteString(path);
int err = remote->SendRequest(FILE_OPER::CREATE_FILE, data, reply, option);
if (err != ERR_NONE) {
ERR_LOG("FileManagerProxy::CreateFile send request fail %{public}d", err);
return err;
}
reply.ReadString(uri);
reply.ReadInt32(err);
return err;
}
IFmsClient *IFmsClient::GetFmsInstance()
{
auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (samgr == nullptr) {
DEBUG_LOG("samgr object is NULL.");
return nullptr;
}
sptr<IRemoteObject> object = samgr->GetSystemAbility(FILE_MANAGER_SERVICE_ID);
if (object == nullptr) {
DEBUG_LOG("FileManager Service object is NULL.");
return nullptr;
}
static FileManagerProxy msProxy(object);
DEBUG_LOG("FileManagerProxy::GetFmsInstance");
return &msProxy;
}
int FileManagerProxy::ListFile(string path, int off, int count, vector<FileInfo> &fileRes)
{
@ -96,14 +93,13 @@ int FileManagerProxy::ListFile(string path, int off, int count, vector<FileInfo>
data.WriteString(path);
data.WriteInt32(off);
data.WriteInt32(count);
err = remote->SendRequest(FMS_MEDIA_LISTFILE, data, reply, option);
err = remote->SendRequest(FILE_OPER::LIST_FILE, data, reply, option);
if (err != ERR_NONE) {
ERR_LOG("FileManagerProxy::ListFile err %{public}d", err);
return err;
}
int fileInfoNum = 0;
reply.ReadInt32(fileInfoNum);
ERR_LOG("FileManagerProxy::ListFile num %{public}d", fileInfoNum);
while (fileInfoNum) {
FileInfo file;
GetFileInfo(file, reply);
@ -111,7 +107,6 @@ int FileManagerProxy::ListFile(string path, int off, int count, vector<FileInfo>
fileInfoNum--;
}
reply.ReadInt32(err);
DEBUG_LOG("FileManagerProxy::ListFile reply %{public}d", err);
return err;
}
@ -125,7 +120,7 @@ int FileManagerProxy::mkdir(string name, string path)
sptr<IRemoteObject> remote = Remote();
data.WriteString(name);
data.WriteString(path);
err = remote->SendRequest(FMS_MEDIA_MKDIR, data, reply, option);
err = remote->SendRequest(FILE_OPER::MKDIR, data, reply, option);
if (err != ERR_NONE) {
ERR_LOG("FileManagerProxy::mkdir err %{public}d", err);
return err;

View File

@ -28,7 +28,8 @@ public:
virtual ~FileOper() = default;
virtual int mkdir(const std::string &name, const std::string &path) = 0;
virtual int ListFile(const std::string &path, int offset, int count, MessageParcel &data) = 0;
virtual int CreateFile(const std::string &name, const std::string &path, std::string &uri);
virtual int CreateFile(const std::string &name, const std::string &path, std::string &uri) = 0;
virtual int OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) = 0;
};
} // OHOS
} // FileManager

View File

@ -153,6 +153,39 @@ bool GetRelativePath(const string &path, string &relativePath)
return true;
}
int MediaFileOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply)
{
int errCode = SUCCESS;
// media process
switch(code) {
case FILE_OPER::MKDIR: {
string name = data.ReadString();
string path = data.ReadString();
errCode = mkdir(name, path);
break;
}
case FILE_OPER::LIST_FILE: {
string path = data.ReadString();
int off = data.ReadInt32();
int count = data.ReadInt32();
errCode = ListFile(path, off, count, reply);
// need reply fileInfo
break;
}
case FILE_OPER::CREATE_FILE: {
string name = data.ReadString();
string path = data.ReadString();
string uri;
errCode = CreateFile(name, path, uri);
reply.WriteString(uri);
break;
}
default:
break;
}
return errCode;
}
int MediaFileOper::ListFile(const string &path, int offset, int count, MessageParcel &reply)
{
//get the relative path from the path uri

View File

@ -26,6 +26,7 @@ public:
int mkdir(const std::string &name, const std::string &path) override;
int ListFile(const std::string &path, int offset, int count, MessageParcel &data) override;
int CreateFile(const std::string &name, const std::string &path, std::string &uri) override;
int OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) override;
};
} // OHOS
} // FileManager

View File

@ -13,21 +13,34 @@
* limitations under the License.
*/
#include "fms_oper_factory.h"
#include "fms_const.h"
#include "fms_file_oper.h"
#include "fms_mediafile_oper.h"
#include "fms_oper_factory.h"
#include "log.h"
using namespace std;
namespace OHOS {
namespace FileManagerService {
FileOper* OperFactory::getFileOper(string oper)
FileOper* OperFactory::getFileOper(int equipmentId)
{
DEBUG_LOG("OperFactory::getFileOper %{public}s.",oper.c_str());
return new MediaFileOper();
DEBUG_LOG("OperFactory::getFileOper %{public}d.", equipmentId);
switch (equipmentId) {
case EQUIPMENT::INTERNAL: {
return new MediaFileOper();
break;
}
case EQUIPMENT::EXTERNAL: {
// do Exteranl storage process;
// return ExternalOper()
break;
}
default: {
break;
}
}
return nullptr;
}
} // namespace FileManagerService

View File

@ -23,7 +23,7 @@ namespace OHOS {
namespace FileManagerService {
class OperFactory {
public:
FileOper* getFileOper(std::string oper);
FileOper* getFileOper(int equipmentId);
};
} // OHOS
} // FileManager

View File

@ -24,81 +24,34 @@ using namespace std;
namespace OHOS {
namespace FileManagerService {
int FileManagerServiceStub::OperMediaProcess(OperFactory &factory, uint32_t code, MessageParcel &data,
MessageParcel &reply)
int getEquipmentCode(uint32_t code)
{
int errCode = SUCCESS;
if (code < FMS_MEDIA_START || code > FMS_MEDIA_END) {
return errCode;
}
auto *fp = factory.getFileOper("media");
// media process
switch(code) {
case FMS_MEDIA_MKDIR: {
string name = data.ReadString();
string path = data.ReadString();
errCode = fp->mkdir(name, path);
break;
}
case FMS_MEDIA_LISTFILE: {
string path = data.ReadString();
int off = data.ReadInt32();
int count = data.ReadInt32();
errCode = fp->ListFile(path, off, count, reply);
// need reply fileInfo
break;
}
case FMS_MEDIA_CREATEFILE: {
string name = data.ReadString();
string path = data.ReadString();
string uri;
errCode = fp->CreateFile(name, path, uri);
reply.WriteString(uri);
break;
}
default:
break;
}
delete fp;
return errCode;
return (code >> EQUIPMENT_SHIFT) & CODE_MASK;
}
int FileManagerServiceStub::OperExtProcess(OperFactory &factory, uint32_t code, MessageParcel &data,
MessageParcel &reply)
int getOperCode(uint32_t code)
{
int errCode = SUCCESS;
if (code < FMS_EXTERNAL_START || code > FMS_EXTERNAL_END) {
return errCode;
}
// do Exteranl storage process;
return errCode;
return code & CODE_MASK;
}
int FileManagerServiceStub::OperProcess(uint32_t code, MessageParcel &data,
MessageParcel &reply)
{
int errCode = SUCCESS;
switch (code) {
case FMS_GET_ROOT: {
// return root base on type
// return fileInfo
break;
}
default: {
OperFactory factory = OperFactory();
// check uri -->Media or --> External
errCode = OperMediaProcess(factory, code, data, reply);
}
int equipmentId = getEquipmentCode(code);
int operCode = getOperCode(code);
OperFactory factory = OperFactory();
auto *fp = factory.getFileOper(equipmentId);
if (fp == nullptr) {
ERR_LOG("OnRemoteRequest inner error %{public}d", code);
return FAIL;
}
int errCode = fp->OperProcess(operCode, data, reply);
delete fp;
return errCode;
}
int FileManagerServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
MessageParcel &reply, MessageOption &option)
{
DEBUG_LOG("OnRemoteRequest %{public}d", code);
// to do checkpermission()
// do file process
int32_t errCode = OperProcess(code, data, reply);

View File

@ -21,7 +21,6 @@
#include "iremote_stub.h"
#include "../fileoper/fms_oper_factory.h"
namespace OHOS {
namespace FileManagerService {
class IFileManagerService : public IRemoteBroker {
@ -31,13 +30,7 @@ public:
class FileManagerServiceStub : public IRemoteStub<IFileManagerService> {
public:
int OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply);
int OperMediaProcess(OperFactory &factory, uint32_t code, MessageParcel &data,
MessageParcel &reply);
int OperExtProcess(OperFactory &factory, uint32_t code, MessageParcel &data,
MessageParcel &reply);
virtual int OnRemoteRequest(uint32_t code, MessageParcel &data,
MessageParcel &reply, MessageOption &option) override;
};