Commits

Patrick Schaller  committed e25e914

PwEntryExtension: added GetFullPath() extension which catches all exceptions; if keyfile string is empty, no empty /k added to command line.
#16

  • Participants
  • Parent commits d712c5b
  • Branches Issue16_SupportRelativePathsAndPlaceholders

Comments (0)

Files changed (1)

File src/KeepassTrueCryptMount/PwEntryExtension.cs

     using KeePass.Util.Spr;
     using System.IO;
 
-    internal static class PwEntryExtension
+    public static class PwEntryExtension
     {
         public static string ToTrueCryptArguments(this PwEntry entry)
         {
 
                 if (!Path.IsPathRooted(volumeString))
                 {
-                    volumeString = SprEngine.Compile(volumeString, new SprContext(entry, null, SprCompileFlags.Paths));
-                    volumeString = Path.GetFullPath(volumeString);
+                    volumeString = SprEngine.Compile(volumeString, new SprContext(entry, null, SprCompileFlags.Paths | SprCompileFlags.EnvVars));
                 }
 
-                arguments.Append(volumeString.QuoteIfNecessary());
+                arguments.Append(volumeString.GetFullPath().QuoteIfNecessary());
             }
 
             // Keyfiles
                 {
                     var keyFile = keyFiles[i];
 
+                    if (string.IsNullOrEmpty(keyFile))
+                    {
+                        continue;
+                    }
+
                     if (!Path.IsPathRooted(keyFile))
                     {
-                        keyFile = SprEngine.Compile(keyFile, new SprContext(entry, null, SprCompileFlags.Paths));
-                        keyFile = Path.GetFullPath(keyFile);
+                        keyFile = SprEngine.Compile(keyFile, new SprContext(entry, null, SprCompileFlags.Paths | SprCompileFlags.EnvVars));
                     }
 
                     arguments.Append(" /k ");
-                    arguments.Append(keyFile.QuoteIfNecessary());
+                    arguments.Append(keyFile.GetFullPath().QuoteIfNecessary());
                 }
                
             }
 
             return filePath;
         }
+
+        /// <summary>
+        /// Returns the absolute path for the specified path string.
+        /// </summary>
+        /// <param name="filePath">The file or directory for which to obtain absolute path information.</param>
+        /// <returns>The fully qualified location of <see cref="filePath"/>, such as "C:\MyFile.txt".</returns>
+        private static string GetFullPath(this string filePath)
+        {
+            try
+            {
+                return Path.GetFullPath(filePath);
+            }
+            catch (NotSupportedException)
+            {
+            }
+            catch (PathTooLongException)
+            {
+            }
+            catch (ArgumentNullException)
+            {
+            }
+            catch (ArgumentException)
+            {
+            }
+
+            return filePath;
+        }
     }
 }