Browse Source

Bug fix: Using drive menu to change to an invalid drive fails silently

Source commit: b2b9e2c30fe094065d7bcdf2804d2bb264f85fc0
Martin Prikryl 3 years ago
parent
commit
598a30acbe
2 changed files with 20 additions and 4 deletions
  1. 14 4
      source/forms/ScpCommander.cpp
  2. 6 0
      source/packages/filemng/DirView.pas

+ 14 - 4
source/forms/ScpCommander.cpp

@@ -2035,13 +2035,23 @@ void __fastcall TScpCommanderForm::LocalPathComboBoxItemClick(TObject * /*Sender
   DebugAssert((LocalPathComboBox->ItemIndex >= 0) && (LocalPathComboBox->ItemIndex < FLocalPathComboBoxPaths->Count));
 
   UnicodeString Path = FLocalPathComboBoxPaths->Strings[LocalPathComboBox->ItemIndex];
-  if (LocalPathComboBox->ItemIndex >= FLocalSpecialPaths)
+  try
   {
-    LocalDirView->ExecuteDrive(DriveInfo->GetDriveKey(Path));
+    if (LocalPathComboBox->ItemIndex >= FLocalSpecialPaths)
+    {
+      LocalDirView->ExecuteDrive(DriveInfo->GetDriveKey(Path));
+    }
+    else
+    {
+      LocalDirView->Path = Path;
+    }
   }
-  else
+  catch (...)
   {
-    LocalDirView->Path = Path;
+    // Changing the path failed, reset the combo box back.
+    // Does not recurse, so infinite recursion should not happen.
+    LocalPathComboUpdate();
+    throw;
   }
 }
 //---------------------------------------------------------------------------

+ 6 - 0
source/packages/filemng/DirView.pas

@@ -2785,6 +2785,7 @@ end;
 procedure TDirView.ExecuteDrive(Drive: string);
 var
   APath: string;
+  DriveRoot: string;
 begin
   if Assigned(FLastPath) and FLastPath.ContainsKey(Drive) then
   begin
@@ -2802,6 +2803,11 @@ begin
     if DriveInfo.IsRealDrive(Drive) then
     begin
       GetDir(Integer(Drive[1]) - Integer('A') + 1, APath);
+      DriveRoot := DriveInfo.GetDriveRoot(Drive);
+      // When the drive is not valid, the GetDir returns the current drive working directory, detect that,
+      // and let it fail later when trying to open root of the invalid drive.
+      if not StartsText(DriveRoot, APath) then
+        APath := DriveRoot;
       APath := ExcludeTrailingPathDelimiter(APath);
     end
       else