[libc-commits] [libc] [libc] implement pathconf/fpathconf (PR #87165)

Nhat Nguyen via libc-commits libc-commits at lists.llvm.org
Sun May 5 09:51:41 PDT 2024


https://github.com/changkhothuychung updated https://github.com/llvm/llvm-project/pull/87165

>From f428a360c32273516999042bb18416fa1a1db35a Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 30 Mar 2024 14:10:47 -0400
Subject: [PATCH 1/5] intiial attempt

---
 libc/config/linux/aarch64/entrypoints.txt |  2 +
 libc/config/linux/riscv/entrypoints.txt   |  2 +
 libc/config/linux/x86_64/entrypoints.txt  |  2 +
 libc/src/unistd/CMakeLists.txt            | 14 +++++
 libc/src/unistd/linux/CMakeLists.txt      | 26 +++++++++
 libc/src/unistd/linux/pathconf.cpp        | 68 +++++++++++++++++++++++
 libc/src/unistd/pathconf.h                | 20 +++++++
 7 files changed, 134 insertions(+)
 create mode 100644 libc/src/unistd/linux/pathconf.cpp
 create mode 100644 libc/src/unistd/pathconf.h

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 78da7f0b334b1f..be0c4bebfd95ad 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -282,6 +282,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.dup3
     libc.src.unistd.execve
     libc.src.unistd.fchdir
+    libc.src.unistd.fpathconf
     libc.src.unistd.fsync
     libc.src.unistd.ftruncate
     libc.src.unistd.getcwd
@@ -293,6 +294,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.link
     libc.src.unistd.linkat
     libc.src.unistd.lseek
+    libc.src.unistd.pathconf
     libc.src.unistd.pread
     libc.src.unistd.pwrite
     libc.src.unistd.read
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 5aae4e246cfb3c..2d8f9b1f6dd82c 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -287,6 +287,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.dup3
     libc.src.unistd.execve
     libc.src.unistd.fchdir
+    libc.src.unistd.fpathconf
     libc.src.unistd.fsync
     libc.src.unistd.ftruncate
     libc.src.unistd.getcwd
@@ -298,6 +299,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.link
     libc.src.unistd.linkat
     libc.src.unistd.lseek
+    libc.src.unistd.pathconf
     libc.src.unistd.pread
     libc.src.unistd.pwrite
     libc.src.unistd.read
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5b428e51aee620..0f00f264ea1490 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -297,6 +297,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.dup3
     libc.src.unistd.execve
     libc.src.unistd.fchdir
+    libc.src.unistd.fpathconf
     libc.src.unistd.fsync
     libc.src.unistd.ftruncate
     libc.src.unistd.getcwd
@@ -308,6 +309,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.link
     libc.src.unistd.linkat
     libc.src.unistd.lseek
+    libc.src.unistd.pathconf
     libc.src.unistd.pread
     libc.src.unistd.pwrite
     libc.src.unistd.read
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index e22b0e1872caa1..20c87f668b381b 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -58,6 +58,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.fork
 )
 
+add_entrypoint_object(
+  fpathconf
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.fpathconf
+)
+
 add_entrypoint_object(
   execv
   ALIAS
@@ -149,6 +156,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.lseek
 )
 
