127 changes: 127 additions & 0 deletions libc/src/unistd/linux/pathconf_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//===-- Linux implementation of pathconf_utils ----------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// This header must go before limits_macros.h otherwise libc header may choose
// to undefine LINK_MAX.
#include <linux/limits.h> // For LINK_MAX and other limits

#include "hdr/limits_macros.h"
#include "hdr/unistd_macros.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/errno/libc_errno.h"
#include "src/sys/statvfs/linux/statfs_utils.h"

// other linux specific includes
#include <linux/bfs_fs.h>
#if __has_include(<linux/ufs_fs.h>)
#include <linux/ufs_fs.h>
#else
// from https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/
#define UFS_MAGIC 0x00011954
#endif
#include <linux/magic.h> // For common FS magics

namespace LIBC_NAMESPACE {

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

long link_max(const statfs_utils::LinuxStatFs &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;
}

long symlinks(const statfs_utils::LinuxStatFs &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;
}

long pathconfig(const statfs_utils::LinuxStatFs &s, int name) {
switch (name) {
case _PC_LINK_MAX:
return link_max(s);

case _PC_FILESIZEBITS:
return filesizebits(s);

case _PC_2_SYMLINKS:
return 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_namelen;

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:
return -1;

default:
libc_errno = EINVAL;
return -1;
}
}

} // namespace LIBC_NAMESPACE
20 changes: 20 additions & 0 deletions libc/src/unistd/linux/pathconf_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for pathconf_utils ----------------*- 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_PATHCONF_UTILS_H
#define LLVM_LIBC_SRC_UNISTD_PATHCONF_UTILS_H

#include "src/sys/statvfs/linux/statfs_utils.h"

namespace LIBC_NAMESPACE {

long pathconfig(const statfs_utils::LinuxStatFs &s, int name);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_UNISTD_PREAD_H
18 changes: 18 additions & 0 deletions libc/src/unistd/pathconf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- 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_PATHCONF_H
#define LLVM_LIBC_SRC_UNISTD_PATHCONF_H

namespace LIBC_NAMESPACE {

long pathconf(const char *path, int name);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_UNISTD_PREAD_H
30 changes: 30 additions & 0 deletions libc/test/src/unistd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,36 @@ add_libc_unittest(
libc.src.unistd.sysconf
)

add_libc_unittest(
fpathconf_test
SUITE
libc_unistd_unittests
SRCS
fpathconf_test.cpp
DEPENDS
libc.hdr.limits_macros
libc.hdr.unistd_macros
libc.hdr.sys_stat_macros
libc.src.unistd.fpathconf
libc.src.fcntl.open
libc.src.unistd.close
)

add_libc_unittest(
pathconf_test
SUITE
libc_unistd_unittests
SRCS
pathconf_test.cpp
DEPENDS
libc.hdr.limits_macros
libc.hdr.unistd_macros
libc.hdr.sys_stat_macros
libc.src.unistd.pathconf
libc.src.fcntl.open
libc.src.unistd.close
)

add_libc_test(
getopt_test
HERMETIC_TEST_ONLY # Uses libc's own stderr
Expand Down
30 changes: 30 additions & 0 deletions libc/test/src/unistd/fpathconf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===-- Unittests for 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 "hdr/fcntl_macros.h"
#include "hdr/limits_macros.h"
#include "hdr/sys_stat_macros.h"
#include "hdr/unistd_macros.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/fpathconf.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;

TEST(LlvmLibcPipeTest, SmokeTest) {
constexpr const char *FILENAME = "fpathconf.test";
auto TEST_FILE = libc_make_test_file_path(FILENAME);
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
EXPECT_EQ(LIBC_NAMESPACE::fpathconf(fd, _PC_SYNC_IO), -1l);
EXPECT_EQ(LIBC_NAMESPACE::fpathconf(fd, _PC_PATH_MAX),
static_cast<long>(_POSIX_PATH_MAX));
LIBC_NAMESPACE::close(fd);
}

// TODO: Functionality tests
30 changes: 30 additions & 0 deletions libc/test/src/unistd/pathconf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===-- Unittests for 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 "hdr/fcntl_macros.h"
#include "hdr/limits_macros.h"
#include "hdr/sys_stat_macros.h"
#include "hdr/unistd_macros.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/pathconf.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;

TEST(LlvmLibcPipeTest, SmokeTest) {
constexpr const char *FILENAME = "pathconf.test";
auto TEST_FILE = libc_make_test_file_path(FILENAME);
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
EXPECT_EQ(LIBC_NAMESPACE::pathconf(FILENAME, _PC_SYNC_IO), -1l);
EXPECT_EQ(LIBC_NAMESPACE::pathconf(FILENAME, _PC_PATH_MAX),
static_cast<long>(_POSIX_PATH_MAX));
LIBC_NAMESPACE::close(fd);
}

// TODO: Functionality tests