-
Notifications
You must be signed in to change notification settings - Fork 1
/
osal.cpp
216 lines (197 loc) · 4.33 KB
/
osal.cpp
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
#include "osal.hpp"
#include "error.hpp"
#include <iostream>
void execShowCmd(const std::string &cmd)
{
std::cout << cmd << std::endl;
exec(cmd);
}
#ifndef _WIN32
#include <cstdlib>
#include <cstring>
#include <dirent.h>
#include <dlfcn.h>
#include <limits.h>
#include <sstream>
#include <stdexcept>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
std::string getDirSeparator()
{
return "/";
}
std::string getCurrentWorkingDir()
{
std::string res;
char *cres = getcwd(nullptr, 0);
res = cres;
free(cres);
return res;
}
std::string getExecPath()
{
char buf[PATH_MAX];
#ifdef __APPLE__
uint32_t size = sizeof(buf);
if (_NSGetExecutablePath(buf, &size) == 0)
return buf;
#else
int count = readlink("/proc/self/exe", buf, sizeof(buf));
if (count >= 0)
return std::string{buf, buf + count};
#endif
return std::string();
}
std::vector<std::string> getFilesList(const std::string &dirPath)
{
std::vector<std::string> res;
DIR *dir;
struct dirent *ent;
if ((dir = opendir(dirPath.c_str())) != NULL)
{
while ((ent = readdir(dir)) != NULL)
if (ent->d_type == DT_LNK || ent->d_type == DT_REG)
res.push_back(ent->d_name);
closedir(dir);
}
return res;
}
time_t getFileModification(const std::string &fileName)
{
struct stat buffer;
if (stat(fileName.c_str(), &buffer) != 0)
return 0;
return buffer.st_mtime;
}
bool isDirExist(const std::string &dir)
{
struct stat buffer;
if (stat(dir.c_str(), &buffer) != 0)
return false;
return (buffer.st_mode & S_IFDIR) != 0;
}
void exec(const std::string &cmd)
{
auto res = system(cmd.c_str());
if (res != 0)
{
if (WIFSIGNALED(res) && (WTERMSIG(res) == SIGINT || WTERMSIG(res) == SIGQUIT))
THROW_ERROR("Interrupt");
if (WIFEXITED(res))
throw WEXITSTATUS(res);
}
}
void makeDir(const std::string &dir)
{
std::istringstream strm(dir);
std::string subDir;
std::string tmp;
auto dirCreated{false};
while (std::getline(strm, tmp, '/'))
{
subDir += tmp;
subDir += "/";
auto res = mkdir(subDir.c_str(), 0777);
if (res != 0)
{
auto err = errno;
if (err != EEXIST)
THROW_ERROR("makeDir(" << dir << "): " << strerror(err));
}
else
dirCreated = true;
}
if (dirCreated)
std::cout << "Make directory: " << dir << "\n";
}
#else
#include <sys/stat.h>
#include <sys/types.h>
#include <windows.h>
#include <algorithm>
std::string getDirSeparator()
{
return "\\";
}
std::string getCurrentWorkingDir()
{
TCHAR NPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH, NPath);
std::transform(
NPath, NPath + strlen(NPath), NPath, [](char ch) { return ch == '\\' ? '/' : ch; });
return NPath;
}
std::string getExecPath()
{
char buffer[MAX_PATH];
GetModuleFileName(nullptr, buffer, MAX_PATH);
return buffer;
}
std::vector<std::string> getFilesList(const std::string &dirPath)
{
std::vector<std::string> res;
WIN32_FIND_DATA fd;
HANDLE handle = FindFirstFile((dirPath + "\\*").c_str(), &fd);
if (handle == INVALID_HANDLE_VALUE)
return res;
do
{
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
continue;
res.push_back(fd.cFileName);
} while (::FindNextFile(handle, &fd));
::FindClose(handle);
return res;
}
time_t getFileModification(const std::string &fileName)
{
struct _stat buffer;
if (_stat(fileName.c_str(), &buffer) != 0)
return 0;
return buffer.st_mtime;
}
bool isDirExist(const std::string &dir)
{
struct _stat buffer;
if (_stat(dir.c_str(), &buffer) != 0)
return false;
return (buffer.st_mode & _S_IFDIR) != 0;
}
void exec(const std::string &cmd)
{
auto res = system(("bash -c '" + cmd + "'").c_str());
if (res != 0)
{
throw res;
}
}
void makeDir(const std::string &dir)
{
std::cout << "Make directory: " << dir << "\n";
std::istringstream strm(dir);
std::string subDir;
std::string tmp;
auto dirCreated{false};
while (std::getline(strm, tmp, '/'))
{
subDir += tmp;
auto res = CreateDirectory(subDir.c_str(), nullptr);
if (!res)
{
if (GetLastError() == ERROR_PATH_NOT_FOUND)
THROW_ERROR("makeDir(" << dir << "): path not found");
}
else
dirCreated = true;
subDir += "\\";
}
if (dirCreated)
std::cout << "Make directory: " << dir << "\n";
}
#endif