]> git.cworth.org Git - apitrace/commitdiff
retrace: Create snapshot directory if it does not exist.
authorJosé Fonseca <jfonseca@vmware.com>
Sat, 25 May 2013 09:39:23 +0000 (10:39 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Sat, 25 May 2013 11:16:39 +0000 (12:16 +0100)
One usually wants to put the snapshots in some directory.

common/os_posix.cpp
common/os_string.hpp
common/os_win32.cpp
retrace/retrace_main.cpp

index 5ec8b363390792748e733a6d8d5e7a9b3afadb24..05ad8e987cdcf4365a8ab0cf7f209f9be07955f5 100644 (file)
@@ -111,6 +111,12 @@ getCurrentDir(void)
     return path;
 }
 
+bool
+createDirectory(const String &path)
+{
+    return mkdir(path, 0777) == 0;
+}
+
 bool
 String::exists(void) const
 {
index 78d02f2cacc5a4bbefcf430249a74361b068377a..3a8eab688552b1bb39f187bc57fc2981f4e5bd1c 100644 (file)
@@ -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 <class InputIterator>
+    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);
index f297a5551357f5b7631519374bc4ad36149090a6..c5be8a88efb190f904d2af4fd098e77cbfab1093 100644 (file)
@@ -69,6 +69,12 @@ getCurrentDir(void)
     return path;
 }
 
+bool
+createDirectory(const String &path)
+{
+    return CreateDirectoryA(path, NULL);
+}
+
 bool
 String::exists(void) const
 {
index 5e31c020490ab86beccb3635914fa82d9e83d479..ddcc27fb82e1133bd2febf717b4ebb0b7b99d8c9 100644 (file)
@@ -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: