Win9x が多く存在していた頃のコードの MFC を使用しない方法での書き直し.
前のものは次の様になっている.
// J048221 SDK32:GetDiskFreeSpace と GetDiskFreeSpaceEx について
typedef BOOL (WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER,PULARGE_INTEGER, PULARGE_INTEGER);
// ..
fResult = ::GetDiskFreeSpace(pszDrive,&SectorsPerCluster,&BytesPerSector,&FreeClusters,&TotalClusters) ;
if (fResult) {
TotalBytes = (__int64)TotalClusters * SectorsPerCluster * BytesPerSector ;
TotalFreeBytes = (__int64)FreeClusters * SectorsPerCluster * BytesPerSector ;
FreeBytesAvailable = TotalFreeBytes ;
}
// ..
{
P_GDFSE pGetDiskFreeSpaceEx = NULL;
#ifdef UNICODE
pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle(_T("kernel32.dll")),"GetDiskFreeSpaceExW") ;
#else
pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle(_T("kernel32.dll")),"GetDiskFreeSpaceExA") ;
#endif
if (pGetDiskFreeSpaceEx) {
fResult = pGetDiskFreeSpaceEx (pszDrive,
(PULARGE_INTEGER)&FreeBytesAvailable,
(PULARGE_INTEGER)&TotalBytes,
(PULARGE_INTEGER)&TotalFreeBytes);
}
}
https://www.betaarchive.com/wiki/index.php?title=Microsoft_KB_Archive/231497
Windows 環境では次の様な感じ?
class DiskFree {
public:
DiskFree () { Free = Total = 0 ; }
public:
u_64 Free ;
u_64 Total ;
} ;
DiskFree GetDiskFree (LPCTSTR path)
{
DiskFree df ;
u_64 freeC = 0 ;
u_64 free = 0 ;
u_64 total = 0 ;
if (::GetDiskFreeSpaceEx(path,(PULARGE_INTEGER)&freeC,(PULARGE_INTEGER)&total,(PULARGE_INTEGER)&free)) {
df.Free = free ;
df.Total= total;
}
return df ;
}
Linux 環境では ::statvfs が使えるみたいで,次の様なコードで取得してデバッガで確認.
#include <iostream>
#include <sys/statvfs.h>
int main()
{
struct statvfs vfs = { 0 } ;
::statvfs(".", &vfs) ;
return 0 ;
}
vfs {...} statvfs
f_bsize 4096 unsigned long
f_frsize 4096 unsigned long
f_blocks 1452408524 __fsblkcnt_t
f_bfree 200259802 __fsblkcnt_t
f_bavail 199469255 __fsblkcnt_t
f_files 183001088 __fsfilcnt_t
f_ffree 181248356 __fsfilcnt_t
f_favail 181248356 __fsfilcnt_t
f_fsid 3941329918106335254 unsigned long
f_flag 4096 unsigned long
f_namemax 255 unsigned long
__f_spare int [6]
[0] 0 int
[1] 0 int
[2] 0 int
[3] 0 int
[4] 0 int
[5] 0 int
Iwao@AS5202T:/volume1/home/Iwao $ cd gcc_test/Test/t_linux/T_vfs/T_s_vfs/
Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/T_vfs/T_s_vfs $ ll
total 12
drwxrwxrwx 2 Iwao users 4.0K Jul 1 22:01 ./
drwxrwxrwx 4 Iwao users 4.0K Jul 1 22:00 ../
-rwxrwxrwx 1 Iwao users 581 Jul 1 21:55 T_s_vfs.cpp*
Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/T_vfs/T_s_vfs $ cat T_s_vfs.cpp
#include <iostream>
#include <clocale>
#include <sys/statvfs.h>
#include "_tdefine.hxx"
#include "t_tstrng.hxx"
int _tmain (int argc,TCHAR* argv[])
{
_tsetlocale(LC_ALL,_T("")) ;
{
struct statvfs vfs = { 0 } ;
if (::statvfs(".", &vfs) == 0) {
u_64 free = vfs.f_bavail * vfs.f_frsize ;
u_64 total= vfs.f_blocks * vfs.f_frsize ;
std::tout << ::To_tstring_Ki(free) << _T(" / ") << ::To_tstring_Ki(total) << std::endl ;
std::tout << ::To_tstring_cs(free) << _T(" / ") << ::To_tstring_cs(total) << std::endl ;
}
}
return 0 ;
}
Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/T_vfs/T_s_vfs $ g++ T_s_vfs.cpp -Wall
Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/T_vfs/T_s_vfs $ ./a.out
760.90 G / 5.41 T
817,005,420,544 / 5,949,065,314,304
Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/T_vfs/T_s_vfs $