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 #include <climits>
17 #include <ctime>
18 #include "log.h"
19 #include "monitor_utils.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FOUNDATION, "MonitorUtils"};
23 }
24
25 namespace OHOS {
26 namespace Media {
27 namespace MediaMonitor {
28
29 static constexpr int NANOSECOND_TO_SECOND = 1000000000;
30
IsRealPath(const std::string & inputPath)31 bool IsRealPath(const std::string& inputPath)
32 {
33 if (inputPath.length() > PATH_MAX) {
34 MEDIA_LOG_E("invalid path");
35 return false;
36 }
37
38 char path[PATH_MAX] = {0};
39 auto realPath = realpath(inputPath.c_str(), path);
40 FALSE_RETURN_V(realPath != nullptr, false);
41 return true;
42 }
43
GetCurSec()44 uint64_t TimeUtils::GetCurSec()
45 {
46 uint64_t result = -1; // -1 for bad result.
47 struct timespec time;
48 int ret = clock_gettime(CLOCK_MONOTONIC, &time);
49 FALSE_RETURN_V_MSG_E(ret >= 0, result, "GetCurSec fail, result:%{public}d", ret);
50 MEDIA_LOG_D("GetCurSec success");
51 result = static_cast<uint64_t>((time.tv_sec) + (time.tv_nsec / NANOSECOND_TO_SECOND));
52 return result;
53 }
54 } // namespace MediaMonitor
55 } // namespace Media
56 } // namespace OHOS