1 /*
2 * Copyright (c) 2021-2022 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 "ecmascript/dfx/hprof/file_stream.h"
17
18 #include <cerrno>
19 #include <climits>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cstring>
23 #include <ios>
24 #include <ostream>
25 #include <unistd.h>
26
27 #include "ecmascript/log_wrapper.h"
28
29 namespace panda::ecmascript {
FileStream(const std::string & fileName)30 FileStream::FileStream(const std::string &fileName)
31 {
32 Initialize(fileName);
33 }
34
EndOfStream()35 void FileStream::EndOfStream()
36 {
37 if (Good()) {
38 fileStream_.close();
39 }
40 }
41
Good()42 bool FileStream::Good()
43 {
44 return fileStream_.good();
45 }
46
Initialize(const std::string & fileName)47 void FileStream::Initialize(const std::string &fileName)
48 {
49 // check file name
50 std::pair<bool, std::string> realPath = FilePathValid(fileName);
51 if (!realPath.first) {
52 LOG_ECMA(ERROR) << "FileStream: check file path failed";
53 fileStream_.close();
54 return;
55 }
56
57 fileStream_.open(realPath.second.c_str(), std::ios::out);
58 if (fileStream_.fail()) {
59 LOG_ECMA(ERROR) << "FileStream: open file failed";
60 }
61 }
62
FilePathValid(const std::string & fileName)63 std::pair<bool, std::string> FileStream::FilePathValid(const std::string &fileName)
64 {
65 if (fileName.empty() || fileName.size() > PATH_MAX) {
66 return std::make_pair(false, "");
67 }
68 char resolvedPath[PATH_MAX] = {0};
69 auto result = realpath(fileName.c_str(), resolvedPath);
70 if (result == resolvedPath || errno == ENOENT) {
71 return std::make_pair(true, std::string(resolvedPath));
72 }
73 return std::make_pair(false, "");
74 }
75
76 // Writes the chunk of data into the stream
WriteChunk(char * data,int32_t size)77 bool FileStream::WriteChunk(char *data, int32_t size)
78 {
79 if (fileStream_.fail()) {
80 return false;
81 }
82
83 std::string str;
84 str.resize(size);
85 for (int32_t i = 0; i < size; ++i) {
86 str[i] = data[i];
87 }
88
89 fileStream_ << str;
90
91 return true;
92 }
93
EndOfStream()94 void FileDescriptorStream::EndOfStream()
95 {
96 if (Good()) {
97 close(fd_);
98 }
99 }
100
Good()101 bool FileDescriptorStream::Good()
102 {
103 return fd_ > 0;
104 }
105
106 // Writes the chunk of data into the stream
WriteChunk(char * data,int32_t size)107 bool FileDescriptorStream::WriteChunk(char *data, int32_t size)
108 {
109 if (fd_ < 0) {
110 return false;
111 }
112
113 std::string str;
114 str.resize(size);
115 for (int32_t i = 0; i < size; ++i) {
116 str[i] = data[i];
117 }
118 int ret = dprintf(fd_, "%s", str.c_str());
119 if (ret < 0) {
120 LOG_ECMA(ERROR) << "Write FD print failed, ret" << ret;
121 return false;
122 }
123 ret = fsync(fd_);
124 if (ret < 0) {
125 LOG_ECMA(ERROR) << "Write FD file failed, ret" << ret;
126 return false;
127 }
128 return true;
129 }
130 }
131