1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "cloud_daemon_service_proxy.h"
16
17 #include <sstream>
18
19 #include "cloud_file_daemon_interface_code.h"
20 #include "dfs_error.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #include "utils_log.h"
24
25 namespace OHOS::FileManagement::CloudFile {
26 using namespace std;
27 using namespace OHOS::FileManagement;
28
StartFuse(int32_t userId,int32_t devFd,const string & path)29 int32_t CloudDaemonServiceProxy::StartFuse(int32_t userId, int32_t devFd, const string &path)
30 {
31 LOGI("Start fuse");
32 MessageParcel data;
33 MessageParcel reply;
34 MessageOption option;
35
36 if (!data.WriteInterfaceToken(GetDescriptor())) {
37 LOGE("Failed to write interface token");
38 return E_BROKEN_IPC;
39 }
40
41 if (!data.WriteInt32(userId)) {
42 LOGE("Failed to send user id");
43 return E_INVAL_ARG;
44 }
45
46 if (!data.WriteFileDescriptor(devFd)) {
47 LOGE("Failed to send the device file fd");
48 return E_INVAL_ARG;
49 }
50
51 if (!data.WriteString(path)) {
52 LOGE("Failed to send the device file path");
53 return E_INVAL_ARG;
54 }
55
56 auto remote = Remote();
57 if (!remote) {
58 LOGE("remote is nullptr");
59 return E_BROKEN_IPC;
60 }
61 int32_t ret = remote->SendRequest(static_cast<uint32_t>(CloudFileDaemonInterfaceCode::CLOUD_DAEMON_CMD_START_FUSE),
62 data, reply, option);
63 if (ret != E_OK) {
64 stringstream ss;
65 ss << "Failed to send out the requeset, errno:" << ret;
66 LOGE("%{public}s", ss.str().c_str());
67 return E_BROKEN_IPC;
68 }
69 LOGI("StartFuseInner Success");
70 return reply.ReadInt32();
71 }
72
GetInstance()73 sptr<ICloudDaemon> CloudDaemonServiceProxy::GetInstance()
74 {
75 LOGI("getinstance");
76 unique_lock<mutex> lock(proxyMutex_);
77 if (serviceProxy_ != nullptr) {
78 return serviceProxy_;
79 }
80
81 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
82 if (samgr == nullptr) {
83 LOGE("Samgr is nullptr");
84 return nullptr;
85 }
86
87 auto object = samgr->CheckSystemAbility(FILEMANAGEMENT_CLOUD_DAEMON_SERVICE_SA_ID);
88 if (object == nullptr) {
89 LOGE("CloudDaemon::Connect object == nullptr");
90 return nullptr;
91 }
92
93 serviceProxy_ = iface_cast<ICloudDaemon>(object);
94 if (serviceProxy_ == nullptr) {
95 LOGE("CloudDaemon::Connect service == nullptr");
96 return nullptr;
97 }
98 return serviceProxy_;
99 }
100
InvaildInstance()101 void CloudDaemonServiceProxy::InvaildInstance()
102 {
103 LOGI("invalid instance");
104 unique_lock<mutex> lock(proxyMutex_);
105 serviceProxy_ = nullptr;
106 }
107 } // namespace OHOS::FileManagement::CloudFile
108