HDFS-15962. Make strcasecmp cross platform (#2883)

This commit is contained in:
Gautham B A 2021-04-09 21:31:33 +05:30 committed by GitHub
parent 1448756505
commit 3148791da4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 71 additions and 25 deletions

View File

@ -20,6 +20,8 @@ if(NEED_LINK_DL)
endif()
include_directories(${Boost_INCLUDE_DIRS} ../../include)
add_library(common_obj OBJECT status.cc sasl_digest_md5.cc ioservice_impl.cc options.cc configuration.cc configuration_loader.cc hdfs_configuration.cc uri.cc util.cc retry_policy.cc cancel_tracker.cc logging.cc libhdfs_events_impl.cc auth_info.cc namenode_info.cc statinfo.cc fsinfo.cc content_summary.cc locks.cc config_parser.cc)
add_library(common $<TARGET_OBJECTS:common_obj> $<TARGET_OBJECTS:uriparser2_obj>)
add_library(common_obj OBJECT $<TARGET_OBJECTS:x_platform_obj> status.cc sasl_digest_md5.cc ioservice_impl.cc options.cc configuration.cc configuration_loader.cc hdfs_configuration.cc uri.cc util.cc retry_policy.cc cancel_tracker.cc logging.cc libhdfs_events_impl.cc auth_info.cc namenode_info.cc statinfo.cc fsinfo.cc content_summary.cc locks.cc config_parser.cc)
add_library(common $<TARGET_OBJECTS:x_platform_obj> $<TARGET_OBJECTS:common_obj> $<TARGET_OBJECTS:uriparser2_obj>)
target_link_libraries(common ${LIB_DL})
target_include_directories(common_obj PRIVATE ../../lib)
target_include_directories(common PRIVATE ../../lib)

View File

@ -33,6 +33,7 @@
#include "configuration.h"
#include "hdfspp/uri.h"
#include "x-platform/syscall.h"
#include <strings.h>
#include <sstream>
@ -124,10 +125,10 @@ optional<bool> Configuration::GetBool(const std::string& key) const {
return optional<bool>();
}
if (!strcasecmp(raw->c_str(), "true")) {
if (XPlatform::Syscall::StringCompareIgnoreCase(*raw, "true")) {
return std::experimental::make_optional(true);
}
if (!strcasecmp(raw->c_str(), "false")) {
if (XPlatform::Syscall::StringCompareIgnoreCase(*raw, "false")) {
return std::experimental::make_optional(false);
}

View File

@ -18,6 +18,7 @@
#include "configuration_loader.h"
#include "common/logging.h"
#include "x-platform/syscall.h"
#include <fstream>
#include <strings.h>
@ -46,17 +47,17 @@ bool is_valid_bool(const std::string& raw) {
return false;
}
if (!strcasecmp(raw.c_str(), "true")) {
if (XPlatform::Syscall::StringCompareIgnoreCase(raw, "true")) {
return true;
}
if (!strcasecmp(raw.c_str(), "false")) {
if (XPlatform::Syscall::StringCompareIgnoreCase(raw, "false")) {
return true;
}
return false;
}
bool str_to_bool(const std::string& raw) {
if (!strcasecmp(raw.c_str(), "true")) {
if (XPlatform::Syscall::StringCompareIgnoreCase(raw, "true")) {
return true;
}

View File

@ -298,7 +298,7 @@ void FileSystemImpl::Connect(const std::string &server,
void FileSystemImpl::ConnectToDefaultFs(const std::function<void(const Status &, FileSystem *)> &handler) {
std::string scheme = options_.defaultFS.get_scheme();
if (strcasecmp(scheme.c_str(), "hdfs") != 0) {
if (!XPlatform::Syscall::StringCompareIgnoreCase(scheme, "hdfs")) {
std::string error_message;
error_message += "defaultFS of [" + options_.defaultFS.str() + "] is not supported";
handler(Status::InvalidArgument(error_message.c_str()), nullptr);

View File

@ -16,7 +16,7 @@
# limitations under the License.
#
list(APPEND rpc_object_items rpc_connection_impl.cc rpc_engine.cc namenode_tracker.cc request.cc sasl_protocol.cc sasl_engine.cc)
list(APPEND rpc_object_items $<TARGET_OBJECTS:x_platform_obj> rpc_connection_impl.cc rpc_engine.cc namenode_tracker.cc request.cc sasl_protocol.cc sasl_engine.cc)
if (CMAKE_USING_CYRUS_SASL)
list(APPEND rpc_object_items cyrus_sasl_engine.cc)
endif (CMAKE_USING_CYRUS_SASL)
@ -25,7 +25,8 @@ if (CMAKE_USING_GSASL)
endif (CMAKE_USING_GSASL)
add_library(rpc_obj OBJECT ${rpc_object_items})
target_include_directories(rpc_obj PRIVATE ../../lib)
add_dependencies(rpc_obj proto)
add_library(rpc $<TARGET_OBJECTS:rpc_obj>)
target_include_directories(rpc PRIVATE ../../lib)

View File

@ -20,6 +20,7 @@
#include "rpc_connection.h"
#include "common/logging.h"
#include "common/optional_wrapper.h"
#include "x-platform/syscall.h"
#include "sasl_engine.h"
#include "sasl_protocol.h"
@ -97,20 +98,17 @@ void SaslProtocol::Authenticate(std::function<void(const Status & status, const
});
} // authenticate() method
AuthInfo::AuthMethod ParseMethod(const std::string & method)
{
if (0 == strcasecmp(method.c_str(), "SIMPLE")) {
return AuthInfo::kSimple;
}
else if (0 == strcasecmp(method.c_str(), "KERBEROS")) {
return AuthInfo::kKerberos;
}
else if (0 == strcasecmp(method.c_str(), "TOKEN")) {
return AuthInfo::kToken;
}
else {
return AuthInfo::kUnknownAuth;
}
AuthInfo::AuthMethod ParseMethod(const std::string & method) {
if (XPlatform::Syscall::StringCompareIgnoreCase(method, "SIMPLE")) {
return AuthInfo::kSimple;
}
if (XPlatform::Syscall::StringCompareIgnoreCase(method, "KERBEROS")) {
return AuthInfo::kKerberos;
}
if (XPlatform::Syscall::StringCompareIgnoreCase(method, "TOKEN")) {
return AuthInfo::kToken;
}
return AuthInfo::kUnknownAuth;
} // ParseMethod()
// build_init_msg():

View File

@ -71,6 +71,19 @@ class Syscall {
*/
static void ClearBufferSafely(void* buffer, size_t sz_bytes);
/**
* Performs a case insensitive equality comparison for the two
* given strings {@link a} and {@link b}.
*
* @param a the first string parameter to compare.
* @param b the second string parameter to compare.
* @returns A boolean indicating whether to two strings are the
* same irrespective of their case. Returns true if they match,
* else false.
*/
static bool StringCompareIgnoreCase(const std::string& a,
const std::string& b);
private:
static bool WriteToStdoutImpl(const char* message);
};

View File

@ -17,6 +17,7 @@
*/
#include <fnmatch.h>
#include <strings.h>
#include <unistd.h>
#include <cstring>
@ -48,3 +49,8 @@ void XPlatform::Syscall::ClearBufferSafely(void* buffer,
explicit_bzero(buffer, sz_bytes);
}
}
bool XPlatform::Syscall::StringCompareIgnoreCase(const std::string& a,
const std::string& b) {
return strcasecmp(a.c_str(), b.c_str()) == 0;
}

View File

@ -20,6 +20,8 @@
#include <WinBase.h>
#include <Windows.h>
#include <cstring>
#include "syscall.h"
#pragma comment(lib, "Shlwapi.lib")
@ -57,3 +59,8 @@ void XPlatform::Syscall::ClearBufferSafely(void* buffer,
SecureZeroMemory(buffer, sz_bytes);
}
}
bool XPlatform::Syscall::StringCompareIgnoreCase(const std::string& a,
const std::string& b) {
return _stricmp(a.c_str(), b.c_str()) == 0;
}

View File

@ -68,3 +68,20 @@ TEST(XPlatformSyscall, ClearBufferSafelyNumbers) {
EXPECT_EQ(number, 0);
}
}
TEST(XPlatformSyscall, StringCompareIgnoreCaseBasic) {
EXPECT_TRUE(XPlatform::Syscall::StringCompareIgnoreCase("aBcDeF", "AbCdEf"));
EXPECT_TRUE(XPlatform::Syscall::StringCompareIgnoreCase("a1B2c3D4e5F",
"A1b2C3d4E5f"));
EXPECT_TRUE(XPlatform::Syscall::StringCompareIgnoreCase(
"a!1@B#2$c%3^D&4*e(5)F", "A!1@b#2$C%3^d&4*E(5)f"));
EXPECT_TRUE(XPlatform::Syscall::StringCompareIgnoreCase(
"a<!>1@B#2$c%3^D&4*e(5)F?:", "A<!>1@b#2$C%3^d&4*E(5)f?:"));
EXPECT_TRUE(XPlatform::Syscall::StringCompareIgnoreCase("12345", "12345"));
EXPECT_TRUE(XPlatform::Syscall::StringCompareIgnoreCase("", ""));
}
TEST(XPlatformSyscall, StringCompareIgnoreCaseNegative) {
EXPECT_FALSE(XPlatform::Syscall::StringCompareIgnoreCase("abcd", "abcde"));
EXPECT_FALSE(XPlatform::Syscall::StringCompareIgnoreCase("12345", "abcde"));
}