[LLD][COFF] Fix pdb loading when the path points to a removable device

Differential Revision: https://reviews.llvm.org/D52666


git-svn-id: https://llvm.org/svn/llvm-project/lld/trunk@343366 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/COFF/PDB.cpp b/COFF/PDB.cpp
index e25fb4e..80f0219 100644
--- a/COFF/PDB.cpp
+++ b/COFF/PDB.cpp
@@ -351,6 +351,12 @@
 
 static Expected<std::unique_ptr<pdb::NativeSession>>
 tryToLoadPDB(const GUID &GuidFromObj, StringRef TSPath) {
+  // Ensure the file exists before anything else. We want to return ENOENT,
+  // "file not found", even if the path points to a removable device (in which
+  // case the return message would be EAGAIN, "resource unavailable try again")
+  if (!llvm::sys::fs::exists(TSPath))
+    return errorCodeToError(std::error_code(ENOENT, std::generic_category()));
+
   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(
       TSPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
   if (!MBOrErr)