diff options
| author | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2021-12-27 00:00:00 +0000 |
|---|---|---|
| committer | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2022-03-18 15:35:13 +0500 |
| commit | f19f813537c7aea1c20749c914e756b54a9c3cf5 (patch) | |
| tree | 816ba62ca7c0fa19f2eb46d9e9d6f7dd7c3a744d /CPP/Windows/FileSystem.cpp | |
| parent | 98e06a519b63b81986abe76d28887f6984a7732b (diff) | |
| download | 7zip-f19f813537c7aea1c20749c914e756b54a9c3cf5.tar.gz 7zip-f19f813537c7aea1c20749c914e756b54a9c3cf5.tar.bz2 7zip-f19f813537c7aea1c20749c914e756b54a9c3cf5.zip | |
'21.07'21.07
Diffstat (limited to 'CPP/Windows/FileSystem.cpp')
| -rw-r--r-- | CPP/Windows/FileSystem.cpp | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/CPP/Windows/FileSystem.cpp b/CPP/Windows/FileSystem.cpp new file mode 100644 index 0000000..6259453 --- /dev/null +++ b/CPP/Windows/FileSystem.cpp | |||
| @@ -0,0 +1,135 @@ | |||
| 1 | // Windows/FileSystem.cpp | ||
| 2 | |||
| 3 | #include "StdAfx.h" | ||
| 4 | |||
| 5 | #ifndef UNDER_CE | ||
| 6 | |||
| 7 | #ifndef _UNICODE | ||
| 8 | #include "../Common/StringConvert.h" | ||
| 9 | #endif | ||
| 10 | |||
| 11 | #include "FileSystem.h" | ||
| 12 | #include "Defs.h" | ||
| 13 | |||
| 14 | #ifndef _UNICODE | ||
| 15 | extern bool g_IsNT; | ||
| 16 | #endif | ||
| 17 | |||
| 18 | namespace NWindows { | ||
| 19 | namespace NFile { | ||
| 20 | namespace NSystem { | ||
| 21 | |||
| 22 | #ifdef _WIN32 | ||
| 23 | |||
| 24 | bool MyGetVolumeInformation( | ||
| 25 | CFSTR rootPath, | ||
| 26 | UString &volumeName, | ||
| 27 | LPDWORD volumeSerialNumber, | ||
| 28 | LPDWORD maximumComponentLength, | ||
| 29 | LPDWORD fileSystemFlags, | ||
| 30 | UString &fileSystemName) | ||
| 31 | { | ||
| 32 | BOOL res; | ||
| 33 | #ifndef _UNICODE | ||
| 34 | if (!g_IsNT) | ||
| 35 | { | ||
| 36 | TCHAR v[MAX_PATH + 2]; v[0] = 0; | ||
| 37 | TCHAR f[MAX_PATH + 2]; f[0] = 0; | ||
| 38 | res = GetVolumeInformation(fs2fas(rootPath), | ||
| 39 | v, MAX_PATH, | ||
| 40 | volumeSerialNumber, maximumComponentLength, fileSystemFlags, | ||
| 41 | f, MAX_PATH); | ||
| 42 | volumeName = MultiByteToUnicodeString(v); | ||
| 43 | fileSystemName = MultiByteToUnicodeString(f); | ||
| 44 | } | ||
| 45 | else | ||
| 46 | #endif | ||
| 47 | { | ||
| 48 | WCHAR v[MAX_PATH + 2]; v[0] = 0; | ||
| 49 | WCHAR f[MAX_PATH + 2]; f[0] = 0; | ||
| 50 | res = GetVolumeInformationW(fs2us(rootPath), | ||
| 51 | v, MAX_PATH, | ||
| 52 | volumeSerialNumber, maximumComponentLength, fileSystemFlags, | ||
| 53 | f, MAX_PATH); | ||
| 54 | volumeName = v; | ||
| 55 | fileSystemName = f; | ||
| 56 | } | ||
| 57 | return BOOLToBool(res); | ||
| 58 | } | ||
| 59 | |||
| 60 | UINT MyGetDriveType(CFSTR pathName) | ||
| 61 | { | ||
| 62 | #ifndef _UNICODE | ||
| 63 | if (!g_IsNT) | ||
| 64 | { | ||
| 65 | return GetDriveType(fs2fas(pathName)); | ||
| 66 | } | ||
| 67 | else | ||
| 68 | #endif | ||
| 69 | { | ||
| 70 | return GetDriveTypeW(fs2us(pathName)); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | typedef BOOL (WINAPI * GetDiskFreeSpaceExA_Pointer)( | ||
| 75 | LPCSTR lpDirectoryName, // directory name | ||
| 76 | PULARGE_INTEGER lpFreeBytesAvailable, // bytes available to caller | ||
| 77 | PULARGE_INTEGER lpTotalNumberOfBytes, // bytes on disk | ||
| 78 | PULARGE_INTEGER lpTotalNumberOfFreeBytes // free bytes on disk | ||
| 79 | ); | ||
| 80 | |||
| 81 | typedef BOOL (WINAPI * GetDiskFreeSpaceExW_Pointer)( | ||
| 82 | LPCWSTR lpDirectoryName, // directory name | ||
| 83 | PULARGE_INTEGER lpFreeBytesAvailable, // bytes available to caller | ||
| 84 | PULARGE_INTEGER lpTotalNumberOfBytes, // bytes on disk | ||
| 85 | PULARGE_INTEGER lpTotalNumberOfFreeBytes // free bytes on disk | ||
| 86 | ); | ||
| 87 | |||
| 88 | bool MyGetDiskFreeSpace(CFSTR rootPath, UInt64 &clusterSize, UInt64 &totalSize, UInt64 &freeSize) | ||
| 89 | { | ||
| 90 | DWORD numSectorsPerCluster, bytesPerSector, numFreeClusters, numClusters; | ||
| 91 | bool sizeIsDetected = false; | ||
| 92 | #ifndef _UNICODE | ||
| 93 | if (!g_IsNT) | ||
| 94 | { | ||
| 95 | GetDiskFreeSpaceExA_Pointer pGetDiskFreeSpaceEx = (GetDiskFreeSpaceExA_Pointer)(void *)GetProcAddress( | ||
| 96 | GetModuleHandle(TEXT("kernel32.dll")), "GetDiskFreeSpaceExA"); | ||
| 97 | if (pGetDiskFreeSpaceEx) | ||
| 98 | { | ||
| 99 | ULARGE_INTEGER freeBytesToCaller2, totalSize2, freeSize2; | ||
| 100 | sizeIsDetected = BOOLToBool(pGetDiskFreeSpaceEx(fs2fas(rootPath), &freeBytesToCaller2, &totalSize2, &freeSize2)); | ||
| 101 | totalSize = totalSize2.QuadPart; | ||
| 102 | freeSize = freeSize2.QuadPart; | ||
| 103 | } | ||
| 104 | if (!::GetDiskFreeSpace(fs2fas(rootPath), &numSectorsPerCluster, &bytesPerSector, &numFreeClusters, &numClusters)) | ||
| 105 | return false; | ||
| 106 | } | ||
| 107 | else | ||
| 108 | #endif | ||
| 109 | { | ||
| 110 | GetDiskFreeSpaceExW_Pointer pGetDiskFreeSpaceEx = (GetDiskFreeSpaceExW_Pointer)(void *)GetProcAddress( | ||
| 111 | GetModuleHandle(TEXT("kernel32.dll")), "GetDiskFreeSpaceExW"); | ||
| 112 | if (pGetDiskFreeSpaceEx) | ||
| 113 | { | ||
| 114 | ULARGE_INTEGER freeBytesToCaller2, totalSize2, freeSize2; | ||
| 115 | sizeIsDetected = BOOLToBool(pGetDiskFreeSpaceEx(fs2us(rootPath), &freeBytesToCaller2, &totalSize2, &freeSize2)); | ||
| 116 | totalSize = totalSize2.QuadPart; | ||
| 117 | freeSize = freeSize2.QuadPart; | ||
| 118 | } | ||
| 119 | if (!::GetDiskFreeSpaceW(fs2us(rootPath), &numSectorsPerCluster, &bytesPerSector, &numFreeClusters, &numClusters)) | ||
| 120 | return false; | ||
| 121 | } | ||
| 122 | clusterSize = (UInt64)bytesPerSector * (UInt64)numSectorsPerCluster; | ||
| 123 | if (!sizeIsDetected) | ||
| 124 | { | ||
| 125 | totalSize = clusterSize * (UInt64)numClusters; | ||
| 126 | freeSize = clusterSize * (UInt64)numFreeClusters; | ||
| 127 | } | ||
| 128 | return true; | ||
| 129 | } | ||
| 130 | |||
| 131 | #endif | ||
| 132 | |||
| 133 | }}} | ||
| 134 | |||
| 135 | #endif | ||
