Change cfx_fileaccess* code to only provide read-only access.

Since PDFium never writes directly to files, remove all the code that
assumes it can.

Change-Id: I3406f5ef4c1676c682a0cf152a7ba25f7cefec92
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/90711
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/cfx_fileaccess_posix.cpp b/core/fxcrt/cfx_fileaccess_posix.cpp
index f2264d3..29c2054 100644
--- a/core/fxcrt/cfx_fileaccess_posix.cpp
+++ b/core/fxcrt/cfx_fileaccess_posix.cpp
@@ -23,24 +23,6 @@
 #define O_LARGEFILE 0
 #endif  // O_LARGEFILE
 
-namespace {
-
-void GetFileMode(uint32_t dwModes, int32_t& nFlags, int32_t& nMasks) {
-  nFlags = O_BINARY | O_LARGEFILE;
-  if (dwModes & FX_FILEMODE_ReadOnly) {
-    nFlags |= O_RDONLY;
-    nMasks = 0;
-  } else {
-    nFlags |= O_RDWR | O_CREAT;
-    if (dwModes & FX_FILEMODE_Truncate) {
-      nFlags |= O_TRUNC;
-    }
-    nMasks = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
-  }
-}
-
-}  // namespace
-
 // static
 std::unique_ptr<FileAccessIface> FileAccessIface::Create() {
   return std::make_unique<CFX_FileAccess_Posix>();
@@ -52,16 +34,13 @@
   Close();
 }
 
-bool CFX_FileAccess_Posix::Open(ByteStringView fileName, uint32_t dwMode) {
+bool CFX_FileAccess_Posix::Open(ByteStringView fileName) {
   if (m_nFD > -1)
     return false;
 
-  int32_t nFlags;
-  int32_t nMasks;
-  GetFileMode(dwMode, nFlags, nMasks);
-
   // TODO(tsepez): check usage of c_str() below.
-  m_nFD = open(fileName.unterminated_c_str(), nFlags, nMasks);
+  m_nFD =
+      open(fileName.unterminated_c_str(), O_BINARY | O_LARGEFILE | O_RDONLY);
   return m_nFD > -1;
 }
 
diff --git a/core/fxcrt/cfx_fileaccess_posix.h b/core/fxcrt/cfx_fileaccess_posix.h
index 1c31011..170d0f1 100644
--- a/core/fxcrt/cfx_fileaccess_posix.h
+++ b/core/fxcrt/cfx_fileaccess_posix.h
@@ -24,7 +24,7 @@
   ~CFX_FileAccess_Posix() override;
 
   // FileAccessIface:
-  bool Open(ByteStringView fileName, uint32_t dwMode) override;
+  bool Open(ByteStringView fileName) override;
   void Close() override;
   FX_FILESIZE GetSize() const override;
   FX_FILESIZE GetPosition() const override;
@@ -39,7 +39,7 @@
   bool Truncate(FX_FILESIZE szFile) override;
 
  private:
-  int32_t m_nFD;
+  int32_t m_nFD = -1;
 };
 
 #endif  // CORE_FXCRT_CFX_FILEACCESS_POSIX_H_
diff --git a/core/fxcrt/cfx_fileaccess_windows.cpp b/core/fxcrt/cfx_fileaccess_windows.cpp
index 371aae3..2c8e9e2 100644
--- a/core/fxcrt/cfx_fileaccess_windows.cpp
+++ b/core/fxcrt/cfx_fileaccess_windows.cpp
@@ -11,24 +11,6 @@
 #include "core/fxcrt/fx_stream.h"
 #include "core/fxcrt/fx_string.h"
 
-namespace {
-
-void GetFileMode(uint32_t dwMode,
-                 uint32_t& dwAccess,
-                 uint32_t& dwShare,
-                 uint32_t& dwCreation) {
-  dwAccess = GENERIC_READ;
-  dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE;
-  if (!(dwMode & FX_FILEMODE_ReadOnly)) {
-    dwAccess |= GENERIC_WRITE;
-    dwCreation = (dwMode & FX_FILEMODE_Truncate) ? CREATE_ALWAYS : OPEN_ALWAYS;
-  } else {
-    dwCreation = OPEN_EXISTING;
-  }
-}
-
-}  // namespace
-
 // static
 std::unique_ptr<FileAccessIface> FileAccessIface::Create() {
   return std::make_unique<CFX_FileAccess_Windows>();
@@ -40,18 +22,14 @@
   Close();
 }
 
