This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG14735cab6554: [lldb] [gdb-remote] Add eOpenOptionReadWrite 
for future gdb compat (authored by mgorny).
Herald added a project: LLDB.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106984/new/

https://reviews.llvm.org/D106984

Files:
  lldb/docs/lldb-platform-packets.txt
  lldb/include/lldb/Host/File.h
  lldb/source/API/SBStream.cpp
  lldb/source/Commands/CommandObjectMemory.cpp
  lldb/source/Commands/CommandObjectPlatform.cpp
  lldb/source/Commands/CommandObjectSettings.cpp
  lldb/source/Commands/CommandObjectTarget.cpp
  lldb/source/Core/Debugger.cpp
  lldb/source/Core/StreamFile.cpp
  lldb/source/Expression/REPL.cpp
  lldb/source/Host/common/File.cpp
  lldb/source/Host/common/FileSystem.cpp
  lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
  lldb/source/Host/windows/Host.cpp
  lldb/source/Interpreter/CommandInterpreter.cpp
  lldb/source/Interpreter/ScriptInterpreter.cpp
  lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
  lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
  
lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
  lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
  lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
  lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
  lldb/source/Target/ModuleCache.cpp
  lldb/source/Target/Platform.cpp
  lldb/source/Target/Process.cpp
  lldb/source/Target/Target.cpp
  lldb/unittests/Host/FileSystemTest.cpp
  lldb/unittests/Host/FileTest.cpp
  lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Index: lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp
===================================================================
--- lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp
+++ lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp
@@ -583,7 +583,7 @@
 
 TEST_F(PythonDataObjectsTest, TestPythonFile) {
   auto file = FileSystem::Instance().Open(FileSpec(FileSystem::DEV_NULL),
-                                          File::eOpenOptionRead);
+                                          File::eOpenOptionReadOnly);
   ASSERT_THAT_EXPECTED(file, llvm::Succeeded());
   auto py_file = PythonFile::FromFile(*file.get(), "r");
   ASSERT_THAT_EXPECTED(py_file, llvm::Succeeded());
@@ -858,4 +858,4 @@
   ASSERT_THAT_EXPECTED(r, llvm::Succeeded());
   auto g = As<std::string>(globals.GetItem("g"));
   ASSERT_THAT_EXPECTED(g, llvm::HasValue("foobarbaz"));
-}
\ No newline at end of file
+}
Index: lldb/unittests/Host/FileTest.cpp
===================================================================
--- lldb/unittests/Host/FileTest.cpp
+++ lldb/unittests/Host/FileTest.cpp
@@ -46,7 +46,7 @@
   llvm::FileRemover remover(name);
   ASSERT_GE(fd, 0);
 
-  NativeFile file(fd, File::eOpenOptionWrite, true);
+  NativeFile file(fd, File::eOpenOptionWriteOnly, true);
   ASSERT_TRUE(file.IsValid());
 
   FILE *stream = file.GetStream();
Index: lldb/unittests/Host/FileSystemTest.cpp
===================================================================
--- lldb/unittests/Host/FileSystemTest.cpp
+++ lldb/unittests/Host/FileSystemTest.cpp
@@ -296,7 +296,7 @@
   FileSpec spec("/file/that/does/not/exist.txt");
 #endif
   FileSystem fs;
-  auto file = fs.Open(spec, File::eOpenOptionRead, 0, true);
+  auto file = fs.Open(spec, File::eOpenOptionReadOnly, 0, true);
   ASSERT_FALSE(file);
   std::error_code code = errorToErrorCode(file.takeError());
   EXPECT_EQ(code.category(), std::system_category());
Index: lldb/source/Target/Target.cpp
===================================================================
--- lldb/source/Target/Target.cpp
+++ lldb/source/Target/Target.cpp
@@ -1022,7 +1022,7 @@
   }
 
   StreamFile out_file(path.c_str(),
-                      File::eOpenOptionTruncate | File::eOpenOptionWrite |
+                      File::eOpenOptionTruncate | File::eOpenOptionWriteOnly |
                           File::eOpenOptionCanCreate |
                           File::eOpenOptionCloseOnExec,
                       lldb::eFilePermissionsFileDefault);
Index: lldb/source/Target/Process.cpp
===================================================================
--- lldb/source/Target/Process.cpp
+++ lldb/source/Target/Process.cpp
@@ -4310,8 +4310,8 @@
       : IOHandler(process->GetTarget().GetDebugger(),
                   IOHandler::Type::ProcessIO),
         m_process(process),
