From cbac4128897a0346e66a6cacabf083789ef07f83 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Sat, 25 May 2013 10:39:23 +0100 Subject: [PATCH] retrace: Create snapshot directory if it does not exist. One usually wants to put the snapshots in some directory. --- common/os_posix.cpp | 6 ++++++ common/os_string.hpp | 43 +++++++++++++++++++++------------------- common/os_win32.cpp | 6 ++++++ retrace/retrace_main.cpp | 17 ++++++++++++++++ 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/common/os_posix.cpp b/common/os_posix.cpp index 5ec8b36..05ad8e9 100644 --- a/common/os_posix.cpp +++ b/common/os_posix.cpp @@ -111,6 +111,12 @@ getCurrentDir(void) return path; } +bool +createDirectory(const String &path) +{ + return mkdir(path, 0777) == 0; +} + bool String::exists(void) const { diff --git a/common/os_string.hpp b/common/os_string.hpp index 78d02f2..3a8eab6 100644 --- a/common/os_string.hpp +++ b/common/os_string.hpp @@ -130,24 +130,23 @@ protected: return false; } - Buffer::iterator rfindSep(void) { - Buffer::iterator it = buffer.end(); +public: - // Skip trailing '\0' - assert(it != buffer.begin()); - --it; - assert(*it == '\0'); + Buffer::iterator rfindSep(bool skipTrailing = true) { + Buffer::iterator it = end(); // Skip trailing separators - while (it != buffer.begin()) { - --it; - if (isSep(*it)) { - // Halt if find the root - if (it == buffer.begin()) { - return it; + if (skipTrailing) { + while (it != buffer.begin()) { + --it; + if (isSep(*it)) { + // Halt if find the root + if (it == buffer.begin()) { + return it; + } + } else { + break; } - } else { - break; } } @@ -159,12 +158,9 @@ protected: } } - return buffer.end(); + return end(); } - -public: - /* * Constructors */ @@ -309,6 +305,11 @@ public: insert(end(), other); } + template + void erase(InputIterator first, InputIterator last) { + buffer.erase(first, last); + } + char *buf(size_t size) { buffer.resize(size); return &buffer[0]; @@ -343,7 +344,7 @@ public: */ void trimDirectory(void) { iterator sep = rfindSep(); - if (sep != buffer.end()) { + if (sep != end()) { buffer.erase(buffer.begin(), sep + 1); } } @@ -358,7 +359,7 @@ public: iterator sep = rfindSep(); // No separator found, so return '.' - if (sep == buffer.end()) { + if (sep == end()) { buffer.resize(2); buffer[0] = '.'; buffer[1] = 0; @@ -393,6 +394,8 @@ public: String getProcessName(); String getCurrentDir(); +bool createDirectory(const String &path); + bool copyFile(const String &srcFileName, const String &dstFileName, bool override = true); bool removeFile(const String &fileName); diff --git a/common/os_win32.cpp b/common/os_win32.cpp index f297a55..c5be8a8 100644 --- a/common/os_win32.cpp +++ b/common/os_win32.cpp @@ -69,6 +69,12 @@ getCurrentDir(void) return path; } +bool +createDirectory(const String &path) +{ + return CreateDirectoryA(path, NULL); +} + bool String::exists(void) const { diff --git a/retrace/retrace_main.cpp b/retrace/retrace_main.cpp index 5e31c02..ddcc27f 100644 --- a/retrace/retrace_main.cpp +++ b/retrace/retrace_main.cpp @@ -714,6 +714,23 @@ int main(int argc, char **argv) if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) { os::setBinaryMode(stdout); retrace::verbosity = -2; + } else { + /* + * Create the snapshot directory if it does not exist. + * + * We can't just use trimFilename() because when applied to + * "/foo/boo/" it would merely return "/foo". + * + * XXX: create nested directories. + */ + os::String prefix(snapshotPrefix); + os::String::iterator sep = prefix.rfindSep(false); + if (sep != prefix.end()) { + prefix.erase(sep, prefix.end()); + if (!os::createDirectory(prefix)) { + std::cerr << "error: failed to create " << prefix.str() << "\n"; + } + } } break; case SNAPSHOT_FORMAT_OPT: -- 2.43.0