Commits

Patrick Schaller committed a03159c

PwEntryExtension: Implemented the support of Keypass placeholders, such as {APPDIR}.
#16

  • Participants
  • Parent commits 7583742
  • Branches Issue16_SupportRelativePathsAndPlaceholders

Comments (1)

  1. Qasim Khalil

    Your changes to the keyfile section of the code (lines 134+) causes an exception for me (ArgumentException) if a keyfile was never specified in the first place.

    Adding a check (keyFilesString.Length > 0) before doing the rest fixes it.

Files changed (2)

File src/KeepassTrueCryptMount/KeepassTrueCryptMountExt.cs

             }
 
             // do mount...
-            var truecryptProcessInfo = new ProcessStartInfo(this.pluginHost.GetTrueCryptExecutable(),entry.ToTrueCryptArguments());
+            var truecryptProcessInfo = new ProcessStartInfo(this.pluginHost.GetTrueCryptExecutable(), entry.ToTrueCryptArguments());
 
             var truecryptProcess = Process.Start(truecryptProcessInfo);
             if(truecryptProcess != null)

File src/KeepassTrueCryptMount/PwEntryExtension.cs

 
     using KeePassLib;
     using KeePass.Util;
+    using KeePass.Util.Spr;
+    using System.IO;
 
     internal static class PwEntryExtension
     {
             {
                 var volumeString = entry.Strings.GetSafe(EntryStrings.Volume).ReadString();
                 arguments.Append(" /v ");
-                if (volumeString.Contains(" ") && !volumeString.StartsWith("\"") && !volumeString.EndsWith("\""))
-                {
-                    arguments.AppendFormat("\"{0}\"", volumeString);
-                }
-                else
+
+                if (!Path.IsPathRooted(volumeString))
                 {
-                    arguments.Append(volumeString);
+                    volumeString = SprEngine.Compile(volumeString, new SprContext(entry, null, SprCompileFlags.Paths));
+                    volumeString = Path.GetFullPath(volumeString);
                 }
+
+                arguments.Append(volumeString.QuoteIfNecessary());
             }
 
             // Keyfiles
             {
                 var keyFilesString = entry.Strings.GetSafe(EntryStrings.KeyFiles).ReadString();
                 String[] keyFiles = keyFilesString.Split(';');
-                for (int i = 0; i < (keyFiles.Length - 1); i++ )
+                for (int i = 0; i < keyFiles.Length; i++)
                 {
-                    arguments.Append(" /k ");
-                    if (keyFiles[i].Contains(" ") && !keyFiles[i].StartsWith("\"") && !keyFiles[i].EndsWith("\""))
-                    {
-                        arguments.AppendFormat("\"{0}\"", keyFiles[i]);
-                    }
-                    else
+                    var keyFile = keyFiles[i];
+
+                    if (!Path.IsPathRooted(keyFile))
                     {
-                        arguments.Append(keyFiles[i]);
+                        keyFile = SprEngine.Compile(keyFile, new SprContext(entry, null, SprCompileFlags.Paths));
+                        keyFile = Path.GetFullPath(keyFile);
                     }
 
+                    arguments.Append(" /k ");
+                    arguments.Append(keyFile.QuoteIfNecessary());
                 }
                
             }
             return bool.TrueString.Equals(entry.Strings.GetSafe(EntryStrings.Enabled).ReadString());
         }
 
-        public static void PerformPasswordAutotype(this PwEntry entry,PwDatabase database)
+        public static void PerformPasswordAutotype(this PwEntry entry, PwDatabase database)
         {
             var askForPasswordString = entry.Strings.GetSafe(EntryStrings.AskForPassword).ReadString();
             bool askForPassword;
 
             if (!askForPassword)
             {
-                AutoType.PerformIntoCurrentWindow(entry,database);
+                AutoType.PerformIntoCurrentWindow(entry, database);
+            }
+        }
+
+        /// <summary>
+        /// Quote unquote the specified file path if it contains spaces.
+        /// </summary>
+        /// <param name="filePath">The file path.</param>
+        /// <returns>A quoted file path if it contains spaces.</returns>
+        private static string QuoteIfNecessary(this string filePath)
+        {
+            if (filePath.Contains(" ") && !filePath.StartsWith("\"") && !filePath.EndsWith("\""))
+            {
+                return string.Format("\"{0}\"", filePath);
             }
+
+            return filePath;
         }
     }
 }