• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (serviceProxy_->AsObject() != nullptr && !serviceProxy_->AsObject()->IsObjectDead()) {
79             return serviceProxy_;
80         }
81     }
82 
83     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
84     if (samgr == nullptr) {
85         LOGE("Samgr is nullptr");
86         return nullptr;
87     }
88 
89     auto object = samgr->CheckSystemAbility(FILEMANAGEMENT_CLOUD_DAEMON_SERVICE_SA_ID);
90     if (object == nullptr) {
91         LOGE("CloudDaemon::Connect object == nullptr");
92         return nullptr;
93     }
94 
95     serviceProxy_ = iface_cast<ICloudDaemon>(object);
96     if (serviceProxy_ == nullptr) {
97         LOGE("CloudDaemon::Connect service == nullptr");
98         return nullptr;
99     }
100     return serviceProxy_;
101 }
102 
InvaildInstance()103 void CloudDaemonServiceProxy::InvaildInstance()
104 {
105     LOGI("invalid instance");
106     unique_lock<mutex> lock(proxyMutex_);
107     serviceProxy_ = nullptr;
108 }
109 } // namespace OHOS::FileManagement::CloudFile
110