[libc-commits] [libc] [libc] add statvfs/fstatvfs (PR #86169)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Fri Mar 22 09:44:28 PDT 2024


================
@@ -0,0 +1,43 @@
+#include "src/__support/CPP/string_view.h"
+#include "src/fcntl/open.h"
+#include "src/sys/statvfs/fstatvfs.h"
+#include "src/sys/statvfs/linux/statfs_utils.h"
+#include "src/unistd/close.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/LibcTest.h"
+#include <linux/magic.h>
+#include <llvm-libc-macros/linux/fcntl-macros.h>
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+
+namespace LIBC_NAMESPACE {
+static int fstatfs(int fd, struct statfs *buf) {
+  using namespace statfs_utils;
+  if (cpp::optional<LinuxStatFs> result = linux_fstatfs(fd)) {
+    *buf = *result;
+    return 0;
+  }
+  return -1;
+}
+} // namespace LIBC_NAMESPACE
+
+struct PathFD {
+  int fd;
+  explicit PathFD(LIBC_NAMESPACE::cpp::string_view path)
+      : fd(LIBC_NAMESPACE::open(path.data(), O_CLOEXEC | O_PATH)) {}
+  ~PathFD() { LIBC_NAMESPACE::close(fd); }
+  operator int() const { return fd; }
+};
+
+TEST(LlvmLibcSysStatvfsTest, FstatfsBasic) {
+  statfs buf[1];
----------------
nickdesaulniers wrote:

Please be consistent in the use of the `struct` keyword for `statfs`.  It's optional in C++ (or has weird rules around forward declarations), so I think you should omit it from the parameter list as well.

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


More information about the libc-commits mailing list