-        m_read_file(GetInputFD(), File::eOpenOptionRead, false),
-        m_write_file(write_fd, File::eOpenOptionWrite, false) {
+        m_read_file(GetInputFD(), File::eOpenOptionReadOnly, false),
+        m_write_file(write_fd, File::eOpenOptionWriteOnly, false) {
     m_pipe.CreateNew(false);
   }
 
Index: lldb/source/Target/Platform.cpp
===================================================================
--- lldb/source/Target/Platform.cpp
+++ lldb/source/Target/Platform.cpp
@@ -1225,7 +1225,7 @@
   LLDB_LOGF(log, "[PutFile] Using block by block transfer....\n");
 
   auto source_open_options =
-      File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
+      File::eOpenOptionReadOnly | File::eOpenOptionCloseOnExec;
   namespace fs = llvm::sys::fs;
   if (fs::is_symlink_file(source.GetPath()))
     source_open_options |= File::eOpenOptionDontFollowSymlinks;
@@ -1240,7 +1240,7 @@
     permissions = lldb::eFilePermissionsFileDefault;
 
   lldb::user_id_t dest_file = OpenFile(
-      destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
+      destination, File::eOpenOptionCanCreate | File::eOpenOptionWriteOnly |
                        File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
       permissions, error);
   LLDB_LOGF(log, "dest_file = %" PRIu64 "\n", dest_file);
@@ -1663,7 +1663,7 @@
     return error;
   }
 
