[libc-commits] [libc] [libc] Implement futimens (PR #222251)

Anirudh Mathur via libc-commits libc-commits at lists.llvm.org
Thu Sep 10 00:08:13 PDT 2026


================
@@ -0,0 +1,136 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for futimens.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/fcntl_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/fcntl/open.h"
+#include "src/stdio/remove.h"
+#include "src/sys/stat/futimens.h"
+#include "src/sys/stat/stat.h"
+#include "src/unistd/close.h"
+
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcFutimensTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+// SUCCESS: Takes an open file descriptor and successfully updates
+// its last access and modified times.
+TEST_F(LlvmLibcFutimensTest, ChangeTimesSpecific) {
+  using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+
+  constexpr const char *FILE_PATH = "futimens_pass.test";
+  auto TEST_FILE = libc_make_test_file_path(FILE_PATH);
+  int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(fd, 0);
+
+  // make a dummy timespec struct
+  struct timespec times[2];
+  times[0].tv_sec = 54321;
+  times[0].tv_nsec = 12345000;
+  times[1].tv_sec = 43210;
+  times[1].tv_nsec = 23456000;
+
+  // ensure futimens succeeds, operating on the fd directly
+  ASSERT_THAT(LIBC_NAMESPACE::futimens(fd, times), Succeeds(0));
+
+  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+
+  // verify the times values against stat of the TEST_FILE
+  struct stat statbuf;
+  ASSERT_EQ(LIBC_NAMESPACE::stat(TEST_FILE, &statbuf), 0);
+
+  // seconds
+  ASSERT_EQ(statbuf.st_atim.tv_sec, times[0].tv_sec);
+  ASSERT_EQ(statbuf.st_mtim.tv_sec, times[1].tv_sec);
+
+  // nanoseconds
+  ASSERT_EQ(statbuf.st_atim.tv_nsec, times[0].tv_nsec);
+  ASSERT_EQ(statbuf.st_mtim.tv_nsec, times[1].tv_nsec);
+
+  ASSERT_THAT(LIBC_NAMESPACE::remove(TEST_FILE), Succeeds(0));
----------------
AnirudhMathur12 wrote:

Done.

https://github.com/llvm/llvm-project/pull/222251


More information about the libc-commits mailing list