[libc] Implement POSIX truncate and ftruncate functions for Linux.
authorSiva Chandra Reddy <sivachandra@google.com>
Thu, 25 Aug 2022 20:04:50 +0000 (20:04 +0000)
committerSiva Chandra Reddy <sivachandra@google.com>
Fri, 26 Aug 2022 19:27:24 +0000 (19:27 +0000)
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D132705

12 files changed:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/spec/posix.td
libc/src/unistd/CMakeLists.txt
libc/src/unistd/ftruncate.h [new file with mode: 0644]
libc/src/unistd/linux/CMakeLists.txt
libc/src/unistd/linux/ftruncate.cpp [new file with mode: 0644]
libc/src/unistd/linux/truncate.cpp [new file with mode: 0644]
libc/src/unistd/truncate.h [new file with mode: 0644]
libc/test/src/unistd/CMakeLists.txt
libc/test/src/unistd/ftruncate_test.cpp [new file with mode: 0644]
libc/test/src/unistd/truncate_test.cpp [new file with mode: 0644]

index dcb39ba..fe56176 100644 (file)
@@ -115,6 +115,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.close
     libc.src.unistd.fchdir
     libc.src.unistd.fsync
+    libc.src.unistd.ftruncate
     libc.src.unistd.link
     libc.src.unistd.linkat
     libc.src.unistd.lseek
@@ -124,6 +125,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.rmdir
     libc.src.unistd.symlink
     libc.src.unistd.symlinkat
+    libc.src.unistd.truncate
     libc.src.unistd.unlink
     libc.src.unistd.unlinkat
     libc.src.unistd.write
index 307bdd3..4317306 100644 (file)
@@ -115,6 +115,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.close
     libc.src.unistd.fchdir
     libc.src.unistd.fsync
+    libc.src.unistd.ftruncate
     libc.src.unistd.link
     libc.src.unistd.linkat
     libc.src.unistd.lseek
@@ -124,6 +125,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.rmdir
     libc.src.unistd.symlink
     libc.src.unistd.symlinkat
+    libc.src.unistd.truncate
     libc.src.unistd.unlink
     libc.src.unistd.unlinkat
     libc.src.unistd.write
