From 13b17e22782ba124e3c618d3eddfeb6c61d4357c Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Fri, 15 Dec 2017 18:21:42 +0100
Subject: [PATCH] Be more careful when resetting the ACLs.

Catch exceptions in case setting the ACLs fails, make the operation
optional by adding the parameter resetAcls (defaulting to false).

Refers to #16
---
 AutoTx/AutoTx.cs | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs
index 2b132a2..d6985a4 100644
--- a/AutoTx/AutoTx.cs
+++ b/AutoTx/AutoTx.cs
@@ -673,7 +673,7 @@ namespace AutoTx
                 writeLogDebug("Finalizing transfer, cleaning up target storage location...");
                 var finalDst = DestinationPath(_status.CurrentTargetTmp);
                 if (!string.IsNullOrWhiteSpace(finalDst)) {
-                    if (MoveAllSubDirs(new DirectoryInfo(ExpandCurrentTargetTmp()), finalDst)) {
+                    if (MoveAllSubDirs(new DirectoryInfo(ExpandCurrentTargetTmp()), finalDst, true)) {
                         _status.CurrentTargetTmp = "";
                     }
                 }
@@ -853,7 +853,7 @@ namespace AutoTx
         /// <param name="sourceDir">The source path as DirectoryInfo object.</param>
         /// <param name="destPath">The destination path as a string.</param>
         /// <returns>True on success, false otherwise.</returns>
-        private bool MoveAllSubDirs(DirectoryInfo sourceDir, string destPath) {
+        private bool MoveAllSubDirs(DirectoryInfo sourceDir, string destPath, bool resetAcls = false) {
             // TODO: check whether _transferState should be adjusted while moving dirs!
             writeLogDebug("MoveAllSubDirs: " + sourceDir.FullName + " to " + destPath);
             try {
@@ -872,10 +872,17 @@ namespace AutoTx
                     writeLogDebug(" - " + subDir.Name + " > " + target);
                     subDir.MoveTo(target);
 
-                    if (_config.EnforceInheritedACLs) {
-                        var acl = Directory.GetAccessControl(target);
-                        acl.SetAccessRuleProtection(false, false);
-                        Directory.SetAccessControl(target, acl);
+                    if (resetAcls && _config.EnforceInheritedACLs) {
+                        try {
+                            var acl = Directory.GetAccessControl(target);
+                            acl.SetAccessRuleProtection(false, false);
+                            Directory.SetAccessControl(target, acl);
+                            writeLogDebug("Successfully reset inherited ACLs on " + target);
+                        }
+                        catch (Exception ex) {
+                            writeLog("Error resetting inherited ACLs on " + target + ":\n" +
+                                ex.Message);
+                        }
                     }
                 }
             }
-- 
GitLab