-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.h
131 lines (112 loc) · 3.79 KB
/
util.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
// Copyright (C) 2019 by Yuri Victorovich. All rights reserved.
#pragma once
#include <string>
#include <vector>
#include <set>
#include <iostream>
#include <sstream>
#include <memory>
#include <functional>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <rang.hpp>
//
// utility macros useful throughout the program
//
#define STR(msg...) \
([&]() { \
std::ostringstream ss; \
ss << msg; \
return ss.str(); \
}())
#define STRg(msg...) \
([]() { \
std::ostringstream ss; \
ss << msg; \
return ss.str(); \
}())
#define CSTR(msg...) (STR(msg).c_str())
template<typename T>
inline std::ostream& operator<<(std::ostream &os, const std::vector<T> &v) {
bool fst = true;
for (auto &e : v) {
if (fst)
fst = false;
else
os << " ";
os << e;
}
return os;
}
template<typename T>
inline std::vector<T> operator+(const std::vector<T> &v1, const std::vector<T> &v2) {
std::vector<T> res = v1;
res.insert(res.end(), v2.begin(), v2.end());
return res;
}
class Run {
public:
Run(const std::function<void()> &fnAction) { fnAction(); }
};
class OnDestroy {
std::function<void()> fnAction;
public:
OnDestroy(const std::function<void()> &newFnAction);
~OnDestroy();
void doNow();
};
class RunAtEnd : public std::unique_ptr<OnDestroy> {
public:
RunAtEnd();
RunAtEnd(const std::function<void()> &newFnAction);
void reset(const std::function<void()> &newFnAction);
void doNow();
};
//
// utility functions
//
namespace Util {
void runCommand(const std::string &cmd, const std::string &what);
std::string runCommandGetOutput(const std::string &cmd, const std::string &what);
void ckSyscallError(int res, const char *syscall, const char *arg, const std::function<bool(int)> whiteWash = [](int err) {return false;});
std::string tmSecMs();
std::string filePathToBareName(const std::string &path);
std::string filePathToFileName(const std::string &path);
int getSysctlInt(const char *name);
void setSysctlInt(const char *name, int value);
std::string getSysctlString(const char *name);
void ensureKernelModuleIsLoaded(const char *name);
std::string gethostname();
std::vector<std::string> splitString(const std::string &str, const std::string &delimiter);
std::string stripTrailingSpace(const std::string &str);
unsigned toUInt(const std::string &str);
std::string pathSubstituteVarsInPath(const std::string &path);
std::string pathSubstituteVarsInString(const std::string &str);
std::vector<std::string> reverseVector(const std::vector<std::string> &v);
namespace Fs {
bool fileExists(const std::string &path);
bool dirExists(const std::string &path);
std::vector<std::string> readFileLines(int fd);
size_t getFileSize(int fd);
void writeFile(const std::string &data, int fd);
void writeFile(const std::string &data, const std::string &file);
void appendFile(const std::string &data, const std::string &file);
void chmod(const std::string &path, mode_t mode);
void chown(const std::string &path, uid_t owner, gid_t group);
void link(const std::string &name1, const std::string &name2);
void unlink(const std::string &file);
void mkdir(const std::string &dir, mode_t mode);
void rmdir(const std::string &dir);
void rmdirFlat(const std::string &dir);
void rmdirHier(const std::string &dir);
bool rmdirFlatExcept(const std::string &dir, const std::set<std::string> &except);
bool rmdirHierExcept(const std::string &dir, const std::set<std::string> &except);
bool isXzArchive(const char *file);
char isElfFileOrDir(const std::string &file); // returns 'E'LF, 'D'ir, or 'N'o
std::set<std::string> findElfFiles(const std::string &dir);
bool hasExtension(const char *file, const char *extension);
void copyFile(const std::string &srcFile, const std::string &dstFile);
std::vector<std::string> expandWildcards(const std::string &wildcardPath, const std::string &cmdPrefix = "");
}
}