Browse Source

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

Gautham B A 4 years ago
parent
commit
3148791da4

+ 4 - 2
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/common/CMakeLists.txt

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

+ 3 - 2
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/common/configuration.cc

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

+ 4 - 3
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/common/configuration_loader.cc

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

+ 1 - 1
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/fs/filesystem.cc

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

+ 3 - 2
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/rpc/CMakeLists.txt

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

+ 12 - 14
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/rpc/sasl_protocol.cc

@@ -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():

+ 13 - 0
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/x-platform/syscall.h

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

+ 6 - 0
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/x-platform/syscall_linux.cc

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

+ 7 - 0
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/x-platform/syscall_windows.cc

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

+ 18 - 1
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/x-platform/syscall_common_test.cc

@@ -67,4 +67,21 @@ TEST(XPlatformSyscall, ClearBufferSafelyNumbers) {
   for (const auto number : numbers) {
     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"));
+}