|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: "Lesson 5: Free Space " |
| 4 | +date: 2024-01-30 |
| 5 | +categories: rust |
| 6 | +permalink: /lesson5/ |
| 7 | +--- |
| 8 | + |
| 9 | +## Rust |
| 10 | + |
| 11 | +```rust |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +``` |
| 21 | + |
| 22 | +## C++ Implementation |
| 23 | + |
| 24 | +```cpp |
| 25 | +// C++ implementation will be added here |
| 26 | + |
| 27 | +#include <windows.h> |
| 28 | +#include <tchar.h> |
| 29 | +#include <stdio.h> |
| 30 | +#include <stdlib.h> |
| 31 | + |
| 32 | +void ReportError(LPCTSTR msg, DWORD exitCode, BOOL exitProgram); |
| 33 | +void ReportSpace(LPCTSTR Message); |
| 34 | + |
| 35 | +int _tmain(int argc, LPTSTR argv[]) { |
| 36 | + HANDLE hFile; |
| 37 | + LARGE_INTEGER FileLen, FileLenH; |
| 38 | + BYTE Buffer[256]; |
| 39 | + OVERLAPPED ov = {0}; |
| 40 | + DWORD nWrite; |
| 41 | + |
| 42 | + while (1) { |
| 43 | + FileLen.QuadPart = 0; |
| 44 | + _tprintf(_T("Enter file length in bytes (0 to quit): ")); |
| 45 | + _tscanf_s(_T("%I64d"), &FileLen.QuadPart); |
| 46 | + |
| 47 | + if (FileLen.QuadPart == 0) |
| 48 | + break; |
| 49 | + |
| 50 | + _tprintf(_T("\nRequested file size: %,20I64d bytes\n"), FileLen.QuadPart); |
| 51 | + FileLenH.QuadPart = FileLen.QuadPart / 2; |
| 52 | + |
| 53 | + ReportSpace(_T("Before file creation")); |
| 54 | + |
| 55 | + hFile = CreateFile(_T("TempTestFile"), GENERIC_READ | GENERIC_WRITE, 0, NULL, |
| 56 | + CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); |
| 57 | + if (hFile == INVALID_HANDLE_VALUE) |
| 58 | + ReportError(_T("Cannot create TempTestFile"), 2, TRUE); |
| 59 | + ReportSpace(_T("After file creation")); |
| 60 | + |
| 61 | + if (!SetFilePointerEx(hFile, FileLen, NULL, FILE_BEGIN)) |
| 62 | + ReportError(_T("Cannot set file pointer"), 3, TRUE); |
| 63 | + |
| 64 | + if (!SetEndOfFile(hFile)) |
| 65 | + ReportError(_T("Cannot set end of file"), 4, TRUE); |
| 66 | + ReportSpace(_T("After setting file length")); |
| 67 | + |
| 68 | + ov.Offset = FileLenH.LowPart; |
| 69 | + ov.OffsetHigh = FileLenH.HighPart; |
| 70 | + |
| 71 | + if (!WriteFile(hFile, Buffer, sizeof(Buffer), &nWrite, &ov)) |
| 72 | + ReportError(_T("Cannot write to middle of file"), 5, TRUE); |
| 73 | + ReportSpace(_T("After writing to middle")); |
| 74 | + |
| 75 | + CloseHandle(hFile); |
| 76 | + DeleteFile(_T("TempTestFile")); |
| 77 | + _tprintf(_T("\n----------------------------------------\n")); |
| 78 | + } |
| 79 | + |
| 80 | + _tprintf(_T("\nEnd of FreeSpace demonstration\n")); |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
| 84 | +void ReportError(LPCTSTR msg, DWORD exitCode, BOOL exitProgram) { |
| 85 | + LPVOID lpMsgBuf; |
| 86 | + DWORD dw = GetLastError(); |
| 87 | + |
| 88 | + FormatMessage( |
| 89 | + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 90 | + NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 91 | + (LPTSTR)&lpMsgBuf, 0, NULL); |
| 92 | + |
| 93 | + _tprintf(_T("\nERROR [%d]: %s\n"), dw, (LPCTSTR)lpMsgBuf); |
| 94 | + LocalFree(lpMsgBuf); |
| 95 | + |
| 96 | + if (exitProgram) { |
| 97 | + ExitProcess(exitCode); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +void ReportSpace(LPCTSTR Message) { |
| 102 | + ULARGE_INTEGER FreeBytes, TotalBytes, NumFreeBytes; |
| 103 | + const double GB = 1024.0 * 1024.0 * 1024.0; |
| 104 | + |
| 105 | + if (!GetDiskFreeSpaceEx(NULL, &FreeBytes, &TotalBytes, &NumFreeBytes)) |
| 106 | + ReportError(_T("Cannot get free space"), 1, TRUE); |
| 107 | + |
| 108 | + _tprintf(_T("\n%25s status:\n"), Message); |
| 109 | + _tprintf(_T(" Total disk space: %12.2f GB\n"), (double)TotalBytes.QuadPart / GB); |
| 110 | + _tprintf(_T(" Actual free space: %12.2f GB\n"), (double)NumFreeBytes.QuadPart / GB); |
| 111 | + _tprintf(_T(" Available to user: %12.2f GB\n"), (double)FreeBytes.QuadPart / GB); |
| 112 | +} |
| 113 | +``` |
| 114 | +
|
| 115 | +### Exercise |
| 116 | +
|
| 117 | +Let us implement this for small disks and observe, in C++23 or C++20. |
| 118 | +
|
0 commit comments