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

View File

@ -23,48 +23,6 @@ using namespace std;
namespace OHOS { namespace OHOS {
namespace FileManagerService { 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) int GetFileInfo(FileInfo &file, MessageParcel &reply)
{ {
string path; string path;
@ -84,6 +42,45 @@ int GetFileInfo(FileInfo &file, MessageParcel &reply)
return SUCCESS; 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) 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.WriteString(path);
data.WriteInt32(off); data.WriteInt32(off);
data.WriteInt32(count); 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) { if (err != ERR_NONE) {
ERR_LOG("FileManagerProxy::ListFile err %{public}d", err); ERR_LOG("FileManagerProxy::ListFile err %{public}d", err);
return err; return err;
} }
int fileInfoNum = 0; int fileInfoNum = 0;
reply.ReadInt32(fileInfoNum); reply.ReadInt32(fileInfoNum);
ERR_LOG("FileManagerProxy::ListFile num %{public}d", fileInfoNum);
while (fileInfoNum) { while (fileInfoNum) {
FileInfo file; FileInfo file;
GetFileInfo(file, reply); GetFileInfo(file, reply);
@ -111,7 +107,6 @@ int FileManagerProxy::ListFile(string path, int off, int count, vector<FileInfo>
fileInfoNum--; fileInfoNum--;
} }
reply.ReadInt32(err); reply.ReadInt32(err);
DEBUG_LOG("FileManagerProxy::ListFile reply %{public}d", err);
return err; return err;
} }
@ -125,7 +120,7 @@ int FileManagerProxy::mkdir(string name, string path)
sptr<IRemoteObject> remote = Remote(); sptr<IRemoteObject> remote = Remote();
data.WriteString(name); data.WriteString(name);
data.WriteString(path); 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) { if (err != ERR_NONE) {
ERR_LOG("FileManagerProxy::mkdir err %{public}d", err); ERR_LOG("FileManagerProxy::mkdir err %{public}d", err);
return err; return err;

View File

@ -28,7 +28,8 @@ public:
virtual ~FileOper() = default; virtual ~FileOper() = default;
virtual int mkdir(const std::string &name, const std::string &path) = 0; 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 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 } // OHOS
} // FileManager } // FileManager

View File

@ -153,6 +153,39 @@ bool GetRelativePath(const string &path, string &relativePath)
return true; 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) int MediaFileOper::ListFile(const string &path, int offset, int count, MessageParcel &reply)
{ {
//get the relative path from the path uri //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 mkdir(const std::string &name, const std::string &path) override;
int ListFile(const std::string &path, int offset, int count, MessageParcel &data) 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 CreateFile(const std::string &name, const std::string &path, std::string &uri) override;
int OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) override;
}; };
} // OHOS } // OHOS
} // FileManager } // FileManager

View File

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

View File

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

View File

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

View File

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