Refactor MkdirP out into a utility.
We want to use it again!
Change-Id: Iec72840e8fa1a8915e8176842f865a2f8acbfd93
diff --git a/aos/events/shm_event_loop.cc b/aos/events/shm_event_loop.cc
index cc11520..382c773 100644
--- a/aos/events/shm_event_loop.cc
+++ b/aos/events/shm_event_loop.cc
@@ -18,6 +18,7 @@
#include "aos/ipc_lib/signalfd.h"
#include "aos/realtime.h"
#include "aos/stl_mutex/stl_mutex.h"
+#include "aos/util/file.h"
#include "aos/util/phased_loop.h"
#include "glog/logging.h"
@@ -72,7 +73,7 @@
size_ = ipc_lib::LocklessQueueMemorySize(config_);
- MkdirP(path);
+ util::MkdirP(path, FLAGS_permissions);
// There are 2 cases. Either the file already exists, or it does not
// already exist and we need to create it. Start by trying to create it. If
@@ -124,23 +125,6 @@
const ipc_lib::LocklessQueueConfiguration &config() const { return config_; }
private:
- void MkdirP(std::string_view path) {
- auto last_slash_pos = path.find_last_of("/");
-
- std::string folder(last_slash_pos == std::string_view::npos
- ? std::string_view("")
- : path.substr(0, last_slash_pos));
- if (folder.empty()) return;
- MkdirP(folder);
- VLOG(1) << "Creating " << folder;
- const int result = mkdir(folder.c_str(), FLAGS_permissions);
- if (result == -1 && errno == EEXIST) {
- VLOG(1) << "Already exists";
- return;
- }
- PCHECK(result == 0) << ": Error creating " << folder;
- }
-
ipc_lib::LocklessQueueConfiguration config_;
int fd_;
diff --git a/aos/util/file.cc b/aos/util/file.cc
index af1f85b..b334ded 100644
--- a/aos/util/file.cc
+++ b/aos/util/file.cc
@@ -1,6 +1,8 @@
#include "aos/util/file.h"
#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <unistd.h>
#include <string_view>
@@ -46,5 +48,23 @@
}
}
+void MkdirP(std::string_view path, mode_t mode) {
+ auto last_slash_pos = path.find_last_of("/");
+
+ std::string folder(last_slash_pos == std::string_view::npos
+ ? std::string_view("")
+ : path.substr(0, last_slash_pos));
+ if (folder.empty()) return;
+ MkdirP(folder, mode);
+ const int result = mkdir(folder.c_str(), mode);
+ if (result == -1 && errno == EEXIST) {
+ VLOG(2) << folder << " already exists";
+ return;
+ } else {
+ VLOG(1) << "Created " << folder;
+ }
+ PCHECK(result == 0) << ": Error creating " << folder;
+}
+
} // namespace util
} // namespace aos
diff --git a/aos/util/file.h b/aos/util/file.h
index 3aebd87..d6724af 100644
--- a/aos/util/file.h
+++ b/aos/util/file.h
@@ -15,6 +15,8 @@
void WriteStringToFileOrDie(const std::string_view filename,
const std::string_view contents);
+void MkdirP(std::string_view path, mode_t mode);
+
} // namespace util
} // namespace aos