1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
40 #include <sys/types.h>
47 #include <google/protobuf/compiler/importer.h>
49 #include <google/protobuf/compiler/parser.h>
50 #include <google/protobuf/io/tokenizer.h>
51 #include <google/protobuf/io/zero_copy_stream_impl.h>
52 #include <google/protobuf/stubs/strutil.h>
60 #define F_OK 00 // not defined by MSVC for whatever reason
65 // Returns true if the text looks like a Windows-style absolute path, starting
66 // with a drive letter. Example: "C:\foo". TODO(kenton): Share this with
67 // copy in command_line_interface.cc?
68 static bool IsWindowsAbsolutePath(const string& text) {
69 #if defined(_WIN32) || defined(__CYGWIN__)
70 return text.size() >= 3 && text[1] == ':' &&
72 (text[2] == '/' || text[2] == '\\') &&
73 text.find_last_of(':') == 1;
79 MultiFileErrorCollector::~MultiFileErrorCollector() {}
81 // This class serves two purposes:
82 // - It implements the ErrorCollector interface (used by Tokenizer and Parser)
83 // in terms of MultiFileErrorCollector, using a particular filename.
84 // - It lets us check if any errors have occurred.
85 class SourceTreeDescriptorDatabase::SingleFileErrorCollector
86 : public io::ErrorCollector {
88 SingleFileErrorCollector(const string& filename,
89 MultiFileErrorCollector* multi_file_error_collector)
90 : filename_(filename),
91 multi_file_error_collector_(multi_file_error_collector),
93 ~SingleFileErrorCollector() {}
95 bool had_errors() { return had_errors_; }
97 // implements ErrorCollector ---------------------------------------
98 void AddError(int line, int column, const string& message) {
99 if (multi_file_error_collector_ != NULL) {
100 multi_file_error_collector_->AddError(filename_, line, column, message);
107 MultiFileErrorCollector* multi_file_error_collector_;
111 // ===================================================================
113 SourceTreeDescriptorDatabase::SourceTreeDescriptorDatabase(
114 SourceTree* source_tree)
115 : source_tree_(source_tree),
116 error_collector_(NULL),
117 using_validation_error_collector_(false),
118 validation_error_collector_(this) {}
120 SourceTreeDescriptorDatabase::~SourceTreeDescriptorDatabase() {}
122 bool SourceTreeDescriptorDatabase::FindFileByName(
123 const string& filename, FileDescriptorProto* output) {
124 scoped_ptr<io::ZeroCopyInputStream> input(source_tree_->Open(filename));
126 if (error_collector_ != NULL) {
127 error_collector_->AddError(filename, -1, 0, "File not found.");
132 // Set up the tokenizer and parser.
133 SingleFileErrorCollector file_error_collector(filename, error_collector_);
134 io::Tokenizer tokenizer(input.get(), &file_error_collector);
137 if (error_collector_ != NULL) {
138 parser.RecordErrorsTo(&file_error_collector);
140 if (using_validation_error_collector_) {
141 parser.RecordSourceLocationsTo(&source_locations_);
145 output->set_name(filename);
146 return parser.Parse(&tokenizer, output) &&
147 !file_error_collector.had_errors();
150 bool SourceTreeDescriptorDatabase::FindFileContainingSymbol(
151 const string& symbol_name, FileDescriptorProto* output) {
155 bool SourceTreeDescriptorDatabase::FindFileContainingExtension(
156 const string& containing_type, int field_number,
157 FileDescriptorProto* output) {
161 // -------------------------------------------------------------------
163 SourceTreeDescriptorDatabase::ValidationErrorCollector::
164 ValidationErrorCollector(SourceTreeDescriptorDatabase* owner)
167 SourceTreeDescriptorDatabase::ValidationErrorCollector::
168 ~ValidationErrorCollector() {}
170 void SourceTreeDescriptorDatabase::ValidationErrorCollector::AddError(
171 const string& filename,
172 const string& element_name,
173 const Message* descriptor,
174 ErrorLocation location,
175 const string& message) {
176 if (owner_->error_collector_ == NULL) return;
179 owner_->source_locations_.Find(descriptor, location, &line, &column);
180 owner_->error_collector_->AddError(filename, line, column, message);
183 // ===================================================================
185 Importer::Importer(SourceTree* source_tree,
186 MultiFileErrorCollector* error_collector)
187 : database_(source_tree),
188 pool_(&database_, database_.GetValidationErrorCollector()) {
189 database_.RecordErrorsTo(error_collector);
192 Importer::~Importer() {}
194 const FileDescriptor* Importer::Import(const string& filename) {
195 return pool_.FindFileByName(filename);
198 // ===================================================================
200 SourceTree::~SourceTree() {}
202 DiskSourceTree::DiskSourceTree() {}
204 DiskSourceTree::~DiskSourceTree() {}
206 static inline char LastChar(const string& str) {
207 return str[str.size() - 1];
210 // Given a path, returns an equivalent path with these changes:
211 // - On Windows, any backslashes are replaced with forward slashes.
212 // - Any instances of the directory "." are removed.
213 // - Any consecutive '/'s are collapsed into a single slash.
214 // Note that the resulting string may be empty.
216 // TODO(kenton): It would be nice to handle "..", e.g. so that we can figure
217 // out that "foo/bar.proto" is inside "baz/../foo". However, if baz is a
218 // symlink or doesn't exist, then things get complicated, and we can't
219 // actually determine this without investigating the filesystem, probably
220 // in non-portable ways. So, we punt.
222 // TODO(kenton): It would be nice to use realpath() here except that it
223 // resolves symbolic links. This could cause problems if people place
224 // symbolic links in their source tree. For example, if you executed:
225 // protoc --proto_path=foo foo/bar/baz.proto
226 // then if foo/bar is a symbolic link, foo/bar/baz.proto will canonicalize
227 // to a path which does not appear to be under foo, and thus the compiler
228 // will complain that baz.proto is not inside the --proto_path.
229 static string CanonicalizePath(string path) {
231 // The Win32 API accepts forward slashes as a path delimiter even though
232 // backslashes are standard. Let's avoid confusion and use only forward
234 if (HasPrefixString(path, "\\\\")) {
235 // Avoid converting two leading backslashes.
236 path = "\\\\" + StringReplace(path.substr(2), "\\", "/", true);
238 path = StringReplace(path, "\\", "/", true);
242 vector<string> parts;
243 vector<string> canonical_parts;
244 SplitStringUsing(path, "/", &parts); // Note: Removes empty parts.
245 for (int i = 0; i < parts.size(); i++) {
246 if (parts[i] == ".") {
249 canonical_parts.push_back(parts[i]);
252 string result = JoinStrings(canonical_parts, "/");
253 if (!path.empty() && path[0] == '/') {
254 // Restore leading slash.
255 result = '/' + result;
257 if (!path.empty() && LastChar(path) == '/' &&
258 !result.empty() && LastChar(result) != '/') {
259 // Restore trailing slash.
265 static inline bool ContainsParentReference(const string& path) {
266 return path == ".." ||
267 HasPrefixString(path, "../") ||
268 HasSuffixString(path, "/..") ||
269 path.find("/../") != string::npos;
272 // Maps a file from an old location to a new one. Typically, old_prefix is
273 // a virtual path and new_prefix is its corresponding disk path. Returns
274 // false if the filename did not start with old_prefix, otherwise replaces
275 // old_prefix with new_prefix and stores the result in *result. Examples:
277 // assert(ApplyMapping("foo/bar", "", "baz", &result));
278 // assert(result == "baz/foo/bar");
280 // assert(ApplyMapping("foo/bar", "foo", "baz", &result));
281 // assert(result == "baz/bar");
283 // assert(ApplyMapping("foo", "foo", "bar", &result));
284 // assert(result == "bar");
286 // assert(!ApplyMapping("foo/bar", "baz", "qux", &result));
287 // assert(!ApplyMapping("foo/bar", "baz", "qux", &result));
288 // assert(!ApplyMapping("foobar", "foo", "baz", &result));
289 static bool ApplyMapping(const string& filename,
290 const string& old_prefix,
291 const string& new_prefix,
293 if (old_prefix.empty()) {
294 // old_prefix matches any relative path.
295 if (ContainsParentReference(filename)) {
296 // We do not allow the file name to use "..".
299 if (HasPrefixString(filename, "/") ||
300 IsWindowsAbsolutePath(filename)) {
301 // This is an absolute path, so it isn't matched by the empty string.
304 result->assign(new_prefix);
305 if (!result->empty()) result->push_back('/');
306 result->append(filename);
308 } else if (HasPrefixString(filename, old_prefix)) {
309 // old_prefix is a prefix of the filename. Is it the whole filename?
310 if (filename.size() == old_prefix.size()) {
311 // Yep, it's an exact match.
312 *result = new_prefix;
315 // Not an exact match. Is the next character a '/'? Otherwise,
316 // this isn't actually a match at all. E.g. the prefix "foo/bar"
317 // does not match the filename "foo/barbaz".
318 int after_prefix_start = -1;
319 if (filename[old_prefix.size()] == '/') {
320 after_prefix_start = old_prefix.size() + 1;
321 } else if (filename[old_prefix.size() - 1] == '/') {
322 // old_prefix is never empty, and canonicalized paths never have
323 // consecutive '/' characters.
324 after_prefix_start = old_prefix.size();
326 if (after_prefix_start != -1) {
327 // Yep. So the prefixes are directories and the filename is a file
329 string after_prefix = filename.substr(after_prefix_start);
330 if (ContainsParentReference(after_prefix)) {
331 // We do not allow the file name to use "..".
334 result->assign(new_prefix);
335 if (!result->empty()) result->push_back('/');
336 result->append(after_prefix);
345 void DiskSourceTree::MapPath(const string& virtual_path,
346 const string& disk_path) {
347 mappings_.push_back(Mapping(virtual_path, CanonicalizePath(disk_path)));
350 DiskSourceTree::DiskFileToVirtualFileResult
351 DiskSourceTree::DiskFileToVirtualFile(
352 const string& disk_file,
353 string* virtual_file,
354 string* shadowing_disk_file) {
355 int mapping_index = -1;
356 string canonical_disk_file = CanonicalizePath(disk_file);
358 for (int i = 0; i < mappings_.size(); i++) {
359 // Apply the mapping in reverse.
360 if (ApplyMapping(canonical_disk_file, mappings_[i].disk_path,
361 mappings_[i].virtual_path, virtual_file)) {
368 if (mapping_index == -1) {
372 // Iterate through all mappings with higher precedence and verify that none
373 // of them map this file to some other existing file.
374 for (int i = 0; i < mapping_index; i++) {
375 if (ApplyMapping(*virtual_file, mappings_[i].virtual_path,
376 mappings_[i].disk_path, shadowing_disk_file)) {
377 if (access(shadowing_disk_file->c_str(), F_OK) >= 0) {
383 shadowing_disk_file->clear();
385 // Verify that we can open the file. Note that this also has the side-effect
386 // of verifying that we are not canonicalizing away any non-existent
388 scoped_ptr<io::ZeroCopyInputStream> stream(OpenDiskFile(disk_file));
389 if (stream == NULL) {
396 bool DiskSourceTree::VirtualFileToDiskFile(const string& virtual_file,
398 scoped_ptr<io::ZeroCopyInputStream> stream(OpenVirtualFile(virtual_file,
400 return stream != NULL;
403 io::ZeroCopyInputStream* DiskSourceTree::Open(const string& filename) {
404 return OpenVirtualFile(filename, NULL);
407 io::ZeroCopyInputStream* DiskSourceTree::OpenVirtualFile(
408 const string& virtual_file,
410 if (virtual_file != CanonicalizePath(virtual_file) ||
411 ContainsParentReference(virtual_file)) {
412 // We do not allow importing of paths containing things like ".." or
413 // consecutive slashes since the compiler expects files to be uniquely
414 // identified by file name.
418 for (int i = 0; i < mappings_.size(); i++) {
419 string temp_disk_file;
420 if (ApplyMapping(virtual_file, mappings_[i].virtual_path,
421 mappings_[i].disk_path, &temp_disk_file)) {
422 io::ZeroCopyInputStream* stream = OpenDiskFile(temp_disk_file);
423 if (stream != NULL) {
424 if (disk_file != NULL) {
425 *disk_file = temp_disk_file;
430 if (errno == EACCES) {
431 // The file exists but is not readable.
432 // TODO(kenton): Find a way to report this more nicely.
433 GOOGLE_LOG(WARNING) << "Read access is denied for file: " << temp_disk_file;
442 io::ZeroCopyInputStream* DiskSourceTree::OpenDiskFile(
443 const string& filename) {
446 file_descriptor = open(filename.c_str(), O_RDONLY);
447 } while (file_descriptor < 0 && errno == EINTR);
448 if (file_descriptor >= 0) {
449 io::FileInputStream* result = new io::FileInputStream(file_descriptor);
450 result->SetCloseOnDelete(true);
457 } // namespace compiler
458 } // namespace protobuf
459 } // namespace google