• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     RETURN_IF(stream_.eof(), 0, "read file end");
66     CHECK_TRUE(msgLen > 0, 0, "read in file %s msg length: %d", path_.c_str(), msgLen);
67     CHECK_TRUE(stream_, 0, "read msg length from %s (offset %zu) failed, or no more data!", path_.c_str(), offset);
68     CHECK_TRUE(helper_.AddSegment(reinterpret_cast<uint8_t*>(&msgLen), sizeof(msgLen)),
69         0, "Add payload for message length failed!");
70 
71     std::vector<char> msgData(msgLen);
72     offset = GetReadPos(stream_);
73     stream_.read(msgData.data(), msgData.size());
74     RETURN_IF(stream_.eof(), 0, "read file end");
75     CHECK_TRUE(stream_, 0, "read msg bytes from %s (offset %zu) failed!", path_.c_str(), offset);
76     CHECK_TRUE(helper_.AddSegment(reinterpret_cast<uint8_t*>(msgData.data()), msgData.size()),
77         0, "Add payload for message bytes failed!");
78 
79     CHECK_TRUE(message.ParseFromArray(msgData.data(), msgData.size()), 0, "ParseFromArray failed!");
80     return sizeof(msgLen) + msgData.size();
81 }
82 
ReadLen()83 long TraceFileReader::ReadLen()
84 {
85     CHECK_TRUE(stream_.is_open(), 0, "binary file %s not open or open failed!", path_.c_str());
86     CHECK_TRUE(!stream_.eof(), 0, "no more data in file %s stream", path_.c_str());
87 
88     uint32_t dataLen = 0;
89     stream_.read(reinterpret_cast<CharPtr>(&dataLen), sizeof(dataLen));
90     RETURN_IF(stream_.eof(), 0, "read file end");
91     CHECK_TRUE(dataLen > 0, 0, "read in file %s data length: %d", path_.c_str(), dataLen);
92     CHECK_TRUE(stream_, 0, "read data length from file %s (offset %zu) failed!", path_.c_str(), GetReadPos(stream_));
93     CHECK_TRUE(helper_.AddSegment(reinterpret_cast<uint8_t*>(&dataLen), sizeof(dataLen)),
94         0, "Add payload for data length failed!");
95     return dataLen;
96 }
97 
ReadData(uint8_t buffer[],uint32_t bufferSize)98 bool TraceFileReader::ReadData(uint8_t buffer[], uint32_t bufferSize)
99 {
100     CHECK_TRUE(stream_.is_open(), false, "binary file %s not open or open failed!", path_.c_str());
101     CHECK_TRUE(!stream_.eof(), false, "no more data in file %s stream", path_.c_str());
102 
103     stream_.read(reinterpret_cast<CharPtr>(buffer), bufferSize);
104     RETURN_IF(stream_.eof(), false, "read file end. read data bytes from %s failed! (offset %zu)",
105               path_.c_str(), GetReadPos(stream_));
106     CHECK_TRUE(stream_, false, "read data bytes from %s (offset %zu) failed!", path_.c_str(), GetReadPos(stream_));
107     CHECK_TRUE(helper_.AddSegment(buffer, bufferSize), false, "Add payload for data bytes failed!");
108     return true;
109 }
110