/
Utils.h
439 lines (352 loc) · 9.7 KB
/
Utils.h
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#ifndef _UTILS_H
#define _UTILS_H
#include <unistd.h>
#include <ctype.h>
#include <sys/time.h>
#include <sys/file.h>
#include <string>
#include <vector>
#include <map>
using std::string;
using std::vector;
using std::map;
#ifdef _DEBUG
#define DEBUG_ONLY(f) f
#else
#define DEBUG_ONLY(f) ((void)0)
#endif
static const char g_HexDigits[] = "0123456789abcdef";
class CUtils {
public:
CUtils();
virtual ~CUtils();
static string GetIP(unsigned long addr);
static unsigned long GetLongIP(const string& sIP);
static string ChangeDir(const string& sPath, const string& sAdd, const string& sHomeDir);
static int MakeDir(const string& sPath, mode_t iMode = 0700);
static void PrintError(const string& sMessage);
static void PrintMessage(const string& sMessage);
static void PrintPrompt(const string& sMessage);
static void PrintAction(const string& sMessage);
static void PrintStatus(bool bSuccess, const string& sMessage = "");
static char* GetPass(const string& sPrompt);
static string ToString(short i);
static string ToString(unsigned short i);
static string ToString(int i);
static string ToString(unsigned int i);
static string ToString(long i);
static string ToString(unsigned long i);
static string ToString(unsigned long long i);
static string ToString(double i);
static string ToString(float i);
static string ToPercent(double d);
static string ToKBytes(double d);
static string Left(const string& s, unsigned int u);
static string Right(const string& s, unsigned int u);
static string& Trim(string& s);
static string& LeftChomp(string& s, unsigned int uLen = 1);
static string& RightChomp(string& s, unsigned int uLen = 1);
static string Token(const string& s, unsigned int uPos, bool bRest = false, char cSep = ' ');
static string Ellipsize(const string& s, unsigned int uLen);
static bool wildcmp(const string& sWild, const string& sString);
static unsigned long long GetMillTime() {
struct timeval tv;
unsigned long long iTime = 0;
gettimeofday(&tv, NULL);
iTime = (unsigned long long) tv.tv_sec * 1000;
iTime += ((unsigned long long) tv.tv_usec / 1000);
return iTime;
}
#ifdef HAVE_LIBSSL
static void GenerateCert(FILE *pOut, bool bEncPrivKey = false);
#endif /* HAVE_LIBSSL */
private:
protected:
};
class CLockFile {
public:
CLockFile() {
m_bCreated = false;
m_fd = 0;
m_pid = 0;
}
CLockFile(const string& sFile) {
Open(sFile);
}
virtual ~CLockFile() {
if (getpid() == m_pid) {
if (m_fd > -1) {
UnLock();
close(m_fd);
if (m_bCreated) {
unlink(m_sFileName.c_str());
}
}
}
}
void Open(const string& sFile) {
m_fd = open(sFile.c_str(), O_RDONLY);
m_bCreated = false;
if (m_fd == -1) {
// i must create the file then
m_fd = open(sFile.c_str(), O_RDWR|O_CREAT, 0644);
m_bCreated = true;
}
m_pid = getpid(); // for destructor
m_sFileName = sFile;
}
//! timeout in milliseconds
bool TryExLock(const string& sLockFile, unsigned long long iTimeout = 0) {
Open(sLockFile);
return TryExLock(iTimeout);
}
bool TryExLock(unsigned long long iTimeout = 0) {
if (iTimeout == 0) {
return Lock(LOCK_EX|LOCK_NB);
}
unsigned long long iNow = CUtils::GetMillTime();
while(true) {
if (Lock(LOCK_EX|LOCK_NB)) {
return true;
}
if ((CUtils::GetMillTime() - iNow) > iTimeout) {
break;
}
usleep(100);
}
return(false);
}
bool TryShLock(unsigned long long iTimeout = 0) {
if (iTimeout == 0) {
return(Lock(LOCK_SH|LOCK_NB));
}
unsigned long long iNow = CUtils::GetMillTime();
while(true) {
if (Lock(LOCK_SH|LOCK_NB)) {
return true;
}
if ((CUtils::GetMillTime() - iNow) > iTimeout) {
break;
}
usleep(100);
}
return false;
}
bool LockEx() { return Lock(LOCK_EX); }
bool LockSh() { return Lock(LOCK_SH); }
bool UnLock() { return Lock(LOCK_UN); }
private:
bool Lock(int iOperation) {
if (m_fd == -1) {
return false;
}
if (::flock(m_fd, iOperation) != 0) {
return false;
}
return true;
}
int m_fd;
int m_pid;
bool m_bCreated;
string m_sFileName;
};
class CException {
public:
typedef enum {
EX_Shutdown,
EX_BadModVersion,
} EType;
CException(EType e) {
m_eType = e;
}
virtual ~CException() {}
EType GetType() const { return m_eType; }
private:
protected:
EType m_eType;
};
class CTable : public vector<map<string, string>* > {
public:
CTable();
virtual ~CTable();
bool AddColumn(const string& sName);
unsigned int AddRow();
bool SetCell(const string& sColumn, const string& sValue, unsigned int uRowIdx = ~0);
bool GetLine(unsigned int uIdx, string& sLine);
unsigned int GetColumnWidth(unsigned int uIdx);
private:
protected:
vector<string> m_vsHeaders;
map<string, unsigned int> m_msuWidths; // Used to cache the width of a column
};
class CFile {
public:
CFile(const string& sLongName);
virtual ~CFile();
enum EFileTypes {
FT_REGULAR,
FT_DIRECTORY,
FT_CHARACTER,
FT_BLOCK,
FT_FIFO,
FT_LINK,
FT_SOCK
};
static bool IsReg(const string& sLongName, bool bUseLstat = false);
static bool IsDir(const string& sLongName, bool bUseLstat = false);
static bool IsChr(const string& sLongName, bool bUseLstat = false);
static bool IsBlk(const string& sLongName, bool bUseLstat = false);
static bool IsFifo(const string& sLongName, bool bUseLstat = false);
static bool IsLnk(const string& sLongName, bool bUseLstat = true);
static bool IsSock(const string& sLongName, bool bUseLstat = false);
bool IsReg(bool bUseLstat = false);
bool IsDir(bool bUseLstat = false);
bool IsChr(bool bUseLstat = false);
bool IsBlk(bool bUseLstat = false);
bool IsFifo(bool bUseLstat = false);
bool IsLnk(bool bUseLstat = true);
bool IsSock(bool bUseLstat = false);
bool access(int mode);
// for gettin file types, using fstat instead
static bool FType(const string sFileName, EFileTypes eType, bool bUseLstat = false);
enum EFileAttr {
FA_Name,
FA_Size,
FA_ATime,
FA_MTime,
FA_CTime,
FA_UID
};
//
// Functions to retrieve file information
//
bool Exists() const;
unsigned long long GetSize() const;
unsigned int GetATime() const;
unsigned int GetMTime() const;
unsigned int GetCTime() const;
int GetUID() const;
int GetGID() const;
static bool Exists(const string& sFile);
static unsigned long long GetSize(const string& sFile);
static unsigned int GetATime(const string& sFile);
static unsigned int GetMTime(const string& sFile);
static unsigned int GetCTime(const string& sFile);
static int GetUID(const string& sFile);
static int GetGID(const string& sFile);
static int GetInfo(const string& sFile, struct stat& st);
//
// Functions to manipulate the file on the filesystem
//
int Delete();
int Move(const string& sNewFileName, bool bOverwrite = false);
static bool Delete(const string& sFileName);
static bool Move(const string& sOldFileName, const string& sNewFileName, bool bOverwrite = false);
bool Chmod(mode_t mode);
static bool Chmod(const string& sFile, mode_t mode);
bool Seek(unsigned long uPos);
bool Open(int iFlags, mode_t iMode = 0644);
int Read(char *pszBuffer, int iBytes);
bool ReadLine(string & sData);
int Write(const char *pszBuffer, u_int iBytes);
int Write(const string & sData);
void Close();
string GetLongName() const;
string GetShortName() const;
void SetFD(int iFD);
private:
string m_sBuffer;
int m_iFD;
protected:
string m_sLongName; //!< Absolute filename (m_sPath + "/" + m_sShortName)
string m_sShortName; //!< Filename alone, without path
};
#ifdef HAVE_LIBSSL
#include <openssl/blowfish.h>
#include <openssl/md5.h>
//! does Blowfish w/64 bit feedback, no padding
class CBlowfish {
public:
/**
* @sPassword key to encrypt with
* @iEncrypt encrypt method (BF_DECRYPT or BF_ENCRYPT)
* @sIvec what to set the ivector to start with, default sets it all 0's
*/
CBlowfish(const string & sPassword, int iEncrypt, const string & sIvec = "");
~CBlowfish();
//! output must be freed
static unsigned char *MD5(const unsigned char *input, u_int ilen);
//! returns an md5 of the string (not hex encoded)
static string MD5(const string & sInput, bool bHexEncode = false);
//! output must be the same size as input
void Crypt(unsigned char *input, unsigned char *output, u_int ibytes);
//! must free result
unsigned char * Crypt(unsigned char *input, u_int ibytes);
string Crypt(const string & sData);
private:
unsigned char *m_ivec;
BF_KEY m_bkey;
int m_iEncrypt, m_num;
};
#endif /* HAVE_LIBSSL */
#define RF_BUFF 4096
inline bool ReadFile(const string & sFilename, string & sLine) {
char inbuff[RF_BUFF];
int bytes;
// clear ourselves out
sLine.clear();
FILE *f = fopen(sFilename.c_str(), "r");
if (!f) {
return false;
}
while((bytes = fread(inbuff, sizeof(char), RF_BUFF, f)) > 0) {
sLine.append(inbuff, bytes);
}
fclose(f);
if (bytes < 0) {
return false;
}
return true;
}
inline bool WriteFile(const string & sFilename, const string & sData) {
FILE *f = fopen(sFilename.c_str(), "w");
if (!f) {
return false;
}
int iRet = fwrite(sData.data(), sizeof(char), sData.length(), f);
fclose(f);
if (iRet <= 0) {
return false;
}
return true;
}
inline bool ReadLine(const string & sData, string & sLine, u_int & iPos) {
sLine.clear();
if (iPos >= sData.length()) {
return false;
}
u_int iFind = sData.find("\n", iPos);
if (iFind == string::npos) {
sLine = sData.substr(iPos, (sData.length() - iPos));
iPos = string::npos;
return true;
}
sLine = sData.substr(iPos, (iFind - iPos)) + "\n";
iPos = iFind + 1;
return true;
}
inline string Lower(const string & sLine) {
string sRet;
for(u_int a = 0; a < sLine.length(); a++) {
sRet += tolower(sLine[a]);
}
return sRet;
}
inline string Upper(const string & sLine) {
string sRet;
for(u_int a = 0; a < sLine.length(); a++) {
sRet += toupper(sLine[a]);
}
return sRet;
}
#endif // !_UTILS_H