Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and improve CreateUSBStick #116

Merged
merged 20 commits into from Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 56 additions & 2 deletions src/drive.c
Expand Up @@ -889,8 +889,62 @@ BOOL MountVolume(char* drive_name, char *drive_guid)
}

/*
* Mount partition #part_nr, residing on the same disk as drive_name to an available
* drive letter. Returns the newly allocated drive string.
* Mount the volume identified by drive_guid on a drive letter, if it is not already, and return the mount point in drive_guid.
* drive_guid should be initialized to "?:\"; the first character will be overwritten with the mounted drive letter.
*/
BOOL EnsureVolumeMounted(char* drive_name, char *drive_guid)
{
char mounted_guid[52]; // You need at least 51 characters on XP
char mounted_letter[16] = {0};
DWORD size;

if (drive_name[0] != '?')
return FALSE;

// For fixed disks, Windows may already have remounted the volume.
if ( (GetVolumePathNamesForVolumeNameA(drive_guid, mounted_letter, sizeof(mounted_letter), &size))
&& (size > 1) ) {
uprintf("Volume is already mounted at %c", mounted_letter[0]);
drive_name[0] = mounted_letter[0];
return TRUE;
}

drive_name[0] = GetUnusedDriveLetter();
if (drive_name[0] == 0) {
uprintf("Could not find an unused drive letter");
return FALSE;
}

if (SetVolumeMountPointA(drive_name, drive_guid)) {
uprintf("%s mounted as %s", drive_guid, drive_name);
return TRUE;
} else {
// If the OS mounted something on that letter in parallel, this operation can fail
// with ERROR_DIR_NOT_EMPTY. If that's the case, just check that mountpoints match
if (GetLastError() == ERROR_DIR_NOT_EMPTY) {
if (!GetVolumeNameForVolumeMountPointA(drive_name, mounted_guid, sizeof(mounted_guid))) {
uprintf("%s already mounted, but volume GUID could not be checked: %s",
drive_name, WindowsErrorString());
return FALSE;
}
if (safe_strcmp(drive_guid, mounted_guid) != 0) {
uprintf("%s already mounted, but volume GUID doesn't match. Hoped for %s, got %s",
drive_name, drive_guid, mounted_guid);
return FALSE;
}
uprintf("%s was already mounted as %s\n", drive_guid, drive_name);
return TRUE;
} else {
return FALSE;
}
}
}

/*
* Mount partition #part_nr, residing on the same disk as drive_name (which must be
* of the form "X:" with no trailing slash) to an available
* drive letter. Returns a borrowed pointer to the newly-allocated drive letter of
* the form "Y:", which must not be freed, or NULL on error.
* We need to do this because, for instance, EFI System partitions are not assigned
* Volume GUIDs by the OS, and we need to have a letter assigned, for when we invoke
* bcdtool for Windows To Go. All in all, the process looks like this:
Expand Down
1 change: 1 addition & 0 deletions src/drive.h
Expand Up @@ -93,6 +93,7 @@ BOOL AnalyzePBR(HANDLE hLogicalVolume);
BOOL GetDrivePartitionData(DWORD DriveIndex, char* FileSystemName, DWORD FileSystemNameSize, BOOL bSilent);
BOOL UnmountVolume(HANDLE hDrive);
BOOL MountVolume(char* drive_name, char *drive_guid);
BOOL EnsureVolumeMounted(char* drive_name, char *drive_guid);
BOOL AltUnmountVolume(const char* drive_name);
char* AltMountVolume(const char* drive_name, uint8_t part_nr);
BOOL RemountVolume(char* drive_name);
Expand Down
3 changes: 3 additions & 0 deletions src/endless/EndlessUsbTool.vcxproj
Expand Up @@ -271,6 +271,7 @@
<ItemGroup>
<ClInclude Include="..\dos.h" />
<ClInclude Include="..\drive.h" />
<ClInclude Include="..\format.h" />
<ClInclude Include="..\localization.h" />
<ClInclude Include="..\msapi_utf8.h" />
<ClInclude Include="..\rufus.h" />
Expand All @@ -289,6 +290,7 @@
<ClInclude Include="grub\grub.h" />
<ClInclude Include="grub\reed_solomon.h" />
<ClInclude Include="Images.h" />
<ClInclude Include="IOUtil.h" />
<ClInclude Include="json\json-forwards.h" />
<ClInclude Include="json\json.h" />
<ClInclude Include="localization_data.h" />
Expand Down Expand Up @@ -513,6 +515,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Images.cpp" />
<ClCompile Include="IOUtil.cpp" />
<ClCompile Include="StringHelperMethods.cpp" />
<ClCompile Include="jsoncpp.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
Expand Down
9 changes: 9 additions & 0 deletions src/endless/EndlessUsbTool.vcxproj.filters
Expand Up @@ -90,6 +90,9 @@
<ClInclude Include="..\dos.h">
<Filter>Header Files\Rufus Headers</Filter>
</ClInclude>
<ClInclude Include="..\format.h">
<Filter>Header Files\Rufus Headers</Filter>
</ClInclude>
<ClInclude Include="DownloadManager.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down Expand Up @@ -153,6 +156,9 @@
<ClInclude Include="UEFIBootSetup.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="IOUtil.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="EndlessUsbTool.cpp">
Expand Down Expand Up @@ -257,6 +263,9 @@
<ClCompile Include="Eosldr.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="IOUtil.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="res\EndlessUsbTool.rc2">
Expand Down