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