Skip to content

[libc] implement pathconf/fpathconf #87165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f428a36
intiial attempt
changkhothuychung Mar 30, 2024
480690f
remove fpathconf
changkhothuychung Mar 30, 2024
c585630
add fpathconf
changkhothuychung Mar 30, 2024
b9436a3
fstatvfs
changkhothuychung Mar 30, 2024
a9d69b4
Merge branch 'main' into pathconf-impl
changkhothuychung May 4, 2024
dc6b994
Merge branch 'main' into pathconf-impl
changkhothuychung May 4, 2024
001e9a2
some fix
changkhothuychung May 5, 2024
ff19692
some fix and add test
changkhothuychung May 11, 2024
1b83f13
some fix
changkhothuychung May 13, 2024
4a6c986
refactor
changkhothuychung May 14, 2024
445bef6
some fix
changkhothuychung May 15, 2024
fa71f67
some fix
changkhothuychung May 15, 2024
751e22c
some changes
changkhothuychung May 17, 2024
2d66e87
some fix
changkhothuychung May 22, 2024
94349c2
move utils to linux
changkhothuychung Jun 11, 2024
f57f003
move header to linux
changkhothuychung Jun 14, 2024
65ff5e6
aplly new patch
changkhothuychung Jun 15, 2024
5f96eb8
Merge branch 'main' into pathconf-impl
SchrodingerZhu Jun 16, 2024
6af318b
fix build issue
SchrodingerZhu Jun 16, 2024
e7c690b
Merge pull request #1 from SchrodingerZhu/zyf-patch
changkhothuychung Jun 16, 2024
0ab4d2c
Merge branch 'main' into pathconf-impl
changkhothuychung Jun 16, 2024
04ab180
fix header files
changkhothuychung Jun 18, 2024
ef770b2
fix dependency
changkhothuychung Jun 19, 2024
7a28909
some fixes
changkhothuychung Jun 23, 2024
722bb11
some fix
changkhothuychung Jul 5, 2024
1e092d2
Merge branch 'main' into pathconf-impl
SchrodingerZhu Jul 6, 2024
2f5614b
apply patch
changkhothuychung Jul 7, 2024
3fb2a75
Merge branch 'pathconf-impl' of https://github.com/changkhothuychung/…
changkhothuychung Jul 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,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
Expand All @@ -294,6 +295,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
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,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
Expand All @@ -299,6 +300,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
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,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
Expand All @@ -312,6 +313,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.link
libc.src.unistd.linkat
libc.src.unistd.lseek
libc.src.unistd.pathconf
libc.src.unistd.pipe
libc.src.unistd.pread
libc.src.unistd.pwrite
Expand Down
14 changes: 14 additions & 0 deletions libc/src/unistd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.fork
)

add_entrypoint_object(
fpathconf
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.fpathconf
)

add_entrypoint_object(
execv
ALIAS
Expand Down Expand Up @@ -160,6 +167,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.lseek
)

add_entrypoint_object(
pathconf
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.pathconf
)

add_entrypoint_object(
pipe
ALIAS
Expand Down
18 changes: 18 additions & 0 deletions libc/src/unistd/fpathconf.h
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions libc/src/unistd/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
pipe
SRCS
Expand Down
118 changes: 118 additions & 0 deletions libc/src/unistd/linux/fpathconf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//===-- 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/__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 filesizebits(const struct statfs &s) {
switch (s.f_type) {
case JFFS2_SUPER_MAGIC:
case MSDOS_SUPER_MAGIC:
case NCP_SUPER_MAGIC:
return 32;
}
return 64;
}

static long link_max(const struct statfs &s) {
switch (s.f_type) {
case EXT2_SUPER_MAGIC:
return 32000;
case MINIX_SUPER_MAGIC:
return 250;
case MINIX2_SUPER_MAGIC:
return 65530;
case REISERFS_SUPER_MAGIC:
return 0xffff - 1000;
case UFS_MAGIC:
return 32000;
}
return LINK_MAX;
}

