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
29namespace OHOS {
30namespace Storage {
31namespace DistributedFile {
32namespace Utils {
33const std::string ORGPKGNAME = "distributedfile_daemon";
34const std::string SOFTBUSNAME = "dsoftbus";
35const std::string DISTRIBUTEDFILE_CONNECT_BEHAVIOR = "DISTRIBUTEDFILE_DAEMON";
36
37enum Uid {
38    UID_ROOT = 0,
39    UID_SYSTEM = 1000,
40    UID_MEDIA_RW = 1023,
41};
42
43enum class BizScene : int32_t {
44    DFS_CONNECT = 0x1,
45};
46
47enum class BizStage : int32_t {
48    DFS_OPEN_SESSION = 0x1,
49    DFS_SENDFILE = 0x2
50};
51
52enum class StageRes : int32_t {
53    STAGE_SUCCESS = 0x1,
54    STAGE_FAIL = 0x2
55};
56
57enum class BizState : int32_t {
58    BIZ_STATE_START = 0x1,
59    BIZ_STATE_END = 0x2,
60    BIZ_STATE_CANCEL = 0x3
61};
62
63struct 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
76void SysEventWrite(std::string &uid);
77void SysEventFileParse(int64_t maxTime);
78
79void RadarDotsReportOpenSession(struct RadarInfo &info);
80void RadarDotsOpenSession(const std::string funcName, const std::string &sessionName,
81    const std::string &peerSssionName, int32_t errCode, StageRes state);
82void RadarDotsReportSendFile(struct RadarInfo &info);
83void RadarDotsSendFile(const std::string funcName, const std::string &sessionName,
84    const std::string &peerSssionName, int32_t errCode, StageRes state);
85
86std::string GetAnonyString(const std::string &value);
87
88void ForceCreateDirectory(const std::string &path);
89void ForceCreateDirectory(const std::string &path, mode_t mode);
90void ForceCreateDirectory(const std::string &path, mode_t mode, uid_t uid, gid_t gid);
91
92void ForceRemoveDirectory(const std::string &path);
93bool ForceRemoveDirectoryDeepFirst(const std::string& path);
94bool IsFile(const std::string &path);
95bool IsFolder(const std::string &name);
96std::vector<std::string> GetFilePath(const std::string &name);
97int32_t ChangeOwnerRecursive(const std::string &path, uid_t uid, gid_t gid);
98bool 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