Browse Source

Fix #5858

provide clear message when specifying a directory as an input file
June 5 years ago
parent
commit
b9f94f7bf5

+ 5 - 2
src/google/protobuf/compiler/command_line_interface.cc

@@ -1274,13 +1274,16 @@ bool CommandLineInterface::MakeProtoProtoPathRelative(
                    "comes first."
                 << std::endl;
       return false;
-    case DiskSourceTree::CANNOT_OPEN:
+    case DiskSourceTree::CANNOT_OPEN: {
       if (in_fallback_database) {
         return true;
       }
+      std::string error_str = source_tree->GetLastErrorMessage().empty() ?
+        strerror(errno) : source_tree->GetLastErrorMessage();
       std::cerr << "Could not map to virtual file: " << *proto << ": "
-                << strerror(errno) << std::endl;
+                << error_str << std::endl;
       return false;
+    }
     case DiskSourceTree::NO_MAPPING: {
       // Try to interpret the path as a virtual path.
       std::string disk_file;

+ 9 - 0
src/google/protobuf/compiler/importer.cc

@@ -490,6 +490,15 @@ io::ZeroCopyInputStream* DiskSourceTree::OpenVirtualFile(
 
 io::ZeroCopyInputStream* DiskSourceTree::OpenDiskFile(
     const std::string& filename) {
+  struct stat sb;
+  int ret = 0;
+  do {
+    ret = stat(filename.c_str(), &sb);
+  } while (ret != 0 && errno == EINTR);
+  if (!S_ISREG(sb.st_mode)) {
+    last_error_message_ = "Input file is not a regular file.";
+    return NULL;
+  }
   int file_descriptor;
   do {
     file_descriptor = open(filename.c_str(), O_RDONLY);