-bool CFX_FileAccess_Windows::Open(ByteStringView fileName, uint32_t dwMode) {
+bool CFX_FileAccess_Windows::Open(ByteStringView fileName) {
   if (m_hFile)
     return false;
 
-  uint32_t dwAccess;
-  uint32_t dwShare;
-  uint32_t dwCreation;
-  GetFileMode(dwMode, dwAccess, dwShare, dwCreation);
-
   WideString wname = FX_UTF8Decode(fileName);
-  m_hFile = ::CreateFileW(wname.c_str(), dwAccess, dwShare, nullptr, dwCreation,
-                          FILE_ATTRIBUTE_NORMAL, nullptr);
+  m_hFile = ::CreateFileW(wname.c_str(), GENERIC_READ,
+                          FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
+                          OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
   if (m_hFile == INVALID_HANDLE_VALUE)
     m_hFile = nullptr;
 
diff --git a/core/fxcrt/cfx_fileaccess_windows.h b/core/fxcrt/cfx_fileaccess_windows.h
index 56c580d..135841f 100644
--- a/core/fxcrt/cfx_fileaccess_windows.h
+++ b/core/fxcrt/cfx_fileaccess_windows.h
@@ -24,7 +24,7 @@
   ~CFX_FileAccess_Windows() override;
 
   // FileAccessIface
-  bool Open(ByteStringView fileName, uint32_t dwMode) override;
+  bool Open(ByteStringView fileName) override;
   void Close() override;
   FX_FILESIZE GetSize() const override;
   FX_FILESIZE GetPosition() const override;
diff --git a/core/fxcrt/fileaccess_iface.h b/core/fxcrt/fileaccess_iface.h
index ae34be5..65ddb99 100644
--- a/core/fxcrt/fileaccess_iface.h
+++ b/core/fxcrt/fileaccess_iface.h
@@ -16,8 +16,8 @@
   static std::unique_ptr<FileAccessIface> Create();
   virtual ~FileAccessIface() = default;
 
-  // `fileName` is UTF-8 on all platforms.
-  virtual bool Open(ByteStringView fileName, uint32_t dwMode) = 0;
+  // Opens in read-only mode. `fileName` is UTF-8 on all platforms.
+  virtual bool Open(ByteStringView fileName) = 0;
   virtual void Close() = 0;
   virtual FX_FILESIZE GetSize() const = 0;
   virtual FX_FILESIZE GetPosition() const = 0;
diff --git a/core/fxcrt/fx_stream.cpp b/core/fxcrt/fx_stream.cpp
index 3039744..e7c42ce 100644
--- a/core/fxcrt/fx_stream.cpp
+++ b/core/fxcrt/fx_stream.cpp
@@ -67,19 +67,12 @@
 }
 
 // static
-RetainPtr<IFX_SeekableStream> IFX_SeekableStream::CreateFromFilename(
-    const char* filename,
-    uint32_t dwModes) {
-  std::unique_ptr<FileAccessIface> pFA = FileAccessIface::Create();
-  if (!pFA->Open(filename, dwModes))
-    return nullptr;
-  return pdfium::MakeRetain<CFX_CRTFileStream>(std::move(pFA));
-}
-
-// static
 RetainPtr<IFX_SeekableReadStream> IFX_SeekableReadStream::CreateFromFilename(
     const char* filename) {
-  return IFX_SeekableStream::CreateFromFilename(filename, FX_FILEMODE_ReadOnly);
+  std::unique_ptr<FileAccessIface> pFA = FileAccessIface::Create();
+  if (!pFA->Open(filename))
+    return nullptr;
+  return pdfium::MakeRetain<CFX_CRTFileStream>(std::move(pFA));
 }
 
 bool IFX_SeekableWriteStream::WriteBlock(const void* pData, size_t size) {
diff --git a/core/fxcrt/fx_stream.h b/core/fxcrt/fx_stream.h
index 7658e0b..46ebb60 100644
--- a/core/fxcrt/fx_stream.h
+++ b/core/fxcrt/fx_stream.h
@@ -14,9 +14,6 @@
 #include "core/fxcrt/fx_types.h"
 #include "core/fxcrt/retain_ptr.h"
 
-constexpr uint32_t FX_FILEMODE_ReadOnly = 1 << 0;
-constexpr uint32_t FX_FILEMODE_Truncate = 1 << 1;
-
 class IFX_WriteStream {
  public:
   virtual bool WriteBlock(const void* pData, size_t size) = 0;
@@ -73,10 +70,6 @@
 class IFX_SeekableStream : public IFX_SeekableReadStream,
                            public IFX_SeekableWriteStream {
  public:
-  // dwModes is a mask of FX_FILEMODE_* from above.
-  static RetainPtr<IFX_SeekableStream> CreateFromFilename(const char* filename,
-                                                          uint32_t dwModes);
-
   // IFX_SeekableWriteStream:
   bool WriteBlock(const void* buffer, size_t size) override;
 };