index a03b04f..4dcf09d 100644 (file)
@@ -282,6 +282,11 @@ def POSIX : StandardSpec<"POSIX"> {
           [ArgSpec<IntType>]
         >,
         FunctionSpec<
+          "ftruncate",
+          RetValSpec<IntType>,
+          [ArgSpec<ConstCharPtr>, ArgSpec<OffTType>]
+        >,
+        FunctionSpec<
           "link",
           RetValSpec<IntType>,
           [ArgSpec<ConstCharPtr>, ArgSpec<ConstCharPtr>]
@@ -327,6 +332,11 @@ def POSIX : StandardSpec<"POSIX"> {
           [ArgSpec<IntType>, ArgSpec<ConstCharPtr>, ArgSpec<IntType>, ArgSpec<ConstCharPtr>]
         >,
         FunctionSpec<
+          "truncate",
+          RetValSpec<IntType>,
+          [ArgSpec<IntType>, ArgSpec<OffTType>]
+        >,
+        FunctionSpec<
           "unlink",
           RetValSpec<IntType>,
           [ArgSpec<ConstCharPtr>]
index 849b83e..d55ed71 100644 (file)
@@ -31,6 +31,13 @@ add_entrypoint_object(
 )
 
 add_entrypoint_object(
+  ftruncate
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.ftruncate
+)
+
+add_entrypoint_object(
   link
   ALIAS
   DEPENDS
@@ -94,6 +101,13 @@ add_entrypoint_object(
 )
 
 add_entrypoint_object(
+  truncate
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.truncate
+)
+
+add_entrypoint_object(
   unlink
   ALIAS
   DEPENDS
diff --git a/libc/src/unistd/ftruncate.h b/libc/src/unistd/ftruncate.h
new file mode 100644 (file)
index 0000000..1971dc5
--- /dev/null
@@ -0,0 +1,20 @@
+//===-- Implementation header for ftruncate ---------------------*- 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_FTRUNCATE_H
+#define LLVM_LIBC_SRC_UNISTD_FTRUNCATE_H
+
+#include <unistd.h>
+
+namespace __llvm_libc {
+
+int ftruncate(int, off_t);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_UNISTD_FTRUNCATE_H
index 132fd7b..249b057 100644 (file)
@@ -51,6 +51,19 @@ add_entrypoint_object(
 )
 
 add_entrypoint_object(
+  ftruncate
+  SRCS
+    ftruncate.cpp
+  HDRS
+    ../ftruncate.h
+  DEPENDS
+    libc.include.unistd
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
   link
   SRCS
     link.cpp
@@ -175,6 +188,19 @@ add_entrypoint_object(
 )
 
 add_entrypoint_object(
+  truncate
+  SRCS
+    truncate.cpp
+  HDRS
+    ../truncate.h
+  DEPENDS
+    libc.include.unistd
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
   unlink
   SRCS
     unlink.cpp
diff --git a/libc/src/unistd/linux/ftruncate.cpp b/libc/src/unistd/linux/ftruncate.cpp
new file mode 100644 (file)
index 0000000..38de441
--- /dev/null
@@ -0,0 +1,29 @@
+//===-- Linux implementation of ftruncate ---------------------------------===//
+//
+// 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/ftruncate.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include <errno.h>
+#include <sys/syscall.h> // For syscall numbers.
+#include <unistd.h>
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, ftruncate, (int fd, off_t len)) {
+  int ret = __llvm_libc::syscall(SYS_ftruncate, fd, len);
+  if (ret < 0) {
+    errno = -ret;
+    return -1;
+  }
+  return 0;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/unistd/linux/truncate.cpp b/libc/src/unistd/linux/truncate.cpp
new file mode 100644 (file)
index 0000000..c24bfa8
--- /dev/null
@@ -0,0 +1,29 @@
+//===-- Linux implementation of truncate ----------------------------------===//
+//
+// 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/truncate.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include <errno.h>
+#include <sys/syscall.h> // For syscall numbers.
+#include <unistd.h>
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, truncate, (const char *path, off_t len)) {
+  int ret = __llvm_libc::syscall(SYS_truncate, path, len);
+  if (ret < 0) {
+    errno = -ret;
+    return -1;
+  }
+  return 0;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/unistd/truncate.h b/libc/src/unistd/truncate.h
new file mode 100644 (file)
index 0000000..a6ef921
--- /dev/null
@@ -0,0 +1,20 @@
+//===-- Implementation header for truncate ----------------------*- 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_TRUNCATE_H
+#define LLVM_LIBC_SRC_UNISTD_TRUNCATE_H
+
+#include <unistd.h>
+
+namespace __llvm_libc {
+
+int truncate(const char *, off_t);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_UNISTD_TRUNCATE_H
index 2c9369c..1ea5fcf 100644 (file)
@@ -33,6 +33,24 @@ add_libc_unittest(
 )
 
 add_libc_unittest(
+  ftruncate_test
+  SUITE
+    libc_unistd_unittests
+  SRCS
+    ftruncate_test.cpp
+  DEPENDS
+    libc.include.errno
+    libc.include.unistd
+    libc.src.fcntl.open
+    libc.src.unistd.close
+    libc.src.unistd.read
+    libc.src.unistd.ftruncate
+    libc.src.unistd.unlink
+    libc.src.unistd.write
+    libc.src.__support.CPP.string_view
+)
+
+add_libc_unittest(
   read_write_test
   SUITE
     libc_unistd_unittests
@@ -170,6 +188,24 @@ add_libc_unittest(
 )
 
 add_libc_unittest(
+  truncate_test
+  SUITE
+    libc_unistd_unittests
+  SRCS
+    truncate_test.cpp
+  DEPENDS
+    libc.include.errno
+    libc.include.unistd
+    libc.src.fcntl.open
+    libc.src.unistd.close
+    libc.src.unistd.read
+    libc.src.unistd.truncate
+    libc.src.unistd.unlink
+    libc.src.unistd.write
+    libc.src.__support.CPP.string_view
+)
+
+add_libc_unittest(
   unlink_test
   SUITE
     libc_unistd_unittests
diff --git a/libc/test/src/unistd/ftruncate_test.cpp b/libc/test/src/unistd/ftruncate_test.cpp
new file mode 100644 (file)
index 0000000..c0bb376
--- /dev/null
@@ -0,0 +1,71 @@
+//===-- Unittests for ftruncate -------------------------------------------===//
+//
+// 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/CPP/string_view.h"
+#include "src/fcntl/open.h"
+#include "src/unistd/close.h"
+#include "src/unistd/ftruncate.h"
+#include "src/unistd/read.h"
+#include "src/unistd/unlink.h"
+#include "src/unistd/write.h"
+#include "test/ErrnoSetterMatcher.h"
+#include "utils/UnitTest/Test.h"
+
+#include <errno.h>
+
+namespace cpp = __llvm_libc::cpp;
+
+TEST(LlvmLibcFtruncateTest, CreateAndTruncate) {
+  using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
+  constexpr const char TEST_FILE[] = "testdata/ftruncate.test";
+  constexpr const char WRITE_DATA[] = "hello, ftruncate";
+  constexpr size_t WRITE_SIZE = sizeof(WRITE_DATA);
+  char buf[WRITE_SIZE];
+
+  // The test strategy is as follows:
+  //   1. Create a normal file with some data in it.
+  //   2. Read it to make sure what was written is actually in the file.
+  //   3. Truncate to 1 byte.
+  //   4. Try to read more than 1 byte and fail.
+  errno = 0;
+  int fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+  ASSERT_EQ(errno, 0);
+  ASSERT_GT(fd, 0);
+  ASSERT_EQ(ssize_t(WRITE_SIZE),
+            __llvm_libc::write(fd, WRITE_DATA, WRITE_SIZE));
+  ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
+
+  fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
+  ASSERT_EQ(errno, 0);
+  ASSERT_GT(fd, 0);
+  ASSERT_EQ(ssize_t(WRITE_SIZE), __llvm_libc::read(fd, buf, WRITE_SIZE));
+  ASSERT_EQ(cpp::string_view(buf), cpp::string_view(WRITE_DATA));
+  ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
+
+  // For ftruncate operation to succeed, the file should be opened for
+  // writing.
+  fd = __llvm_libc::open(TEST_FILE, O_WRONLY);
+  ASSERT_GT(fd, 0);
+  ASSERT_EQ(errno, 0);
+  ASSERT_THAT(__llvm_libc::ftruncate(fd, off_t(1)), Succeeds(0));
+  ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
+
+  fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
+  ASSERT_EQ(errno, 0);
+  ASSERT_GT(fd, 0);
+  ASSERT_EQ(ssize_t(1), __llvm_libc::read(fd, buf, WRITE_SIZE));
+  ASSERT_EQ(buf[0], WRITE_DATA[0]);
+  ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
+
+  ASSERT_THAT(__llvm_libc::unlink(TEST_FILE), Succeeds(0));
+}
+
+TEST(LlvmLibcFtruncateTest, TruncateBadFD) {
+  using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
+  ASSERT_THAT(__llvm_libc::ftruncate(1, off_t(1)), Fails(EINVAL));
+}
diff --git a/libc/test/src/unistd/truncate_test.cpp b/libc/test/src/unistd/truncate_test.cpp
new file mode 100644 (file)
index 0000000..ae7765e
--- /dev/null
@@ -0,0 +1,67 @@
+//===-- Unittests for truncate --------------------------------------------===//
+//
+// 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/CPP/string_view.h"
+#include "src/fcntl/open.h"
+#include "src/unistd/close.h"
+#include "src/unistd/read.h"
+#include "src/unistd/truncate.h"
+#include "src/unistd/unlink.h"
+#include "src/unistd/write.h"
+#include "test/ErrnoSetterMatcher.h"
+#include "utils/UnitTest/Test.h"
+
+#include <errno.h>
+
+namespace cpp = __llvm_libc::cpp;
+
+TEST(LlvmLibcTruncateTest, CreateAndTruncate) {
+  using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
+  constexpr const char TEST_FILE[] = "testdata/truncate.test";
+  constexpr const char WRITE_DATA[] = "hello, truncate";
+  constexpr size_t WRITE_SIZE = sizeof(WRITE_DATA);
+  char buf[WRITE_SIZE];
+
+  // The test strategy is as follows:
+  //   1. Create a normal file with some data in it.
+  //   2. Read it to make sure what was written is actually in the file.
+  //   3. Truncate to 1 byte.
+  //   4. Try to read more than 1 byte and fail.
+  errno = 0;
+  int fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+  ASSERT_EQ(errno, 0);
+  ASSERT_GT(fd, 0);
+  ASSERT_EQ(ssize_t(WRITE_SIZE),
+            __llvm_libc::write(fd, WRITE_DATA, WRITE_SIZE));
+  ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
+
+  fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
+  ASSERT_EQ(errno, 0);
+  ASSERT_GT(fd, 0);
+  ASSERT_EQ(ssize_t(WRITE_SIZE), __llvm_libc::read(fd, buf, WRITE_SIZE));
+  ASSERT_EQ(cpp::string_view(buf), cpp::string_view(WRITE_DATA));
+  ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
+
+  ASSERT_THAT(__llvm_libc::truncate(TEST_FILE, off_t(1)), Succeeds(0));
+
+  fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
+  ASSERT_EQ(errno, 0);
+  ASSERT_GT(fd, 0);
+  ASSERT_EQ(ssize_t(1), __llvm_libc::read(fd, buf, WRITE_SIZE));
+  ASSERT_EQ(buf[0], WRITE_DATA[0]);
+  ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
+
+  ASSERT_THAT(__llvm_libc::unlink(TEST_FILE), Succeeds(0));
+}
+
+TEST(LlvmLibcTruncateTest, TruncateNonExistentFile) {
+  using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
+  ASSERT_THAT(
+      __llvm_libc::truncate("non-existent-dir/non-existent-file", off_t(1)),
+      Fails(ENOENT));
+}