Add futimens.

Bug: 10239370
Change-Id: I518340084103dc339ef8a065d4837d6258a1381d
This commit is contained in:
Elliott Hughes 2013-08-08 17:13:33 -07:00
parent 632fd5183a
commit d0be7c8f9a
5 changed files with 91 additions and 1 deletions

View file

@ -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 \

34
libc/bionic/futimens.cpp Normal file
View file

@ -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);
}

View file

@ -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

View file

@ -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 \

53
tests/sys_stat_test.cpp Normal file
View file

@ -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);
}