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 #include <memory>
16
17 #include "trace_file_reader.h"
18
19 #include "logging.h"
20
21 using CharPtr = std::unique_ptr<char>::pointer;
22
~TraceFileReader()23 TraceFileReader::~TraceFileReader()
24 {
25 if (stream_.is_open()) {
26 stream_.close();
27 }
28 }
29
GetHeader() const30 TraceFileHeader TraceFileReader::GetHeader() const
31 {
32 return header_;
33 }
34
ValidateHeader()35 bool TraceFileReader::ValidateHeader()
36 {
37 return helper_.Validate(header_);
38 }
39
Open(const std::string & path)40 bool TraceFileReader::Open(const std::string& path)
41 {
42 stream_.open(path, std::ios_base::in | std::ios_base::binary);
43 CHECK_TRUE(stream_.is_open(), false, "open %s failed, %d!", path.c_str(), errno);
44
45 stream_.read(reinterpret_cast<CharPtr>(&header_), sizeof(header_));
46 CHECK_TRUE(stream_, false, "read header from %s failed!", path_.c_str());
47
48 path_ = path;
49 return true;
50 }
51
GetReadPos(std::ifstream & stream)52 static size_t GetReadPos(std::ifstream& stream)
53 {
54 return static_cast<size_t>(stream.tellg());
55 }
56
Read(MessageLite & message)57 long TraceFileReader::Read(MessageLite& message)
58 {
59 CHECK_TRUE(stream_.is_open(), 0, "binary file %s not open or open failed!", path_.c_str());
60 CHECK_TRUE(!stream_.eof(), 0, "no more data in file %s stream", path_.c_str());
61
62 uint32_t msgLen = 0;
63 size_t offset = GetReadPos(stream_);
64 stream_.read(reinterpret_cast<CharPtr>(&msgLen), sizeof(msgLen));
65 CHECK_TRUE(stream_, 0, "read msg length from %s (offset %zu) failed, or no more data!", path_.c_str(), offset);
66 CHECK_TRUE(helper_.AddSegment(reinterpret_cast<uint8_t*>(&msgLen), sizeof(msgLen)),
67 0, "Add payload for message length failed!");
68
69 std::vector<char> msgData(msgLen);
70 offset = GetReadPos(stream_);
71 stream_.read(msgData.data(), msgData.size());
72 CHECK_TRUE(stream_, 0, "read msg bytes from %s (offset %zu) failed!", path_.c_str(), offset);
73 CHECK_TRUE(helper_.AddSegment(reinterpret_cast<uint8_t*>(msgData.data()), msgData.size()),
74 0, "Add payload for message bytes failed!");
75
76 CHECK_TRUE(message.ParseFromArray(msgData.data(), msgData.size()), 0, "ParseFromArray failed!");
77 return sizeof(msgLen) + msgData.size();
78 }
79
Read(uint8_t buffer[],uint32_t bufferSize)80 long TraceFileReader::Read(uint8_t buffer[], uint32_t bufferSize)
81 {
82 CHECK_TRUE(stream_.is_open(), 0, "binary file %s not open or open failed!", path_.c_str());
83 CHECK_TRUE(!stream_.eof(), 0, "no more data in file %s stream", path_.c_str());
84
85 uint32_t dataLen = 0;
86 stream_.read(reinterpret_cast<CharPtr>(&dataLen), sizeof(dataLen));
87 CHECK_TRUE(stream_, 0, "read data length from file %s (offset %zu) failed!", path_.c_str(), GetReadPos(stream_));
88 CHECK_TRUE(bufferSize >= dataLen, 0, "buffer size does not enough to read data bytes!");
89 CHECK_TRUE(helper_.AddSegment(reinterpret_cast<uint8_t*>(&dataLen), sizeof(dataLen)),
90 0, "Add payload for data length failed!");
91
92 stream_.read(reinterpret_cast<CharPtr>(buffer), dataLen);
93 CHECK_TRUE(stream_, 0, "read data bytes from %s (offset %zu) failed!", path_.c_str(), GetReadPos(stream_));
94 CHECK_TRUE(helper_.AddSegment(buffer, dataLen), 0, "Add payload for data bytes failed!");
95 return dataLen;
96 }
97