From 48923d23d88adb9052b17cec5e4ab49fbbc262cf Mon Sep 17 00:00:00 2001
From: Nico Schottelius <nico@bento.schottelius.org>
Date: Sun, 1 Dec 2013 18:37:08 +0100
Subject: [PATCH 1/3] if ... THEN ;-)

Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
---
 cdist/conf/type/__directory/gencode-remote | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/cdist/conf/type/__directory/gencode-remote b/cdist/conf/type/__directory/gencode-remote
index ebe07318..23fa4ed3 100755
--- a/cdist/conf/type/__directory/gencode-remote
+++ b/cdist/conf/type/__directory/gencode-remote
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# 2011-2012 Nico Schottelius (nico-cdist at schottelius.org)
+# 2011-2013 Nico Schottelius (nico-cdist at schottelius.org)
 # 2013 Steven Armstrong (steven-cdist armstrong.cc)
 #
 # This file is part of cdist.
@@ -69,7 +69,7 @@ set_mode() {
 
 case "$state_should" in
    present)
-      if [ "$type" != "directory" ];
+      if [ "$type" != "directory" ]; then
          # our destination is not a directory, remove whatever is there
          # and then create our directory and set all attributes
          set_attributes=1

From 7cf0d60b085f29716597ce11748e290b12a7bbfc Mon Sep 17 00:00:00 2001
From: Nico Schottelius <nico@bento.schottelius.org>
Date: Sun, 1 Dec 2013 18:42:07 +0100
Subject: [PATCH 2/3] catch permissionserror when deleting old cache

Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
---
 cdist/exec/local.py | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/cdist/exec/local.py b/cdist/exec/local.py
index 3a3ac706..88b49be9 100644
--- a/cdist/exec/local.py
+++ b/cdist/exec/local.py
@@ -186,8 +186,13 @@ class Local(object):
     def save_cache(self):
         destination = os.path.join(self.cache_path, self.target_host)
         self.log.debug("Saving " + self.base_path + " to " + destination)
-        if os.path.exists(destination):
-            shutil.rmtree(destination)
+
+        try:
+            if os.path.exists(destination):
+                shutil.rmtree(destination)
+        except PermissionError as e:
+            raise cdist.Error("Cannot delete old cache %s: %s" % (destination, e))
+
         shutil.move(self.base_path, destination)
 
     def _create_conf_path_and_link_conf_dirs(self):

From 538a5b4964db93afc070ea89b84ff3ce6109d1fa Mon Sep 17 00:00:00 2001
From: Nico Schottelius <nico@bento.schottelius.org>
Date: Sun, 1 Dec 2013 18:42:43 +0100
Subject: [PATCH 3/3] ++changes

Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
---
 docs/changelog | 1 +
 1 file changed, 1 insertion(+)

diff --git a/docs/changelog b/docs/changelog
index ec584026..67eaca1a 100644
--- a/docs/changelog
+++ b/docs/changelog
@@ -9,6 +9,7 @@ Changelog
 	* Type __file: Only remove file when state is absent (Steven Armstrong)
 	* Type __link: Only remove link when state is absent (Steven Armstrong)
 	* Type __directory: Only remove directory when state is absent (Steven Armstrong)
+	* Core: Fix backtrace when cache cannot be deleted
 
 2.3.6: 2013-11-25
 	* New Type: __locale