1
0
mirror of https://github.com/veracrypt/VeraCrypt.git synced 2025-11-11 11:08:02 -06:00

Windows: workaround to solve system favorites failing to mount because underlying drive takes long time to be recognized by Windows.

This commit is contained in:
Mounir IDRASSI
2015-07-13 15:16:01 +02:00
parent a43a5ebb21
commit 52146825de

View File

@@ -8156,31 +8156,32 @@ void ExtractCommandLine (HWND hwndDlg, char *lpszCommandLine)
static SERVICE_STATUS SystemFavoritesServiceStatus;
static SERVICE_STATUS_HANDLE SystemFavoritesServiceStatusHandle;
static void SystemFavoritesServiceLogMessage (const string &errorMessage, WORD wType)
{
HANDLE eventSource = RegisterEventSource (NULL, TC_SYSTEM_FAVORITES_SERVICE_NAME);
if (eventSource)
{
LPCTSTR strings[] = { TC_SYSTEM_FAVORITES_SERVICE_NAME, errorMessage.c_str() };
ReportEvent (eventSource, wType, 0, 0xC0000000 + wType, NULL, array_capacity (strings), 0, strings, NULL);
DeregisterEventSource (eventSource);
}
}
static void SystemFavoritesServiceLogError (const string &errorMessage)
{
HANDLE eventSource = RegisterEventSource (NULL, TC_SYSTEM_FAVORITES_SERVICE_NAME);
if (eventSource)
{
LPCTSTR strings[] = { TC_SYSTEM_FAVORITES_SERVICE_NAME, errorMessage.c_str() };
ReportEvent (eventSource, EVENTLOG_ERROR_TYPE, 0, 0xC0000001, NULL, array_capacity (strings), 0, strings, NULL);
DeregisterEventSource (eventSource);
}
SystemFavoritesServiceLogMessage (errorMessage, EVENTLOG_ERROR_TYPE);
}
static void SystemFavoritesServiceLogInfo (const string &errorMessage)
static void SystemFavoritesServiceLogWarning (const string &warningMessage)
{
HANDLE eventSource = RegisterEventSource (NULL, TC_SYSTEM_FAVORITES_SERVICE_NAME);
if (eventSource)
{
LPCTSTR strings[] = { TC_SYSTEM_FAVORITES_SERVICE_NAME, errorMessage.c_str() };
ReportEvent (eventSource, EVENTLOG_INFORMATION_TYPE, 0, 0xC0000002, NULL, array_capacity (strings), 0, strings, NULL);
DeregisterEventSource (eventSource);
SystemFavoritesServiceLogMessage (warningMessage, EVENTLOG_WARNING_TYPE);
}
static void SystemFavoritesServiceLogInfo (const string &infoMessage)
{
SystemFavoritesServiceLogMessage (infoMessage, EVENTLOG_INFORMATION_TYPE);
}
@@ -8487,68 +8488,8 @@ void DismountIdleVolumes ()
}
}
BOOL MountFavoriteVolumes (BOOL systemFavorites, BOOL logOnMount, BOOL hotKeyMount, const FavoriteVolume &favoriteVolumeToMount)
static BOOL MountFavoriteVolumeBase (const FavoriteVolume &favorite, BOOL& lastbExplore, BOOL& userForcedReadOnly, BOOL systemFavorites, BOOL logOnMount, BOOL hotKeyMount, const FavoriteVolume &favoriteVolumeToMount)
{
BOOL bRet = TRUE;
BOOL lastbExplore;
BOOL userForcedReadOnly = FALSE;
if (ServiceMode)
{
// in service case, intialize some global variable here.
LastKnownMountList.ulMountedDrives = 0;
LoadDriveLetters (MainDlg, GetDlgItem (MainDlg, IDC_DRIVELIST), 0);
}
mountOptions = defaultMountOptions;
VolumePassword.Length = 0;
MultipleMountOperationInProgress = (favoriteVolumeToMount.Path.empty() || FavoriteMountOnArrivalInProgress);
vector <FavoriteVolume> favorites;
if (systemFavorites)
{
try
{
if (ServiceMode)
SystemFavoritesServiceLogInfo (string ("Reading System Favorites XML file"));
LoadFavoriteVolumes (favorites, true);
if (ServiceMode)
{
char szTmp[32];
StringCbPrintf (szTmp, sizeof(szTmp), "%d", (int) favorites.size());
SystemFavoritesServiceLogInfo (string ("Loaded %d favorites from the file") + szTmp);
}
}
catch (...)
{
if (ServiceMode)
SystemFavoritesServiceLogError (string ("An error occured while reading System Favorites XML file"));
return false;
}
}
else if (!favoriteVolumeToMount.Path.empty())
favorites.push_back (favoriteVolumeToMount);
else
favorites = FavoriteVolumes;
foreach (const FavoriteVolume &favorite, favorites)
{
if (ServiceMode && systemFavorites && favorite.DisconnectedDevice)
{
SystemFavoritesServiceLogError (string ("Favorite \"") + favorite.Path + "\" is disconnected. It will be ignored.");
}
if (favorite.DisconnectedDevice
|| (logOnMount && !favorite.MountOnLogOn)
|| (hotKeyMount && favorite.DisableHotkeyMount))
{
continue;
}
BOOL status = TRUE;
int drive;
drive = toupper (favorite.MountPoint[0]) - 'A';
@@ -8619,6 +8560,10 @@ BOOL MountFavoriteVolumes (BOOL systemFavorites, BOOL logOnMount, BOOL hotKeyMou
if (ServiceMode)
{
// Update the service status to avoid being killed
SystemFavoritesServiceStatus.dwCheckPoint++;
SystemFavoritesServiceSetStatus (SERVICE_START_PENDING, 120000);
if (status)
{
SystemFavoritesServiceLogInfo (string ("Favorite \"") + favorite.Path + string ("\" mounted successfully as ") + (char) (drive + 'A') + ":");
@@ -8626,9 +8571,6 @@ BOOL MountFavoriteVolumes (BOOL systemFavorites, BOOL logOnMount, BOOL hotKeyMou
else
{
SystemFavoritesServiceLogError (string ("Favorite \"") + favorite.Path + "\" failed to mount");
// Update the service status to avoid being killed
SystemFavoritesServiceStatus.dwCheckPoint++;
SystemFavoritesServiceSetStatus (SERVICE_START_PENDING, 120000);
}
}
@@ -8664,9 +8606,6 @@ skipMount:
SystemFavoritesServiceLogError (string ("The filesystem of the volume mounted as ") + (char) (drive + 'A') + ": was not cleanly dismounted and needs to be checked for errors.");
}
if (!status)
bRet = FALSE;
}
else if (!systemFavorites && !favoriteVolumeToMount.Path.empty())
Error ("DRIVE_LETTER_UNAVAILABLE", MainDlg);
@@ -8674,6 +8613,128 @@ skipMount:
{
SystemFavoritesServiceLogError (string ("The drive letter ") + (char) (drive + 'A') + string (" used by favorite \"") + favorite.Path + "\" is already taken.\nThis system favorite will not be mounted");
}
return status;
}
BOOL MountFavoriteVolumes (BOOL systemFavorites, BOOL logOnMount, BOOL hotKeyMount, const FavoriteVolume &favoriteVolumeToMount)
{
BOOL bRet = TRUE, status = TRUE;
BOOL lastbExplore;
BOOL userForcedReadOnly = FALSE;
if (ServiceMode)
{
// in service case, intialize some global variable here.
LastKnownMountList.ulMountedDrives = 0;
LoadDriveLetters (MainDlg, GetDlgItem (MainDlg, IDC_DRIVELIST), 0);
}
mountOptions = defaultMountOptions;
VolumePassword.Length = 0;
MultipleMountOperationInProgress = (favoriteVolumeToMount.Path.empty() || FavoriteMountOnArrivalInProgress);
vector <FavoriteVolume> favorites, skippedSystemFavorites;
if (systemFavorites)
{
try
{
if (ServiceMode)
SystemFavoritesServiceLogInfo (string ("Reading System Favorites XML file"));
LoadFavoriteVolumes (favorites, true);
if (ServiceMode)
{
char szTmp[32];
StringCbPrintf (szTmp, sizeof(szTmp), "%d", (int) favorites.size());
SystemFavoritesServiceLogInfo (string ("Loaded %d favorites from the file") + szTmp);
}
}
catch (...)
{
if (ServiceMode)
SystemFavoritesServiceLogError (string ("An error occured while reading System Favorites XML file"));
return false;
}
}
else if (!favoriteVolumeToMount.Path.empty())
favorites.push_back (favoriteVolumeToMount);
else
favorites = FavoriteVolumes;
foreach (const FavoriteVolume &favorite, favorites)
{
if (ServiceMode && systemFavorites && favorite.DisconnectedDevice)
{
skippedSystemFavorites.push_back (favorite);
SystemFavoritesServiceLogWarning (string ("Favorite \"") + favorite.Path + "\" is disconnected. It will be ignored.");
}
if (favorite.DisconnectedDevice
|| (logOnMount && !favorite.MountOnLogOn)
|| (hotKeyMount && favorite.DisableHotkeyMount))
{
continue;
}
status = MountFavoriteVolumeBase (favorite, lastbExplore, userForcedReadOnly, systemFavorites, logOnMount, hotKeyMount, favoriteVolumeToMount);
if (!status)
bRet = FALSE;
}
if (systemFavorites && ServiceMode && !skippedSystemFavorites.empty())
{
// Some drives need more time to initialize correctly.
// We retry 4 times after sleeping 5 seconds
int retryCounter = 0;
size_t remainingFavorites = skippedSystemFavorites.size();
while ((remainingFavorites > 0) && (retryCounter++ < 4))
{
Sleep (5000);
SystemFavoritesServiceLogInfo (string ("Trying to mount skipped system favorites"));
// Update the service status to avoid being killed
SystemFavoritesServiceStatus.dwCheckPoint++;
SystemFavoritesServiceSetStatus (SERVICE_START_PENDING, 120000);
for (vector <FavoriteVolume>::iterator favorite = skippedSystemFavorites.begin();
favorite != skippedSystemFavorites.end(); favorite++)
{
if (favorite->DisconnectedDevice)
{
// check if the favorite is here and get its path
string resolvedPath = VolumeGuidPathToDevicePath (favorite->Path);
if (!resolvedPath.empty())
{
favorite->DisconnectedDevice = false;
favorite->VolumePathId = favorite->Path;
favorite->Path = resolvedPath;
remainingFavorites--;
// favorite OK.
SystemFavoritesServiceLogInfo (string ("Favorite \"") + favorite->VolumePathId + "\" is connected. Performing mount.");
status = MountFavoriteVolumeBase (*favorite, lastbExplore, userForcedReadOnly, systemFavorites, logOnMount, hotKeyMount, favoriteVolumeToMount);
if (!status)
bRet = FALSE;
}
}
}
if (remainingFavorites == 0)
SystemFavoritesServiceLogInfo (string ("All skipped system favorites have been processed"));
else
{
char szTmp[32];
StringCbPrintfA (szTmp, sizeof(szTmp), "%d", (int) remainingFavorites);
SystemFavoritesServiceLogWarning (string ("Number of unprocessed system favorites is ") + szTmp);
}
}
}
MultipleMountOperationInProgress = FALSE;