int CDiskRepartition::GetVolumesLetter(PCHAR VolumeName)
{
    DWORD  CharCount = MAX_PATH + 1;
    char Names[20];
    PWCHAR NameIdx   = NULL;
    BOOL   Success   = FALSE;
    for (;;) 
    {
        Success = GetVolumePathNamesForVolumeNameA(
            VolumeName, (LPCH)Names, sizeof(Names), &CharCount
            );

        if ( Success ) 
        {
            return Names[0];
        }

        if ( GetLastError() != ERROR_MORE_DATA ) 
        {
            return -1;
        }
		else
        {
            return Names[0];
        }
    }
    return -1;
}
void PrintDirectoryReparsePoint(const char *path) {
    int size = strlen(path) + 2;
    char *directory = (char *) malloc(size);
    strcpy_s(directory, size, path);
    NormalizeSlashes(directory, '\\');
    if (directory[strlen(directory) - 1] != '\\')
        strcat_s(directory, size, "\\");

    char volumeName[_MAX_PATH];
    int rc = GetVolumeNameForVolumeMountPointA(directory, volumeName, sizeof(volumeName));
    if (rc) {
        char volumePathNames[_MAX_PATH];
        DWORD returnLength;
        rc = GetVolumePathNamesForVolumeNameA(volumeName, volumePathNames, sizeof(volumePathNames), &returnLength);
        if (rc) {
            char *p = volumePathNames;
            while (*p) {
                if (_stricmp(p, directory))   // if it's not the path we've already found
                {
                    NormalizeSlashes(directory, '/');
                    NormalizeSlashes(p, '/');
                    puts(directory);
                    puts(p);
                }
                p += strlen(p) + 1;
            }
        }
    }
    free(directory);
}
Esempio n. 3
0
/*
 * Mount the volume identified by drive_guid to mountpoint drive_name
 */
BOOL MountVolume(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, but with a different letter
	// than the one we want. If that's the case, we need to unmount first.
	if ( (GetVolumePathNamesForVolumeNameA(drive_guid, mounted_letter, sizeof(mounted_letter), &size))
	  && (size > 1) && (mounted_letter[0] != drive_name[0]) ) {
		uprintf("Volume is already mounted, but as %c: instead of %c: - Unmounting...\n", mounted_letter[0], drive_name[0]);
		if (!DeleteVolumeMountPointA(mounted_letter))
			uprintf("Failed to unmount volume: %s", WindowsErrorString());
		// Also delete the destination mountpoint if needed (Don't care about errors)
		DeleteVolumeMountPointA(drive_name);
		Sleep(200);
	}

	if (!SetVolumeMountPointA(drive_name, drive_guid)) {
		// If the OS was faster than us at remounting the drive, 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\n",
					drive_name, WindowsErrorString());
				return FALSE;
			}
			if (safe_strcmp(drive_guid, mounted_guid) != 0) {
				uprintf("%s already mounted, but volume GUID doesn't match:\r\n  expected %s, got %s\n",
					drive_name, drive_guid, mounted_guid);
				return FALSE;
			}
			uprintf("%s was already mounted as %s\n", drive_guid, drive_name);
		} else {
			return FALSE;
		}
	}
	return TRUE;
}