imay commented on a change in pull request #2089: Change refactor and
reorganize the file utils
URL: https://github.com/apache/incubator-doris/pull/2089#discussion_r343455116
##########
File path: be/src/util/file_utils.cpp
##########
@@ -33,36 +33,57 @@
#include <openssl/md5.h>
+#include "gutil/strings/substitute.h"
+
#include "env/env.h"
#include "olap/file_helper.h"
#include "util/defer_op.h"
namespace doris {
-Status FileUtils::create_dir(const std::string& dir_path) {
- try {
- if (boost::filesystem::exists(dir_path.c_str())) {
- // No need to create one
- if (!boost::filesystem::is_directory(dir_path.c_str())) {
- std::stringstream ss;
- ss << "Path(" << dir_path << ") already exists, but not a
directory.";
- return Status::InternalError(ss.str());
- }
- } else {
- if (!boost::filesystem::create_directories(dir_path.c_str())) {
- std::stringstream ss;
- ss << "make directory failed. path=" << dir_path;
- return Status::InternalError(ss.str());
+using strings::Substitute;
+
+Status FileUtils::create_dir(const std::string& path, Env* env) {
+ if (path.empty()) {
+ return Status::InvalidArgument(Substitute("Unknown primitive
type($0)", path));
+ }
+
+ boost::filesystem::path p(path);
+
+ string partial_path;
+ for (boost::filesystem::path::iterator it = p.begin(); it != p.end();
++it) {
+ partial_path = partial_path.empty() ? it->string() : partial_path +
"/" + it->string();
+ bool is_dir = false;
+
+ Status s = env->is_directory(partial_path, &is_dir);
+
+ if (s.ok()) {
+ if (is_dir) {
+ // It's a normal directory.
+ continue;
}
+
+ // Maybe a file or a symlink. Let's try to follow the symlink.
+ string real_partial_path;
+ RETURN_IF_ERROR(env->canonicalize(partial_path,
&real_partial_path));
+
+ s = env->is_directory(real_partial_path, &is_dir);
+ if (s.ok() && is_dir) {
+ // It's a symlink to a directory.
+ continue;
+ }
Review comment:
If this path is normal file, we should return ERROR. No need to call another
create_dir
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]