1 /*
2 * Copyright (c) 2021-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 #include "util.h"
17 #include <climits>
18
19 #ifdef WIN32
20 #include <windows.h>
21 #include <io.h>
22
usleep(__int64 usec)23 void usleep(__int64 usec)
24 {
25 HANDLE timer = CreateWaitableTimer(nullptr, TRUE, nullptr);
26 // set interval to 100 nanosecond, using relative time
27 LARGE_INTEGER dueTime;
28 dueTime.QuadPart = -(10 * usec); // 10
29 SetWaitableTimer(timer, &dueTime, 0, nullptr, nullptr, 0);
30 WaitForSingleObject(timer, INFINITE);
31 CloseHandle(timer);
32 }
33
realpath(const char * __restrict name,char * __restrict resolved)34 char *realpath(const char *__restrict name, char *__restrict resolved)
35 {
36 return _fullpath(resolved, name, PATH_MAX);
37 }
38
access(const char * name,int type)39 int access(const char *name, int type)
40 {
41 return _access(name, type);
42 }
43 #else
44 #include <unistd.h>
45 #endif
46
47 namespace OHOS {
48 namespace Media {
49 namespace OSAL {
SleepFor(unsigned ms)50 void SleepFor(unsigned ms)
51 {
52 constexpr int factor = 1000;
53 usleep(ms * factor);
54 }
55
ConvertFullPath(const std::string & partialPath,std::string & fullPath)56 bool ConvertFullPath(const std::string& partialPath, std::string& fullPath)
57 {
58 if (partialPath.empty() || partialPath.length() >= PATH_MAX) {
59 return false;
60 }
61 char tmpPath[PATH_MAX] = {0};
62 if (realpath(partialPath.c_str(), tmpPath) == nullptr) {
63 return false;
64 }
65 if (access(tmpPath, F_OK) || access(tmpPath, R_OK)) {
66 return false;
67 }
68 fullPath = tmpPath;
69 return true;
70 }
71 } // namespace OSAL
72 } // namespace Media
73 } // namespace OHOS