1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
// ****************************************************************************
// Project: GUYMAGER
// ****************************************************************************
// Programmer: Guy Voncken
// Police Grand-Ducale
// Service de Police Judiciaire
// Section Nouvelles Technologies
// ****************************************************************************
// Module: Different utility functions
// ****************************************************************************
// Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
// 2018, 2019, 2020
// Guy Voncken
//
// This file is part of Guymager.
//
// Guymager is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Guymager is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Guymager. If not, see <http://www.gnu.org/licenses/>.
#include "common.h"
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#if (QT_VERSION >= 0x050000)
#include <QtWidgets> //lint !e537 Repeated include
#else
#include <QtGui> //lint !e537 Repeated include
#endif
#include "config.h"
#include "main.h"
#include "util.h"
#include <sys/statvfs.h>
// ----------------
// Global vars
// ----------------
#define UTIL_ZEROBLOCK_MAXSIZE 65536
static char *pUtilZeroBlock = nullptr;
static unsigned int UtilZeroBlockSize = 0;
// ----------------
// Functions
// ----------------
void *UtilMemAlloc (size_t Size, const char */*pFile*/, int /*Line*/)
{
return malloc(Size);
}
void UtilMemFree (void *pMem, const char */*pFile*/, int /*Line*/)
{
free (pMem);
}
int UtilCalcDecimals (t_uint64 Value)
{
QString Str;
Str.setNum (Value);
return Str.length();
}
bool UtilIsZero (unsigned char *pData, unsigned int DataLen)
{
unsigned int ToCompare;
unsigned int Remaining = DataLen;
bool IsZero = true;
while (Remaining)
{
ToCompare = GETMIN(Remaining, UtilZeroBlockSize);
IsZero = (memcmp (pData, pUtilZeroBlock, ToCompare) == 0);
if (!IsZero)
break;
Remaining -= ToCompare;
pData += ToCompare;
}
return IsZero;
}
unsigned int UtilGetMaxZcompressedBufferSize (unsigned int UncompressedBufferSize)
{
return (unsigned int)(UncompressedBufferSize * 1.001f) + 12;
}
unsigned long long UtilGetInstalledRAM (void)
{
unsigned long long Bytes;
int Pages, PageSize;
Pages = sysconf(_SC_PHYS_PAGES);
PageSize = sysconf(_SC_PAGESIZE);
if ((Pages < 0) || (PageSize < 0))
{
LOG_ERROR ("Amount of instlled RAM could not be determined, assume 256MB");
Bytes = 256*1024*1024;
}
else
{
Bytes = (unsigned long long) sysconf(_SC_PHYS_PAGES) *
(unsigned long long) sysconf(_SC_PAGESIZE );
}
return Bytes;
}
unsigned int UtilGetNumberOfCPUs (void)
{
int ret;
ret = sysconf( _SC_NPROCESSORS_ONLN );
if (ret<0)
{
LOG_ERROR ("Number of CPUs could not be determined, assume 1 CPU");
ret = 1;
}
return (unsigned int) ret;
}
APIRET UtilGetFreeSpace (const QString &DirPath, quint64 *pFreeBytes)
{
struct statvfs vfs;
if (statvfs (DirPath.toLocal8Bit(), &vfs) == -1)
return ERROR_UTIL_FREESPACE_UNKNOWN;
*pFreeBytes = vfs.f_bfree * vfs.f_bsize;
return NO_ERROR;
}
QString UtilSizeToHuman (unsigned long long Size, bool SI, int FracLen, int UnitThreshold)
{
QString SizeHuman;
const char *pBaseUnit = "Byte";
const char *pUnit;
double Sz;
double Divisor;
if (SI)
Divisor = 1000.0;
else Divisor = 1024.0;
if (UnitThreshold == -1) // auto
UnitThreshold = (int) Divisor;
Sz = Size;
pUnit = pBaseUnit;
if (Sz >= UnitThreshold) { Sz = Sz / Divisor; pUnit = SI ? "kB" : "KiB"; }
if (Sz >= UnitThreshold) { Sz = Sz / Divisor; pUnit = SI ? "MB" : "MiB"; }
if (Sz >= UnitThreshold) { Sz = Sz / Divisor; pUnit = SI ? "GB" : "GiB"; }
if (Sz >= UnitThreshold) { Sz = Sz / Divisor; pUnit = SI ? "TB" : "TiB"; }
if (Sz >= UnitThreshold) { Sz = Sz / Divisor; pUnit = SI ? "PB" : "PiB"; }
if (Sz >= UnitThreshold) { Sz = Sz / Divisor; pUnit = SI ? "EB" : "EiB"; }
if (Sz >= UnitThreshold) { Sz = Sz / Divisor; pUnit = SI ? "ZB" : "ZiB"; }
if (Sz >= UnitThreshold) { Sz = Sz / Divisor; pUnit = SI ? "YB" : "YiB"; }
if (FracLen == -1) // auto
{
if (pUnit == pBaseUnit) FracLen = 0; // no frac if unit is bytes
else if (Sz >= 100) FracLen = 0;
else if (Sz >= 10 ) FracLen = 1;
else FracLen = 2;
}
SizeHuman = MainGetpNumberLocale()->toString (Sz, 'f', FracLen) + pUnit;
return SizeHuman;
}
// UtilClose is wrapper and replacement for fclose. UtilClose was added because of rare
// fclose problems when writing to network paths.
#ifdef UTIL_CLOSE_OLD
//#define UTIL_EMULATE_CLOSE_ERROR
int UtilClose (FILE *pFile) // This old function retries the close operation several times in case of errors. It was found
{ // that this is not the right to go as guymager may hang when calling fclose a second time if
const int MaxRetries = 10; // the first call was unsuccessful.
const int SleepSeconds = 5;
int rc, err;
for (int i=0;;i++)
{
#ifdef UTIL_EMULATE_CLOSE_ERROR
#warning "Debugging flag UTIL_EMULATE_CLOSE_ERROR is active"
static int CloseErrorCount=0;
if ((CloseErrorCount>=3) && (CloseErrorCount<=5))
rc = -1;
else rc = fclose (pFile);
CloseErrorCount++;
#else
rc = fclose (pFile);
#endif
if (rc==0)
{
if (i>0)
LOG_INFO ("fclose successful after %d tries", i+1)
break;
}
err = errno;
LOG_INFO ("fclose returned %d (errno %d - %s)", rc, err, strerror(err))
if (i>=MaxRetries)
{
LOG_ERROR ("Too many fclose errors, giving up!")
break;
}
sleep (SleepSeconds);
}
return rc;
}
#else
int UtilClose (FILE *pFile)
{
int ret;
int err;
#define LOG_FUNCTION_ERROR(FnName) \
{ \
err = errno; \
LOG_ERROR ("%s returned %d (errno %d - %s)", FnName, ret, err, strerror(err)) \
}
if CONFIG (AvoidCifsProblems)
{
ret = fflush (pFile);
if (ret)
LOG_FUNCTION_ERROR ("fflush")
ret = fileno (pFile);
if (ret == -1)
LOG_FUNCTION_ERROR ("fileno")
else
{
ret = fsync (ret);
if (ret == -1)
LOG_FUNCTION_ERROR ("fsync")
}
}
ret = fclose (pFile);
if (ret)
LOG_FUNCTION_ERROR ("fclose")
#undef LOG_FUNCTION_ERROR
return ret;
}
#endif
t_UtilMem::t_UtilMem()
{
oSize = 0;
poMem = nullptr;
}
t_UtilMem::t_UtilMem (unsigned long long Size)
{
oSize = Size;
poMem = malloc (Size);
}
t_UtilMem::~t_UtilMem()
{
if (poMem)
free(poMem);
};
void* t_UtilMem::GetpMem (void)
{
return poMem;
};
unsigned long long t_UtilMem::Size(void)
{
return oSize;
}
int t_UtilMem::Set (const char *pStr)
{
size_t StrLen = strlen (pStr) +1;
int ret = 0;
if (StrLen > (oSize))
{
StrLen = oSize;
ret = -1;
}
if (StrLen)
{
memcpy (poMem, pStr, StrLen-1);
((char*)poMem)[StrLen] ='\0';
}
return ret;
}
APIRET UtilInit (void)
{
int MaxFifoBockSize;
MaxFifoBockSize = GETMAX(CONFIG(FifoBlockSizeEWF), CONFIG(FifoBlockSizeAFF));
UtilZeroBlockSize = GETMIN(UTIL_ZEROBLOCK_MAXSIZE, MaxFifoBockSize);
pUtilZeroBlock = (char *) malloc (UtilZeroBlockSize);
memset(pUtilZeroBlock, 0, UtilZeroBlockSize);
CHK (TOOL_ERROR_REGISTER_CODE (ERROR_UTIL_FREESPACE_UNKNOWN))
return NO_ERROR;
}
APIRET UtilDeInit (void)
{
free (pUtilZeroBlock);
return NO_ERROR;
}
|