[libc][NFC] Add a separate flag for capturing the '+' in fopen mode string.
authorSiva Chandra Reddy <sivachandra@google.com>
Thu, 17 Mar 2022 08:39:58 +0000 (08:39 +0000)
committerSiva Chandra Reddy <sivachandra@google.com>
Thu, 17 Mar 2022 15:44:04 +0000 (15:44 +0000)
Having a separate flag helps in setting up proper flags when
implementing, say the Linux specialization of File.

Along the way, a signature for a function which is to be used to open
files has been added. The implementation of the function is to be
included in platform specializations.

Reviewed By: lntue

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

libc/src/__support/File/file.cpp
libc/src/__support/File/file.h

index 6d1d3b54f25d2dd5b76f184a1e727319ddc79ad0..ed765f5c88321d4efb5d8830daac14711eeeca90 100644 (file)
@@ -215,8 +215,7 @@ File::ModeFlags File::mode_flags(const char *mode) {
       ++main_mode_count;
       break;
     case '+':
-      flags |= (static_cast<ModeFlags>(OpenMode::WRITE) |
-                static_cast<ModeFlags>(OpenMode::READ));
+      flags |= static_cast<ModeFlags>(OpenMode::PLUS);
       break;
     case 'b':
       flags |= static_cast<ModeFlags>(ContentType::BINARY);
index 6bc31fa80e0fa2ea3a263ad3cda42c73afe8fcbf..26849ad96b23e8e51313dbadbf5ce9d2147fbfd6 100644 (file)
@@ -21,6 +21,8 @@ namespace __llvm_libc {
 // suitable for their platform.
 class File {
 public:
+  static constexpr size_t DEFAULT_BUFFER_SIZE = 1024;
+
   using LockFunc = void(File *);
   using UnlockFunc = void(File *);
 
@@ -41,6 +43,7 @@ public:
     READ = 0x1,
     WRITE = 0x2,
     APPEND = 0x4,
+    PLUS = 0x8,
   };
 
   // Denotes a file opened in binary mode (which is specified by including
@@ -97,11 +100,13 @@ private:
 protected:
   bool write_allowed() const {
     return mode & (static_cast<ModeFlags>(OpenMode::WRITE) |
-                   static_cast<ModeFlags>(OpenMode::APPEND));
+                   static_cast<ModeFlags>(OpenMode::APPEND) |
+                   static_cast<ModeFlags>(OpenMode::PLUS));
   }
 
   bool read_allowed() const {
-    return mode & static_cast<ModeFlags>(OpenMode::READ);
+    return mode & (static_cast<ModeFlags>(OpenMode::READ) |
+                   static_cast<ModeFlags>(OpenMode::PLUS));
   }
 
 public:
@@ -185,6 +190,10 @@ public:
   FileLock(FileLock &&) = delete;
 };
 
+// The implementaiton of this function is provided by the platfrom_file
+// library.
+File *openfile(const char *path, const char *mode);
+
 } // namespace __llvm_libc
 
 #endif // LLVM_LIBC_SRC_SUPPORT_OSUTIL_FILE_H