-  auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead,
+  auto src_fd = OpenFile(src_file_spec, File::eOpenOptionReadOnly,
                          lldb::eFilePermissionsFileDefault, error);
 
   if (error.Fail()) {
Index: lldb/source/Target/ModuleCache.cpp
===================================================================
--- lldb/source/Target/ModuleCache.cpp
+++ lldb/source/Target/ModuleCache.cpp
@@ -159,7 +159,7 @@
   m_file_spec = JoinPath(lock_dir_spec, uuid.GetAsString().c_str());
 
   auto file = FileSystem::Instance().Open(
-      m_file_spec, File::eOpenOptionWrite | File::eOpenOptionCanCreate |
+      m_file_spec, File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate |
                        File::eOpenOptionCloseOnExec);
   if (file)
     m_file_up = std::move(file.get());
Index: lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
@@ -1114,10 +1114,12 @@
   auto writable = As<bool>(obj.CallMethod("writable"));
   if (!writable)
     return writable.takeError();
-  if (readable.get())
-    options |= File::eOpenOptionRead;
-  if (writable.get())
-    options |= File::eOpenOptionWrite;
+  if (readable.get() && writable.get())
+    options |= File::eOpenOptionReadWrite;
+  else if (writable.get())
+    options |= File::eOpenOptionWriteOnly;
+  else if (readable.get())
+    options |= File::eOpenOptionReadOnly;
   return options;
 #else
   PythonString py_mode = obj.GetAttributeValue("mode").AsType<PythonString>();
@@ -1413,7 +1415,10 @@
   if (!options)
     return options.takeError();
 
-  if (options.get() & File::eOpenOptionWrite) {
+  File::OpenOptions rw =
+      options.get() & (File::eOpenOptionReadOnly | File::eOpenOptionWriteOnly |
+                       File::eOpenOptionReadWrite);
+  if (rw == File::eOpenOptionWriteOnly || rw == File::eOpenOptionReadWrite) {
     // LLDB and python will not share I/O buffers.  We should probably
     // flush the python buffers now.
     auto r = CallMethod("flush");
Index: lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
===================================================================
--- lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -104,7 +104,7 @@
 // and get the packet history dumped to a file.
 void DumpProcessGDBRemotePacketHistory(void *p, const char *path) {
   auto file = FileSystem::Instance().Open(
-      FileSpec(path), File::eOpenOptionWrite | File::eOpenOptionCanCreate);
+      FileSpec(path), File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate);
   if (!file) {
     llvm::consumeError(file.takeError());
     return;
Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
===================================================================
--- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
+++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
@@ -575,7 +575,7 @@
       }
 
       std::string buffer(count, 0);
-      NativeFile file(fd, File::eOpenOptionRead, false);
+      NativeFile file(fd, File::eOpenOptionReadOnly, false);
       Status error = file.Read(static_cast<void *>(&buffer[0]), count, offset);
       const ssize_t bytes_read = error.Success() ? count : -1;
       const int save_errno = error.GetError();
@@ -607,7 +607,7 @@
     if (packet.GetChar() == ',') {
       std::string buffer;
       if (packet.GetEscapedBinaryData(buffer)) {
-        NativeFile file(fd, File::eOpenOptionWrite, false);
+        NativeFile file(fd, File::eOpenOptionWriteOnly, false);
         size_t count = buffer.size();
         Status error =
             file.Write(static_cast<const void *>(&buffer[0]), count, offset);
Index: lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
===================================================================
--- lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
+++ lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
@@ -205,7 +205,7 @@
     // close dst
     LLDB_LOGF(log, "[GetFile] Using block by block transfer....\n");
     Status error;
-    user_id_t fd_src = OpenFile(source, File::eOpenOptionRead,
+    user_id_t fd_src = OpenFile(source, File::eOpenOptionReadOnly,
                                 lldb::eFilePermissionsFileDefault, error);
 
     if (fd_src == UINT64_MAX)
@@ -218,7 +218,7 @@
       permissions = lldb::eFilePermissionsFileDefault;
 
     user_id_t fd_dst = FileCache::GetInstance().OpenFile(
-        destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
+        destination, File::eOpenOptionCanCreate | File::eOpenOptionWriteOnly |
                          File::eOpenOptionTruncate,
         permissions, error);
 
Index: lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
===================================================================
--- lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
+++ lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
@@ -6825,7 +6825,7 @@
 
         std::string core_file_path(outfile.GetPath());
         auto core_file = FileSystem::Instance().Open(
-            outfile, File::eOpenOptionWrite | File::eOpenOptionTruncate |
+            outfile, File::eOpenOptionWriteOnly | File::eOpenOptionTruncate |
                          File::eOpenOptionCanCreate);
         if (!core_file) {
           error = core_file.takeError();
Index: lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
===================================================================
--- lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
+++ lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
@@ -2660,7 +2660,7 @@
   FileSpec file_spec(path);
   FileSystem::Instance().Resolve(file_spec);
   auto file = FileSystem::Instance().Open(
-      file_spec, File::eOpenOptionWrite | File::eOpenOptionCanCreate |
+      file_spec, File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate |
                      File::eOpenOptionTruncate);
 
   if (!file) {
@@ -4585,8 +4585,9 @@
     if (outfile_spec) {
       // Open output file
       std::string path = outfile_spec.GetPath();
-      auto file = FileSystem::Instance().Open(
-          outfile_spec, File::eOpenOptionWrite | File::eOpenOptionCanCreate);
+      auto file = FileSystem::Instance().Open(outfile_spec,
+                                              File::eOpenOptionWriteOnly |
+                                                  File::eOpenOptionCanCreate);
       if (file) {
         output_stream_storage =
             std::make_unique<StreamFile>(std::move(file.get()));
Index: lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
===================================================================
--- lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
+++ lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
@@ -45,7 +45,7 @@
     llvm::SmallString<128> result_path;
     llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
     if (temp_fd != -1) {
-      lldb_private::NativeFile file(temp_fd, File::eOpenOptionWrite, true);
+      lldb_private::NativeFile file(temp_fd, File::eOpenOptionWriteOnly, true);
       text = "#line 1 \"" + std::string(result_path) + "\"\n" + text;
       size_t bytes_written = text.size();
       file.Write(text.c_str(), bytes_written);
Index: lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
===================================================================
--- lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
+++ lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
@@ -1069,7 +1069,7 @@
     }
 
     if (temp_fd != -1) {
-      lldb_private::NativeFile file(temp_fd, File::eOpenOptionWrite, true);
+      lldb_private::NativeFile file(temp_fd, File::eOpenOptionWriteOnly, true);
       const size_t expr_text_len = strlen(expr_text);
       size_t bytes_written = expr_text_len;
       if (file.Write(expr_text, bytes_written).Success()) {
Index: lldb/source/Interpreter/ScriptInterpreter.cpp
===================================================================
--- lldb/source/Interpreter/ScriptInterpreter.cpp
+++ lldb/source/Interpreter/ScriptInterpreter.cpp
@@ -141,12 +141,12 @@
         new ScriptInterpreterIORedirect(debugger, result));
 
   auto nullin = FileSystem::Instance().Open(FileSpec(FileSystem::DEV_NULL),
-                                            File::eOpenOptionRead);
+                                            File::eOpenOptionReadOnly);
   if (!nullin)
     return nullin.takeError();
 
   auto nullout = FileSystem::Instance().Open(FileSpec(FileSystem::DEV_NULL),
-                                             File::eOpenOptionWrite);
+                                             File::eOpenOptionWriteOnly);
   if (!nullout)
     return nullin.takeError();
 
Index: lldb/source/Interpreter/CommandInterpreter.cpp
===================================================================
--- lldb/source/Interpreter/CommandInterpreter.cpp
+++ lldb/source/Interpreter/CommandInterpreter.cpp
@@ -2433,7 +2433,7 @@
 
   std::string cmd_file_path = cmd_file.GetPath();
   auto input_file_up =
-      FileSystem::Instance().Open(cmd_file, File::eOpenOptionRead);
+      FileSystem::Instance().Open(cmd_file, File::eOpenOptionReadOnly);
   if (!input_file_up) {
     std::string error = llvm::toString(input_file_up.takeError());
     result.AppendErrorWithFormatv(
@@ -2954,7 +2954,7 @@
     return false;
   };
 
-  File::OpenOptions flags = File::eOpenOptionWrite |
+  File::OpenOptions flags = File::eOpenOptionWriteOnly |
                             File::eOpenOptionCanCreate |
                             File::eOpenOptionTruncate;
 
Index: lldb/source/Host/windows/Host.cpp
===================================================================
--- lldb/source/Host/windows/Host.cpp
+++ lldb/source/Host/windows/Host.cpp
@@ -35,7 +35,7 @@
   // Open the PE File as a binary file, and parse just enough information to
   // determine the machine type.
   auto imageBinaryP = FileSystem::Instance().Open(
-      executable, File::eOpenOptionRead, lldb::eFilePermissionsUserRead);
+      executable, File::eOpenOptionReadOnly, lldb::eFilePermissionsUserRead);
   if (!imageBinaryP)
     return llvm::errorToBool(imageBinaryP.takeError());
   File &imageBinary = *imageBinaryP.get();
Index: lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
===================================================================
--- lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
+++ lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
@@ -87,8 +87,10 @@
 ConnectionFileDescriptor::ConnectionFileDescriptor(int fd, bool owns_fd)
     : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
       m_waiting_for_accept(false), m_child_processes_inherit(false) {
-  m_write_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, owns_fd);
-  m_read_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionRead, false);
+  m_write_sp =
+      std::make_shared<NativeFile>(fd, File::eOpenOptionWriteOnly, owns_fd);
+  m_read_sp =
+      std::make_shared<NativeFile>(fd, File::eOpenOptionReadOnly, false);
 
   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION |
                                                   LIBLLDB_LOG_OBJECT));
@@ -219,10 +221,10 @@
             m_read_sp = std::move(tcp_socket);
             m_write_sp = m_read_sp;
           } else {
-            m_read_sp =
-                std::make_shared<NativeFile>(fd, File::eOpenOptionRead, false);
-            m_write_sp =
-                std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, false);
+            m_read_sp = std::make_shared<NativeFile>(
+                fd, File::eOpenOptionReadOnly, false);
+            m_write_sp = std::make_shared<NativeFile>(
+                fd, File::eOpenOptionWriteOnly, false);
           }
           m_uri = std::string(*addr);
           return eConnectionStatusSuccess;
@@ -271,8 +273,10 @@
           ::fcntl(fd, F_SETFL, flags);
         }
       }
-      m_read_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionRead, true);
-      m_write_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, false);
+      m_read_sp =
+          std::make_shared<NativeFile>(fd, File::eOpenOptionReadOnly, true);
+      m_write_sp =
+          std::make_shared<NativeFile>(fd, File::eOpenOptionWriteOnly, false);
       return eConnectionStatusSuccess;
     }
 #endif
