Skip to content

Commit 1cd58e6

Browse files
author
Andrey
committed
fixes
1 parent 464c1c3 commit 1cd58e6

File tree

2 files changed

+69
-29
lines changed

2 files changed

+69
-29
lines changed

src/models/SpaceManager.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,14 @@ bool SpaceManager::recoverSpace() {
135135
scriptFile << "foreach ($vol in $volumes) {\n";
136136
scriptFile << " $part = Get-Partition | Where-Object { $_.AccessPaths -contains $vol.Path }\n";
137137
scriptFile << " if ($part) {\n";
138-
scriptFile << " Remove-PartitionAccessPath -DiskNumber 0 -PartitionNumber $part.PartitionNumber -AccessPath "
139-
"$vol.Path -Confirm:$false\n";
138+
// Only remove drive letter access paths, not GUID paths
139+
scriptFile << " $accessPaths = $part.AccessPaths | Where-Object { $_ -match '^[A-Z]:\\\\$' }\n";
140+
scriptFile << " foreach ($path in $accessPaths) {\n";
141+
scriptFile << " try {\n";
142+
scriptFile << " Remove-PartitionAccessPath -DiskNumber 0 -PartitionNumber $part.PartitionNumber "
143+
"-AccessPath $path -Confirm:$false -ErrorAction SilentlyContinue\n";
144+
scriptFile << " } catch { }\n";
145+
scriptFile << " }\n";
140146
scriptFile << " Remove-Partition -DiskNumber 0 -PartitionNumber $part.PartitionNumber -Confirm:$false\n";
141147
scriptFile << " }\n";
142148
scriptFile << "}\n";

src/models/VolumeDetector.cpp

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -538,50 +538,84 @@ int VolumeDetector::countEfiPartitions() {
538538
}
539539

540540
bool VolumeDetector::isWindowsUsingEfiPartition() {
541-
// Check if Windows is installed and using the ISOEFI partition
541+
// Check if the SYSTEM'S ACTUAL EFI partition has the ISOEFI label
542+
// This prevents false positives when ISOEFI contains copied Windows files but isn't the active EFI
542543
std::string logDir = Utils::getExeDirectory() + "logs";
543544
CreateDirectoryA(logDir.c_str(), NULL);
544545
std::ofstream debugLog((logDir + "\\windows_efi_detection.log").c_str());
545546

546-
debugLog << "Checking if Windows is using ISOEFI partition...\n";
547+
debugLog << "Checking if Windows SYSTEM EFI partition is labeled as ISOEFI...\n";
547548

548-
std::string efiDrive = getEfiPartitionDriveLetter();
549+
// Use PowerShell to get the actual system EFI partition (not just any EFI labeled partition)
550+
char tempPath[MAX_PATH];
551+
GetTempPathA(MAX_PATH, tempPath);
552+
char tempFile[MAX_PATH];
553+
GetTempFileNameA(tempPath, "efi_check", 0, tempFile);
554+
std::string psFile = std::string(tempFile) + ".ps1";
555+
std::string outFile = std::string(tempFile) + "_out.txt";
549556

550-
if (efiDrive.empty()) {
551-
debugLog << "ISOEFI partition not found or not mounted\n";
557+
std::ofstream scriptFile(psFile);
558+
if (!scriptFile) {
559+
debugLog << "Failed to create PowerShell script\n";
552560
debugLog.close();
553561
return false;
554562
}
555563

556-
debugLog << "ISOEFI partition found at: " << efiDrive << "\n";
557-
558-
std::vector<std::string> windowsBootFiles = {efiDrive + "EFI\\Microsoft\\Boot\\bootmgfw.efi",
559-
efiDrive + "EFI\\Microsoft\\Boot\\BCD",
560-
efiDrive + "EFI\\Microsoft\\Boot\\bootmgr.efi"};
561-
562-
int foundFiles = 0;
563-
for (const auto &filePath : windowsBootFiles) {
564-
DWORD attrs = GetFileAttributesA(filePath.c_str());
565-
bool fileExists = (attrs != INVALID_FILE_ATTRIBUTES && !(attrs & FILE_ATTRIBUTE_DIRECTORY));
564+
// Get the actual system EFI partition (partition with GptType EFI and with ESP attribute)
565+
scriptFile << "$systemEfi = Get-Partition | Where-Object { $_.GptType -eq '{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}' "
566+
"-and $_.IsActive -eq $true } | Select-Object -First 1\n";
567+
scriptFile << "if ($systemEfi) {\n";
568+
scriptFile << " $vol = Get-Volume | Where-Object { $_.Path -in (Get-Partition -DiskNumber $systemEfi.DiskNumber "
569+
"-PartitionNumber $systemEfi.PartitionNumber).AccessPaths }\n";
570+
scriptFile << " if ($vol) {\n";
571+
scriptFile << " $vol.FileSystemLabel | Out-File -FilePath '" << outFile << "' -Encoding ASCII\n";
572+
scriptFile << " }\n";
573+
scriptFile << "}\n";
574+
scriptFile.close();
575+
576+
// Execute PowerShell script
577+
std::string cmd = "powershell -ExecutionPolicy Bypass -NoProfile -File \"" + psFile + "\"";
578+
STARTUPINFOA si = {sizeof(si)};
579+
PROCESS_INFORMATION pi;
580+
si.dwFlags = STARTF_USESHOWWINDOW;
581+
si.wShowWindow = SW_HIDE;
582+
583+
bool scriptRan = false;
584+
if (CreateProcessA(NULL, const_cast<char *>(cmd.c_str()), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si,
585+
&pi)) {
586+
WaitForSingleObject(pi.hProcess, 10000); // 10 seconds timeout
587+
CloseHandle(pi.hProcess);
588+
CloseHandle(pi.hThread);
589+
scriptRan = true;
590+
}
566591

567-
debugLog << "Checking: " << filePath << " - " << (fileExists ? "EXISTS" : "NOT FOUND") << "\n";
592+
DeleteFileA(psFile.c_str());
568593

569-
if (fileExists) {
570-
foundFiles++;
571-
}
594+
if (!scriptRan) {
595+
debugLog << "Failed to run PowerShell script\n";
596+
debugLog.close();
597+
return false;
572598
}
573599

574-
std::string markerPath = efiDrive + "BOOTTHATISO_TEMP_PARTITION.txt";
575-
DWORD markerAttrs = GetFileAttributesA(markerPath.c_str());
576-
bool hasMarker = (markerAttrs != INVALID_FILE_ATTRIBUTES && !(markerAttrs & FILE_ATTRIBUTE_DIRECTORY));
600+
// Read the result
601+
std::ifstream resultFile(outFile);
602+
std::string systemEfiLabel;
603+
if (resultFile) {
604+
std::getline(resultFile, systemEfiLabel);
605+
// Trim whitespace
606+
systemEfiLabel.erase(0, systemEfiLabel.find_first_not_of(" \t\r\n"));
607+
systemEfiLabel.erase(systemEfiLabel.find_last_not_of(" \t\r\n") + 1);
608+
resultFile.close();
609+
}
610+
DeleteFileA(outFile.c_str());
577611

578-
debugLog << "BootThatISO marker file: " << (hasMarker ? "PRESENT" : "ABSENT") << "\n";
579-
debugLog << "Windows boot files found: " << foundFiles << " out of " << windowsBootFiles.size() << "\n";
612+
debugLog << "System EFI partition label: '" << systemEfiLabel << "'\n";
580613

581-
bool windowsIsUsing = (foundFiles >= 2);
614+
bool isUsingIsoefi = (_stricmp(systemEfiLabel.c_str(), EFI_VOLUME_LABEL) == 0);
582615

583-
debugLog << "Conclusion: Windows is " << (windowsIsUsing ? "USING" : "NOT USING") << " ISOEFI\n";
616+
debugLog << "Conclusion: Windows " << (isUsingIsoefi ? "IS" : "IS NOT")
617+
<< " using ISOEFI as system EFI partition\n";
584618
debugLog.close();
585619

586-
return windowsIsUsing;
620+
return isUsingIsoefi;
587621
}

0 commit comments

Comments
 (0)