]> git.tdb.fi Git - libs/core.git/blobdiff - source/fs/redirectedpath.cpp
Add RAII class for atomic overwrites
[libs/core.git] / source / fs / redirectedpath.cpp
diff --git a/source/fs/redirectedpath.cpp b/source/fs/redirectedpath.cpp
new file mode 100644 (file)
index 0000000..6ca59cc
--- /dev/null
@@ -0,0 +1,38 @@
+#include <exception>
+#include "redirectedpath.h"
+#include "stat.h"
+#include "utils.h"
+
+using namespace std;
+
+namespace Msp {
+namespace FS {
+
+RedirectedPath::RedirectedPath(const Path &p, const char *ext):
+       Path(p.str()+ext),
+       original(p)
+{ }
+
+RedirectedPath::RedirectedPath(const Path &p, const string &ext):
+       Path(p.str()+ext),
+       original(p)
+{ }
+
+RedirectedPath::RedirectedPath(const Path &p, const Path &r):
+       Path(r),
+       original(p)
+{ }
+
+RedirectedPath::~RedirectedPath()
+{
+       if(exists(*this))
+       {
+               if(uncaught_exception())
+                       unlink(*this);
+               else
+                       rename(*this, original);
+       }
+}
+
+} // namespace FS
+} // namespace Msp