am 2125c155: am d392e044: Merge "Move realpath.c to upstream-freebsd."

* commit '2125c15506cbe9e4062d72ebb723d164e9c01c1f':
  Move realpath.c to upstream-freebsd.
This commit is contained in:
Elliott Hughes 2013-03-04 23:56:56 +00:00 committed by Android Git Automerger
commit 81e0f35acc
6 changed files with 155 additions and 14 deletions

View File

@ -195,7 +195,6 @@ libc_common_src_files := \
bionic/ptsname_r.c \
bionic/pututline.c \
bionic/pwrite.c \
bionic/realpath.c \
bionic/reboot.c \
bionic/recv.c \
bionic/sched_cpualloc.c \
@ -317,6 +316,9 @@ libc_bionic_src_files := \
bionic/__vsprintf_chk.cpp \
bionic/wchar.cpp \
libc_upstream_freebsd_src_files := \
upstream-freebsd/lib/libc/stdlib/realpath.c \
libc_upstream_netbsd_src_files := \
upstream-netbsd/common/lib/libc/hash/sha1/sha1.c \
upstream-netbsd/common/lib/libc/inet/inet_addr.c \
@ -685,6 +687,29 @@ LOCAL_SYSTEM_SHARED_LIBRARIES :=
include $(BUILD_STATIC_LIBRARY)
# ========================================================
# libc_freebsd.a - upstream FreeBSD C library code
# ========================================================
#
# These files are built with the freebsd-compat.h header file
# automatically included.
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(libc_upstream_freebsd_src_files)
LOCAL_CFLAGS := \
$(libc_common_cflags) \
-I$(LOCAL_PATH)/upstream-freebsd \
-I$(LOCAL_PATH)/upstream-freebsd/libc/include \
-include upstream-freebsd/freebsd-compat.h
LOCAL_C_INCLUDES := $(libc_common_c_includes)
LOCAL_MODULE := libc_freebsd
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
LOCAL_SYSTEM_SHARED_LIBRARIES :=
include $(BUILD_STATIC_LIBRARY)
# ========================================================
# libc_netbsd.a - upstream NetBSD C library code
# ========================================================
@ -737,7 +762,7 @@ LOCAL_CFLAGS := $(libc_common_cflags) \
LOCAL_C_INCLUDES := $(libc_common_c_includes)
LOCAL_MODULE := libc_common
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
LOCAL_WHOLE_STATIC_LIBRARIES := libbionic_ssp libc_bionic libc_netbsd
LOCAL_WHOLE_STATIC_LIBRARIES := libbionic_ssp libc_bionic libc_freebsd libc_netbsd
LOCAL_SYSTEM_SHARED_LIBRARIES :=
# TODO: split out the asflags.

View File

@ -0,0 +1,20 @@
/*
* 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.
*/
#ifndef _BIONIC_FREEBSD_COMPAT_H_included
#define _BIONIC_FREEBSD_COMPAT_H_included
#endif

View File

