[util] update on DirName() on BSD-derived system
This is a follow-up to e201c15715834f925624463a7ccac8d59c3464eb.
Change-Id: Ic70ea96640d281bda372bd54c7d67bb1a59e0a8b
Reviewed-on: http://gerrit.cloudera.org:8080/16467
Tested-by: Kudu Jenkins
Reviewed-by: Grant Henke <granthenke@apache.org>
diff --git a/src/kudu/util/path_util.cc b/src/kudu/util/path_util.cc
index 54171bf..9875024 100644
--- a/src/kudu/util/path_util.cc
+++ b/src/kudu/util/path_util.cc
@@ -17,7 +17,7 @@
#include "kudu/util/path_util.h"
-// Use the POSIX version of dirname(3).
+// Use the POSIX version of basename(3)/dirname(3).
#include <libgen.h>
#if defined(__APPLE__)
@@ -32,7 +32,6 @@
#if defined(__APPLE__)
#include <cerrno>
-#include <mutex>
#endif // defined(__APPLE__)
#include <glog/logging.h>
@@ -103,12 +102,19 @@
}
string DirName(const string& path) {
- unique_ptr<char[], FreeDeleter> path_copy(strdup(path.c_str()));
#if defined(__APPLE__)
- static std::mutex lock;
- std::lock_guard<std::mutex> l(lock);
-#endif // defined(__APPLE__)
- return ::dirname(path_copy.get());
+ char buf[MAXPATHLEN];
+ auto* ret = dirname_r(path.c_str(), buf);
+ if (PREDICT_FALSE(ret == nullptr)) {
+ int err = errno;
+ LOG(FATAL) << strings::Substitute("dirname_r() failed: $0",
+ ErrnoToString(err));
+ }
+ return ret;
+#else
+ unique_ptr<char[], FreeDeleter> path_copy(strdup(path.c_str()));
+ return dirname(path_copy.get());
+#endif // #if defined(__APPLE__) ... #else
}
string BaseName(const string& path) {