static VOID GetDriveNameWithLetter(LPWSTR pwszText, UINT cchTextMax, LPCWSTR pwszDrive) { DWORD dwMaxComp, dwFileSys; SIZE_T cchText = 0; if (GetVolumeInformationW(pwszDrive, pwszText, cchTextMax, NULL, &dwMaxComp, &dwFileSys, NULL, 0)) { cchText = wcslen(pwszText); if (cchText == 0) { /* load default volume label */ cchText = LoadStringW(shell32_hInstance, IDS_DRIVE_FIXED, pwszText, cchTextMax); } } StringCchPrintfW(pwszText + cchText, cchTextMax - cchText, L" (%c)", pwszDrive[0]); }
/* * Return the drive letter and volume label * If the drive doesn't have a volume assigned, space is returned for the letter */ BOOL GetDriveLabel(DWORD DriveIndex, char* letters, char** label) { HANDLE hPhysical; DWORD size; char AutorunPath[] = "#:\\autorun.inf", *AutorunLabel = NULL; wchar_t wDrivePath[] = L"#:\\"; wchar_t wVolumeLabel[MAX_PATH+1]; static char VolumeLabel[MAX_PATH+1]; *label = STR_NO_LABEL; if (!GetDriveLetters(DriveIndex, letters)) return FALSE; if (letters[0] == 0) { // Drive without volume assigned - always enabled return TRUE; } // We only care about an autorun.inf if we have a single volume AutorunPath[0] = letters[0]; wDrivePath[0] = letters[0]; // Try to read an extended label from autorun first. Fallback to regular label if not found. // In the case of card readers with no card, users can get an annoying popup asking them // to insert media. Use IOCTL_STORAGE_CHECK_VERIFY to prevent this hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE); if (DeviceIoControl(hPhysical, IOCTL_STORAGE_CHECK_VERIFY, NULL, 0, NULL, 0, &size, NULL)) AutorunLabel = get_token_data_file("label", AutorunPath); else if (GetLastError() == ERROR_NOT_READY) uprintf("Ignoring autorun.inf label for drive %c: %s\n", letters[0], (HRESULT_CODE(GetLastError()) == ERROR_NOT_READY)?"No media":WindowsErrorString()); safe_closehandle(hPhysical); if (AutorunLabel != NULL) { uprintf("Using autorun.inf label for drive %c: '%s'\n", letters[0], AutorunLabel); safe_strcpy(VolumeLabel, sizeof(VolumeLabel), AutorunLabel); safe_free(AutorunLabel); *label = VolumeLabel; } else if (GetVolumeInformationW(wDrivePath, wVolumeLabel, ARRAYSIZE(wVolumeLabel), NULL, NULL, NULL, NULL, 0) && *wVolumeLabel) { wchar_to_utf8_no_alloc(wVolumeLabel, VolumeLabel, sizeof(VolumeLabel)); *label = VolumeLabel; } return TRUE; }
QString FileSystem::fileSystemForPath(const QString & path) { // See also QStorageInfo (Qt >=5.4) and GetVolumeInformationByHandleW (>= Vista) QString drive = path.left(3); if (! drive.endsWith(":\\")) return QString(); const size_t fileSystemBufferSize = 4096; TCHAR fileSystemBuffer[fileSystemBufferSize]; if (! GetVolumeInformationW( reinterpret_cast<LPCWSTR>(drive.utf16()), NULL, 0, NULL, NULL, NULL, fileSystemBuffer, fileSystemBufferSize)) { return QString(); } return QString::fromUtf16(reinterpret_cast<const ushort *>(fileSystemBuffer)); }
/** * Query the properties of a mounted filesystem. * * @returns iprt status code. * @param pszFsPath Path within the mounted filesystem. * @param pProperties Where to store the properties. */ RTR3DECL(int) RTFsQueryProperties(const char *pszFsPath, PRTFSPROPERTIES pProperties) { /* * Validate & get valid root path. */ AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER); AssertMsgReturn(VALID_PTR(pProperties), ("%p", pProperties), VERR_INVALID_PARAMETER); PRTUTF16 pwszFsRoot; int rc = rtFsGetRoot(pszFsPath, &pwszFsRoot); if (RT_FAILURE(rc)) return rc; /* * Do work. */ DWORD dwMaxName; DWORD dwFlags; DWORD dwSerial; if (GetVolumeInformationW(pwszFsRoot, NULL, 0, &dwSerial, &dwMaxName, &dwFlags, NULL, 0)) { memset(pProperties, 0, sizeof(*pProperties)); pProperties->cbMaxComponent = dwMaxName; pProperties->fFileCompression = !!(dwFlags & FILE_FILE_COMPRESSION); pProperties->fCompressed = !!(dwFlags & FILE_VOLUME_IS_COMPRESSED); pProperties->fReadOnly = !!(dwFlags & FILE_READ_ONLY_VOLUME); pProperties->fSupportsUnicode = !!(dwFlags & FILE_UNICODE_ON_DISK); pProperties->fCaseSensitive = false; /* win32 is case preserving only */ /** @todo r=bird: What about FILE_CASE_SENSITIVE_SEARCH ? Is this set for NTFS * as well perchance? If so, better mention it instead of just setting * fCaseSensitive to false. */ pProperties->fRemote = false; /* no idea yet */ } else { DWORD Err = GetLastError(); rc = RTErrConvertFromWin32(Err); Log(("RTFsQuerySizes(%s,): GetVolumeInformation failed with lasterr %d (%Rrc)\n", pszFsPath, Err, rc)); } RTUtf16Free(pwszFsRoot); return rc; }
VDStringW VDGetRootVolumeLabel(const wchar_t *rootPath) { union { char a[MAX_PATH * 2]; wchar_t w[MAX_PATH]; } buf; DWORD maxComponentLength; DWORD fsFlags; VDStringW name; if (VDIsWindowsNT()) { if (GetVolumeInformationW(rootPath, buf.w, vdcountof(buf.w), NULL, &maxComponentLength, &fsFlags, NULL, 0)) name = buf.w; } else { if (GetVolumeInformationA(VDTextWToA(rootPath).c_str(), buf.a, vdcountof(buf.a), NULL, &maxComponentLength, &fsFlags, NULL, 0)) name = VDTextAToW(buf.a); } return name; }
/* * Return the drive letter and volume label */ BOOL GetDriveLabel(DWORD DriveIndex, char* letter, char** label) { HANDLE hDrive, hPhysical; DWORD size; char AutorunPath[] = "#:\\autorun.inf", *AutorunLabel = NULL; wchar_t wDrivePath[] = L"#:\\"; wchar_t wVolumeLabel[MAX_PATH+1]; static char VolumeLabel[MAX_PATH+1]; *label = STR_NO_LABEL; hDrive = GetDriveHandle(DriveIndex, letter, FALSE, FALSE); if (hDrive == INVALID_HANDLE_VALUE) return FALSE; safe_closehandle(hDrive); AutorunPath[0] = *letter; wDrivePath[0] = *letter; // Try to read an extended label from autorun first. Fallback to regular label if not found. // In the case of card readers with no card, users can get an annoying popup asking them // to insert media. Use IOCTL_STORAGE_CHECK_VERIFY to prevent this hPhysical = GetDriveHandle(DriveIndex, NULL, FALSE, FALSE); if (DeviceIoControl(hPhysical, IOCTL_STORAGE_CHECK_VERIFY, NULL, 0, NULL, 0, &size, NULL)) AutorunLabel = get_token_data_file("label", AutorunPath); else if (GetLastError() == ERROR_NOT_READY) uprintf("Ignoring autorun.inf label for drive %c: %s\n", *letter, (HRESULT_CODE(GetLastError()) == ERROR_NOT_READY)?"No media":WindowsErrorString()); safe_closehandle(hPhysical); if (AutorunLabel != NULL) { uprintf("Using autorun.inf label for drive %c: '%s'\n", *letter, AutorunLabel); strncpy(VolumeLabel, AutorunLabel, sizeof(VolumeLabel)); safe_free(AutorunLabel); *label = VolumeLabel; } else if (GetVolumeInformationW(wDrivePath, wVolumeLabel, sizeof(wVolumeLabel), NULL, NULL, NULL, NULL, 0) && *wVolumeLabel) { wchar_to_utf8_no_alloc(wVolumeLabel, VolumeLabel, sizeof(VolumeLabel)); *label = VolumeLabel; } return TRUE; }
static VOID ChkDskNow(HWND hwndDlg, LPCWSTR pwszDrive) { //DWORD ClusterSize = 0; WCHAR wszFs[30]; ULARGE_INTEGER TotalNumberOfFreeBytes, FreeBytesAvailableUser; BOOLEAN bCorrectErrors = FALSE, bScanDrive = FALSE; if(!GetVolumeInformationW(pwszDrive, NULL, 0, NULL, NULL, NULL, wszFs, _countof(wszFs))) { FIXME("failed to get drive fs type\n"); return; } if (!GetDiskFreeSpaceExW(pwszDrive, &FreeBytesAvailableUser, &TotalNumberOfFreeBytes, NULL)) { FIXME("failed to get drive space type\n"); return; } /*if (!GetDefaultClusterSize(wszFs, &ClusterSize, &TotalNumberOfFreeBytes)) { FIXME("invalid cluster size\n"); return; }*/ if (SendDlgItemMessageW(hwndDlg, 14000, BM_GETCHECK, 0, 0) == BST_CHECKED) bCorrectErrors = TRUE; if (SendDlgItemMessageW(hwndDlg, 14001, BM_GETCHECK, 0, 0) == BST_CHECKED) bScanDrive = TRUE; hChkdskDrvDialog = hwndDlg; bChkdskSuccess = FALSE; SendDlgItemMessageW(hwndDlg, 14002, PBM_SETRANGE, 0, MAKELPARAM(0, 100)); Chkdsk((LPWSTR)pwszDrive, (LPWSTR)wszFs, bCorrectErrors, TRUE, FALSE, bScanDrive, NULL, NULL, ChkdskCallback); // FIXME: casts hChkdskDrvDialog = NULL; bChkdskSuccess = FALSE; }
Bool File_DoesVolumeSupportAcls(const char *path) // IN: { Bool succeeded = FALSE; #if defined(_WIN32) Bool res; char *vol; char *vol2; const utf16_t *vol2W; DWORD fsFlags; ASSERT(path); File_SplitName(path, &vol, NULL, NULL); vol2 = Unicode_Append(vol, DIRSEPS); vol2W = UNICODE_GET_UTF16(vol2); res = GetVolumeInformationW(vol2W, NULL, 0, NULL, NULL, &fsFlags, NULL, 0); UNICODE_RELEASE_UTF16(vol2W); if (res) { if ((fsFlags & FS_PERSISTENT_ACLS) == 0) { goto exit; } } else { Log(LGPFX" %s: GetVolumeInformation failed: %d\n", __FUNCTION__, GetLastError()); goto exit; } succeeded = TRUE; exit: free(vol); free(vol2); #endif return succeeded; }
int IsFileSystemOldFATW(wchar_t *dir) { static wchar_t lastDrive = (wchar_t)'\0'; /* cached drive of last GetVolumeInformation call */ static int lastDriveOldFAT = 0; /* cached OldFAT value of last GetVolumeInformation call */ wchar_t root[4]; DWORD vfnsize; DWORD vfsflags; /* * We separate FAT and HPFS+other file systems here. * I consider other systems to be similar to HPFS/NTFS, i.e. * support for long file names and being case sensitive to some extent. */ wcsncpy(root, dir, 3); if ( iswalpha(root[0]) && (root[1] == (wchar_t)':') ) { root[0] = towupper(dir[0]); root[2] = (wchar_t)'\\'; root[3] = 0; } else { root[0] = (wchar_t)'\\'; root[1] = 0; } if (lastDrive == root[0]) { return lastDriveOldFAT; } if ( !GetVolumeInformationW(root, NULL, 0, NULL, &vfnsize, &vfsflags, NULL, 0)) { fprintf(mesg, "zip diagnostic: GetVolumeInformation failed\n"); return(FALSE); } lastDrive = root[0]; lastDriveOldFAT = vfnsize <= 12; return lastDriveOldFAT; }
UINT WINAPI DiscoverRemoveableDrives(ULONGLONG limitSize) { UINT index = 0; wchar_t buffer[2048] = { 0 }; wchar_t drivePath[8] = { 0 }; wchar_t driveLetter[4] = { 0 }; DWORD iSub = 0; DWORD iLength= GetLogicalDriveStringsW(2048, buffer); for (iSub = 0; iSub < iLength; iSub += 4) { wcscpy_s(drivePath, buffer+iSub); if (GetDriveTypeW(drivePath) == DRIVE_REMOVABLE&&GetVolumeInformationW(drivePath, NULL, 0, NULL, NULL, NULL, NULL, 0) == TRUE) { wcscpy_s(driveLetter, drivePath); (wcsrchr(driveLetter, _T(':')))[1] = 0; ULARGE_INTEGER lpFreeToCaller; ULARGE_INTEGER lpTotalSize; ULARGE_INTEGER lpFreeSize; if (GetDiskFreeSpaceExW(drivePath, &lpFreeToCaller, &lpTotalSize, &lpFreeSize) ==TRUE) { if (limitSize>0){ if (lpTotalSize.QuadPart < limitSize) continue; } swprintf_s(g_DriveList[index].sizeInfo, L"%s The total size of USB device: %4.1f GB |Free Space: %4.1f GB", drivePath, (float)(lpTotalSize.QuadPart) / (1024 * 1024 * 1024), (float)(lpFreeSize.QuadPart) / (1024 * 1024 * 1024)); wcscpy_s(g_DriveList[index].driveLetter, driveLetter); wcscpy_s(g_DriveList[index].drivePath, drivePath); index++; } } } return index; }
nsresult sbWinGetVolumeLabel(const nsAString& aVolumeMountPath, nsACString& aVolumeLabel) { BOOL success; // Get the volume label. WCHAR volumeLabel[MAX_PATH+1]; WCHAR fileSystemName[MAX_PATH+1]; success = GetVolumeInformationW(aVolumeMountPath.BeginReading(), volumeLabel, NS_ARRAY_LENGTH(volumeLabel), NULL, NULL, NULL, fileSystemName, NS_ARRAY_LENGTH(fileSystemName)); NS_ENSURE_TRUE(success, NS_ERROR_FAILURE); // Return results. aVolumeLabel.Assign(NS_ConvertUTF16toUTF8(volumeLabel)); return NS_OK; }
static int statfs (const unsigned char *path, struct statfs *buf) { HINSTANCE h; FARPROC f; int retval = 0; WCHAR tmp [MAX_PATH], resolved_path [MAX_PATH]; WCHAR * wpath = (WCHAR *)utf8_to_utf16(path); realpath(wpath, resolved_path); free(wpath); if (!resolved_path) retval = - 1; else { /* check whether GetDiskFreeSpaceExA is supported */ h = LoadLibraryA ("kernel32.dll"); if (h) f = GetProcAddress (h, "GetDiskFreeSpaceExW"); else f = NULL; if (f) { ULARGE_INTEGER bytes_free, bytes_total, bytes_free2; if (!f (resolved_path, &bytes_free2, &bytes_total, &bytes_free)) { errno = ENOENT; retval = - 1; } else { buf -> f_bsize = FAKED_BLOCK_SIZE; buf -> f_bfree = (bytes_free.QuadPart) / FAKED_BLOCK_SIZE; buf -> f_files = buf -> f_blocks = (bytes_total.QuadPart) / FAKED_BLOCK_SIZE; buf -> f_ffree = buf -> f_bavail = (bytes_free2.QuadPart) / FAKED_BLOCK_SIZE; } } else { DWORD sectors_per_cluster, bytes_per_sector; if (h) FreeLibrary (h); if (!GetDiskFreeSpaceW (resolved_path, §ors_per_cluster, &bytes_per_sector, &buf -> f_bavail, &buf -> f_blocks)) { errno = ENOENT; retval = - 1; } else { buf -> f_bsize = sectors_per_cluster * bytes_per_sector; buf -> f_files = buf -> f_blocks; buf -> f_ffree = buf -> f_bavail; buf -> f_bfree = buf -> f_bavail; } } if (h) FreeLibrary (h); } /* get the FS volume information */ if (wcsspn (L":", resolved_path) > 0) resolved_path [3] = '\0'; /* we want only the root */ if (GetVolumeInformationW (resolved_path, NULL, 0, &buf -> f_fsid, &buf -> f_namelen, NULL, tmp, MAX_PATH)) /* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getvolumeinformation.asp */ { if (_wcsicmp (L"NTFS", tmp) == 0) { buf -> f_type = NTFS_SUPER_MAGIC; } else { buf -> f_type = MSDOS_SUPER_MAGIC; } } else { errno = ENOENT; retval = - 1; } return retval; }
/* * @implemented */ BOOL WINAPI GetVolumeInformationA(IN LPCSTR lpRootPathName, IN LPSTR lpVolumeNameBuffer, IN DWORD nVolumeNameSize, OUT LPDWORD lpVolumeSerialNumber OPTIONAL, OUT LPDWORD lpMaximumComponentLength OPTIONAL, OUT LPDWORD lpFileSystemFlags OPTIONAL, OUT LPSTR lpFileSystemNameBuffer OPTIONAL, IN DWORD nFileSystemNameSize) { UNICODE_STRING FileSystemNameU; UNICODE_STRING VolumeNameU = { 0, 0, NULL }; ANSI_STRING VolumeName; ANSI_STRING FileSystemName; PWCHAR RootPathNameW; BOOL Result; if (!(RootPathNameW = FilenameA2W(lpRootPathName, FALSE))) return FALSE; if (lpVolumeNameBuffer) { VolumeNameU.MaximumLength = (USHORT)nVolumeNameSize * sizeof(WCHAR); VolumeNameU.Buffer = RtlAllocateHeap (RtlGetProcessHeap (), 0, VolumeNameU.MaximumLength); if (VolumeNameU.Buffer == NULL) { goto FailNoMem; } } if (lpFileSystemNameBuffer) { FileSystemNameU.Length = 0; FileSystemNameU.MaximumLength = (USHORT)nFileSystemNameSize * sizeof(WCHAR); FileSystemNameU.Buffer = RtlAllocateHeap (RtlGetProcessHeap (), 0, FileSystemNameU.MaximumLength); if (FileSystemNameU.Buffer == NULL) { if (VolumeNameU.Buffer != NULL) { RtlFreeHeap(RtlGetProcessHeap(), 0, VolumeNameU.Buffer); } FailNoMem: SetLastError(ERROR_NOT_ENOUGH_MEMORY); return FALSE; } } Result = GetVolumeInformationW (RootPathNameW, lpVolumeNameBuffer ? VolumeNameU.Buffer : NULL, nVolumeNameSize, lpVolumeSerialNumber, lpMaximumComponentLength, lpFileSystemFlags, lpFileSystemNameBuffer ? FileSystemNameU.Buffer : NULL, nFileSystemNameSize); if (Result) { if (lpVolumeNameBuffer) { VolumeNameU.Length = wcslen(VolumeNameU.Buffer) * sizeof(WCHAR); VolumeName.Length = 0; VolumeName.MaximumLength = (USHORT)nVolumeNameSize; VolumeName.Buffer = lpVolumeNameBuffer; } if (lpFileSystemNameBuffer) { FileSystemNameU.Length = wcslen(FileSystemNameU.Buffer) * sizeof(WCHAR); FileSystemName.Length = 0; FileSystemName.MaximumLength = (USHORT)nFileSystemNameSize; FileSystemName.Buffer = lpFileSystemNameBuffer; } /* convert unicode strings to ansi (or oem) */ if (bIsFileApiAnsi) { if (lpVolumeNameBuffer) { RtlUnicodeStringToAnsiString (&VolumeName, &VolumeNameU, FALSE); } if (lpFileSystemNameBuffer) { RtlUnicodeStringToAnsiString (&FileSystemName, &FileSystemNameU, FALSE); } } else { if (lpVolumeNameBuffer) { RtlUnicodeStringToOemString (&VolumeName, &VolumeNameU, FALSE); } if (lpFileSystemNameBuffer) { RtlUnicodeStringToOemString (&FileSystemName, &FileSystemNameU, FALSE); } } } if (lpVolumeNameBuffer) { RtlFreeHeap (RtlGetProcessHeap (), 0, VolumeNameU.Buffer); } if (lpFileSystemNameBuffer) { RtlFreeHeap (RtlGetProcessHeap (), 0, FileSystemNameU.Buffer); } return Result; }
//---------------------------------------------------------------------- // // WMain // // Engine. Just get command line switches and fire off a format. This // could also be done in a GUI like Explorer does when you select a // drive and run a check on it. // // We do this in UNICODE because the chkdsk command expects PWCHAR // arguments. // //---------------------------------------------------------------------- int wmain( int argc, WCHAR *argv[] ) { int badArg; DWORD media; DWORD driveType; WCHAR fileSystem[1024]; WCHAR volumeName[1024]; WCHAR input[1024]; DWORD serialNumber; DWORD flags, maxComponent; ULARGE_INTEGER freeBytesAvailableToCaller, totalNumberOfBytes, totalNumberOfFreeBytes; _tprintf(L"\nFormatx v1.0 by Mark Russinovich\n"); _tprintf(L"Systems Internals - http://www.sysinternals.com\n\n"); // // Get function pointers // if( !LoadFMIFSEntryPoints()) { _tprintf(L"Could not located FMIFS entry points.\n\n"); return -1; } // // Parse command line // if( (badArg = ParseCommandLine( argc, argv ))) { _tprintf(L"Unknown argument: %s\n", argv[badArg] ); Usage(argv[0]); return -1; } // // Get the drive's format // if( !Drive ) { _tprintf(L"Required drive parameter is missing.\n\n"); Usage( argv[0] ); return -1; } else { wcscpy( RootDirectory, Drive ); } RootDirectory[2] = L'\\'; RootDirectory[3] = (WCHAR) 0; // // See if the drive is removable or not // driveType = GetDriveTypeW( RootDirectory ); if( driveType != DRIVE_FIXED ) { _tprintf(L"Insert a new floppy in drive %C:\nand press Enter when ready...", RootDirectory[0] ); fgetws( input, sizeof(input)/2, stdin ); media = FMIFS_FLOPPY; } // // Determine the drive's file system format // if( !GetVolumeInformationW( RootDirectory, volumeName, sizeof(volumeName)/2, &serialNumber, &maxComponent, &flags, fileSystem, sizeof(fileSystem)/2)) { PrintWin32Error( L"Could not query volume", GetLastError()); return -1; } if( !GetDiskFreeSpaceExW( RootDirectory, &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes )) { PrintWin32Error( L"Could not query volume size", GetLastError()); return -1; } _tprintf(L"The type of the file system is %s.\n", fileSystem ); // // Make sure they want to do this // if( driveType == DRIVE_FIXED ) { if( volumeName[0] ) { while(1 ) { _tprintf(L"Enter current volume label for drive %C: ", RootDirectory[0] ); fgetws( input, sizeof(input)/2, stdin ); input[ wcslen( input ) - 1] = 0; if( !wcsicmp( input, volumeName )) { break; } _tprintf(L"An incorrect volume label was entered for this drive.\n"); } } while( 1 ) { _tprintf(L"\nWARNING, ALL DATA ON NON_REMOVABLE DISK\n"); _tprintf(L"DRIVE %C: WILL BE LOST!\n", RootDirectory[0] ); _tprintf(L"Proceed with Format (Y/N)? " ); fgetws( input, sizeof(input)/2, stdin ); if( input[0] == L'Y' || input[0] == L'y' ) break; if( input[0] == L'N' || input[0] == L'n' ) { _tprintf(L"\n"); return 0; } } media = FMIFS_HARDDISK; } // // Tell the user we're doing a long format if appropriate // if( !QuickFormat ) { if( totalNumberOfBytes.QuadPart > 1024*1024*10 ) { _tprintf(L"Verifying %dM\n", (DWORD) (totalNumberOfBytes.QuadPart/(1024*1024))); } else { _tprintf(L"Verifying %.1fM\n", ((float)(LONGLONG)totalNumberOfBytes.QuadPart)/(float)(1024.0*1024.0)); } } else { if( totalNumberOfBytes.QuadPart > 1024*1024*10 ) { _tprintf(L"QuickFormatting %dM\n", (DWORD) (totalNumberOfBytes.QuadPart/(1024*1024))); } else { _tprintf(L"QuickFormatting %.2fM\n", ((float)(LONGLONG)totalNumberOfBytes.QuadPart)/(float)(1024.0*1024.0)); } _tprintf(L"Creating file system structures.\n"); } // // Format away! // FormatEx( RootDirectory, media, Format, Label, QuickFormat, ClusterSize, FormatExCallback ); if( Error ) return -1; _tprintf(L"Format complete.\n"); // // Enable compression if desired // if( CompressDrive ) { if( !EnableVolumeCompression( RootDirectory, TRUE )) { _tprintf(L"Volume does not support compression.\n"); } } // // Get the label if we don't have it // if( !GotALabel ) { _tprintf(L"Volume Label (11 characters, Enter for none)? " ); fgetws( input, sizeof(LabelString)/2, stdin ); input[ wcslen(input)-1] = 0; if( !SetVolumeLabelW( RootDirectory, input )) { PrintWin32Error(L"Could not label volume", GetLastError()); return -1; } } if( !GetVolumeInformationW( RootDirectory, volumeName, sizeof(volumeName)/2, &serialNumber, &maxComponent, &flags, fileSystem, sizeof(fileSystem)/2)) { PrintWin32Error( L"Could not query volume", GetLastError()); return -1; } // // Print out some stuff including the formatted size // if( !GetDiskFreeSpaceExW( RootDirectory, &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes )) { PrintWin32Error( L"Could not query volume size", GetLastError()); return -1; } _tprintf(L"\n%I64d bytes total disk space.\n", totalNumberOfBytes.QuadPart ); _tprintf(L"%I64d bytes available on disk.\n", totalNumberOfFreeBytes.QuadPart ); // // Get the drive's serial number // if( !GetVolumeInformationW( RootDirectory, volumeName, sizeof(volumeName)/2, &serialNumber, &maxComponent, &flags, fileSystem, sizeof(fileSystem)/2)) { PrintWin32Error( L"Could not query volume", GetLastError()); return -1; } _tprintf(L"\nVolume Serial Number is %04X-%04X\n", serialNumber >> 16, serialNumber & 0xFFFF ); return 0; }
/* * File system functions */ JNIEXPORT void JNICALL Java_net_rubygrapefruit_platform_internal_jni_PosixFileSystemFunctions_listFileSystems(JNIEnv *env, jclass target, jobject info, jobject result) { wchar_t* volumeName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1)); jclass info_class = env->GetObjectClass(info); jmethodID method = env->GetMethodID(info_class, "add", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V"); HANDLE handle = FindFirstVolumeW(volumeName, MAX_PATH+1); if (handle == INVALID_HANDLE_VALUE) { free(volumeName); mark_failed_with_errno(env, "could not find first volume", result); return; } wchar_t* deviceName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1)); wchar_t* pathNames = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1)); wchar_t* fsName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1)); while(true) { // Chop off the trailing '\' size_t len = wcslen(volumeName); if (len < 5) { mark_failed_with_message(env, "volume name is too short", result); break; } volumeName[len-1] = L'\0'; if (QueryDosDeviceW(&volumeName[4], deviceName, MAX_PATH+1) == 0) { mark_failed_with_errno(env, "could not query dos device", result); break; } volumeName[len-1] = L'\\'; DWORD used; if (GetVolumePathNamesForVolumeNameW(volumeName, pathNames, MAX_PATH+1, &used) == 0) { // TODO - try again if the buffer is too small mark_failed_with_errno(env, "could not query volume paths", result); break; } wchar_t* cur = pathNames; if (cur[0] != L'\0') { if(GetVolumeInformationW(cur, NULL, 0, NULL, NULL, NULL, fsName, MAX_PATH+1) == 0) { if (GetLastError() != ERROR_NOT_READY) { mark_failed_with_errno(env, "could not query volume information", result); break; } wcscpy(fsName, L"unknown"); } for (;cur[0] != L'\0'; cur += wcslen(cur) + 1) { env->CallVoidMethod(info, method, env->NewString((jchar*)deviceName, wcslen(deviceName)), env->NewString((jchar*)fsName, wcslen(fsName)), env->NewString((jchar*)cur, wcslen(cur)), JNI_FALSE); } } if (FindNextVolumeW(handle, volumeName, MAX_PATH) == 0) { if (GetLastError() != ERROR_NO_MORE_FILES) { mark_failed_with_errno(env, "could find next volume", result); } break; } } free(volumeName); free(deviceName); free(pathNames); free(fsName); FindVolumeClose(handle); }
uint32_t path_from_handle(HANDLE handle, wchar_t *path, uint32_t path_buffer_len) { static NTSTATUS (WINAPI *pNtQueryVolumeInformationFile)( _In_ HANDLE FileHandle, _Out_ PIO_STATUS_BLOCK IoStatusBlock, _Out_ PVOID FsInformation, _In_ ULONG Length, _In_ FS_INFORMATION_CLASS FsInformationClass ); if(pNtQueryVolumeInformationFile == NULL) { *(FARPROC *) &pNtQueryVolumeInformationFile = GetProcAddress( GetModuleHandle("ntdll"), "NtQueryVolumeInformationFile"); } static NTSTATUS (WINAPI *pNtQueryInformationFile)( _In_ HANDLE FileHandle, _Out_ PIO_STATUS_BLOCK IoStatusBlock, _Out_ PVOID FileInformation, _In_ ULONG Length, _In_ FILE_INFORMATION_CLASS FileInformationClass ); if(pNtQueryInformationFile == NULL) { *(FARPROC *) &pNtQueryInformationFile = GetProcAddress( GetModuleHandle("ntdll"), "NtQueryInformationFile"); } IO_STATUS_BLOCK status = {}; FILE_FS_VOLUME_INFORMATION volume_information; unsigned char buf[FILE_NAME_INFORMATION_REQUIRED_SIZE]; FILE_NAME_INFORMATION *name_information = (FILE_NAME_INFORMATION *) buf; // get the volume serial number of the directory handle if(NT_SUCCESS(pNtQueryVolumeInformationFile(handle, &status, &volume_information, sizeof(volume_information), FileFsVolumeInformation)) == 0) { return 0; } unsigned long serial_number; // enumerate all harddisks in order to find the // corresponding serial number wcscpy(path, L"?:\\"); for (path[0] = 'A'; path[0] <= 'Z'; path[0]++) { if(GetVolumeInformationW(path, NULL, 0, &serial_number, NULL, NULL, NULL, 0) == 0 || serial_number != volume_information.VolumeSerialNumber) { continue; } // obtain the relative path for this filename on the given harddisk if(NT_SUCCESS(pNtQueryInformationFile(handle, &status, name_information, FILE_NAME_INFORMATION_REQUIRED_SIZE, FileNameInformation))) { uint32_t length = name_information->FileNameLength / sizeof(wchar_t); // NtQueryInformationFile omits the "C:" part in a // filename, apparently wcsncpy(path + 2, name_information->FileName, path_buffer_len - 2); return length + 2 < path_buffer_len ? length + 2 : path_buffer_len - 1; } } return 0; }
QString Utils::filesystemName(QString path) { QString name; #if defined(Q_OS_WIN32) wchar_t volname[MAX_PATH+1]; bool res = GetVolumeInformationW((LPTSTR)path.utf16(), volname, MAX_PATH+1, NULL, NULL, NULL, NULL, 0); if(res) { name = QString::fromWCharArray(volname); } #endif #if defined(Q_OS_MACX) // BSD label does not include folder. QString bsd = Utils::resolveDevicename(path).remove("/dev/"); if(bsd.isEmpty()) { return name; } OSStatus result; ItemCount index = 1; do { FSVolumeRefNum volrefnum; HFSUniStr255 volname; result = FSGetVolumeInfo(kFSInvalidVolumeRefNum, index, &volrefnum, kFSVolInfoFSInfo, NULL, &volname, NULL); if(result == noErr) { GetVolParmsInfoBuffer volparms; /* PBHGetVolParmsSync() is not available for 64bit while FSGetVolumeParms() is available in 10.5+. Thus we need to use PBHGetVolParmsSync() for 10.4, and that also requires 10.4 to always use 32bit. Qt 4 supports 32bit on 10.6 Cocoa only. */ #if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050 if(FSGetVolumeParms(volrefnum, &volparms, sizeof(volparms)) == noErr) #else HParamBlockRec hpb; hpb.ioParam.ioNamePtr = NULL; hpb.ioParam.ioVRefNum = volrefnum; hpb.ioParam.ioBuffer = (Ptr)&volparms; hpb.ioParam.ioReqCount = sizeof(volparms); if(PBHGetVolParmsSync(&hpb) == noErr) #endif { if(volparms.vMServerAdr == 0) { if(bsd == (char*)volparms.vMDeviceID) { name = QString::fromUtf16((const ushort*)volname.unicode, (int)volname.length); break; } } } } index++; } while(result == noErr); #endif LOG_INFO() << "Volume name of" << path << "is" << name; return name; }
static void InitializeGeneralDriveDialog(HWND hwndDlg, WCHAR * szDrive) { WCHAR szVolumeName[MAX_PATH+1] = {0}; DWORD MaxComponentLength = 0; DWORD FileSystemFlags = 0; WCHAR FileSystemName[MAX_PATH+1] = {0}; WCHAR szFormat[50]; WCHAR szBuffer[128]; BOOL ret; UINT DriveType; ULARGE_INTEGER FreeBytesAvailable; LARGE_INTEGER TotalNumberOfFreeBytes; LARGE_INTEGER TotalNumberOfBytes; ret = GetVolumeInformationW(szDrive, szVolumeName, MAX_PATH+1, NULL, &MaxComponentLength, &FileSystemFlags, FileSystemName, MAX_PATH+1); if (ret) { /* set volume label */ SendDlgItemMessageW(hwndDlg, 14000, WM_SETTEXT, (WPARAM)NULL, (LPARAM)szVolumeName); /* set filesystem type */ SendDlgItemMessageW(hwndDlg, 14002, WM_SETTEXT, (WPARAM)NULL, (LPARAM)FileSystemName); } DriveType = GetDriveTypeW(szDrive); if (DriveType == DRIVE_FIXED || DriveType == DRIVE_CDROM) { if(GetDiskFreeSpaceExW(szDrive, &FreeBytesAvailable, (PULARGE_INTEGER)&TotalNumberOfBytes, (PULARGE_INTEGER)&TotalNumberOfFreeBytes)) { WCHAR szResult[128]; LONGLONG Result; HANDLE hVolume; DWORD BytesReturned = 0; swprintf(szResult, L"\\\\.\\%c:", towupper(szDrive[0])); hVolume = CreateFileW(szResult, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); if (hVolume != INVALID_HANDLE_VALUE) { ret = DeviceIoControl(hVolume, IOCTL_DISK_GET_LENGTH_INFO, NULL, 0, (LPVOID)&TotalNumberOfBytes, sizeof(ULARGE_INTEGER), &BytesReturned, NULL); if (ret && StrFormatByteSizeW(TotalNumberOfBytes.QuadPart, szResult, sizeof(szResult) / sizeof(WCHAR))) SendDlgItemMessageW(hwndDlg, 14007, WM_SETTEXT, (WPARAM)NULL, (LPARAM)szResult); CloseHandle(hVolume); } TRACE("szResult %s hVOlume %p ret %d LengthInformation %ul Bytesreturned %d\n", debugstr_w(szResult), hVolume, ret, TotalNumberOfBytes.QuadPart, BytesReturned); if (StrFormatByteSizeW(TotalNumberOfBytes.QuadPart - FreeBytesAvailable.QuadPart, szResult, sizeof(szResult) / sizeof(WCHAR))) SendDlgItemMessageW(hwndDlg, 14003, WM_SETTEXT, (WPARAM)NULL, (LPARAM)szResult); if (StrFormatByteSizeW(FreeBytesAvailable.QuadPart, szResult, sizeof(szResult) / sizeof(WCHAR))) SendDlgItemMessageW(hwndDlg, 14005, WM_SETTEXT, (WPARAM)NULL, (LPARAM)szResult); Result = GetFreeBytesShare(TotalNumberOfFreeBytes.QuadPart, TotalNumberOfBytes.QuadPart); /* set free bytes percentage */ swprintf(szResult, L"%02d%%", Result); SendDlgItemMessageW(hwndDlg, 14006, WM_SETTEXT, (WPARAM)0, (LPARAM)szResult); /* store used share amount */ Result = 100 - Result; swprintf(szResult, L"%02d%%", Result); SendDlgItemMessageW(hwndDlg, 14004, WM_SETTEXT, (WPARAM)0, (LPARAM)szResult); if (DriveType == DRIVE_FIXED) { if (LoadStringW(shell32_hInstance, IDS_DRIVE_FIXED, szBuffer, sizeof(szBuffer) / sizeof(WCHAR))) SendDlgItemMessageW(hwndDlg, 14001, WM_SETTEXT, (WPARAM)0, (LPARAM)szBuffer); } else /* DriveType == DRIVE_CDROM) */ { if (LoadStringW(shell32_hInstance, IDS_DRIVE_CDROM, szBuffer, sizeof(szBuffer) / sizeof(WCHAR))) SendDlgItemMessageW(hwndDlg, 14001, WM_SETTEXT, (WPARAM)0, (LPARAM)szBuffer); } } } /* set drive description */ SendDlgItemMessageW(hwndDlg, 14009, WM_GETTEXT, (WPARAM)50, (LPARAM)szFormat); swprintf(szBuffer, szFormat, szDrive); SendDlgItemMessageW(hwndDlg, 14009, WM_SETTEXT, (WPARAM)NULL, (LPARAM)szBuffer); }
static VOID Test(PWSTR Prefix) { static PWSTR Sddl = L"D:P(A;;GA;;;SY)(A;;GA;;;BA)(A;;GA;;;WD)"; static const GUID ReparseGuid = { 0x2cf25cfa, 0x41af, 0x4796, { 0xb5, 0xef, 0xac, 0xa3, 0x85, 0x3, 0xe2, 0xd8 } }; WCHAR FileName[1024], VolumeName[MAX_PATH]; PSECURITY_DESCRIPTOR SecurityDescriptor; HANDLE Handle; BOOL Success; UINT8 RdBuffer[4096], WrBuffer[4096]; REPARSE_GUID_DATA_BUFFER ReparseDataBuf; DWORD BytesTransferred, Offset; WIN32_FIND_DATAW FindData; WIN32_FIND_STREAM_DATA FindStreamData; memset(WrBuffer, 'B', sizeof WrBuffer); Success = ConvertStringSecurityDescriptorToSecurityDescriptorW( Sddl, SDDL_REVISION_1, &SecurityDescriptor, 0); ASSERT(Success); wsprintfW(FileName, L"%s\\", Prefix); Success = GetVolumeInformationW(FileName, VolumeName, MAX_PATH, 0, 0, 0, 0, 0); ASSERT(Success); wsprintfW(FileName, L"%s\\", Prefix); Success = SetVolumeLabelW(FileName, VolumeName); //ASSERT(Success); wsprintfW(FileName, L"%s\\fscrash", Prefix); Success = CreateDirectoryW(FileName, 0); ASSERT(Success); wsprintfW(FileName, L"%s\\fscrash\\file0", Prefix); Handle = CreateFileW(FileName, GENERIC_ALL, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); ASSERT(INVALID_HANDLE_VALUE != Handle); Success = CloseHandle(Handle); ASSERT(Success); wsprintfW(FileName, L"%s\\fscrash\\file0", Prefix); Handle = CreateFileW(FileName, GENERIC_ALL, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); ASSERT(INVALID_HANDLE_VALUE != Handle); Success = WriteFile(Handle, WrBuffer, sizeof WrBuffer, &BytesTransferred, 0); ASSERT(Success); ASSERT(sizeof WrBuffer == BytesTransferred); Success = FlushFileBuffers(Handle); ASSERT(Success); Offset = SetFilePointer(Handle, 0, 0, FILE_BEGIN); ASSERT(0 == Offset); Success = ReadFile(Handle, RdBuffer, sizeof RdBuffer, &BytesTransferred, 0); ASSERT(Success); ASSERT(sizeof WrBuffer == BytesTransferred); Offset = SetFilePointer(Handle, 0, 0, FILE_BEGIN); ASSERT(0 == Offset); Success = SetEndOfFile(Handle); ASSERT(Success); Offset = GetFileSize(Handle, 0); ASSERT(0 == Offset); Success = LockFile(Handle, 0, 0, 1, 0); ASSERT(Success); Success = UnlockFile(Handle, 0, 0, 1, 0); ASSERT(Success); Success = SetKernelObjectSecurity(Handle, DACL_SECURITY_INFORMATION, SecurityDescriptor); ASSERT(Success); Success = GetKernelObjectSecurity(Handle, DACL_SECURITY_INFORMATION, 0, 0, &BytesTransferred); ASSERT(!Success); ASSERT(ERROR_INSUFFICIENT_BUFFER == GetLastError()); ReparseDataBuf.ReparseTag = 0x1234; ReparseDataBuf.ReparseDataLength = 0; ReparseDataBuf.Reserved = 0; memcpy(&ReparseDataBuf.ReparseGuid, &ReparseGuid, sizeof ReparseGuid); Success = DeviceIoControl(Handle, FSCTL_SET_REPARSE_POINT, &ReparseDataBuf, REPARSE_GUID_DATA_BUFFER_HEADER_SIZE + ReparseDataBuf.ReparseDataLength, 0, 0, &BytesTransferred, 0); ASSERT(Success); Success = CloseHandle(Handle); ASSERT(Success); wsprintfW(FileName, L"%s\\fscrash\\*", Prefix); Handle = FindFirstFileW(FileName, &FindData); ASSERT(INVALID_HANDLE_VALUE != Handle); do { } while (FindNextFileW(Handle, &FindData)); ASSERT(ERROR_NO_MORE_FILES == GetLastError()); Success = FindClose(Handle); ASSERT(Success); wsprintfW(FileName, L"%s\\fscrash\\file0", Prefix); Handle = FindFirstStreamW(FileName, FindStreamInfoStandard, &FindStreamData, 0); ASSERT(INVALID_HANDLE_VALUE != Handle); do { } while (FindNextStreamW(Handle, &FindStreamData)); ASSERT(ERROR_HANDLE_EOF == GetLastError()); Success = FindClose(Handle); ASSERT(Success); wsprintfW(FileName, L"%s\\fscrash\\file0", Prefix); Success = DeleteFileW(FileName); ASSERT(Success); wsprintfW(FileName, L"%s\\fscrash", Prefix); Success = RemoveDirectoryW(FileName); ASSERT(Success); LocalFree(SecurityDescriptor); }
JNIEXPORT void JNICALL Java_net_rubygrapefruit_platform_internal_jni_PosixFileSystemFunctions_listFileSystems(JNIEnv *env, jclass target, jobject info, jobject result) { jclass info_class = env->GetObjectClass(info); jmethodID method = env->GetMethodID(info_class, "add", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZZ)V"); DWORD required = GetLogicalDriveStringsW(0, NULL); if (required == 0) { mark_failed_with_errno(env, "could not determine logical drive buffer size", result); return; } wchar_t* buffer = (wchar_t*)malloc(sizeof(wchar_t) * (required + 1)); wchar_t* deviceName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH + 1)); wchar_t* fileSystemName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH + 1)); if (GetLogicalDriveStringsW(required, buffer) == 0) { mark_failed_with_errno(env, "could not determine logical drives", result); } else { wchar_t* cur = buffer; for (;cur[0] != L'\0'; cur += wcslen(cur) + 1) { DWORD type = GetDriveTypeW(cur); jboolean remote = type == DRIVE_REMOTE; // chop off trailing '\' size_t len = wcslen(cur); cur[len-1] = L'\0'; // create device name \\.\C: wchar_t devPath[7]; swprintf(devPath, 7, L"\\\\.\\%s", cur); if (QueryDosDeviceW(cur, deviceName, MAX_PATH+1) == 0) { mark_failed_with_errno(env, "could not map device for logical drive", result); break; } cur[len-1] = L'\\'; DWORD available = 1; if (!remote) { HANDLE hDevice = CreateFileW(devPath, // like "\\.\E:" FILE_READ_ATTRIBUTES, // read access to the attributes FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, // share mode NULL, OPEN_EXISTING, 0, NULL); if (hDevice != INVALID_HANDLE_VALUE) { DWORD cbBytesReturned; DWORD bSuccess = DeviceIoControl (hDevice, // device to be queried IOCTL_STORAGE_CHECK_VERIFY2, NULL, 0, // no input buffer NULL, 0, // no output buffer &cbBytesReturned, // # bytes returned (LPOVERLAPPED) NULL); // synchronous I/O if (!bSuccess) { available = 0; } CloseHandle(hDevice); } } jboolean casePreserving = JNI_TRUE; if (available) { DWORD flags; if (GetVolumeInformationW(cur, NULL, 0, NULL, NULL, &flags, fileSystemName, MAX_PATH+1) == 0) { mark_failed_with_errno(env, "could not get volume information", result); break; } casePreserving = (flags & FILE_CASE_PRESERVED_NAMES) != 0; } else { if (type == DRIVE_CDROM) { swprintf(fileSystemName, MAX_PATH+1, L"cdrom"); } else { swprintf(fileSystemName, MAX_PATH+1, L"unknown"); } } env->CallVoidMethod(info, method, wchar_to_java(env, cur, wcslen(cur), result), wchar_to_java(env, fileSystemName, wcslen(fileSystemName), result), wchar_to_java(env, deviceName, wcslen(deviceName), result), remote, JNI_FALSE, casePreserving); } } free(buffer); free(deviceName); free(fileSystemName); }
void WmdmLister::GuessDriveLetter(DeviceInfo* info) { qLog(Debug) << "Guessing drive letter for" << info->name_; // Windows XP puts the drive letter in brackets at the end of the name QRegExp drive_letter("\\(([A-Z]:)\\)$"); if (drive_letter.indexIn(info->name_) != -1) { qLog(Debug) << "Looks like an XP drive" << drive_letter.cap(1); CheckDriveLetter(info, drive_letter.cap(1)); return; } // Windows 7 sometimes has the drive letter as the whole name drive_letter = QRegExp("^([A-Z]:)\\\\$"); if (drive_letter.indexIn(info->name_) != -1) { qLog(Debug) << "Looks like a win7 drive" << drive_letter.cap(1); CheckDriveLetter(info, drive_letter.cap(1)); return; } // Otherwise Windows 7 uses the drive's DOS label as its whole name. // Let's enumerate all the volumes on the system and find one with that // label, then get its drive letter. Yay! wchar_t volume_name[MAX_PATH + 1]; HANDLE handle = FindFirstVolumeW(volume_name, MAX_PATH); forever { // QueryDosDeviceW doesn't allow a trailing backslash, so remove it. int length = wcslen(volume_name); volume_name[length - 1] = L'\0'; wchar_t device_name[MAX_PATH + 1]; QueryDosDeviceW(&volume_name[4], device_name, MAX_PATH); volume_name[length - 1] = L'\\'; // Don't do cd-roms or floppies if (QString::fromWCharArray(device_name).contains("HarddiskVolume")) { wchar_t volume_path[MAX_PATH + 1]; DWORD volume_path_length = MAX_PATH; GetVolumePathNamesForVolumeNameW( volume_name, volume_path, volume_path_length, &volume_path_length); if (wcslen(volume_path) == 3) { ScopedWCharArray name(QString(MAX_PATH + 1, '\0')); ScopedWCharArray type(QString(MAX_PATH + 1, '\0')); DWORD serial = 0; if (!GetVolumeInformationW(volume_path, name, MAX_PATH, &serial, NULL, NULL, type, MAX_PATH)) { qLog(Warning) << "Error getting volume information for" << QString::fromWCharArray(volume_path); } else { if (name.ToString() == info->name_ && name.characters() != 0) { // We found it! qLog(Debug) << "Looks like a win7 drive name" << QString::fromWCharArray(volume_path); if (CheckDriveLetter(info, QString::fromWCharArray(volume_path))) { info->device_name_ = QString::fromWCharArray(device_name); info->volume_name_ = QString::fromWCharArray(volume_name); } break; } } } } if (!FindNextVolumeW(handle, volume_name, MAX_PATH)) break; } FindVolumeClose(handle); }
//---------------------------------------------------------------------- // // WMain // // Engine. Just get command line switches and fire off a format. This // could also be done in a GUI like Explorer does when you select a // drive and run a check on it. // // We do this in UNICODE because the chkdsk command expects PWCHAR // arguments. // //---------------------------------------------------------------------- int wmain(int argc, WCHAR *argv[]) { int badArg; DWORD media = FMIFS_HARDDISK; DWORD driveType; WCHAR fileSystem[1024]; WCHAR volumeName[1024]; WCHAR input[1024]; DWORD serialNumber; DWORD flags, maxComponent; ULARGE_INTEGER freeBytesAvailableToCaller, totalNumberOfBytes, totalNumberOfFreeBytes; WCHAR szMsg[RC_STRING_MAX_SIZE]; wprintf(L"\n" L"Formatx v1.0 by Mark Russinovich\n" L"Systems Internals - http://www.sysinternals.com\n" L"ReactOS adaptation 1999 by Emanuele Aliberti\n\n"); #ifndef FMIFS_IMPORT_DLL // // Get function pointers // if (!LoadFMIFSEntryPoints()) { PrintResourceString(STRING_FMIFS_FAIL); return -1; } #endif // // Parse command line // badArg = ParseCommandLine(argc, argv); if (badArg) { PrintResourceString(STRING_UNKNOW_ARG, argv[badArg]); Usage(argv[0]); return -1; } // // Get the drive's format // if (!Drive) { PrintResourceString(STRING_DRIVE_PARM); Usage(argv[0]); return -1; } else { wcscpy(RootDirectory, Drive); } RootDirectory[2] = L'\\'; RootDirectory[3] = L'\0'; // // See if the drive is removable or not // driveType = GetDriveTypeW(RootDirectory); switch (driveType) { case DRIVE_UNKNOWN : LoadStringW(GetModuleHandle(NULL), STRING_ERROR_DRIVE_TYPE, szMsg, ARRAYSIZE(szMsg)); PrintWin32Error(szMsg, GetLastError()); return -1; case DRIVE_REMOTE: case DRIVE_CDROM: PrintResourceString(STRING_NO_SUPPORT); return -1; case DRIVE_NO_ROOT_DIR: LoadStringW(GetModuleHandle(NULL), STRING_NO_VOLUME, szMsg, ARRAYSIZE(szMsg)); PrintWin32Error(szMsg, GetLastError()); return -1; case DRIVE_REMOVABLE: PrintResourceString(STRING_INSERT_DISK, RootDirectory[0]); fgetws(input, ARRAYSIZE(input), stdin); media = FMIFS_FLOPPY; break; case DRIVE_FIXED: case DRIVE_RAMDISK: media = FMIFS_HARDDISK; break; } // Reject attempts to format the system drive { WCHAR path[MAX_PATH + 1]; UINT rc; rc = GetWindowsDirectoryW(path, MAX_PATH); if (rc == 0 || rc > MAX_PATH) // todo: Report "Unable to query system directory" return -1; if (towlower(path[0]) == towlower(Drive[0])) { // todo: report "Cannot format system drive" PrintResourceString(STRING_NO_SUPPORT); return -1; } } // // Determine the drive's file system format // if (!GetVolumeInformationW(RootDirectory, volumeName, ARRAYSIZE(volumeName), &serialNumber, &maxComponent, &flags, fileSystem, ARRAYSIZE(fileSystem))) { LoadStringW(GetModuleHandle(NULL), STRING_NO_VOLUME, szMsg, ARRAYSIZE(szMsg)); PrintWin32Error(szMsg, GetLastError()); return -1; } if (!GetDiskFreeSpaceExW(RootDirectory, &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes)) { LoadStringW(GetModuleHandle(NULL), STRING_NO_VOLUME_SIZE, szMsg, ARRAYSIZE(szMsg)); PrintWin32Error(szMsg, GetLastError()); return -1; } PrintResourceString(STRING_FILESYSTEM, fileSystem); // // Make sure they want to do this // if (driveType == DRIVE_FIXED) { if (volumeName[0]) { while (TRUE) { PrintResourceString(STRING_LABEL_NAME_EDIT, RootDirectory[0]); fgetws(input, ARRAYSIZE(input), stdin); input[wcslen(input) - 1] = 0; if (!wcsicmp(input, volumeName)) break; PrintResourceString(STRING_ERROR_LABEL); } } PrintResourceString(STRING_YN_FORMAT, RootDirectory[0]); LoadStringW(GetModuleHandle(NULL), STRING_YES_NO_FAQ, szMsg, ARRAYSIZE(szMsg)); while (TRUE) { fgetws(input, ARRAYSIZE(input), stdin); if (_wcsnicmp(&input[0], &szMsg[0], 1) == 0) break; if (_wcsnicmp(&input[0], &szMsg[1], 1) == 0) { wprintf(L"\n"); return 0; } } } // // Tell the user we're doing a long format if appropriate // if (!QuickFormat) { LoadStringW(GetModuleHandle(NULL), STRING_VERIFYING, szMsg, ARRAYSIZE(szMsg)); if (totalNumberOfBytes.QuadPart > 1024*1024*10) { PrintString(L"%s %luM\n", szMsg, (DWORD)(totalNumberOfBytes.QuadPart/(1024*1024))); } else { PrintString(L"%s %.1fM\n", szMsg, ((float)(LONGLONG)totalNumberOfBytes.QuadPart)/(float)(1024.0*1024.0)); } } else { LoadStringW(GetModuleHandle(NULL), STRING_FAST_FMT, szMsg, ARRAYSIZE(szMsg)); if (totalNumberOfBytes.QuadPart > 1024*1024*10) { PrintString(L"%s %luM\n", szMsg, (DWORD)(totalNumberOfBytes.QuadPart/(1024*1024))); } else { PrintString(L"%s %.2fM\n", szMsg, ((float)(LONGLONG)totalNumberOfBytes.QuadPart)/(float)(1024.0*1024.0)); } PrintResourceString(STRING_CREATE_FSYS); } // // Format away! // FormatEx(RootDirectory, media, FileSystem, Label, QuickFormat, ClusterSize, FormatExCallback); if (Error) return -1; PrintResourceString(STRING_FMT_COMPLETE); // // Enable compression if desired // if (CompressDrive) { if (!EnableVolumeCompression(RootDirectory, TRUE)) PrintResourceString(STRING_VOL_COMPRESS); } // // Get the label if we don't have it // if (!GotALabel) { PrintResourceString(STRING_ENTER_LABEL); fgetws(input, ARRAYSIZE(LabelString), stdin); input[wcslen(input) - 1] = 0; if (!SetVolumeLabelW(RootDirectory, input)) { LoadStringW(GetModuleHandle(NULL), STRING_NO_LABEL, szMsg, ARRAYSIZE(szMsg)); PrintWin32Error(szMsg, GetLastError()); return -1; } } if (!GetVolumeInformationW(RootDirectory, volumeName, ARRAYSIZE(volumeName), &serialNumber, &maxComponent, &flags, fileSystem, ARRAYSIZE(fileSystem))) { LoadStringW(GetModuleHandle(NULL), STRING_NO_VOLUME, szMsg, ARRAYSIZE(szMsg)); PrintWin32Error(szMsg, GetLastError()); return -1; } // // Print out some stuff including the formatted size // if (!GetDiskFreeSpaceExW(RootDirectory, &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes)) { LoadStringW(GetModuleHandle(NULL), STRING_NO_VOLUME_SIZE, szMsg, ARRAYSIZE(szMsg)); PrintWin32Error(szMsg, GetLastError()); return -1; } PrintResourceString(STRING_FREE_SPACE, totalNumberOfBytes.QuadPart, totalNumberOfFreeBytes.QuadPart); // // Get the drive's serial number // if (!GetVolumeInformationW(RootDirectory, volumeName, ARRAYSIZE(volumeName), &serialNumber, &maxComponent, &flags, fileSystem, ARRAYSIZE(fileSystem))) { LoadStringW(GetModuleHandle(NULL), STRING_NO_VOLUME, szMsg, ARRAYSIZE(szMsg)); PrintWin32Error(szMsg, GetLastError()); return -1; } PrintResourceString(STRING_SERIAL_NUMBER, (unsigned int)(serialNumber >> 16), (unsigned int)(serialNumber & 0xFFFF)); return 0; }
BOOL SH_ShowDriveProperties(WCHAR * drive, LPCITEMIDLIST pidlFolder, LPCITEMIDLIST * apidl) { HPSXA hpsx = NULL; HPROPSHEETPAGE hpsp[MAX_PROPERTY_SHEET_PAGE]; PROPSHEETHEADERW psh; BOOL ret; UINT i; WCHAR szName[MAX_PATH+6]; DWORD dwMaxComponent, dwFileSysFlags; IDataObject * pDataObj = NULL; UINT DriveType; ZeroMemory(&psh, sizeof(PROPSHEETHEADERW)); psh.dwSize = sizeof(PROPSHEETHEADERW); //psh.dwFlags = PSH_USECALLBACK | PSH_PROPTITLE; psh.hwndParent = NULL; psh.u2.nStartPage = 0; psh.u3.phpage = hpsp; if (GetVolumeInformationW(drive, szName, sizeof(szName)/sizeof(WCHAR), NULL, &dwMaxComponent, &dwFileSysFlags, NULL, 0)) { psh.pszCaption = szName; psh.dwFlags |= PSH_PROPTITLE; if (!wcslen(szName)) { /* FIXME * check if disk is a really a local hdd */ i = LoadStringW(shell32_hInstance, IDS_DRIVE_FIXED, szName, sizeof(szName)/sizeof(WCHAR)-6); if (i > 0 && i < (sizeof(szName)/sizeof(WCHAR)) - 6) { szName[i] = L' '; szName[i+1] = L'('; wcscpy(&szName[i+2], drive); szName[i+4] = L')'; szName[i+5] = L'\0'; } } } DriveType = GetDriveTypeW(drive); for (i = 0; i < DRIVE_PROPERTY_PAGES; i++) { if (PropPages[i].DriveType == (UINT)-1 || (PropPages[i].DriveType != (UINT)-1 && PropPages[i].DriveType == DriveType)) { HPROPSHEETPAGE hprop = SH_CreatePropertySheetPage(PropPages[i].resname, PropPages[i].dlgproc, (LPARAM)drive, NULL); if (hprop) { hpsp[psh.nPages] = hprop; psh.nPages++; } } } if (SHCreateDataObject(pidlFolder, 1, apidl, NULL, &IID_IDataObject, (void**)&pDataObj) == S_OK) { hpsx = SHCreatePropSheetExtArrayEx(HKEY_CLASSES_ROOT, L"Drive", MAX_PROPERTY_SHEET_PAGE-DRIVE_PROPERTY_PAGES, pDataObj); if (hpsx) { SHAddFromPropSheetExtArray(hpsx, (LPFNADDPROPSHEETPAGE)AddPropSheetPageProc, (LPARAM)&psh); } } ret = PropertySheetW(&psh); if (pDataObj) IDataObject_Release(pDataObj); if (hpsx) SHDestroyPropSheetExtArray(hpsx); if (ret < 0) return FALSE; else return TRUE; }
VOID CDrvDefExt::InitGeneralPage(HWND hwndDlg) { WCHAR wszVolumeName[MAX_PATH+1] = {0}; WCHAR wszFileSystem[MAX_PATH+1] = {0}; WCHAR wszBuf[128]; BOOL bRet; bRet = GetVolumeInformationW(m_wszDrive, wszVolumeName, _countof(wszVolumeName), NULL, NULL, NULL, wszFileSystem, _countof(wszFileSystem)); if (bRet) { /* Set volume label and filesystem */ SetDlgItemTextW(hwndDlg, 14000, wszVolumeName); SetDlgItemTextW(hwndDlg, 14002, wszFileSystem); } /* Set drive type and icon */ UINT DriveType = GetDriveTypeW(m_wszDrive); UINT IconId, TypeStrId = 0; switch (DriveType) { case DRIVE_CDROM: IconId = IDI_SHELL_CDROM; TypeStrId = IDS_DRIVE_CDROM; break; case DRIVE_REMOVABLE: IconId = IDI_SHELL_FLOPPY; break; case DRIVE_RAMDISK: IconId = IDI_SHELL_RAMDISK; break; default: IconId = IDI_SHELL_DRIVE; TypeStrId = IDS_DRIVE_FIXED; } HICON hIcon = (HICON)LoadImage(shell32_hInstance, MAKEINTRESOURCE(IconId), IMAGE_ICON, 32, 32, LR_SHARED); if (hIcon) SendDlgItemMessageW(hwndDlg, 14016, STM_SETICON, (WPARAM)hIcon, 0); if (TypeStrId && LoadStringW(shell32_hInstance, TypeStrId, wszBuf, _countof(wszBuf))) SetDlgItemTextW(hwndDlg, 14001, wszBuf); ULARGE_INTEGER FreeBytesAvailable, TotalNumberOfBytes; if(GetDiskFreeSpaceExW(m_wszDrive, &FreeBytesAvailable, &TotalNumberOfBytes, NULL)) { /* Init free space percentage used for drawing piechart */ m_FreeSpacePerc = (UINT)(FreeBytesAvailable.QuadPart * 100ull / TotalNumberOfBytes.QuadPart); /* Used space */ if (SH_FormatByteSize(TotalNumberOfBytes.QuadPart - FreeBytesAvailable.QuadPart, wszBuf, _countof(wszBuf))) SetDlgItemTextW(hwndDlg, 14003, wszBuf); if (StrFormatByteSizeW(TotalNumberOfBytes.QuadPart - FreeBytesAvailable.QuadPart, wszBuf, _countof(wszBuf))) SetDlgItemTextW(hwndDlg, 14004, wszBuf); /* Free space */ if (SH_FormatByteSize(FreeBytesAvailable.QuadPart, wszBuf, _countof(wszBuf))) SetDlgItemTextW(hwndDlg, 14005, wszBuf); if (StrFormatByteSizeW(FreeBytesAvailable.QuadPart, wszBuf, _countof(wszBuf))) SetDlgItemTextW(hwndDlg, 14006, wszBuf); /* Total space */ if (SH_FormatByteSize(TotalNumberOfBytes.QuadPart, wszBuf, _countof(wszBuf))) SetDlgItemTextW(hwndDlg, 14007, wszBuf); if (StrFormatByteSizeW(TotalNumberOfBytes.QuadPart, wszBuf, _countof(wszBuf))) SetDlgItemTextW(hwndDlg, 14008, wszBuf); } /* Set drive description */ WCHAR wszFormat[50]; GetDlgItemTextW(hwndDlg, 14009, wszFormat, _countof(wszFormat)); swprintf(wszBuf, wszFormat, m_wszDrive[0]); SetDlgItemTextW(hwndDlg, 14009, wszBuf); }
VOID InitializeFormatDriveDlg(HWND hwndDlg, PFORMAT_DRIVE_CONTEXT pContext) { WCHAR szText[120]; WCHAR szDrive[4] = { L'C', ':', '\\', 0 }; WCHAR szFs[30] = {0}; INT Length, TempLength; DWORD dwSerial, dwMaxComp, dwFileSys; ULARGE_INTEGER FreeBytesAvailableUser, TotalNumberOfBytes; DWORD dwIndex, dwDefault; UCHAR uMinor, uMajor; BOOLEAN Latest; HWND hDlgCtrl; Length = GetWindowTextW(hwndDlg, szText, sizeof(szText)/sizeof(WCHAR)); if (Length < 0) Length = 0; szDrive[0] = pContext->Drive + L'A'; if (GetVolumeInformationW(szDrive, &szText[Length+1], (sizeof(szText)/sizeof(WCHAR))- Length - 2, &dwSerial, &dwMaxComp, &dwFileSys, szFs, sizeof(szFs)/sizeof(WCHAR))) { szText[Length] = L' '; szText[(sizeof(szText)/sizeof(WCHAR))-1] = L'\0'; TempLength = wcslen(&szText[Length+1]); if (!TempLength) { /* load default volume label */ TempLength = LoadStringW(shell32_hInstance, IDS_DRIVE_FIXED, &szText[Length+1], (sizeof(szText)/sizeof(WCHAR))- Length - 2); } else { /* set volume label */ szText[(sizeof(szText)/sizeof(WCHAR))-1] = L'\0'; SendDlgItemMessageW(hwndDlg, 28679, WM_SETTEXT, 0, (LPARAM)&szText[Length+1]); } Length += TempLength + 1; } if (Length + 4 < (sizeof(szText)/sizeof(WCHAR))) { szText[Length] = L' '; szText[Length+1] = L'('; szText[Length+2] = szDrive[0]; szText[Length+3] = L')'; Length +=4; } if (Length < (sizeof(szText)/sizeof(WCHAR))) szText[Length] = L'\0'; else szText[(sizeof(szText)/sizeof(WCHAR))-1] = L'\0'; /* set window text */ SetWindowTextW(hwndDlg, szText); if (GetDiskFreeSpaceExW(szDrive, &FreeBytesAvailableUser, &TotalNumberOfBytes, NULL)) { if (StrFormatByteSizeW(TotalNumberOfBytes.QuadPart, szText, sizeof(szText)/sizeof(WCHAR))) { /* add drive capacity */ szText[(sizeof(szText)/sizeof(WCHAR))-1] = L'\0'; SendDlgItemMessageW(hwndDlg, 28673, CB_ADDSTRING, 0, (LPARAM)szText); SendDlgItemMessageW(hwndDlg, 28673, CB_SETCURSEL, 0, (LPARAM)0); } } if (pContext->Options & SHFMT_OPT_FULL) { /* check quick format button */ SendDlgItemMessageW(hwndDlg, 28674, BM_SETCHECK, BST_CHECKED, 0); } /* enumerate all available filesystems */ dwIndex = 0; dwDefault = 0; hDlgCtrl = GetDlgItem(hwndDlg, 28677); while(pContext->QueryAvailableFileSystemFormat(dwIndex, szText, &uMajor, &uMinor, &Latest)) { szText[(sizeof(szText)/sizeof(WCHAR))-1] = L'\0'; if (!wcsicmp(szText, szFs)) dwDefault = dwIndex; SendMessageW(hDlgCtrl, CB_ADDSTRING, 0, (LPARAM)szText); dwIndex++; } if (!dwIndex) { ERR("no filesystem providers\n"); return; } /* select default filesys */ SendMessageW(hDlgCtrl, CB_SETCURSEL, dwDefault, 0); /* setup cluster combo */ InsertDefaultClusterSizeForFs(hwndDlg, pContext); /* hide progress control */ ShowWindow(GetDlgItem(hwndDlg, 28678), SW_HIDE); }
QStringList Utils::mountpoints(enum MountpointsFilter type) { QStringList supported; QStringList tempList; #if defined(Q_OS_WIN32) supported << "FAT32" << "FAT16" << "FAT12" << "FAT" << "HFS"; QFileInfoList list = QDir::drives(); for(int i=0; i<list.size();i++) { wchar_t t[32]; memset(t, 0, 32); if(GetVolumeInformationW((LPCWSTR)list.at(i).absolutePath().utf16(), NULL, 0, NULL, NULL, NULL, t, 32) == 0) { // on error empty retrieved type -- don't rely on // GetVolumeInformation not changing it. memset(t, 0, sizeof(t)); } QString fstype = QString::fromWCharArray(t); if(type == MountpointsAll || supported.contains(fstype)) { tempList << list.at(i).absolutePath(); LOG_INFO() << "Added:" << list.at(i).absolutePath() << "type" << fstype; } else { LOG_INFO() << "Ignored:" << list.at(i).absolutePath() << "type" << fstype; } } #elif defined(Q_OS_MACX) || defined(Q_OS_OPENBSD) supported << "vfat" << "msdos" << "hfs"; int num; struct statfs *mntinf; num = getmntinfo(&mntinf, MNT_WAIT); while(num--) { if(type == MountpointsAll || supported.contains(mntinf->f_fstypename)) { tempList << QString(mntinf->f_mntonname); LOG_INFO() << "Added:" << mntinf->f_mntonname << "is" << mntinf->f_mntfromname << "type" << mntinf->f_fstypename; } else { LOG_INFO() << "Ignored:" << mntinf->f_mntonname << "is" << mntinf->f_mntfromname << "type" << mntinf->f_fstypename; } mntinf++; } #elif defined(Q_OS_LINUX) supported << "vfat" << "msdos" << "hfsplus"; FILE *mn = setmntent("/etc/mtab", "r"); if(!mn) return QStringList(""); struct mntent *ent; while((ent = getmntent(mn))) { if(type == MountpointsAll || supported.contains(ent->mnt_type)) { tempList << QString(ent->mnt_dir); LOG_INFO() << "Added:" << ent->mnt_dir << "is" << ent->mnt_fsname << "type" << ent->mnt_type; } else { LOG_INFO() << "Ignored:" << ent->mnt_dir << "is" << ent->mnt_fsname << "type" << ent->mnt_type; } } endmntent(mn); #else #error Unknown Platform #endif return tempList; }
/** * Tests whether a file can be trashed * @param wszPath Path to the file to be trash * @returns TRUE if the file can be trashed, FALSE otherwise */ BOOL TRASH_CanTrashFile(LPCWSTR wszPath) { LONG ret; DWORD dwNukeOnDelete, dwType, VolSerialNumber, MaxComponentLength; DWORD FileSystemFlags, dwSize, dwDisposition; HKEY hKey; WCHAR szBuffer[10]; WCHAR szKey[150] = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Bitbucket\\Volume\\"; if (wszPath[1] != L':') { /* path is UNC */ return FALSE; } // Copy and retrieve the root path from get given string WCHAR wszRootPathName[MAX_PATH]; StringCbCopy(wszRootPathName, sizeof(wszRootPathName), wszPath); PathStripToRootW(wszRootPathName); // Test to see if the drive is fixed (non removable) if (GetDriveTypeW(wszRootPathName) != DRIVE_FIXED) { /* no bitbucket on removable media */ return FALSE; } if (!GetVolumeInformationW(wszRootPathName, NULL, 0, &VolSerialNumber, &MaxComponentLength, &FileSystemFlags, NULL, 0)) { ERR("GetVolumeInformationW failed with %u wszRootPathName=%s\n", GetLastError(), debugstr_w(wszRootPathName)); return FALSE; } swprintf(szBuffer, L"%04X-%04X", LOWORD(VolSerialNumber), HIWORD(VolSerialNumber)); wcscat(szKey, szBuffer); if (RegCreateKeyExW(HKEY_CURRENT_USER, szKey, 0, NULL, 0, KEY_WRITE, NULL, &hKey, &dwDisposition) != ERROR_SUCCESS) { ERR("RegCreateKeyExW failed\n"); return FALSE; } if (dwDisposition & REG_CREATED_NEW_KEY) { /* per default move to bitbucket */ dwNukeOnDelete = 0; RegSetValueExW(hKey, L"NukeOnDelete", 0, REG_DWORD, (LPBYTE)&dwNukeOnDelete, sizeof(DWORD)); /* per default unlimited size */ dwSize = -1; RegSetValueExW(hKey, L"MaxCapacity", 0, REG_DWORD, (LPBYTE)&dwSize, sizeof(DWORD)); RegCloseKey(hKey); return TRUE; } else { dwSize = sizeof(dwNukeOnDelete); ret = RegQueryValueExW(hKey, L"NukeOnDelete", NULL, &dwType, (LPBYTE)&dwNukeOnDelete, &dwSize); if (ret != ERROR_SUCCESS) { if (ret == ERROR_FILE_NOT_FOUND) { /* restore key and enable bitbucket */ dwNukeOnDelete = 0; RegSetValueExW(hKey, L"NukeOnDelete", 0, REG_DWORD, (LPBYTE)&dwNukeOnDelete, sizeof(DWORD)); } RegCloseKey(hKey); return TRUE; } else if (dwNukeOnDelete) { /* do not delete to bitbucket */ RegCloseKey(hKey); return FALSE; } /* FIXME * check if bitbucket is full */ RegCloseKey(hKey); return TRUE; } }