nsresult sbWinGetVolumeGUIDPath(DEVINST aDevInst, nsAString& aVolumeGUIDPath) { BOOL success; nsresult rv; // Get the volume device interface path. GUID guid = GUID_DEVINTERFACE_VOLUME; nsAutoString volumeDeviceInterfacePath; rv = sbWinGetDevicePath(aDevInst, &guid, volumeDeviceInterfacePath); NS_ENSURE_SUCCESS(rv, rv); // Get the volume GUID path. The mount point must end with "\\". static const DWORD volumeGUIDPathLength = 51; WCHAR volumeGUIDPath[volumeGUIDPathLength]; volumeDeviceInterfacePath.AppendLiteral("\\"); success = GetVolumeNameForVolumeMountPointW(volumeDeviceInterfacePath.get(), volumeGUIDPath, volumeGUIDPathLength); NS_ENSURE_TRUE(success, NS_ERROR_FAILURE); // Return results. aVolumeGUIDPath.Assign(volumeGUIDPath); return NS_OK; }
/* FMIFS.7 */ VOID NTAPI FormatEx( IN PWCHAR DriveRoot, IN FMIFS_MEDIA_FLAG MediaFlag, IN PWCHAR Format, IN PWCHAR Label, IN BOOLEAN QuickFormat, IN ULONG ClusterSize, IN PFMIFSCALLBACK Callback) { PIFS_PROVIDER Provider; UNICODE_STRING usDriveRoot; UNICODE_STRING usLabel; BOOLEAN Argument = FALSE; WCHAR VolumeName[MAX_PATH]; //CURDIR CurDir; Provider = GetProvider(Format); if (!Provider) { /* Unknown file system */ Callback(DONE, 0, &Argument); return; } #if 1 DPRINT1("Warning: use GetVolumeNameForVolumeMountPointW() instead!\n"); swprintf(VolumeName, L"\\??\\%c:", towupper(DriveRoot[0])); RtlCreateUnicodeString(&usDriveRoot, VolumeName); /* Code disabled as long as our storage stack doesn't understand IOCTL_MOUNTDEV_QUERY_DEVICE_NAME */ #else if (!GetVolumeNameForVolumeMountPointW(DriveRoot, VolumeName, MAX_PATH) || !RtlDosPathNameToNtPathName_U(VolumeName, &usDriveRoot, NULL, &CurDir)) { /* Report an error. */ Callback(DONE, 0, &Argument); return; } #endif RtlInitUnicodeString(&usLabel, Label); DPRINT("FormatEx - %S\n", Format); Provider->FormatEx(&usDriveRoot, MediaFlag, &usLabel, QuickFormat, ClusterSize, Callback); RtlFreeUnicodeString(&usDriveRoot); }
nsresult sbWinVolumeIsReady(DEVINST aDevInst, PRBool* aIsReady) { // Validate arguments. NS_ENSURE_ARG_POINTER(aIsReady); // Function variables. PRBool success; nsresult rv; // Get the volume device interface path. GUID guid = GUID_DEVINTERFACE_VOLUME; nsAutoString volumeDeviceInterfacePath; rv = sbWinGetDevicePath(aDevInst, &guid, volumeDeviceInterfacePath); NS_ENSURE_SUCCESS(rv, rv); // Try getting the volume GUID path. If this fails, the volume is not ready. static const DWORD volumeGUIDPathLength = 51; WCHAR volumeGUIDPath[volumeGUIDPathLength]; volumeDeviceInterfacePath.AppendLiteral("\\"); success = GetVolumeNameForVolumeMountPointW(volumeDeviceInterfacePath.get(), volumeGUIDPath, volumeGUIDPathLength); if (!success) { *aIsReady = PR_FALSE; return NS_OK; } // Try getting the volume information. If this fails, the volume is not // ready. DWORD fileSystemFlags; WCHAR volumeLabel[MAX_PATH+1]; WCHAR fileSystemName[MAX_PATH+1]; success = GetVolumeInformationW(volumeDeviceInterfacePath.BeginReading(), volumeLabel, NS_ARRAY_LENGTH(volumeLabel), NULL, NULL, &fileSystemFlags, fileSystemName, NS_ARRAY_LENGTH(fileSystemName)); if (!success) { *aIsReady = PR_FALSE; return NS_OK; } // The volume is ready. *aIsReady = PR_TRUE; return NS_OK; }
static PyObject * winutil_get_removable_drives(PyObject *self, PyObject *args) { HDEVINFO hDevInfo; BOOL iterate = TRUE, ddebug = FALSE; PSP_DEVICE_INTERFACE_DETAIL_DATA interfaceDetailData; DWORD i; unsigned int j, length; WCHAR volume[BUFSIZE]; struct tagDrives g_drives[MAX_DRIVES]; PyObject *volumes, *key, *candidates, *pdebug = Py_False, *temp; if (!PyArg_ParseTuple(args, "|O", &pdebug)) { return NULL; } // Find all removable drives for (j = 0; j < MAX_DRIVES; j++) g_drives[j].letter = 0; if (!get_all_removable_disks(g_drives)) return NULL; volumes = PyDict_New(); if (volumes == NULL) return PyErr_NoMemory(); ddebug = PyObject_IsTrue(pdebug); hDevInfo = create_device_info_set((LPGUID)&GUID_DEVINTERFACE_VOLUME, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (hDevInfo == INVALID_HANDLE_VALUE) { Py_DECREF(volumes); return NULL; } // Enumerate through the set for (i=0; iterate; i++) { candidates = PyList_New(0); if (candidates == NULL) { Py_DECREF(volumes); return PyErr_NoMemory();} interfaceDetailData = get_device_ancestors(hDevInfo, i, candidates, &iterate, ddebug); if (interfaceDetailData == NULL) { PyErr_Print(); Py_DECREF(candidates); candidates = NULL; continue; } length = wcslen(interfaceDetailData->DevicePath); interfaceDetailData->DevicePath[length] = L'\\'; interfaceDetailData->DevicePath[length+1] = 0; if (ddebug) console_out(L"Device path: %s\n", interfaceDetailData->DevicePath); // On Vista+ DevicePath contains the information we need. temp = PyUnicode_FromWideChar(interfaceDetailData->DevicePath, length); if (temp == NULL) return PyErr_NoMemory(); PyList_Append(candidates, temp); Py_DECREF(temp); if(GetVolumeNameForVolumeMountPointW(interfaceDetailData->DevicePath, volume, BUFSIZE)) { if (ddebug) console_out(L"Volume: %s\n", volume); for(j = 0; j < MAX_DRIVES; j++) { if(g_drives[j].letter != 0 && wcscmp(g_drives[j].volume, volume)==0) { if (ddebug) printf("Found drive: %c\n", (char)g_drives[j].letter); fflush(stdout); key = PyBytes_FromFormat("%c", (char)g_drives[j].letter); if (key == NULL) return PyErr_NoMemory(); PyDict_SetItem(volumes, key, candidates); Py_DECREF(key); key = NULL; break; } } } Py_XDECREF(candidates); candidates = NULL; PyMem_Free(interfaceDetailData); } //for SetupDiDestroyDeviceInfoList(hDevInfo); return volumes; }
/* * @implemented */ BOOL WINAPI GetVolumePathNameW(IN LPCWSTR lpszFileName, IN LPWSTR lpszVolumePathName, IN DWORD cchBufferLength) { DWORD PathLength; UNICODE_STRING UnicodeFilePath; LPWSTR FilePart; PWSTR FullFilePath, FilePathName; ULONG PathSize; WCHAR VolumeName[MAX_PATH]; DWORD ErrorCode; BOOL Result = FALSE; if (!lpszFileName || !lpszVolumePathName || !cchBufferLength) { SetLastError(ERROR_INVALID_PARAMETER); return FALSE; } if (!(PathLength = GetFullPathNameW(lpszFileName, 0, NULL, NULL))) { return Result; } else { PathLength = PathLength + 10; PathSize = PathLength * sizeof(WCHAR); if (!(FullFilePath = RtlAllocateHeap(RtlGetProcessHeap(), 0, PathSize))) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return Result; } if (!GetFullPathNameW(lpszFileName, PathLength, FullFilePath, &FilePart)) { RtlFreeHeap(RtlGetProcessHeap(), 0, FullFilePath); return Result; } RtlInitUnicodeString(&UnicodeFilePath, FullFilePath); if (UnicodeFilePath.Buffer[UnicodeFilePath.Length / sizeof(WCHAR) - 1] != '\\') { UnicodeFilePath.Length += sizeof(WCHAR); UnicodeFilePath.Buffer[UnicodeFilePath.Length / sizeof(WCHAR) - 1] = '\\'; UnicodeFilePath.Buffer[UnicodeFilePath.Length / sizeof(WCHAR)] = '\0'; } if (!(FilePathName = RtlAllocateHeap(RtlGetProcessHeap(), 0, PathSize))) { RtlFreeHeap(RtlGetProcessHeap(), 0, FullFilePath); SetLastError(ERROR_NOT_ENOUGH_MEMORY); return Result; } while (!GetVolumeNameForVolumeMountPointW(UnicodeFilePath.Buffer, VolumeName, MAX_PATH)) { if (((UnicodeFilePath.Length == 4) && (UnicodeFilePath.Buffer[0] == '\\') && (UnicodeFilePath.Buffer[1] == '\\')) || ((UnicodeFilePath.Length == 6) && (UnicodeFilePath.Buffer[1] == ':'))) { break; } UnicodeFilePath.Length -= sizeof(WCHAR); UnicodeFilePath.Buffer[UnicodeFilePath.Length / sizeof(WCHAR)] = '\0'; memcpy(FilePathName, UnicodeFilePath.Buffer, UnicodeFilePath.Length); FilePathName[UnicodeFilePath.Length / sizeof(WCHAR)] = '\0'; if (!GetFullPathNameW(FilePathName, PathLength, FullFilePath, &FilePart)) { goto Cleanup2; } if (!FilePart) { RtlInitUnicodeString(&UnicodeFilePath, FullFilePath); UnicodeFilePath.Length += sizeof(WCHAR); UnicodeFilePath.Buffer[UnicodeFilePath.Length / sizeof(WCHAR) - 1] = '\\'; UnicodeFilePath.Buffer[UnicodeFilePath.Length / sizeof(WCHAR)] = '\0'; break; } FilePart[0] = '\0'; RtlInitUnicodeString(&UnicodeFilePath, FullFilePath); } } if (UnicodeFilePath.Length > (cchBufferLength * sizeof(WCHAR)) - sizeof(WCHAR)) { ErrorCode = ERROR_FILENAME_EXCED_RANGE; goto Cleanup1; } memcpy(lpszVolumePathName, UnicodeFilePath.Buffer, UnicodeFilePath.Length); lpszVolumePathName[UnicodeFilePath.Length / sizeof(WCHAR)] = '\0'; Result = TRUE; goto Cleanup2; Cleanup1: SetLastError(ErrorCode); Cleanup2: RtlFreeHeap(RtlGetProcessHeap(), 0, FullFilePath); RtlFreeHeap(RtlGetProcessHeap(), 0, FilePathName); return Result; }