@ -30,8 +30,9 @@
static char sccsid[] = "@(#)realpath.c 8.1 (Berkeley) 2/16/94";
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: release/9.0.0/lib/libc/stdlib/realpath.c 217144 2011-01-08 11:04:30Z kib $");
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <sys/param.h>
#include <sys/stat.h>
@ -39,6 +40,7 @@ __FBSDID("$FreeBSD: release/9.0.0/lib/libc/stdlib/realpath.c 217144 2011-01-08 1
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "un-namespace.h"
/*
* Find the real name of path, by removing all ".", ".." and symlink
@ -52,7 +54,7 @@ realpath(const char * __restrict path, char * __restrict resolved)
char *p, *q, *s;
size_t left_len, resolved_len;
unsigned symlinks;
int m, serrno, slen;
int m, slen;
char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
if (path == NULL) {
@ -63,7 +65,6 @@ realpath(const char * __restrict path, char * __restrict resolved)
errno = ENOENT;
return (NULL);
}
serrno = errno;
if (resolved == NULL) {
resolved = malloc(PATH_MAX);
if (resolved == NULL)
@ -130,8 +131,29 @@ realpath(const char * __restrict path, char * __restrict resolved)
resolved[resolved_len++] = '/';
resolved[resolved_len] = '\0';
}
if (next_token[0] == '\0')
if (next_token[0] == '\0') {
/*
* Handle consequential slashes. The path
* before slash shall point to a directory.
*
* Only the trailing slashes are not covered
* by other checks in the loop, but we verify
* the prefix for any (rare) "//" or "/\0"
* occurence to not implement lookahead.
*/
if (lstat(resolved, &sb) != 0) {
if (m)
free(resolved);
return (NULL);
}
if (!S_ISDIR(sb.st_mode)) {
if (m)
free(resolved);
errno = ENOTDIR;
return (NULL);
}
continue;
}
else if (strcmp(next_token, ".") == 0)
continue;
else if (strcmp(next_token, "..") == 0) {
@ -149,9 +171,7 @@ realpath(const char * __restrict path, char * __restrict resolved)
}
/*
* Append the next path component and lstat() it. If
* lstat() fails we still can return successfully if
* there are no more path components left.
* Append the next path component and lstat() it.
*/
resolved_len = strlcat(resolved, next_token, PATH_MAX);
if (resolved_len >= PATH_MAX) {
@ -161,10 +181,6 @@ realpath(const char * __restrict path, char * __restrict resolved)
return (NULL);
}
if (lstat(resolved, &sb) != 0) {
if (errno == ENOENT && p == NULL) {
errno = serrno;
return (resolved);
}
if (m)
free(resolved);
return (NULL);
@ -210,7 +226,8 @@ realpath(const char * __restrict path, char * __restrict resolved)
symlink[slen] = '/';
symlink[slen + 1] = 0;
}
left_len = strlcat(symlink, left, sizeof(left));
left_len = strlcat(symlink, left,
sizeof(symlink));
if (left_len >= sizeof(left)) {
if (m)
free(resolved);

View File

@ -0,0 +1,20 @@
/*
* 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.
*/
#ifndef _BIONIC_FREEBSD_NAMESPACE_H_included
#define _BIONIC_FREEBSD_NAMESPACE_H_included
#endif

View File

@ -0,0 +1,20 @@
/*
* 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.
*/
#ifndef _BIONIC_FREEBSD_NAMESPACE_H_included
#define _BIONIC_FREEBSD_NAMESPACE_H_included
#endif

View File

@ -17,6 +17,8 @@
#include <gtest/gtest.h>
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
@ -70,3 +72,40 @@ TEST(stdlib, posix_memalign) {
// Can't align to a non-power of 2.
ASSERT_EQ(EINVAL, posix_memalign(&p, 81, 128));
}
TEST(stdlib, realpath__NULL_filename) {
errno = 0;
char* p = realpath(NULL, NULL);
ASSERT_TRUE(p == NULL);
ASSERT_EQ(EINVAL, errno);
}
TEST(stdlib, realpath__empty_filename) {
errno = 0;
char* p = realpath("", NULL);
ASSERT_TRUE(p == NULL);
ASSERT_EQ(ENOENT, errno);
}
TEST(stdlib, realpath__ENOENT) {
errno = 0;
char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL);
ASSERT_TRUE(p == NULL);
ASSERT_EQ(ENOENT, errno);
}
TEST(stdlib, realpath) {
// Get the name of this executable.
char executable_path[PATH_MAX];
int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
ASSERT_NE(rc, -1);
executable_path[rc] = '\0';
char buf[PATH_MAX + 1];
char* p = realpath("/proc/self/exe", buf);
ASSERT_STREQ(executable_path, p);
p = realpath("/proc/self/exe", NULL);
ASSERT_STREQ(executable_path, p);
free(p);
}