• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 HIDEBUG_UTIL_H_
17 #define HIDEBUG_UTIL_H_
18 
19 #include <cstdint>
20 #include <functional>
21 #include <memory>
22 #include <optional>
23 #include <string>
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 
28 int64_t GetElapsedNanoSecondsSinceBoot();
29 
30 int64_t GetRealNanoSecondsTimestamp();
31 
32 template<typename T>
33 class CachedValue {
34 public:
35     CachedValue() = default;
GetOrUpdateCachedValue(int64_t effectiveTime,std::function<bool (T &)> getValue)36     std::optional<T> GetOrUpdateCachedValue(int64_t effectiveTime, std::function<bool(T &)> getValue)
37     {
38         int64_t currentTime = GetElapsedNanoSecondsSinceBoot();
39         if (currentTime <= expirationTime_) {
40             return cachedValue_;
41         }
42         if (getValue(cachedValue_)) {
43             expirationTime_ = currentTime + effectiveTime;
44             return cachedValue_;
45         }
46         return {};
47     };
48 
49 private:
50     T cachedValue_;
51     int64_t expirationTime_ = -1;
52 };
53 
54 enum class DirectoryType {
55     CACHE = 0,
56     FILE = 1,
57 };
58 
59 std::string GetProcessDir(DirectoryType type);
60 
61 std::vector<std::string> SplitStr(const std::string& origin, char delimiter,
62     const std::function<bool(std::string&)>& filter = nullptr);
63 
64 bool GetXAttr(const std::string& fileName, const std::string& key, std::string& value, size_t maxLength);
65 
66 bool SetXAttr(const std::string& fileName, const std::string& key, const std::string& value);
67 
68 bool CreateFile(const std::string &path);
69 
70 bool CreateDirectory(const std::string &path, unsigned mode);
71 
72 bool IsLegalPath(const std::string& path);
73 
74 uint64_t GetFileSize(const std::string& path);
75 
76 class SmartFile {
77 public:
78     static std::unique_ptr<SmartFile> OpenFile(const std::string& path, const std::string& mode);
79     ~SmartFile();
80     SmartFile(SmartFile&& other) = delete;
81     SmartFile& operator=(SmartFile&& other) = delete;
82     SmartFile(const SmartFile&) = delete;
83     SmartFile& operator=(const SmartFile&) = delete;
84     bool Write(const void *__restrict dataPtr, size_t itemSize, size_t dataNum);
85     size_t Read(void *__restrict dataPtr, size_t itemSize, size_t dataNum);
86 private:
SmartFile(FILE * file)87     explicit SmartFile(FILE *file) : file_(file) {}
88     FILE *file_;
89 };
90 
91 bool IsBetaVersion();
92 
93 bool IsDebuggableHap();
94 
95 bool IsDeveloperOptionsEnabled();
96 }
97 }
98 #endif // HIDEBUG_UTIL_H_
99