From 5ff5945f865842051719d6c4b46311a86353eec3 Mon Sep 17 00:00:00 2001
From: Hal Finkel <hfinkel@anl.gov>
Date: Thu, 30 Apr 2020 21:10:20 -0500
Subject: [PATCH] Don't pass O_CREAT when not necessary

---
 GenericIO.cxx | 12 +++++++-----
 GenericIO.h   |  6 +++---
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/GenericIO.cxx b/GenericIO.cxx
index 83702c3..1912a48 100644
--- a/GenericIO.cxx
+++ b/GenericIO.cxx
@@ -84,10 +84,11 @@ GenericFileIO_MPI::~GenericFileIO_MPI() {
   (void) MPI_File_close(&FH);
 }
 
-void GenericFileIO_MPI::open(const std::string &FN, bool ForReading) {
+void GenericFileIO_MPI::open(const std::string &FN, bool ForReading, bool MustExist) {
   FileName = FN;
 
-  int amode = ForReading ? MPI_MODE_RDONLY : (MPI_MODE_WRONLY | MPI_MODE_CREATE);
+  int amode = ForReading ? MPI_MODE_RDONLY : (MPI_MODE_WRONLY |
+                                              (!MustExist ? MPI_MODE_CREATE : 0));
   if (MPI_File_open(Comm, const_cast<char *>(FileName.c_str()), amode,
                     MPI_INFO_NULL, &FH) != MPI_SUCCESS)
     throw runtime_error((!ForReading ? "Unable to create the file: " :
@@ -183,10 +184,11 @@ GenericFileIO_POSIX::~GenericFileIO_POSIX() {
   if (FH != -1) close(FH);
 }
 
-void GenericFileIO_POSIX::open(const std::string &FN, bool ForReading) {
+void GenericFileIO_POSIX::open(const std::string &FN, bool ForReading, bool MustExist) {
   FileName = FN;
 
-  int flags = ForReading ? O_RDONLY : (O_WRONLY | O_CREAT);
+  int flags = ForReading ? O_RDONLY : (O_WRONLY |
+                                       (!MustExist ? O_CREAT : 0));
   int mode = S_IRUSR | S_IWUSR | S_IRGRP;
   errno = 0;
   if ((FH = ::open(FileName.c_str(), flags, mode)) == -1)
@@ -784,7 +786,7 @@ nocomp:
   else
     FH.get() = new GenericFileIO_POSIX();
 
-  FH.get()->open(LocalFileName);
+  FH.get()->open(LocalFileName, false, true);
 
   uint64_t Offset = RHLocal.Start;
   for (size_t i = 0; i < Vars.size(); ++i) {
diff --git a/GenericIO.h b/GenericIO.h
index ce14f59..cfb37b1 100644
--- a/GenericIO.h
+++ b/GenericIO.h
@@ -64,7 +64,7 @@ public:
   virtual ~GenericFileIO() {}
 
 public:
-  virtual void open(const std::string &FN, bool ForReading = false) = 0;
+  virtual void open(const std::string &FN, bool ForReading = false, bool MustExist = false) = 0;
   virtual void setSize(size_t sz) = 0;
   virtual void read(void *buf, size_t count, off_t offset,
                     const std::string &D) = 0;
@@ -82,7 +82,7 @@ public:
   virtual ~GenericFileIO_MPI();
 
 public:
-  virtual void open(const std::string &FN, bool ForReading = false);
+  virtual void open(const std::string &FN, bool ForReading = false, bool MustExist = false);
   virtual void setSize(size_t sz);
   virtual void read(void *buf, size_t count, off_t offset, const std::string &D);
   virtual void write(const void *buf, size_t count, off_t offset, const std::string &D);
@@ -108,7 +108,7 @@ public:
   ~GenericFileIO_POSIX();
 
 public:
-  void open(const std::string &FN, bool ForReading = false);
+  void open(const std::string &FN, bool ForReading = false, bool MustExist = false);
   void setSize(size_t sz);
   void read(void *buf, size_t count, off_t offset, const std::string &D);
   void write(const void *buf, size_t count, off_t offset, const std::string &D);
-- 
GitLab