• 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 
16 #include "ipc/cloud_daemon.h"
17 
18 #include <exception>
19 #include <stdexcept>
20 #include <thread>
21 
22 #include "iremote_object.h"
23 #include "system_ability_definition.h"
24 
25 #include "dfs_error.h"
26 #include "fuse_manager/fuse_manager.h"
27 #include "utils_log.h"
28 
29 namespace OHOS {
30 namespace FileManagement {
31 namespace CloudFile {
32 using namespace std;
33 
34 REGISTER_SYSTEM_ABILITY_BY_ID(CloudDaemon, FILEMANAGEMENT_CLOUD_DAEMON_SERVICE_SA_ID, true);
35 
PublishSA()36 void CloudDaemon::PublishSA()
37 {
38     LOGI("Begin to init");
39     if (!registerToService_) {
40         bool ret = SystemAbility::Publish(this);
41         if (!ret) {
42             throw runtime_error("Failed to publish the daemon");
43         }
44         registerToService_ = true;
45     }
46     LOGI("Init finished successfully");
47 }
48 
OnStart()49 void CloudDaemon::OnStart()
50 {
51     LOGI("Begin to start service");
52     if (state_ == ServiceRunningState::STATE_RUNNING) {
53         LOGI("Daemon has already started");
54         return;
55     }
56 
57     try {
58         PublishSA();
59         AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
60     } catch (const exception &e) {
61         LOGE("%{public}s", e.what());
62     }
63 
64     state_ = ServiceRunningState::STATE_RUNNING;
65     LOGI("Start service successfully");
66 }
67 
OnStop()68 void CloudDaemon::OnStop()
69 {
70     LOGI("Begin to stop");
71     state_ = ServiceRunningState::STATE_NOT_START;
72     registerToService_ = false;
73     LOGI("Stop finished successfully");
74 }
75 
StartFuse(int32_t userId,int32_t devFd,const string & path)76 int32_t CloudDaemon::StartFuse(int32_t userId, int32_t devFd, const string &path)
77 {
78     LOGI("Start Fuse");
79 
80     std::thread([=]() {
81         int32_t ret = FuseManager::GetInstance().StartFuse(userId, devFd, path);
82         LOGI("start fuse result %d", ret);
83         }).detach();
84     return E_OK;
85 }
86 } // namespace CloudFile
87 } // namespace FileManagement
88 } // namespace OHOS
89