1
1
Fork 0
mirror of https://github.com/pbatard/rufus.git synced 2024-08-14 23:57:05 +00:00

[core] display device paths in GetHandle()

* Also reorder bLockDrive and bWriteAccess parameters
This commit is contained in:
Pete Batard 2017-04-08 21:58:10 +01:00
parent eb57d116cc
commit 477ff95f93
5 changed files with 39 additions and 25 deletions

View file

@ -57,6 +57,7 @@ const GUID PARTITION_SYSTEM_GUID =
* Globals * Globals
*/ */
RUFUS_DRIVE_INFO SelectedDrive; RUFUS_DRIVE_INFO SelectedDrive;
extern BOOL lock_drive;
/* /*
* The following methods get or set the AutoMount setting (which is different from AutoRun) * The following methods get or set the AutoMount setting (which is different from AutoRun)
@ -117,37 +118,51 @@ BOOL GetAutoMount(BOOL* enabled)
* Open a drive or volume with optional write and lock access * Open a drive or volume with optional write and lock access
* Return INVALID_HANDLE_VALUE (/!\ which is DIFFERENT from NULL /!\) on failure. * Return INVALID_HANDLE_VALUE (/!\ which is DIFFERENT from NULL /!\) on failure.
*/ */
static HANDLE GetHandle(char* Path, BOOL bWriteAccess, BOOL bLockDrive, BOOL bWriteShare) static HANDLE GetHandle(char* Path, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWriteShare)
{ {
int i; int i;
DWORD size; DWORD size;
HANDLE hDrive = INVALID_HANDLE_VALUE; HANDLE hDrive = INVALID_HANDLE_VALUE;
char DevPath[MAX_PATH];
if (Path == NULL) if ((safe_strlen(Path) < 5) || (Path[0] != '\\') || (Path[1] != '\\') || (Path[3] != '\\'))
goto out; goto out;
// Resolve a device path, so that users can seek for it in Process Explorer
// in case of access issues.
if (QueryDosDeviceA(&Path[4], DevPath, sizeof(DevPath)) == 0)
strcpy(DevPath, "???");
for (i = 0; i < DRIVE_ACCESS_RETRIES; i++) { for (i = 0; i < DRIVE_ACCESS_RETRIES; i++) {
// Don't enable FILE_SHARE_WRITE (unless specifically requested) so that // Try without FILE_SHARE_WRITE (unless specifically requested) so that
// we won't be bothered by the OS or other apps when we set up our data. // we won't be bothered by the OS or other apps when we set up our data.
// However this means we might have to wait for an access gap... // However this means we might have to wait for an access gap...
// We keep FILE_SHARE_READ though, as this shouldn't hurt us any, and is // We keep FILE_SHARE_READ though, as this shouldn't hurt us any, and is
// required for enumeration. // required for enumeration.
hDrive = CreateFileA(Path, GENERIC_READ|(bWriteAccess?GENERIC_WRITE:0), hDrive = CreateFileA(Path, GENERIC_READ|(bWriteAccess?GENERIC_WRITE:0),
FILE_SHARE_READ|(bWriteShare?FILE_SHARE_WRITE:0), NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); FILE_SHARE_READ|((bWriteAccess && bWriteShare)?FILE_SHARE_WRITE:0),
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDrive != INVALID_HANDLE_VALUE) if (hDrive != INVALID_HANDLE_VALUE)
break; break;
if ((GetLastError() != ERROR_SHARING_VIOLATION) && (GetLastError() != ERROR_ACCESS_DENIED)) if ((GetLastError() != ERROR_SHARING_VIOLATION) && (GetLastError() != ERROR_ACCESS_DENIED))
break; break;
if (i == 0) if (i == 0) {
uprintf("Waiting for access..."); uprintf("Waiting for access...");
} else if (!lock_drive && bWriteAccess && !bWriteShare && (i > (2*DRIVE_ACCESS_RETRIES)/3)) {
// If we can't seem to get a hold of the drive for a long time,
// and Alt-, is enabled, make a last ditch effort with FILE_SHARE_WRITE...
uprintf("Can't access %s - Retrying with write sharing enabled...");
bWriteShare = TRUE;
}
Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES); Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES);
} }
if (hDrive == INVALID_HANDLE_VALUE) { if (hDrive == INVALID_HANDLE_VALUE) {
uprintf("Could not open drive %s: %s\n", Path, WindowsErrorString()); uprintf("Could not open %s [%s]: %s\n", Path, DevPath, WindowsErrorString());
goto out; goto out;
} }
if (bWriteAccess) { if (bWriteAccess) {
uprintf("Opened drive %s for write access\n", Path); uprintf("Opened %s [%s] for write access\n", Path, DevPath);
} }
if (bLockDrive) { if (bLockDrive) {
@ -163,7 +178,7 @@ static HANDLE GetHandle(char* Path, BOOL bWriteAccess, BOOL bLockDrive, BOOL bWr
Sleep(DRIVE_ACCESS_TIMEOUT/DRIVE_ACCESS_RETRIES); Sleep(DRIVE_ACCESS_TIMEOUT/DRIVE_ACCESS_RETRIES);
} }
// If we reached this section, either we didn't manage to get a lock or the user cancelled // If we reached this section, either we didn't manage to get a lock or the user cancelled
uprintf("Could not get exclusive access to device %s: %s\n", Path, WindowsErrorString()); uprintf("Could not get exclusive access to %s [%s]: %s\n", Path, DevPath, WindowsErrorString());
safe_closehandle(hDrive); safe_closehandle(hDrive);
} }
@ -190,11 +205,11 @@ out:
/* /*
* Return a handle to the physical drive identified by DriveIndex * Return a handle to the physical drive identified by DriveIndex
*/ */
HANDLE GetPhysicalHandle(DWORD DriveIndex, BOOL bWriteAccess, BOOL bLockDrive) HANDLE GetPhysicalHandle(DWORD DriveIndex, BOOL bLockDrive, BOOL bWriteAccess)
{ {
HANDLE hPhysical = INVALID_HANDLE_VALUE; HANDLE hPhysical = INVALID_HANDLE_VALUE;
char* PhysicalPath = GetPhysicalName(DriveIndex); char* PhysicalPath = GetPhysicalName(DriveIndex);
hPhysical = GetHandle(PhysicalPath, bWriteAccess, bLockDrive, FALSE); hPhysical = GetHandle(PhysicalPath, bLockDrive, bWriteAccess, FALSE);
safe_free(PhysicalPath); safe_free(PhysicalPath);
return hPhysical; return hPhysical;
} }
@ -261,7 +276,6 @@ char* GetLogicalName(DWORD DriveIndex, BOOL bKeepTrailingBackslash, BOOL bSilent
continue; continue;
} }
// If we can't have FILE_SHARE_WRITE, forget it
hDrive = CreateFileA(volume_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, hDrive = CreateFileA(volume_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDrive == INVALID_HANDLE_VALUE) { if (hDrive == INVALID_HANDLE_VALUE) {
@ -315,7 +329,7 @@ BOOL WaitForLogical(DWORD DriveIndex)
* Returns INVALID_HANDLE_VALUE on error or NULL if no logical path exists (typical * Returns INVALID_HANDLE_VALUE on error or NULL if no logical path exists (typical
* of unpartitioned drives) * of unpartitioned drives)
*/ */
HANDLE GetLogicalHandle(DWORD DriveIndex, BOOL bWriteAccess, BOOL bLockDrive, BOOL bWriteShare) HANDLE GetLogicalHandle(DWORD DriveIndex, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWriteShare)
{ {
HANDLE hLogical = INVALID_HANDLE_VALUE; HANDLE hLogical = INVALID_HANDLE_VALUE;
char* LogicalPath = GetLogicalName(DriveIndex, FALSE, FALSE); char* LogicalPath = GetLogicalName(DriveIndex, FALSE, FALSE);
@ -325,7 +339,7 @@ HANDLE GetLogicalHandle(DWORD DriveIndex, BOOL bWriteAccess, BOOL bLockDrive, BO
return NULL; return NULL;
} }
hLogical = GetHandle(LogicalPath, bWriteAccess, bLockDrive, bWriteShare); hLogical = GetHandle(LogicalPath, bLockDrive, bWriteAccess, bWriteShare);
free(LogicalPath); free(LogicalPath);
return hLogical; return hLogical;
} }

View file

@ -79,10 +79,10 @@ extern RUFUS_DRIVE_INFO SelectedDrive;
BOOL SetAutoMount(BOOL enable); BOOL SetAutoMount(BOOL enable);
BOOL GetAutoMount(BOOL* enabled); BOOL GetAutoMount(BOOL* enabled);
char* GetPhysicalName(DWORD DriveIndex); char* GetPhysicalName(DWORD DriveIndex);
HANDLE GetPhysicalHandle(DWORD DriveIndex, BOOL bWriteAccess, BOOL bLockDrive); HANDLE GetPhysicalHandle(DWORD DriveIndex, BOOL bLockDrive, BOOL bWriteAccess);
char* GetLogicalName(DWORD DriveIndex, BOOL bKeepTrailingBackslash, BOOL bSilent); char* GetLogicalName(DWORD DriveIndex, BOOL bKeepTrailingBackslash, BOOL bSilent);
BOOL WaitForLogical(DWORD DriveIndex); BOOL WaitForLogical(DWORD DriveIndex);
HANDLE GetLogicalHandle(DWORD DriveIndex, BOOL bWriteAccess, BOOL bLockDrive, BOOL bWriteShare); HANDLE GetLogicalHandle(DWORD DriveIndex, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWriteShare);
int GetDriveNumber(HANDLE hDrive, char* path); int GetDriveNumber(HANDLE hDrive, char* path);
BOOL GetDriveLetters(DWORD DriveIndex, char* drive_letters); BOOL GetDriveLetters(DWORD DriveIndex, char* drive_letters);
UINT GetDriveTypeFromIndex(DWORD DriveIndex); UINT GetDriveTypeFromIndex(DWORD DriveIndex);

View file

@ -1641,7 +1641,7 @@ DWORD WINAPI FormatThread(void* param)
extra_partitions = XP_COMPAT; extra_partitions = XP_COMPAT;
PrintInfoDebug(0, MSG_225); PrintInfoDebug(0, MSG_225);
hPhysicalDrive = GetPhysicalHandle(DriveIndex, TRUE, lock_drive); hPhysicalDrive = GetPhysicalHandle(DriveIndex, lock_drive, TRUE);
if (hPhysicalDrive == INVALID_HANDLE_VALUE) { if (hPhysicalDrive == INVALID_HANDLE_VALUE) {
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_OPEN_FAILED; FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_OPEN_FAILED;
goto out; goto out;
@ -1683,7 +1683,7 @@ DWORD WINAPI FormatThread(void* param)
uprintf("Will use '%c:' as volume mountpoint\n", drive_name[0]); uprintf("Will use '%c:' as volume mountpoint\n", drive_name[0]);
// ...but we need a lock to the logical drive to be able to write anything to it // ...but we need a lock to the logical drive to be able to write anything to it
hLogicalVolume = GetLogicalHandle(DriveIndex, FALSE, TRUE, FALSE); hLogicalVolume = GetLogicalHandle(DriveIndex, TRUE, FALSE, FALSE);
if (hLogicalVolume == INVALID_HANDLE_VALUE) { if (hLogicalVolume == INVALID_HANDLE_VALUE) {
uprintf("Could not lock volume\n"); uprintf("Could not lock volume\n");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_OPEN_FAILED; FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_OPEN_FAILED;
@ -1925,7 +1925,7 @@ DWORD WINAPI FormatThread(void* param)
} else { } else {
// We still have a lock, which we need to modify the volume boot record // We still have a lock, which we need to modify the volume boot record
// => no need to reacquire the lock... // => no need to reacquire the lock...
hLogicalVolume = GetLogicalHandle(DriveIndex, TRUE, FALSE, FALSE); hLogicalVolume = GetLogicalHandle(DriveIndex, FALSE, TRUE, FALSE);
if ((hLogicalVolume == INVALID_HANDLE_VALUE) || (hLogicalVolume == NULL)) { if ((hLogicalVolume == INVALID_HANDLE_VALUE) || (hLogicalVolume == NULL)) {
uprintf("Could not re-mount volume for partition boot record access\n"); uprintf("Could not re-mount volume for partition boot record access\n");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_OPEN_FAILED; FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_OPEN_FAILED;
@ -2074,7 +2074,7 @@ DWORD WINAPI SaveImageThread(void* param)
LastRefresh = 0; LastRefresh = 0;
switch (img_save->Type) { switch (img_save->Type) {
case IMG_SAVE_TYPE_VHD: case IMG_SAVE_TYPE_VHD:
hPhysicalDrive = GetPhysicalHandle(img_save->DeviceNum, FALSE, TRUE); hPhysicalDrive = GetPhysicalHandle(img_save->DeviceNum, TRUE, FALSE);
break; break;
case IMG_SAVE_TYPE_ISO: case IMG_SAVE_TYPE_ISO:
hPhysicalDrive = CreateFileA(img_save->DevicePath, GENERIC_READ, FILE_SHARE_READ, hPhysicalDrive = CreateFileA(img_save->DevicePath, GENERIC_READ, FILE_SHARE_READ,

View file

@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 242, 376 IDD_DIALOG DIALOGEX 12, 12, 242, 376
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_ACCEPTFILES EXSTYLE WS_EX_ACCEPTFILES
CAPTION "Rufus 2.13.1082" CAPTION "Rufus 2.13.1083"
FONT 8, "Segoe UI Symbol", 400, 0, 0x0 FONT 8, "Segoe UI Symbol", 400, 0, 0x0
BEGIN BEGIN
LTEXT "Device",IDS_DEVICE_TXT,9,6,200,8 LTEXT "Device",IDS_DEVICE_TXT,9,6,200,8
@ -334,8 +334,8 @@ END
// //
VS_VERSION_INFO VERSIONINFO VS_VERSION_INFO VERSIONINFO
FILEVERSION 2,13,1082,0 FILEVERSION 2,13,1083,0
PRODUCTVERSION 2,13,1082,0 PRODUCTVERSION 2,13,1083,0
FILEFLAGSMASK 0x3fL FILEFLAGSMASK 0x3fL
#ifdef _DEBUG #ifdef _DEBUG
FILEFLAGS 0x1L FILEFLAGS 0x1L
@ -352,13 +352,13 @@ BEGIN
BEGIN BEGIN
VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)" VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)"
VALUE "FileDescription", "Rufus" VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "2.13.1082" VALUE "FileVersion", "2.13.1083"
VALUE "InternalName", "Rufus" VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "© 2011-2017 Pete Batard (GPL v3)" VALUE "LegalCopyright", "© 2011-2017 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html" VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html"
VALUE "OriginalFilename", "rufus.exe" VALUE "OriginalFilename", "rufus.exe"
VALUE "ProductName", "Rufus" VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "2.13.1082" VALUE "ProductVersion", "2.13.1083"
END END
END END
BLOCK "VarFileInfo" BLOCK "VarFileInfo"

View file

@ -137,7 +137,7 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type)
* weird reason.the Syslinux install process *MUST* have FILE_SHARE_WRITE * weird reason.the Syslinux install process *MUST* have FILE_SHARE_WRITE
* on the volume, or else creating 'ldlinux.sys' will fail... * on the volume, or else creating 'ldlinux.sys' will fail...
*/ */
d_handle = GetLogicalHandle(drive_index, TRUE, FALSE, TRUE); d_handle = GetLogicalHandle(drive_index, FALSE, TRUE, TRUE);
if ((d_handle == INVALID_HANDLE_VALUE) || (d_handle == NULL)) { if ((d_handle == INVALID_HANDLE_VALUE) || (d_handle == NULL)) {
uprintf("Could open volume for Syslinux installation"); uprintf("Could open volume for Syslinux installation");
goto out; goto out;