Apollo 10.0
自动驾驶开放平台
record_file_reader.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include "cyber/common/file.h"
20
21namespace apollo {
22namespace cyber {
23namespace record {
24
26
27bool RecordFileReader::Open(const std::string& path) {
28 std::lock_guard<std::mutex> lock(mutex_);
29 path_ = path;
31 AERROR << "File not exist, file: " << path_;
32 return false;
33 }
34 fd_ = open(path_.data(), O_RDONLY);
35 if (fd_ < 0) {
36 AERROR << "Open file failed, file: " << path_ << ", fd: " << fd_
37 << ", errno: " << errno;
38 return false;
39 }
40 end_of_file_ = false;
41 if (!ReadHeader()) {
42 AERROR << "Read header section fail, file: " << path_;
43 return false;
44 }
45 return true;
46}
47
49 if (fd_ >= 0) {
50 close(fd_);
51 fd_ = -1;
52 }
53}
54
56 if (!SetPosition(sizeof(struct Section) + HEADER_LENGTH)) {
57 AERROR << "Reset position fail, file: " << path_;
58 return false;
59 }
60 end_of_file_ = false;
61 return true;
62}
63
64bool RecordFileReader::ReadHeader() {
65 Section section;
66 if (!ReadSection(&section)) {
67 AERROR << "Read header section fail, file is broken or it is not a record "
68 "file.";
69 return false;
70 }
71 if (section.type != SectionType::SECTION_HEADER) {
72 AERROR << "Check section type failed"
73 << ", expect: " << SectionType::SECTION_HEADER
74 << ", actual: " << section.type;
75 return false;
76 }
77 if (!ReadSection<proto::Header>(section.size, &header_)) {
78 AERROR << "Read header section fail, file is broken or it is not a record "
79 "file.";
80 return false;
81 }
82 if (!SetPosition(sizeof(struct Section) + HEADER_LENGTH)) {
83 AERROR << "Skip bytes for reaching the nex section failed.";
84 return false;
85 }
86 return true;
87}
88
90 if (!header_.is_complete()) {
91 AERROR << "Record file is not complete.";
92 return false;
93 }
95 AERROR << "Skip bytes for reaching the index section failed.";
96 return false;
97 }
98 Section section;
99 if (!ReadSection(&section)) {
100 AERROR << "Read index section fail, maybe file is broken.";
101 return false;
102 }
103 if (section.type != SectionType::SECTION_INDEX) {
104 AERROR << "Check section type failed"
105 << ", expect: " << SectionType::SECTION_INDEX
106 << ", actual: " << section.type;
107 return false;
108 }
109 if (!ReadSection<proto::Index>(section.size, &index_)) {
110 AERROR << "Read index section fail.";
111 return false;
112 }
113 Reset();
114 return true;
115}
116
118 ssize_t count = read(fd_, section, sizeof(struct Section));
119 if (count < 0) {
120 AERROR << "Read fd failed, fd_: " << fd_ << ", errno: " << errno;
121 return false;
122 } else if (count == 0) {
123 end_of_file_ = true;
124 AINFO << "Reach end of file.";
125 return false;
126 } else if (count != sizeof(struct Section)) {
127 AERROR << "Read fd failed, fd_: " << fd_
128 << ", expect count: " << sizeof(struct Section)
129 << ", actual count: " << count;
130 return false;
131 }
132 return true;
133}
134
136 int64_t pos = CurrentPosition();
137 if (size > INT64_MAX - pos) {
138 AERROR << "Current position plus skip count is larger than INT64_MAX, "
139 << pos << " + " << size << " > " << INT64_MAX;
140 return false;
141 }
142 if (!SetPosition(pos + size)) {
143 AERROR << "Skip failed, file: " << path_ << ", current position: " << pos
144 << "skip count: " << size;
145 return false;
146 }
147 return true;
148}
149
153
154} // namespace record
155} // namespace cyber
156} // namespace apollo
bool Open(const std::string &path) override
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
bool PathExists(const std::string &path)
Check if the path exists.
Definition file.cc:195
class register implement
Definition arena_queue.h:37
optional uint64 index_position
Definition record.proto:68
proto::SectionType type
Definition section.h:25