From d0be7c8f9a06b3ca8ea7647ea35c8f9dc63f0fe1 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <enh@google.com>
Date: Thu, 8 Aug 2013 17:13:33 -0700
Subject: [PATCH] Add futimens.

Bug: 10239370
Change-Id: I518340084103dc339ef8a065d4837d6258a1381d
---
 libc/Android.mk          |  1 +
 libc/bionic/futimens.cpp | 34 ++++++++++++++++++++++++++
 libc/include/sys/stat.h  |  3 ++-
 tests/Android.mk         |  1 +
 tests/sys_stat_test.cpp  | 53 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 91 insertions(+), 1 deletion(-)
 create mode 100644 libc/bionic/futimens.cpp
 create mode 100644 tests/sys_stat_test.cpp

diff --git a/libc/Android.mk b/libc/Android.mk
index f353c416f..db97a74ce 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -190,6 +190,7 @@ libc_bionic_src_files := \
     bionic/eventfd_read.cpp \
     bionic/eventfd_write.cpp \
     bionic/__fgets_chk.cpp \
+    bionic/futimens.cpp \
     bionic/getauxval.cpp \
     bionic/getcwd.cpp \
     bionic/libc_init_common.cpp \
diff --git a/libc/bionic/futimens.cpp b/libc/bionic/futimens.cpp
new file mode 100644
index 000000000..1ca8eb59a
--- /dev/null
+++ b/libc/bionic/futimens.cpp
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <errno.h>
+#include <sys/stat.h>
+
+int futimens(int fd, const struct timespec times[2]) {
+  return utimensat(fd, NULL, times, 0);
+}
diff --git a/libc/include/sys/stat.h b/libc/include/sys/stat.h
index c7715a3ce..10627b953 100644
--- a/libc/include/sys/stat.h
+++ b/libc/include/sys/stat.h
@@ -168,7 +168,8 @@ extern int renameat(int olddirfd, const char *oldpath, int newdirfd, const char
 
 # define UTIME_NOW      ((1l << 30) - 1l)
 # define UTIME_OMIT     ((1l << 30) - 2l)
-extern int utimensat (int fd, const char *path, const struct timespec times[2], int flags);
+extern int utimensat(int fd, const char *path, const struct timespec times[2], int flags);
+extern int futimens(int fd, const struct timespec times[2]);
 
 __END_DECLS
 
diff --git a/tests/Android.mk b/tests/Android.mk
index efee17bca..177e4520b 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -80,6 +80,7 @@ test_src_files = \
     string_test.cpp \
     strings_test.cpp \
     stubs_test.cpp \
+    sys_stat_test.cpp \
     system_properties_test.cpp \
     time_test.cpp \
     unistd_test.cpp \
diff --git a/tests/sys_stat_test.cpp b/tests/sys_stat_test.cpp
new file mode 100644
index 000000000..a23100a6c
--- /dev/null
+++ b/tests/sys_stat_test.cpp
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+
+TEST(sys_stat, futimens) {
+  FILE* fp = tmpfile();
+  ASSERT_TRUE(fp != NULL);
+
+  int fd = fileno(fp);
+  ASSERT_NE(fd, -1);
+
+  timespec times[2];
+  times[0].tv_sec = 123;
+  times[0].tv_nsec = 0;
+  times[1].tv_sec = 456;
+  times[1].tv_nsec = 0;
+  ASSERT_EQ(0, futimens(fd, times)) << strerror(errno);
+
+  struct stat sb;
+  ASSERT_EQ(0, fstat(fd, &sb));
+  ASSERT_EQ(times[0].tv_sec, static_cast<long>(sb.st_atime));
+  ASSERT_EQ(times[1].tv_sec, static_cast<long>(sb.st_mtime));
+
+  fclose(fp);
+}
+
+TEST(sys_stat, futimens_EBADF) {
+  timespec times[2];
+  times[0].tv_sec = 123;
+  times[0].tv_nsec = 0;
+  times[1].tv_sec = 456;
+  times[1].tv_nsec = 0;
+  ASSERT_EQ(-1, futimens(-1, times));
+  ASSERT_EQ(EBADF, errno);
+}