cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
FileSystem.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
25#ifdef _WIN32
26# include <Windows.h>
27#else
28# include <sys/statvfs.h>
29#endif
30
31namespace cppcore {
32
34struct FSSpace {
35 long capacity{0L};
36 long free{0L};
37 long inUse{0L};
38};
39
42public:
45 explicit FileSystem(const char *location);
46
48 ~FileSystem() = default;
49
51 FileSystem(const FileSystem &) = delete;
52 FileSystem &operator=(const FileSystem &) = delete;
53
55 void refresh();
56
60
61private:
62 const char *mDrive{nullptr};
63 FSSpace mFsSpace{};
64};
65
66inline FileSystem::FileSystem(const char *location) : mDrive(location) {
67 // empty
68}
69
70inline void FileSystem::refresh() {
71 if (mDrive == nullptr) {
72 return;
73 }
74#ifdef _WIN32
75 ULARGE_INTEGER freeByteAvailable, totalNumberOfBytes, totalNumberOfFreeBytes;
76 BOOL result = ::GetDiskFreeSpaceEx(reinterpret_cast<LPCSTR>(mDrive), &freeByteAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes);
77 if (TRUE == result) {
78 mFsSpace.capacity = totalNumberOfBytes.QuadPart / (1024 * 1024 * 1024);
79 mFsSpace.free = freeByteAvailable.QuadPart / (1024 * 1024 * 1024);
80 mFsSpace.inUse = mFsSpace.capacity - mFsSpace.free;
81 }
82#else
83 struct statvfs stats{};
84 statvfs(mDrive, &stats);
85 mFsSpace.capacity = stats.f_bsize;
86 mFsSpace.free = stats.f_bsize * stats.f_bfree;
87 mFsSpace.inUse = mFsSpace.capacity - mFsSpace.free;
88#endif
89}
90
92 refresh();
93
94 return &mFsSpace;
95}
96
97} // Namespace cppcore
FileSystem & operator=(const FileSystem &)=delete
FSSpace * getFreeDiskSpace()
Will return the free disk info.
Definition FileSystem.h:91
~FileSystem()=default
The class destructor.
FileSystem(const char *location)
The class constructor with the location.
Definition FileSystem.h:66
void refresh()
Will perform a refresh.
Definition FileSystem.h:70
FileSystem(const FileSystem &)=delete
The copy constructor is not allowed.
The file-system space info.
Definition FileSystem.h:34
long free
The free capacity.
Definition FileSystem.h:36
long inUse
The used capacity.
Definition FileSystem.h:37
long capacity
The capacity in total.
Definition FileSystem.h:35