+add_entrypoint_object(
+  pathconf
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.pathconf
+)
+
 add_entrypoint_object(
   pread
   ALIAS
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index df85d44e9e9edc..0ee59d5391ac5e 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -105,6 +105,19 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  fpathconf
+  SRCS
+    fpathconf.cpp
+  HDRS
+    ../fpathconf.h
+  DEPENDS
+    libc.include.unistd
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   execv
   SRCS
@@ -273,6 +286,19 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  pathconf
+  SRCS
+  pathconf.cpp
+  HDRS
+    ../pathconf.h
+  DEPENDS
+    libc.include.unistd
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   pread
   SRCS
diff --git a/libc/src/unistd/linux/pathconf.cpp b/libc/src/unistd/linux/pathconf.cpp
new file mode 100644
index 00000000000000..fed94e7d423716
--- /dev/null
+++ b/libc/src/unistd/linux/pathconf.cpp
@@ -0,0 +1,68 @@
+//===-- Linux implementation of pathconf ----------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/pread.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+#include "src/__support/macros/sanitizer.h" // for MSAN_UNPOISON
+#include "src/errno/libc_errno.h"
+#include <stdint.h> // For uint64_t.
+#include <sys/statvfs.h>
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE {
+
+static long pathconfig(const struct statvfs &s, int name) {
+  switch (name) {
+  case _PC_LINK_MAX:
+    return _POSIX_LINK_MAX;
+
+  case _PC_MAX_CANON:
+    return _POSIX_MAX_CANON;
+
+  case _PC_MAX_INPUT:
+    return _POSIX_MAX_INPUT;
+
+  case _PC_NAME_MAX:
+    return _POSIX_NAME_MAX;
+
+  case _PC_PATH_MAX:
+    return _POSIX_PATH_MAX;
+
+  case _PC_PIPE_BUF:
+    return _POSIX_PIPE_BUF;
+
+  case _PC_CHOWN_RESTRICTED:
+    return _POSIX_CHOWN_RESTRICTED;
+
+  case _PC_NO_TRUNC:
+    return _POSIX_NO_TRUNC;
+
+  case _PC_VDISABLE:
+    return _POSIX_VDISABLE;
+  }
+}
+
+LLVM_LIBC_FUNCTION(long, pathconf, (char *path, int name)) {
+  struct statvfs sb;
+  if (statvfs(path, &sb) == -1) {
+    return -1;
+  }
+  return pathconfig(sb, name);
+}
+
+LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
+  struct statvfs sb;
+  if (statvfs(path, &sb) == -1) {
+    return -1;
+  }
+  return pathconfig(sb, name);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/unistd/pathconf.h b/libc/src/unistd/pathconf.h
new file mode 100644
index 00000000000000..a397cd9323bbb8
--- /dev/null
+++ b/libc/src/unistd/pathconf.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for pathconf ----------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_PREAD_H
+#define LLVM_LIBC_SRC_UNISTD_PREAD_H
+
+#include <unistd.h>
+
+namespace LIBC_NAMESPACE {
+
+long pathconf(char *path, int name);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_UNISTD_PREAD_H

>From 480690f84a39ac8dbeb06402928decf792a46f36 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 30 Mar 2024 14:22:00 -0400
Subject: [PATCH 2/5] remove fpathconf

---
 libc/src/unistd/CMakeLists.txt       |  7 -------
 libc/src/unistd/linux/CMakeLists.txt | 13 -------------
 2 files changed, 20 deletions(-)

diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 20c87f668b381b..9e81d5904447de 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -58,13 +58,6 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.fork
 )
 
-add_entrypoint_object(
-  fpathconf
-  ALIAS
-  DEPENDS
-    .${LIBC_TARGET_OS}.fpathconf
-)
-
 add_entrypoint_object(
   execv
   ALIAS
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 0ee59d5391ac5e..dfea71ed3d3dc4 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -105,19 +105,6 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
-add_entrypoint_object(
-  fpathconf
-  SRCS
-    fpathconf.cpp
-  HDRS
-    ../fpathconf.h
-  DEPENDS
-    libc.include.unistd
-    libc.include.sys_syscall
-    libc.src.__support.OSUtil.osutil
-    libc.src.errno.errno
-)
-
 add_entrypoint_object(
   execv
   SRCS

>From c585630cab9f7e1d4ed80b36a4f6b57a8af6a7cf Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 30 Mar 2024 14:31:36 -0400
Subject: [PATCH 3/5] add fpathconf

---
 libc/src/unistd/CMakeLists.txt       |  7 ++++
 libc/src/unistd/fpathconf.h          | 18 +++++++++
 libc/src/unistd/linux/CMakeLists.txt | 13 ++++++
 libc/src/unistd/linux/fpathconf.cpp  | 60 ++++++++++++++++++++++++++++
 libc/src/unistd/linux/pathconf.cpp   |  8 ----
 5 files changed, 98 insertions(+), 8 deletions(-)
 create mode 100644 libc/src/unistd/fpathconf.h
 create mode 100644 libc/src/unistd/linux/fpathconf.cpp

diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 9e81d5904447de..20c87f668b381b 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -58,6 +58,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.fork
 )
 
+add_entrypoint_object(
+  fpathconf
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.fpathconf
+)
+
 add_entrypoint_object(
   execv
   ALIAS
diff --git a/libc/src/unistd/fpathconf.h b/libc/src/unistd/fpathconf.h
new file mode 100644
index 00000000000000..d80d94ca3c2ed3
--- /dev/null
+++ b/libc/src/unistd/fpathconf.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for fpathconf ---------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_FSYNC_H
+#define LLVM_LIBC_SRC_UNISTD_FSYNC_H
+
+namespace LIBC_NAMESPACE {
+
+long fpathconf(int fd, int name);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_UNISTD_FSYNC_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index dfea71ed3d3dc4..0ee59d5391ac5e 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -105,6 +105,19 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  fpathconf
+  SRCS
+    fpathconf.cpp
+  HDRS
+    ../fpathconf.h
+  DEPENDS
+    libc.include.unistd
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   execv
   SRCS
diff --git a/libc/src/unistd/linux/fpathconf.cpp b/libc/src/unistd/linux/fpathconf.cpp
new file mode 100644
index 00000000000000..16bb0b6334e2c1
--- /dev/null
+++ b/libc/src/unistd/linux/fpathconf.cpp
@@ -0,0 +1,60 @@
+//===-- Linux implementation of fpathconf ---------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/fsync.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include "src/errno/libc_errno.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE {
+
+static long pathconfig(const struct statvfs &s, int name) {
+  switch (name) {
+  case _PC_LINK_MAX:
+    return _POSIX_LINK_MAX;
+
+  case _PC_MAX_CANON:
+    return _POSIX_MAX_CANON;
+
+  case _PC_MAX_INPUT:
+    return _POSIX_MAX_INPUT;
+
+  case _PC_NAME_MAX:
+    return _POSIX_NAME_MAX;
+
+  case _PC_PATH_MAX:
+    return _POSIX_PATH_MAX;
+
+  case _PC_PIPE_BUF:
+    return _POSIX_PIPE_BUF;
+
+  case _PC_CHOWN_RESTRICTED:
+    return _POSIX_CHOWN_RESTRICTED;
+
+  case _PC_NO_TRUNC:
+    return _POSIX_NO_TRUNC;
+
+  case _PC_VDISABLE:
+    return _POSIX_VDISABLE;
+  }
+}
+
+LLVM_LIBC_FUNCTION(int, fsync, (int fd)) {
+  LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
+    struct statvfs sb;
+    if (statvfs(path, &sb) == -1) {
+      return -1;
+    }
+    return pathconfig(sb, name);
+  }
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/unistd/linux/pathconf.cpp b/libc/src/unistd/linux/pathconf.cpp
index fed94e7d423716..b1ecc4c80c3d44 100644
--- a/libc/src/unistd/linux/pathconf.cpp
+++ b/libc/src/unistd/linux/pathconf.cpp
@@ -57,12 +57,4 @@ LLVM_LIBC_FUNCTION(long, pathconf, (char *path, int name)) {
   return pathconfig(sb, name);
 }
 
-LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
-  struct statvfs sb;
-  if (statvfs(path, &sb) == -1) {
-    return -1;
-  }
-  return pathconfig(sb, name);
-}
-
 } // namespace LIBC_NAMESPACE

>From b9436a33b4545f19f4043be4ebb71c855d51f104 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 30 Mar 2024 14:43:13 -0400
Subject: [PATCH 4/5] fstatvfs

---
 libc/src/unistd/linux/pathconf.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/src/unistd/linux/pathconf.cpp b/libc/src/unistd/linux/pathconf.cpp
index b1ecc4c80c3d44..a4ffb11a5acced 100644
--- a/libc/src/unistd/linux/pathconf.cpp
+++ b/libc/src/unistd/linux/pathconf.cpp
@@ -13,7 +13,7 @@
 #include "src/__support/macros/sanitizer.h" // for MSAN_UNPOISON
 #include "src/errno/libc_errno.h"
 #include <stdint.h> // For uint64_t.
-#include <sys/statvfs.h>
+#include <sys/fstatvfs.h>
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE {
@@ -51,7 +51,7 @@ static long pathconfig(const struct statvfs &s, int name) {
 
 LLVM_LIBC_FUNCTION(long, pathconf, (char *path, int name)) {
   struct statvfs sb;
-  if (statvfs(path, &sb) == -1) {
+  if (fstatvfs(path, &sb) == -1) {
     return -1;
   }
   return pathconfig(sb, name);

>From 001e9a25308b3f82e80c2c0027763c5177160cb8 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sun, 5 May 2024 12:51:25 -0400
Subject: [PATCH 5/5] some fix

---
 libc/src/unistd/linux/fpathconf.cpp | 14 +++++---------
 libc/src/unistd/linux/pathconf.cpp  | 16 ++++++++++------
 2 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/libc/src/unistd/linux/fpathconf.cpp b/libc/src/unistd/linux/fpathconf.cpp
index 16bb0b6334e2c1..f3909c857762c9 100644
--- a/libc/src/unistd/linux/fpathconf.cpp
+++ b/libc/src/unistd/linux/fpathconf.cpp
@@ -6,8 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/unistd/fsync.h"
-
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
@@ -47,14 +45,12 @@ static long pathconfig(const struct statvfs &s, int name) {
   }
 }
 
-LLVM_LIBC_FUNCTION(int, fsync, (int fd)) {
-  LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
-    struct statvfs sb;
-    if (statvfs(path, &sb) == -1) {
-      return -1;
-    }
-    return pathconfig(sb, name);
+LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
+  struct statvfs sb;
+  if (statvfs(path, &sb) == -1) {
+    return -1;
   }
+  return pathconfig(sb, name);
 }
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/unistd/linux/pathconf.cpp b/libc/src/unistd/linux/pathconf.cpp
index a4ffb11a5acced..a79a4dcd781f90 100644
--- a/libc/src/unistd/linux/pathconf.cpp
+++ b/libc/src/unistd/linux/pathconf.cpp
@@ -18,10 +18,10 @@
 
 namespace LIBC_NAMESPACE {
 
-static long pathconfig(const struct statvfs &s, int name) {
+static long pathconfig(const struct statfs &s, int name) {
   switch (name) {
   case _PC_LINK_MAX:
-    return _POSIX_LINK_MAX;
+    return s.f_type;
 
   case _PC_MAX_CANON:
     return _POSIX_MAX_CANON;
@@ -30,7 +30,7 @@ static long pathconfig(const struct statvfs &s, int name) {
     return _POSIX_MAX_INPUT;
 
   case _PC_NAME_MAX:
-    return _POSIX_NAME_MAX;
+    return s.f_namemax;
 
   case _PC_PATH_MAX:
     return _POSIX_PATH_MAX;
@@ -50,11 +50,15 @@ static long pathconfig(const struct statvfs &s, int name) {
 }
 
 LLVM_LIBC_FUNCTION(long, pathconf, (char *path, int name)) {
-  struct statvfs sb;
-  if (fstatvfs(path, &sb) == -1) {
+  // struct statfs sb;
+  // if (fstatfs(PathFD(path), &sb) == -1) {
+  //   return -1;
+  // }
+  cpp::optional<LinuxStatFs> result = linux_statfs(const char *path);
+  if (!result.has_value()) {
     return -1;
   }
-  return pathconfig(sb, name);
+  return pathconfig(result.value(), name);
 }
 
 } // namespace LIBC_NAMESPACE



More information about the libc-commits mailing list