Index: lldb/source/Host/common/FileSystem.cpp
===================================================================
--- lldb/source/Host/common/FileSystem.cpp
+++ lldb/source/Host/common/FileSystem.cpp
@@ -381,13 +381,13 @@
   return const_cast<FileSystem &>(fs).Open(path, flags, mode);
 }
 
-static int GetOpenFlags(uint32_t options) {
-  const bool read = options & File::eOpenOptionRead;
-  const bool write = options & File::eOpenOptionWrite;
-
+static int GetOpenFlags(File::OpenOptions options) {
   int open_flags = 0;
-  if (write) {
-    if (read)
+  File::OpenOptions rw =
+      options & (File::eOpenOptionReadOnly | File::eOpenOptionWriteOnly |
+                 File::eOpenOptionReadWrite);
+  if (rw == File::eOpenOptionWriteOnly || rw == File::eOpenOptionReadWrite) {
+    if (rw == File::eOpenOptionReadWrite)
       open_flags |= O_RDWR;
     else
       open_flags |= O_WRONLY;
@@ -403,7 +403,7 @@
 
     if (options & File::eOpenOptionCanCreateNewOnly)
       open_flags |= O_CREAT | O_EXCL;
-  } else if (read) {
+  } else if (rw == File::eOpenOptionReadOnly) {
     open_flags |= O_RDONLY;
 
 #ifndef _WIN32
Index: lldb/source/Host/common/File.cpp
===================================================================
--- lldb/source/Host/common/File.cpp
+++ lldb/source/Host/common/File.cpp
@@ -41,20 +41,23 @@
 
 Expected<const char *>
 File::GetStreamOpenModeFromOptions(File::OpenOptions options) {
+  File::OpenOptions rw =
+      options & (File::eOpenOptionReadOnly | File::eOpenOptionWriteOnly |
+                 File::eOpenOptionReadWrite);
+
   if (options & File::eOpenOptionAppend) {
-    if (options & File::eOpenOptionRead) {
+    if (rw == File::eOpenOptionReadWrite) {
       if (options & File::eOpenOptionCanCreateNewOnly)
         return "a+x";
       else
         return "a+";
-    } else if (options & File::eOpenOptionWrite) {
+    } else if (rw == File::eOpenOptionWriteOnly) {
       if (options & File::eOpenOptionCanCreateNewOnly)
         return "ax";
       else
         return "a";
     }
-  } else if (options & File::eOpenOptionRead &&
-             options & File::eOpenOptionWrite) {
+  } else if (rw == File::eOpenOptionReadWrite) {
     if (options & File::eOpenOptionCanCreate) {
       if (options & File::eOpenOptionCanCreateNewOnly)
         return "w+x";
@@ -62,10 +65,10 @@
         return "w+";
     } else
       return "r+";
-  } else if (options & File::eOpenOptionRead) {
-    return "r";
-  } else if (options & File::eOpenOptionWrite) {
+  } else if (rw == File::eOpenOptionWriteOnly) {
     return "w";
+  } else if (rw == File::eOpenOptionReadOnly) {
+    return "r";
   }
   return llvm::createStringError(
       llvm::inconvertibleErrorCode(),
@@ -75,16 +78,17 @@
 Expected<File::OpenOptions> File::GetOptionsFromMode(llvm::StringRef mode) {
   OpenOptions opts =
       llvm::StringSwitch<OpenOptions>(mode)
-          .Cases("r", "rb", eOpenOptionRead)
-          .Cases("w", "wb", eOpenOptionWrite)
+          .Cases("r", "rb", eOpenOptionReadOnly)
+          .Cases("w", "wb", eOpenOptionWriteOnly)
           .Cases("a", "ab",
-                 eOpenOptionWrite | eOpenOptionAppend | eOpenOptionCanCreate)
-          .Cases("r+", "rb+", "r+b", eOpenOptionRead | eOpenOptionWrite)
+                 eOpenOptionWriteOnly | eOpenOptionAppend |
+                 eOpenOptionCanCreate)
+          .Cases("r+", "rb+", "r+b", eOpenOptionReadWrite)
           .Cases("w+", "wb+", "w+b",
-                 eOpenOptionRead | eOpenOptionWrite | eOpenOptionCanCreate |
-                     eOpenOptionTruncate)
+                 eOpenOptionReadWrite | eOpenOptionCanCreate |
+                 eOpenOptionTruncate)
           .Cases("a+", "ab+", "a+b",
-                 eOpenOptionRead | eOpenOptionWrite | eOpenOptionAppend |
+                 eOpenOptionReadWrite | eOpenOptionAppend |
                      eOpenOptionCanCreate)
           .Default(OpenOptions());
   if (opts)
@@ -310,9 +314,15 @@
     if (m_own_stream) {
       if (::fclose(m_stream) == EOF)
         error.SetErrorToErrno();
-    } else if (m_options & eOpenOptionWrite) {
-      if (::fflush(m_stream) == EOF)
-        error.SetErrorToErrno();
+    } else {
+      File::OpenOptions rw =
+          m_options & (File::eOpenOptionReadOnly | File::eOpenOptionWriteOnly |
+                       File::eOpenOptionReadWrite);
+
+      if (rw == eOpenOptionWriteOnly || rw == eOpenOptionReadWrite) {
+        if (::fflush(m_stream) == EOF)
+          error.SetErrorToErrno();
+      }
     }
   }
   if (DescriptorIsValid() && m_own_descriptor) {
@@ -732,10 +742,15 @@
 
 mode_t File::ConvertOpenOptionsForPOSIXOpen(OpenOptions open_options) {
   mode_t mode = 0;
-  if (open_options & eOpenOptionRead && open_options & eOpenOptionWrite)
+  File::OpenOptions rw =
+      open_options & (File::eOpenOptionReadOnly | File::eOpenOptionWriteOnly |
+                      File::eOpenOptionReadWrite);
+  if (rw == eOpenOptionReadWrite)
     mode |= O_RDWR;
-  else if (open_options & eOpenOptionWrite)
+  else if (rw == eOpenOptionWriteOnly)
     mode |= O_WRONLY;
+  else if (rw == eOpenOptionReadOnly)
+    mode |= O_RDONLY;
 
   if (open_options & eOpenOptionAppend)
     mode |= O_APPEND;
Index: lldb/source/Expression/REPL.cpp
===================================================================
--- lldb/source/Expression/REPL.cpp
+++ lldb/source/Expression/REPL.cpp
@@ -445,7 +445,7 @@
           if (!m_repl_source_path.empty()) {
             auto file = FileSystem::Instance().Open(
                 FileSpec(m_repl_source_path),
-                File::eOpenOptionWrite | File::eOpenOptionTruncate |
+                File::eOpenOptionWriteOnly | File::eOpenOptionTruncate |
                     File::eOpenOptionCanCreate,
                 lldb::eFilePermissionsFileDefault);
             if (file) {
Index: lldb/source/Core/StreamFile.cpp
===================================================================
--- lldb/source/Core/StreamFile.cpp
+++ lldb/source/Core/StreamFile.cpp
@@ -21,8 +21,8 @@
 }
 
 StreamFile::StreamFile(int fd, bool transfer_ownership) : Stream() {
-  m_file_sp =
-      std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, transfer_ownership);
+  m_file_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWriteOnly,
+                                           transfer_ownership);
 }
 
 StreamFile::StreamFile(FILE *fh, bool transfer_ownership) : Stream() {
Index: lldb/source/Core/Debugger.cpp
===================================================================
--- lldb/source/Core/Debugger.cpp
+++ lldb/source/Core/Debugger.cpp
@@ -1243,7 +1243,7 @@
       log_stream_sp = pos->second.lock();
     if (!log_stream_sp) {
       File::OpenOptions flags =
-          File::eOpenOptionWrite | File::eOpenOptionCanCreate;
+          File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate;
       if (log_options & LLDB_LOG_OPTION_APPEND)
         flags |= File::eOpenOptionAppend;
       else
Index: lldb/source/Commands/CommandObjectTarget.cpp
===================================================================
--- lldb/source/Commands/CommandObjectTarget.cpp
+++ lldb/source/Commands/CommandObjectTarget.cpp
@@ -272,7 +272,7 @@
 
     if (core_file) {
       auto file = FileSystem::Instance().Open(
-          core_file, lldb_private::File::eOpenOptionRead);
+          core_file, lldb_private::File::eOpenOptionReadOnly);
 
       if (!file) {
         result.AppendErrorWithFormatv("Cannot open '{0}': {1}.",
@@ -286,7 +286,7 @@
       FileSpec symfile(m_symbol_file.GetOptionValue().GetCurrentValue());
       if (symfile) {
         auto file = FileSystem::Instance().Open(
-            symfile, lldb_private::File::eOpenOptionRead);
+            symfile, lldb_private::File::eOpenOptionReadOnly);
 
         if (!file) {
           result.AppendErrorWithFormatv("Cannot open '{0}': {1}.",
Index: lldb/source/Commands/CommandObjectSettings.cpp
===================================================================
--- lldb/source/Commands/CommandObjectSettings.cpp
+++ lldb/source/Commands/CommandObjectSettings.cpp
@@ -369,7 +369,7 @@
     FileSpec file_spec(m_options.m_filename);
     FileSystem::Instance().Resolve(file_spec);
     std::string path(file_spec.GetPath());
-    auto options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
+    auto options = File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate;
     if (m_options.m_append)
       options |= File::eOpenOptionAppend;
     else
Index: lldb/source/Commands/CommandObjectPlatform.cpp
===================================================================
--- lldb/source/Commands/CommandObjectPlatform.cpp
+++ lldb/source/Commands/CommandObjectPlatform.cpp
@@ -498,8 +498,8 @@
                 lldb::eFilePermissionsWorldRead;
       lldb::user_id_t fd = platform_sp->OpenFile(
           FileSpec(cmd_line),
-          File::eOpenOptionRead | File::eOpenOptionWrite |
-              File::eOpenOptionAppend | File::eOpenOptionCanCreate,
+          File::eOpenOptionReadWrite | File::eOpenOptionAppend |
+               File::eOpenOptionCanCreate,
           perms, error);
       if (error.Success()) {
         result.AppendMessageWithFormat("File Descriptor = %" PRIu64 "\n", fd);
Index: lldb/source/Commands/CommandObjectMemory.cpp
===================================================================
--- lldb/source/Commands/CommandObjectMemory.cpp
+++ lldb/source/Commands/CommandObjectMemory.cpp
@@ -754,7 +754,7 @@
     if (outfile_spec) {
 
       File::OpenOptions open_options =
-          File::eOpenOptionWrite | File::eOpenOptionCanCreate;
+          File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate;
       const bool append = m_outfile_options.GetAppend().GetCurrentValue();
       open_options |=
           append ? File::eOpenOptionAppend : File::eOpenOptionTruncate;
Index: lldb/source/API/SBStream.cpp
===================================================================
--- lldb/source/API/SBStream.cpp
+++ lldb/source/API/SBStream.cpp
@@ -90,7 +90,7 @@
       local_data = std::string(
           static_cast<StreamString *>(m_opaque_up.get())->GetString());
   }
-  auto open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
+  auto open_options = File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate;
   if (append)
     open_options |= File::eOpenOptionAppend;
   else
Index: lldb/include/lldb/Host/File.h
===================================================================
--- lldb/include/lldb/Host/File.h
+++ lldb/include/lldb/Host/File.h
@@ -44,8 +44,11 @@
   // * https://sourceware.org/gdb/onlinedocs/gdb/Open-Flags.html#Open-Flags
   // * rdar://problem/46788934
   enum OpenOptions : uint32_t {
-    eOpenOptionRead = (1u << 0),  // Open file for reading
-    eOpenOptionWrite = (1u << 1), // Open file for writing
+    eOpenOptionReadOnly = (1u << 0),  // Open file for reading (only)
+    eOpenOptionWriteOnly = (1u << 1), // Open file for writing (only)
+    eOpenOptionReadWrite =
+        eOpenOptionReadOnly |
+        eOpenOptionWriteOnly, // Open file for both reading and writing
     eOpenOptionAppend =
         (1u << 2), // Don't truncate file when opening, append to end of file
     eOpenOptionTruncate = (1u << 3),    // Truncate file when opening
@@ -303,8 +306,8 @@
   /// Some options like eOpenOptionDontFollowSymlinks only make
   /// sense when a file is being opened (or not at all)
   /// and may not be preserved for this method.  But any valid
-  /// File should return either or both of eOpenOptionRead and
-  /// eOpenOptionWrite here.
+  /// File should return either eOpenOptionReadOnly, eOpenOptionWriteOnly
+  /// or eOpenOptionReadWrite here.
   ///
   /// \return
   ///    OpenOptions flags for this file, or an error.
Index: lldb/docs/lldb-platform-packets.txt
===================================================================
--- lldb/docs/lldb-platform-packets.txt
+++ lldb/docs/lldb-platform-packets.txt
@@ -375,7 +375,7 @@
 //  COMPATIBILITY
 //    The gdb-remote serial protocol documentatio defines a vFile:open:
 //    packet which uses incompatible flag values, e.g. 1 means O_WRONLY
-//    in gdb's vFile:open:, but it means eOpenOptionRead to lldb's
+//    in gdb's vFile:open:, but it means eOpenOptionReadOnly to lldb's
 //    implementation.
 
 //----------------------------------------------------------------------
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to