Antares Simulator
Power System Simulator
diskfreespace.hxx
1 /*
2 ** Copyright 2007-2025, RTE (https://www.rte-france.com)
3 ** See AUTHORS.txt
4 ** SPDX-License-Identifier: MPL-2.0
5 ** This file is part of Antares-Simulator,
6 ** Adequacy and Performance assessment for interconnected energy networks.
7 **
8 ** Antares_Simulator is free software: you can redistribute it and/or modify
9 ** it under the terms of the Mozilla Public Licence 2.0 as published by
10 ** the Mozilla Foundation, either version 2 of the License, or
11 ** (at your option) any later version.
12 **
13 ** Antares_Simulator is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** Mozilla Public Licence 2.0 for more details.
17 **
18 ** You should have received a copy of the Mozilla Public Licence 2.0
19 ** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20 */
21 
22 #include <yuni/yuni.h>
23 #include <yuni/core/system/memory.h>
24 #include <yuni/core/system/windows.hdr.h>
25 #include <yuni/core/string/wstring.h>
26 #include <antares/logs/logs.h>
27 
28 using namespace Yuni;
29 
30 template<class StringT>
31 static inline StringT& BytesToStringW(StringT& out, uint64_t size)
32 {
33  if (0 == size)
34  return out << L"0 byte";
35 
36  // bytes
37  if (size < 1024)
38  return out << size << L" bytes";
39  // KiB
40  if (size < 1024 * 1024)
41  return out << (size / 1024) << L" KiB";
42  // MiB
43  if (size < 1024 * 1024 * 1024)
44  return out << (size / (1024 * 1024)) << L" MiB";
45 
46  // GiB
47  double s = size / (1024. * 1024 * 1024);
48  return out << Math::Round(s, 1) << L" GiB";
49 }
50 
51 static inline uint64_t DiskFreeSpace(const AnyString& folder)
52 {
53  if (folder.empty())
54  return (uint64_t)-1; // error
55 
56 #ifdef YUNI_OS_WINDOWS
57  unsigned __int64 i64FreeBytesToCaller;
58  // unsigned __int64 i64TotalBytes;
59  // unsigned __int64 i64FreeBytes;
60 
61  {
62  Yuni::WString wstr(folder);
63  if (wstr.empty())
64  return (uint64_t)-1; // error
65 
66  if (GetDiskFreeSpaceExW(wstr.c_str(),
67  (PULARGE_INTEGER)&i64FreeBytesToCaller,
68  nullptr, //(PULARGE_INTEGER)&i64TotalBytes,
69  nullptr /*(PULARGE_INTEGER)&i64FreeBytes*/)
70  != 0)
71  {
72  // +1 to not be strictly equal to 0
73  // It won't change anything anyway
74  return i64FreeBytesToCaller;
75  }
76  }
77  {
78  // the previous call may abort due to the UNC path
79  // retrying with the standard path
80  Yuni::WString wstr(folder);
81  if (wstr.empty())
82  return (uint64_t)-1; // error
83 
84  if (GetDiskFreeSpaceExW(wstr.c_str(),
85  (PULARGE_INTEGER)&i64FreeBytesToCaller,
86  nullptr, //(PULARGE_INTEGER)&i64TotalBytes,
87  nullptr /*(PULARGE_INTEGER)&i64FreeBytes*/)
88  != 0)
89  {
90  // +1 to not be strictly equal to 0
91  // It won't change anything anyway
92  return i64FreeBytesToCaller;
93  }
94  else
95  {
96  // Antares::logs.error() << "last error : " << GetLastError() << " : " << folder;
97  }
98  }
99 #endif
100 
101  (void)folder;
102  return (uint64_t)-1; // obviously an error
103 }