[libc-commits] [libc] feat(filemode): implement class and helper functions to handle file modes for an opened file (PR #220906)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Fri Sep 11 12:53:00 PDT 2026


================
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the defualt constants of Linux file open flags.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/fcntl_macros.h" // For mode_t and other flags to the open syscall
+#include "hdr/sys_stat_macros.h" // For S_IS*, S_IF*, and S_IR* flags.
+#include "hdr/types/mode_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/fcntl.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+class LinuxFileFlags {
+public:
+  static constexpr int CREATE_AND_APPEND = O_CREAT | O_APPEND;
+  static constexpr int READ_AND_WRITE = O_RDWR;
+  static constexpr int WRITE_ONLY = O_WRONLY;
+  static constexpr int READ_ONLY = O_RDONLY;
+  static constexpr int CREATE_OR_TRUNCATE = O_CREAT | O_TRUNC;
+
+  // File created will have 0666 permissions.
+  static constexpr mode_t OPEN_MODE =
+      S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+
+  static constexpr int is_file_descriptor_opened_in_read_only(int flag) {
+    return (flag & O_ACCMODE) == O_RDONLY;
+  }
+
+  static constexpr int is_file_descriptor_opened_in_write_only(int flag) {
+    return (flag & O_ACCMODE) == O_WRONLY;
+  }
+
+  static constexpr int file_has_append_flag(int flag) {
+    return flag & O_APPEND;
+  }
----------------
michaelrj-google wrote:

these functions should be annotated `LIBC_INLINE` as well, since they're in a header.

https://github.com/llvm/llvm-project/pull/220906


More information about the libc-commits mailing list