1 /*
2  * Copyright (c) 2021 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 UTILS_DIRECTORY_H
17 #define UTILS_DIRECTORY_H
18 
19 #include <string>
20 #include <sys/types.h>
21 #include <vector>
22 #include <cstring>
23 #include <unistd.h>
24 #include <dirent.h>
25 #include <iostream>
26 
27 #include "nlohmann/json.hpp"
28 
29 namespace OHOS {
30 namespace Storage {
31 namespace DistributedFile {
32 namespace Utils {
33 const std::string ORGPKGNAME = "distributedfile_daemon";
34 const std::string SOFTBUSNAME = "dsoftbus";
35 const std::string DISTRIBUTEDFILE_CONNECT_BEHAVIOR = "DISTRIBUTEDFILE_DAEMON";
36 
37 enum Uid {
38     UID_ROOT = 0,
39     UID_SYSTEM = 1000,
40     UID_MEDIA_RW = 1023,
41 };
42 
43 enum class BizScene : int32_t {
44     DFS_CONNECT = 0x1,
45 };
46 
47 enum class BizStage : int32_t {
48     DFS_OPEN_SESSION = 0x1,
49     DFS_SENDFILE = 0x2
50 };
51 
52 enum class StageRes : int32_t {
53     STAGE_SUCCESS = 0x1,
54     STAGE_FAIL = 0x2
55 };
56 
57 enum class BizState : int32_t {
58     BIZ_STATE_START = 0x1,
59     BIZ_STATE_END = 0x2,
60     BIZ_STATE_CANCEL = 0x3
61 };
62 
63 struct RadarInfo {
64     std::string funcName;
65     std::string localSessionName;
66     std::string peerSessionName;
67     int32_t errCode;
68     StageRes state;
69 };
70 
71 
72 #define DEMO_SYNC_SYS_EVENT(eventName, type, ...)    \
73     HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, eventName,   \
74                     type, ##__VA_ARGS__)    \
75 
76 void SysEventWrite(std::string &uid);
77 void SysEventFileParse(int64_t maxTime);
78 
79 void RadarDotsReportOpenSession(struct RadarInfo &info);
80 void RadarDotsOpenSession(const std::string funcName, const std::string &sessionName,
81     const std::string &peerSssionName, int32_t errCode, StageRes state);
82 void RadarDotsReportSendFile(struct RadarInfo &info);
83 void RadarDotsSendFile(const std::string funcName, const std::string &sessionName,
84     const std::string &peerSssionName, int32_t errCode, StageRes state);
85 
86 std::string GetAnonyString(const std::string &value);
87 
88 void ForceCreateDirectory(const std::string &path);
89 void ForceCreateDirectory(const std::string &path, mode_t mode);
90 void ForceCreateDirectory(const std::string &path, mode_t mode, uid_t uid, gid_t gid);
91 
92 void ForceRemoveDirectory(const std::string &path);
93 bool ForceRemoveDirectoryDeepFirst(const std::string& path);
94 bool IsFile(const std::string &path);
95 bool IsFolder(const std::string &name);
96 std::vector<std::string> GetFilePath(const std::string &name);
97 int32_t ChangeOwnerRecursive(const std::string &path, uid_t uid, gid_t gid);
98 bool IsInt32(const nlohmann::json &jsonObj, const std::string &key);
99 } // namespace Utils
100 } // namespace DistributedFile
101 } // namespace Storage
102 } // namespace OHOS
103 #endif // UTILS_DIRECTORY_H