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 #ifndef FILEMANAGEMENT_DFS_SERVICE_CONNECTION_DETECTOR_H
17 #define FILEMANAGEMENT_DFS_SERVICE_CONNECTION_DETECTOR_H
18 
19 #include "os_account_manager.h"
20 
21 #include <fstream>
22 #include <string>
23 
24 namespace OHOS {
25 namespace Storage {
26 namespace DistributedFile {
27 using namespace std;
28 const std::string HMDFS_PATH = "/mnt/hmdfs/<currentUserId>/account";
29 const std::string SYS_HMDFS_PATH = "/sys/fs/hmdfs/";
30 const std::string CONNECTION_STATUS_FILE_NAME = "status";
31 const std::string CURRENT_USER_ID_FLAG = "<currentUserId>";
32 constexpr int32_t MAX_RETRY = 25;
33 constexpr int32_t CHECK_SESSION_DELAY_TIME = 200000;
34 constexpr int32_t NO_ERROR = 0;
35 constexpr int32_t INVALID_USER_ID = -1;
36 constexpr int DISMATCH = 0;
37 constexpr int MATCH = 1;
38 class ConnectionDetector {
39 public:
40     static bool GetConnectionStatus(const std::string &targetDir, const std::string &networkId);
41     static int32_t RepeatGetConnectionStatus(const std::string &targetDir, const std::string &networkId);
42     static std::string ParseHmdfsPath();
43     static uint64_t MocklispHash(const string &str);
44 
45 private:
46     static std::string GetCellByIndex(const std::string &str, int targetIndex);
47     static bool MatchConnectionStatus(ifstream &inputFile);
48     static bool MatchConnectionGroup(const std::string &fileName, const string &networkId);
49     static bool CheckValidDir(const std::string &path);
50     static int32_t GetCurrentUserId();
51 };
52 } // namespace DistributedFile
53 } // namespace Storage
54 } // namespace OHOS
55 #endif // FILEMANAGEMENT_DFS_SERVICE_CONNECTION_DETECTOR_H
56