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 #include "file.h"
17 #include <atomic>
18 #include <cerrno>
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22
23 #include "log.h"
24 #if defined(_WIN32)
25 #include <direct.h>
26 #include <io.h>
27 #include <windows.h>
28 #endif
29
30 namespace SysTuning {
31 namespace base {
32 static TraceParserStatus g_status = TRACE_PARSER_ABNORMAL;
33
SetAnalysisResult(TraceParserStatus stat)34 void SetAnalysisResult(TraceParserStatus stat)
35 {
36 g_status = stat;
37 }
GetAnalysisResult()38 TraceParserStatus GetAnalysisResult()
39 {
40 return g_status;
41 }
42
Read(int fd,uint8_t * dst,size_t dstSize)43 ssize_t Read(int fd, uint8_t* dst, size_t dstSize)
44 {
45 #if defined(_WIN32)
46 return _read(fd, dst, static_cast<unsigned>(dstSize));
47 #else
48 ssize_t ret = -1;
49 do {
50 ret = read(fd, dst, dstSize);
51 } while (ret == -1 && errno == EINTR);
52 return ret;
53 #endif
54 }
OpenFile(const std::string & path,int flags,uint32_t mode)55 int OpenFile(const std::string& path, int flags, uint32_t mode)
56 {
57 TS_ASSERT((flags & O_CREAT) == 0 || mode != kFileModeInvalid);
58 #if defined(_WIN32)
59 int fd(_open(path.c_str(), flags | O_BINARY, mode));
60 #else
61 int fd(open(path.c_str(), flags | O_CLOEXEC, mode));
62 #endif
63 return fd;
64 }
65
GetExecutionDirectoryPath()66 std::string GetExecutionDirectoryPath()
67 {
68 char currPath[1024] = {0};
69 #if defined(_WIN32)
70 ::GetModuleFileNameA(NULL, currPath, MAX_PATH);
71 (strrchr(currPath, '\\'))[1] = 0;
72 #else
73 readlink("/proc/self/exe", currPath, sizeof(currPath) - 1);
74 #endif
75 std::string str(currPath);
76 return str.substr(0, str.find_last_of('/'));
77 }
78 } // namespace base
79 } // namespace SysTuning
80