Merge "Add <glob.h>."

am: b736c41ffe

Change-Id: I5d44bae5f36d51dfa95d4e513d8aab307c198b1f
This commit is contained in:
Elliott Hughes 2017-09-28 17:23:31 +00:00 committed by android-build-merger
commit a9b9b12728
21 changed files with 1576 additions and 7 deletions

View file

@ -299,13 +299,14 @@ cc_library_static {
cc_library_static {
defaults: ["libc_defaults"],
srcs: [
"upstream-freebsd/lib/libc/gen/glob.c",
"upstream-freebsd/lib/libc/stdlib/realpath.c",
],
cflags: [
"-Wno-sign-compare",
"-include freebsd-compat.h",
"-Wframe-larger-than=15000",
"-Wframe-larger-than=66000",
],
local_include_dirs: [

View file

@ -1910,6 +1910,75 @@ SUCH DAMAGE.
-------------------------------------------------------------------
Copyright (c) 1989, 1993
The Regents of the University of California. All rights reserved.
This code is derived from software contributed to Berkeley by
Guido van Rossum.
Copyright (c) 2011 The FreeBSD Foundation
All rights reserved.
Portions of this software were developed by David Chisnall
under sponsorship from the FreeBSD Foundation.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. 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.
3. Neither the name of the University nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
-------------------------------------------------------------------
Copyright (c) 1989, 1993
The Regents of the University of California. All rights reserved.
This code is derived from software contributed to Berkeley by
Guido van Rossum.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. 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.
3. Neither the name of the University nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
-------------------------------------------------------------------
Copyright (c) 1989, 1993
The Regents of the University of California. All rights reserved.

98
libc/include/glob.h Normal file
View file

@ -0,0 +1,98 @@
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Guido van Rossum.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* @(#)glob.h 8.1 (Berkeley) 6/2/93
* $FreeBSD$
*/
#ifndef _GLOB_H_
#define _GLOB_H_
#include <sys/cdefs.h>
#include <sys/types.h>
struct dirent;
struct stat;
typedef struct {
size_t gl_pathc; /* Count of total paths so far. */
size_t gl_matchc; /* Count of paths matching pattern. */
size_t gl_offs; /* Reserved at beginning of gl_pathv. */
int gl_flags; /* Copy of flags parameter to glob. */
char** gl_pathv; /* List of paths matching pattern. */
/* Copy of `__error_callback` parameter to glob. */
int (*gl_errfunc)(const char* __failure_path, int __failure_errno);
/*
* Alternate filesystem access methods for glob; replacement
* versions of closedir(3), readdir(3), opendir(3), stat(2)
* and lstat(2).
*/
void (*gl_closedir)(void*);
struct dirent* (*gl_readdir)(void*);
void* (*gl_opendir)(const char*);
int (*gl_lstat)(const char*, struct stat*);
int (*gl_stat)(const char*, struct stat*);
} glob_t;
/* Believed to have been introduced in 1003.2-1992 */
#define GLOB_APPEND 0x0001 /* Append to output from previous call. */
#define GLOB_DOOFFS 0x0002 /* Prepend `gl_offs` null pointers (leaving space for exec, say). */
#define GLOB_ERR 0x0004 /* Return on error. */
#define GLOB_MARK 0x0008 /* Append "/" to the names of returned directories. */
#define GLOB_NOCHECK 0x0010 /* Return pattern itself if nothing matches. */
#define GLOB_NOSORT 0x0020 /* Don't sort. */
#define GLOB_NOESCAPE 0x2000 /* Disable backslash escaping. */
/* Error values returned by glob(3) */
#define GLOB_NOSPACE (-1) /* Malloc call failed. */
#define GLOB_ABORTED (-2) /* Unignored error. */
#define GLOB_NOMATCH (-3) /* No match and GLOB_NOCHECK was not set. */
#if __USE_BSD
#define GLOB_ALTDIRFUNC 0x0040 /* Use alternately specified directory funcs. */
#define GLOB_BRACE 0x0080 /* Expand braces like csh. */
#define GLOB_MAGCHAR 0x0100 /* Set in `gl_flags` if the pattern had globbing characters. */
#define GLOB_NOMAGIC 0x0200 /* GLOB_NOCHECK without magic chars (csh). */
#define GLOB_QUOTE 0x0400 /* Quote special chars with \. */
#define GLOB_TILDE 0x0800 /* Expand tilde names from the passwd file. */
#define GLOB_LIMIT 0x1000 /* limit number of returned paths */
#endif
__BEGIN_DECLS
int glob(const char* __pattern, int __flags, int (*__error_callback)(const char* __failure_path, int __failure_errno), glob_t* __result_ptr) __INTRODUCED_IN_FUTURE;
void globfree(glob_t* __result_ptr) __INTRODUCED_IN_FUTURE;
__END_DECLS
#endif

View file

@ -1323,6 +1323,8 @@ LIBC_P {
__freading; # future
__fwriting; # future
getlogin_r; # future
glob; # future
globfree; # future
hcreate; # future
hcreate_r; # future
hdestroy; # future

View file

@ -1243,6 +1243,8 @@ LIBC_P {
__freading; # future
__fwriting; # future
getlogin_r; # future
glob; # future
globfree; # future
hcreate; # future
hcreate_r; # future
hdestroy; # future

View file

@ -1348,6 +1348,8 @@ LIBC_P {
__freading; # future
__fwriting; # future
getlogin_r; # future
glob; # future
globfree; # future
hcreate; # future
hcreate_r; # future
hdestroy; # future

View file

@ -1307,6 +1307,8 @@ LIBC_P {
__freading; # future
__fwriting; # future
getlogin_r; # future
glob; # future
globfree; # future
hcreate; # future
hcreate_r; # future
hdestroy; # future

View file

@ -1243,6 +1243,8 @@ LIBC_P {
__freading; # future
__fwriting; # future
getlogin_r; # future
glob; # future
globfree; # future
hcreate; # future
hcreate_r; # future
hdestroy; # future

View file

@ -1305,6 +1305,8 @@ LIBC_P {
__freading; # future
__fwriting; # future
getlogin_r; # future
glob; # future
globfree; # future
hcreate; # future
hcreate_r; # future
hdestroy; # future

View file

@ -1243,6 +1243,8 @@ LIBC_P {
__freading; # future
__fwriting; # future
getlogin_r; # future
glob; # future
globfree; # future
hcreate; # future
hcreate_r; # future
hdestroy; # future

View file

@ -18,6 +18,10 @@
#define _BIONIC_FREEBSD_COMPAT_H_included
#define _BSD_SOURCE
#include <sys/cdefs.h>
#include <stddef.h> // For size_t.
#define REPLACE_GETOPT
/*
@ -40,4 +44,12 @@
/* Redirect internal C library calls to the public function. */
#define _nanosleep nanosleep
/* FreeBSD has this as API, but we just use it internally. */
void* reallocarray(void*, size_t, size_t);
/* FreeBSD has this, but we can't really implement it correctly on Linux. */
#define issetugid() 0
#define ARG_MAX sysconf(_SC_ARG_MAX)
#endif

File diff suppressed because it is too large Load diff

View file

@ -272,7 +272,7 @@ LIBC {
*;
};
LIBC_O {
LIBC_O { # introduced=O
global:
cacoshl;
cacosl;

View file

@ -272,7 +272,7 @@ LIBC {
*;
};
LIBC_O {
LIBC_O { # introduced=O
global:
cacoshl;
cacosl;

View file

@ -272,7 +272,7 @@ LIBC {
*;
};
LIBC_O {
LIBC_O { # introduced=O
global:
cacoshl;
cacosl;

View file

@ -272,7 +272,7 @@ LIBC {
*;
};
LIBC_O {
LIBC_O { # introduced=O
global:
cacoshl;
cacosl;

View file

@ -272,7 +272,7 @@ LIBC {
*;
};
LIBC_O {
LIBC_O { # introduced=O
global:
cacoshl;
cacosl;

View file

@ -272,7 +272,7 @@ LIBC {
*;
};
LIBC_O {
LIBC_O { # introduced=O
global:
cacoshl;
cacosl;

View file

@ -71,6 +71,7 @@ cc_test_library {
"ftw_test.cpp",
"getauxval_test.cpp",
"getcwd_test.cpp",
"glob_test.cpp",
"grp_pwd_test.cpp",
"iconv_test.cpp",
"ifaddrs_test.cpp",

249
tests/glob_test.cpp Normal file
View file

@ -0,0 +1,249 @@
/*
* Copyright (C) 2017 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 <glob.h>
#include <dirent.h>
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include "TemporaryFile.h"
#if defined(__BIONIC__)
#define ASSERT_MATCH_COUNT(n_,g_) ASSERT_EQ(n_, g_.gl_matchc)
#else
#define ASSERT_MATCH_COUNT(n_,g_)
#endif
//
// Helper for use with GLOB_ALTDIRFUNC to iterate over the elements of `fake_dir`.
//
static std::vector<std::string> fake_dir;
static size_t fake_dir_offset;
static void fake_closedir(void*) {
}
static dirent* fake_readdir(void*) {
static dirent d;
if (fake_dir_offset >= fake_dir.size()) return nullptr;
strcpy(d.d_name, fake_dir[fake_dir_offset++].c_str());
return &d;
}
static void* fake_opendir(const char* path) {
fake_dir_offset = 0;
if (strcmp(path, "/opendir-fail/") == 0) {
errno = EINVAL;
return nullptr;
}
return &fake_dir;
}
static int fake_lstat(const char*, struct stat*) {
return 0;
}
static int fake_stat(const char*, struct stat*) {
return 0;
}
static void InstallFake(glob_t* g) {
g->gl_closedir = fake_closedir;
g->gl_readdir = fake_readdir;
g->gl_opendir = fake_opendir;
g->gl_lstat = fake_lstat;
g->gl_stat = fake_stat;
}
TEST(glob, glob_result_GLOB_NOMATCH) {
glob_t g = {};
ASSERT_EQ(GLOB_NOMATCH, glob("/will/match/nothing", 0, nullptr, &g));
ASSERT_EQ(0U, g.gl_pathc);
ASSERT_MATCH_COUNT(0U, g);
}
TEST(glob, glob_GLOB_APPEND) {
glob_t g = {};
ASSERT_EQ(0, glob("/proc/version", 0, nullptr, &g));
ASSERT_EQ(1U, g.gl_pathc);
ASSERT_MATCH_COUNT(1U, g);
ASSERT_STREQ("/proc/version", g.gl_pathv[0]);
ASSERT_EQ(nullptr, g.gl_pathv[1]);
ASSERT_EQ(0, glob("/proc/version", GLOB_APPEND, nullptr, &g));
ASSERT_EQ(2U, g.gl_pathc);
ASSERT_MATCH_COUNT(1U, g);
ASSERT_STREQ("/proc/version", g.gl_pathv[0]);
ASSERT_STREQ("/proc/version", g.gl_pathv[1]);
ASSERT_EQ(nullptr, g.gl_pathv[2]);
globfree(&g);
}
TEST(glob, glob_GLOB_DOOFFS) {
glob_t g = {};
g.gl_offs = 2;
ASSERT_EQ(0, glob("/proc/version", GLOB_DOOFFS, nullptr, &g));
ASSERT_EQ(1U, g.gl_pathc);
ASSERT_MATCH_COUNT(1U, g);
ASSERT_EQ(nullptr, g.gl_pathv[0]);
ASSERT_EQ(nullptr, g.gl_pathv[1]);
ASSERT_STREQ("/proc/version", g.gl_pathv[2]);
ASSERT_EQ(nullptr, g.gl_pathv[3]);
globfree(&g);
}
static std::string g_failure_path;
static int g_failure_errno;
static int test_error_callback_result;
static int test_error_callback(const char* failure_path, int failure_errno) {
g_failure_path = failure_path;
g_failure_errno = failure_errno;
return test_error_callback_result;
}
TEST(glob, glob_gl_errfunc) {
glob_t g = {};
InstallFake(&g);
test_error_callback_result = 0;
g_failure_errno = 0;
ASSERT_EQ(GLOB_NOMATCH, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC, test_error_callback, &g));
ASSERT_EQ("/opendir-fail/", g_failure_path);
ASSERT_EQ(EINVAL, g_failure_errno);
test_error_callback_result = 1;
g_failure_errno = 0;
ASSERT_EQ(GLOB_ABORTED, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC, test_error_callback, &g));
ASSERT_EQ("/opendir-fail/", g_failure_path);
ASSERT_EQ(EINVAL, g_failure_errno);
}
TEST(glob, glob_GLOB_ERR) {
glob_t g = {};
InstallFake(&g);
ASSERT_EQ(GLOB_NOMATCH, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC, nullptr, &g));
ASSERT_EQ(GLOB_ABORTED, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC | GLOB_ERR, nullptr, &g));
}
TEST(glob, glob_GLOB_MARK) {
TemporaryDir td;
// The pattern we're about to pass doesn't have a trailing '/'...
ASSERT_NE('/', std::string(td.dirname).back());
glob_t g = {};
// Using GLOB_MARK gets you a trailing '/' on a directory...
ASSERT_EQ(0, glob(td.dirname, GLOB_MARK, nullptr, &g));
ASSERT_EQ(1U, g.gl_pathc);
ASSERT_MATCH_COUNT(1U, g);
ASSERT_EQ(std::string(td.dirname) + "/", g.gl_pathv[0]);
ASSERT_EQ(nullptr, g.gl_pathv[1]);
TemporaryFile tf;
// But not on a file...
ASSERT_EQ(0, glob(tf.filename, GLOB_MARK, nullptr, &g));
ASSERT_EQ(1U, g.gl_pathc);
ASSERT_MATCH_COUNT(1U, g);
ASSERT_STREQ(tf.filename, g.gl_pathv[0]);
ASSERT_EQ(nullptr, g.gl_pathv[1]);
globfree(&g);
}
TEST(glob, glob_GLOB_NOCHECK) {
glob_t g = {};
ASSERT_EQ(0, glob("/will/match/nothing", GLOB_NOCHECK, nullptr, &g));
ASSERT_EQ(1U, g.gl_pathc);
ASSERT_MATCH_COUNT(0U, g);
ASSERT_STREQ("/will/match/nothing", g.gl_pathv[0]);
ASSERT_EQ(nullptr, g.gl_pathv[1]);
globfree(&g);
}
TEST(glob, glob_GLOB_NOSORT) {
fake_dir = { "c", "a", "d", "b" };
glob_t g = {};
InstallFake(&g);
ASSERT_EQ(0, glob("*", GLOB_ALTDIRFUNC, nullptr, &g));
ASSERT_EQ(4U, g.gl_pathc);
ASSERT_MATCH_COUNT(4U, g);
ASSERT_STREQ("a", g.gl_pathv[0]);
ASSERT_STREQ("b", g.gl_pathv[1]);
ASSERT_STREQ("c", g.gl_pathv[2]);
ASSERT_STREQ("d", g.gl_pathv[3]);
ASSERT_EQ(nullptr, g.gl_pathv[4]);
ASSERT_EQ(0, glob("*", GLOB_ALTDIRFUNC | GLOB_NOSORT, nullptr, &g));
ASSERT_EQ(4U, g.gl_pathc);
ASSERT_MATCH_COUNT(4U, g);
ASSERT_STREQ("c", g.gl_pathv[0]);
ASSERT_STREQ("a", g.gl_pathv[1]);
ASSERT_STREQ("d", g.gl_pathv[2]);
ASSERT_STREQ("b", g.gl_pathv[3]);
ASSERT_EQ(nullptr, g.gl_pathv[4]);
}
TEST(glob, glob_GLOB_MAGCHAR) {
glob_t g = {};
ASSERT_EQ(GLOB_NOMATCH, glob("/does-not-exist", 0, nullptr, &g));
ASSERT_TRUE((g.gl_flags & GLOB_MAGCHAR) == 0);
ASSERT_EQ(GLOB_NOMATCH, glob("/does-not-exist*", 0, nullptr, &g));
ASSERT_TRUE((g.gl_flags & GLOB_MAGCHAR) != 0);
// We can lie, but glob(3) will turn that into truth...
ASSERT_EQ(GLOB_NOMATCH, glob("/does-not-exist", GLOB_MAGCHAR, nullptr, &g));
ASSERT_TRUE((g.gl_flags & GLOB_MAGCHAR) == 0);
}
static void CheckGlob(const char* pattern, const std::vector<std::string>& expected_matches) {
glob_t g = {};
InstallFake(&g);
int expected_result = expected_matches.empty() ? GLOB_NOMATCH : 0;
ASSERT_EQ(expected_result, glob(pattern, GLOB_ALTDIRFUNC, nullptr, &g)) << pattern;
ASSERT_EQ(expected_matches.size(), g.gl_pathc);
ASSERT_MATCH_COUNT(expected_matches.size(), g);
for (size_t i = 0; i < expected_matches.size(); ++i) {
ASSERT_EQ(expected_matches[i], g.gl_pathv[i]);
}
if (!expected_matches.empty()) {
ASSERT_EQ(nullptr, g.gl_pathv[expected_matches.size()]);
}
globfree(&g);
}
TEST(glob, glob_globbing) {
fake_dir = { "f1", "f2", "f30", "f40" };
CheckGlob("f?", { "f1", "f2" });
CheckGlob("f??", { "f30", "f40" });
CheckGlob("f*", { "f1", "f2", "f30", "f40" });
}
TEST(glob, glob_globbing_rsc) {
// https://research.swtch.com/glob
fake_dir = { "axbxcxdxe" };
CheckGlob("a*b*c*d*e*", { "axbxcxdxe" });
fake_dir = { "axbxcxdxexxx" };
CheckGlob("a*b*c*d*e*", { "axbxcxdxexxx" });
fake_dir = { "abxbbxdbxebxczzx" };
CheckGlob("a*b?c*x", { "abxbbxdbxebxczzx" });
fake_dir = { "abxbbxdbxebxczzy" };
CheckGlob("a*b?c*x", {});
fake_dir = { std::string(100, 'a') };
CheckGlob("a*a*a*a*b", {});
}