static long _2_symlinks(const struct statfs &s) {
switch (s.f_type) {
case ADFS_SUPER_MAGIC:
case BFS_MAGIC:
case CRAMFS_MAGIC:
case EFS_SUPER_MAGIC:
case MSDOS_SUPER_MAGIC:
case QNX4_SUPER_MAGIC:
return 0;
}
return 1;
}

static long fpathconfig(const struct fstatfs &s, int name) {
switch (name) {
case _PC_LINK_MAX:
return link_max(s);

case _PC_FILESIZEBITS:
return filesizebits(s);

case _PC_2_SYMLINKS:
return _2_symlinks(s);

case _PC_REC_MIN_XFER_SIZE:
return s.f_bsize;

case _PC_ALLOC_SIZE_MIN:
case _PC_REC_XFER_ALIGN:
return s.f_frsize;

case _PC_MAX_CANON:
return _POSIX_MAX_CANON;

case _PC_MAX_INPUT:
return _POSIX_MAX_INPUT;

case _PC_NAME_MAX:
return s.f_namemax;

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;

case _PC_ASYNC_IO:
case _PC_PRIO_IO:
case _PC_REC_INCR_XFER_SIZE:
case _PC_REC_MAX_XFER_SIZE:
case _PC_SYMLINK_MAX:
case _PC_SYNC_IO:
default:
return -1;
}
}

LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
struct fstatfs sb;
cpp::optional<LinuxStatFs> result = linux_fstatfs(fd);
if (!result.has_value()) {
return -1;
}
return fpathconfig(sb, name);
}

} // namespace LIBC_NAMESPACE
121 changes: 121 additions & 0 deletions libc/src/unistd/linux/pathconf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//===-- 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/fstatvfs.h>
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE {

static long filesizebits(const struct statfs &s) {
switch (s.f_type) {
case JFFS2_SUPER_MAGIC:
case MSDOS_SUPER_MAGIC:
case NCP_SUPER_MAGIC:
return 32;
}
return 64;
}

static long link_max(const struct statfs &s) {
switch (s.f_type) {
case EXT2_SUPER_MAGIC:
return 32000;
case MINIX_SUPER_MAGIC:
return 250;
case MINIX2_SUPER_MAGIC:
return 65530;
case REISERFS_SUPER_MAGIC:
return 0xffff - 1000;
case UFS_MAGIC:
return 32000;
}
return LINK_MAX;
}

static long _2_symlinks(const struct statfs &s) {
switch (s.f_type) {
case ADFS_SUPER_MAGIC:
case BFS_MAGIC:
case CRAMFS_MAGIC:
case EFS_SUPER_MAGIC:
case MSDOS_SUPER_MAGIC:
case QNX4_SUPER_MAGIC:
return 0;
}
return 1;
}

static long pathconfig(const struct statfs &s, int name) {
switch (name) {
case _PC_LINK_MAX:
return link_max(s);

case _PC_FILESIZEBITS:
return filesizebits(s);

case _PC_2_SYMLINKS:
return _2_symlinks(s);

case _PC_REC_MIN_XFER_SIZE:
return s.f_bsize;

case _PC_ALLOC_SIZE_MIN:
case _PC_REC_XFER_ALIGN:
return s.f_frsize;

case _PC_MAX_CANON:
return _POSIX_MAX_CANON;

case _PC_MAX_INPUT:
return _POSIX_MAX_INPUT;

case _PC_NAME_MAX:
return s.f_namemax;

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;

case _PC_ASYNC_IO:
case _PC_PRIO_IO:
case _PC_REC_INCR_XFER_SIZE:
case _PC_REC_MAX_XFER_SIZE:
case _PC_SYMLINK_MAX:
case _PC_SYNC_IO:
default:
return -1;
}
}

LLVM_LIBC_FUNCTION(long, pathconf, (char *path, int name)) {
cpp::optional<LinuxStatFs> result = linux_statfs(const char *path);
if (!result.has_value()) {
return -1;
}
return pathconfig(result.value(), name);
}

} // namespace LIBC_NAMESPACE
20 changes: 20 additions & 0 deletions libc/src/unistd/pathconf.h
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading