source: other-projects/gs2-export-cdrom-installer/trunk/DiskSpace.cpp@ 21525

Last change on this file since 21525 was 15927, checked in by anna, 16 years ago

The old method for checking free disk space overflows (returns a negative number) when the physical free disk space is bigger than 4G. So instead of comparing by byte, now we compare by K. The return type is also changed, from unsigned long to double.

  • Property svn:keywords set to Author Date Id Revision
File size: 2.1 KB
Line 
1#include <windows.h>
2
3#include "DiskSpace.h"
4
5typedef BOOL (* diskEx) (LPCTSTR root, PULARGE_INTEGER a, PULARGE_INTEGER B, PULARGE_INTEGER C);
6
7DiskSpace::DiskSpace(const char *rootName)
8{
9 char rootPath[128];
10 DWORD totalClusters;
11 OSVERSIONINFO osVerInfo;
12
13 // copy the rootname to our own path
14 strcpy(rootPath, rootName);
15
16 // get some guide to the os version
17 osVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
18 GetVersionEx(&osVerInfo);
19
20 // if get disk free space fails then note fatal initialisation error
21 if (!GetDiskFreeSpace(rootName, &sectorsPerCluster, &bytesPerSector,
22 &freeClusters, &totalClusters))
23 { this->initialised = FALSE;
24 return;
25 }
26
27 // call the advanced form of getDiskSpace if we have access to it; this
28 // will cope with 2Gb+ partitions correctly
29 if (osVerInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
30 LOWORD(osVerInfo.dwBuildNumber) > 1000 ||
31 TRUE)
32 { HMODULE module;
33 FARPROC proc;
34 ULARGE_INTEGER freeBytes, freeToCaller, totalBytes;
35
36 module = LoadLibrary("KERNEL32.DLL");
37 proc = GetProcAddress(module, "GetDiskFreeSpaceEx");
38 if (proc == NULL)
39 { this->initialised = TRUE;
40 return;
41 }
42 (* ((diskEx) proc))(rootPath, &freeBytes, &freeToCaller, &totalBytes);
43 // Get free clusters - use the ULARGE_INTEGER variables to get the
44 // result and avoid overflow problems
45 //freeBytes = freeBytes / sectorsPerCluster; //(sectorsPerCluster * bytesPerSector);
46 //freeClusters = totalBytes;
47 }
48 this->initialised = TRUE;
49 return;
50}
51
52BOOL DiskSpace::initialisedOk()
53{ return this->initialised;
54}
55
56unsigned double DiskSpace::totalFreeSpace()
57{ return this->freeClusters * (((double) (this->sectorsPerCluster * this->bytesPerSector)) / (double) (1024));
58}
59
60int DiskSpace::fileClusters(int fileSize)
61{ int sectors, clusters;
62
63 sectors = (fileSize + this->bytesPerSector - 1) / this->bytesPerSector;
64 clusters = (sectors + this->sectorsPerCluster - 1) / this->sectorsPerCluster;
65 return clusters;
66}
67
68int DiskSpace::fileSpace(int fileSize)
69{ return this->fileClusters(fileSize) * this->bytesPerSector * this->sectorsPerCluster;
70}
71
Note: See TracBrowser for help on using the repository browser.