Skip to content

Commit 7f3c40a

Browse files
[libc] implement pathconf/fpathconf (#87165)
1 parent d043e4c commit 7f3c40a

21 files changed

+516
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ set(TARGET_LIBC_ENTRYPOINTS
290290
libc.src.unistd.dup3
291291
libc.src.unistd.execve
292292
libc.src.unistd.fchdir
293+
libc.src.unistd.fpathconf
293294
libc.src.unistd.fsync
294295
libc.src.unistd.ftruncate
295296
libc.src.unistd.getcwd
@@ -301,6 +302,7 @@ set(TARGET_LIBC_ENTRYPOINTS
301302
libc.src.unistd.link
302303
libc.src.unistd.linkat
303304
libc.src.unistd.lseek
305+
libc.src.unistd.pathconf
304306
libc.src.unistd.pread
305307
libc.src.unistd.pwrite
306308
libc.src.unistd.read

libc/config/linux/riscv/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ set(TARGET_LIBC_ENTRYPOINTS
289289
libc.src.unistd.dup3
290290
libc.src.unistd.execve
291291
libc.src.unistd.fchdir
292+
libc.src.unistd.fpathconf
292293
libc.src.unistd.fsync
293294
libc.src.unistd.ftruncate
294295
libc.src.unistd.getcwd
@@ -300,6 +301,7 @@ set(TARGET_LIBC_ENTRYPOINTS
300301
libc.src.unistd.link
301302
libc.src.unistd.linkat
302303
libc.src.unistd.lseek
304+
libc.src.unistd.pathconf
303305
libc.src.unistd.pread
304306
libc.src.unistd.pwrite
305307
libc.src.unistd.read

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ set(TARGET_LIBC_ENTRYPOINTS
308308
libc.src.unistd.dup3
309309
libc.src.unistd.execve
310310
libc.src.unistd.fchdir
311+
libc.src.unistd.fpathconf
311312
libc.src.unistd.fsync
312313
libc.src.unistd.ftruncate
313314
libc.src.unistd.getcwd
@@ -319,6 +320,7 @@ set(TARGET_LIBC_ENTRYPOINTS
319320
libc.src.unistd.link
320321
libc.src.unistd.linkat
321322
libc.src.unistd.lseek
323+
libc.src.unistd.pathconf
322324
libc.src.unistd.pipe
323325
libc.src.unistd.pread
324326
libc.src.unistd.pwrite

libc/hdr/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ add_proxy_header_library(
7878
libc.include.llvm-libc-macros.sys_epoll_macros
7979
)
8080

81+
add_proxy_header_library(
82+
sys_stat_macros
83+
HDRS
84+
sys_stat_macros.h
85+
FULL_BUILD_DEPENDS
86+
libc.include.sys_stat
87+
libc.include.llvm-libc-macros.sys_stat_macros
88+
)
89+
90+
add_proxy_header_library(
91+
unistd_macros
92+
HDRS
93+
unistd_macros.h
94+
FULL_BUILD_DEPENDS
95+
libc.include.unistd
96+
libc.include.llvm-libc-macros.unistd_macros
97+
)
98+
8199
add_proxy_header_library(
82100
time_macros
83101
HDRS
@@ -97,4 +115,13 @@ add_proxy_header_library(
97115
libc.include.float
98116
)
99117

118+
add_proxy_header_library(
119+
limits_macros
120+
HDRS
121+
limits_macros.h
122+
FULL_BUILD_DEPENDS
123+
libc.include.limits
124+
libc.include.llvm-libc-macros.limits_macros
125+
)
126+
100127
add_subdirectory(types)

libc/hdr/limits_macros.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from limits.h --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_LIMITS_MACROS_H
10+
#define LLVM_LIBC_HDR_LIMITS_MACROS_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-macros/limits-macros.h"
15+
16+
#else // Overlay mode
17+
18+
#include <limits.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_LIMITS_MACROS_H

libc/hdr/sys_stat_macros.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from sys/stat.h ------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_SYS_STAT_MACROS_H
10+
#define LLVM_LIBC_HDR_SYS_STAT_MACROS_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-macros/sys-stat-macros.h"
15+
16+
#else // Overlay mode
17+
18+
#include <sys/stat.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_SYS_STAT_MACROS_H

libc/hdr/unistd_macros.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from unistd.h --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_UNISTD_MACROS_H
10+
#define LLVM_LIBC_HDR_UNISTD_MACROS_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-macros/unistd-macros.h"
15+
16+
#else // Overlay mode
17+
18+
#include <unistd.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_UNISTD_MACROS_H

libc/include/llvm-libc-macros/limits-macros.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,16 @@
225225
#define ULLONG_MIN 0ULL
226226
#endif // ULLONG_MIN
227227

228+
#ifndef _POSIX_MAX_CANON
229+
#define _POSIX_MAX_CANON 255
230+
#endif
231+
232+
#ifndef _POSIX_MAX_INPUT
233+
#define _POSIX_MAX_INPUT 255
234+
#endif
235+
236+
#ifndef _POSIX_NAME_MAX
237+
#define _POSIX_PATH_MAX 256
238+
#endif
239+
228240
#endif // LLVM_LIBC_MACROS_LIMITS_MACROS_H

libc/include/llvm-libc-macros/linux/unistd-macros.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,33 @@
1818
#define _SC_PAGESIZE 1
1919
#define _SC_PAGE_SIZE _SC_PAGESIZE
2020

21+
#define _PC_FILESIZEBITS 0
22+
#define _PC_LINK_MAX 1
23+
#define _PC_MAX_CANON 2
24+
#define _PC_MAX_INPUT 3
25+
#define _PC_NAME_MAX 4
26+
#define _PC_PATH_MAX 5
27+
#define _PC_PIPE_BUF 6
28+
#define _PC_2_SYMLINKS 7
29+
#define _PC_ALLOC_SIZE_MIN 8
30+
#define _PC_REC_INCR_XFER_SIZE 9
31+
#define _PC_REC_MAX_XFER_SIZE 10
32+
#define _PC_REC_MIN_XFER_SIZE 11
33+
#define _PC_REC_XFER_ALIGN 12
34+
#define _PC_SYMLINK_MAX 13
35+
#define _PC_CHOWN_RESTRICTED 14
36+
#define _PC_NO_TRUNC 15
37+
#define _PC_VDISABLE 16
38+
#define _PC_ASYNC_IO 17
39+
#define _PC_PRIO_IO 18
40+
#define _PC_SYNC_IO 19
41+
42+
// TODO: Move these limit macros to a separate file
43+
#define _POSIX_CHOWN_RESTRICTED 1
44+
#define _POSIX_PIPE_BUF 512
45+
#define _POSIX_NO_TRUNC 1
46+
#define _POSIX_VDISABLE '\0'
47+
2148
// Macro to set up the call to the __llvm_libc_syscall function
2249
// This is to prevent the call from having fewer than 6 arguments, since six
2350
// arguments are always passed to the syscall. Unnecessary arguments are

libc/src/sys/statvfs/linux/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_header_library(
88
libc.src.__support.common
99
libc.src.__support.CPP.optional
1010
libc.include.sys_syscall
11+
libc.include.llvm-libc-types.struct_statvfs
1112
)
1213

1314
add_entrypoint_object(

libc/src/unistd/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ add_entrypoint_object(
6969
.${LIBC_TARGET_OS}.fork
7070
)
7171

72+
add_entrypoint_object(
73+
fpathconf
74+
ALIAS
75+
DEPENDS
76+
.${LIBC_TARGET_OS}.fpathconf
77+
)
78+
7279
add_entrypoint_object(
7380
execv
7481
ALIAS
@@ -160,6 +167,14 @@ add_entrypoint_object(
160167
.${LIBC_TARGET_OS}.lseek
161168
)
162169

170+
add_entrypoint_object(
171+
pathconf
172+
ALIAS
173+
DEPENDS
174+
.${LIBC_TARGET_OS}.pathconf
175+
)
176+
177+
163178
add_entrypoint_object(
164179
pipe
165180
ALIAS

libc/src/unistd/fpathconf.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for fpathconf ---------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_UNISTD_FPATHCONF_H
10+
#define LLVM_LIBC_SRC_UNISTD_FPATHCONF_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
long fpathconf(int fd, int name);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_UNISTD_FSYNC_H

libc/src/unistd/linux/CMakeLists.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ add_entrypoint_object(
105105
libc.src.errno.errno
106106
)
107107

108+
add_entrypoint_object(
109+
fpathconf
110+
SRCS
111+
fpathconf.cpp
112+
HDRS
113+
../fpathconf.h
114+
DEPENDS
115+
libc.include.unistd
116+
libc.include.sys_syscall
117+
libc.src.__support.OSUtil.osutil
118+
libc.src.errno.errno
119+
libc.src.unistd.linux.pathconf_utils
120+
)
121+
108122
add_entrypoint_object(
109123
execv
110124
SRCS
@@ -273,6 +287,34 @@ add_entrypoint_object(
273287
libc.src.errno.errno
274288
)
275289

290+
add_entrypoint_object(
291+
pathconf
292+
SRCS
293+
pathconf.cpp
294+
HDRS
295+
../pathconf.h
296+
DEPENDS
297+
libc.include.unistd
298+
libc.include.sys_syscall
299+
libc.src.__support.OSUtil.osutil
300+
libc.src.errno.errno
301+
libc.src.unistd.linux.pathconf_utils
302+
)
303+
304+
add_object_library(
305+
pathconf_utils
306+
SRCS
307+
pathconf_utils.cpp
308+
HDRS
309+
pathconf_utils.h
310+
DEPENDS
311+
libc.hdr.limits_macros
312+
libc.hdr.unistd_macros
313+
libc.src.__support.OSUtil.osutil
314+
libc.src.errno.errno
315+
libc.src.sys.statvfs.linux.statfs_utils
316+
)
317+
276318
add_entrypoint_object(
277319
pipe
278320
SRCS

libc/src/unistd/linux/fpathconf.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Linux implementation of fpathconf ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/unistd/fpathconf.h"
10+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
11+
#include "src/__support/common.h"
12+
#include "src/sys/statvfs/linux/statfs_utils.h"
13+
#include "src/unistd/linux/pathconf_utils.h"
14+
15+
namespace LIBC_NAMESPACE {
16+
17+
LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
18+
if (cpp::optional<statfs_utils::LinuxStatFs> result =
19+
statfs_utils::linux_fstatfs(fd))
20+
return pathconfig(result.value(), name);
21+
return -1;
22+
}
23+
24+
} // namespace LIBC_NAMESPACE

libc/src/unistd/linux/pathconf.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Linux implementation of pathconf ----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/unistd/pathconf.h"
10+
#include "src/errno/libc_errno.h"
11+
#include "src/sys/statvfs/linux/statfs_utils.h"
12+
#include "src/unistd/linux/pathconf_utils.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(long, pathconf, (const char *path, int name)) {
17+
if (cpp::optional<statfs_utils::LinuxStatFs> result =
18+
statfs_utils::linux_statfs(path))
19+
return pathconfig(result.value(), name);
20+
return -1;
21+
}
22+
23+
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)