Helper.cc
2.95 KB
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
/**
* Helper.cc
*
* Created on: Dec 13, 2012
* Author: AKKA IS
*/
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>
#include <regex.h>
#include <sys/stat.h>
#include <cerrno>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <iostream>
#include <sstream>
#include "Helper.hh"
namespace AMDA {
namespace Helpers {
/**
* @brief get files in rootDir with pattern
* @return the number of match file
* @param rootDir path of entries search
* @param files return's list of match files
* @param pattern regexec pattern
*/
int Helper::getMatchFiles(const char* rootDir, std::vector<std::string> &files,
const char* pattern) {
DIR *dp;
errno = 0;
if ((dp = opendir(rootDir)) == NULL) {
return errno;
} else {
struct dirent *dirp;
while ((dirp = readdir(dp)) != NULL) {
if (format(dirp->d_name, pattern)) {
files.push_back(std::string(dirp->d_name));
}
}
closedir(dp);
}
return 0;
}
/**
* @brief return true if subject match format
*/
bool Helper::format(const char* subject, const char* format) {
bool ok = false;
regex_t re;
if (regcomp(&re, format, REG_EXTENDED | REG_NOSUB) != 0) {
/* Report error. */
} else {
int status = regexec(&re, subject, (size_t) 0, NULL, 0);
regfree(&re);
if (status != 0) {
/* Report error. */
} else {
ok = true;
}
}
return ok;
}
int Helper::mkdir(const char* path) {
errno = 0;
DIR *dir = opendir(path);
if(dir == NULL) {
errno = 0;
if (::mkdir(path, S_IRWXU|S_IRGRP|S_IXGRP) != 0) {
return errno;
}
} else {
closedir(dir);
}
return 0;
}
std::string Helper::_pid;
const std::string &Helper::getPID() {
if(_pid == "") {
std::stringstream buf;
buf << getpid();
_pid = buf.str();
}
return _pid;
}
time_t Helper::dateOfFile(const char* path) {
struct stat buf;
if(lstat(path, &buf) == 0) {
return buf.st_mtime;
}
return 0;
}
#define SIZE_LINE 128
bool Helper::SystemCommand(std::string command, log4cxx::LoggerPtr pLogger) {
bool anError = false;
std::string fullCommand = command + " 2>&1";
std::string outputCommand;
LOG4CXX_INFO(pLogger, fullCommand)
FILE *processOutput = popen(fullCommand.c_str(), "r");
char line[SIZE_LINE + 1];
while(fgets(line, SIZE_LINE, processOutput) != NULL) {
outputCommand += line;
}
int status = pclose(processOutput);
if (!WIFEXITED(status) ) {
anError = true;
LOG4CXX_ERROR(pLogger, "Compilation: " << outputCommand);
if (WIFSIGNALED(status)) {
LOG4CXX_ERROR(pLogger, "Compilation: " << "End cause by signal " << WTERMSIG(status));
}
if (WCOREDUMP(status)) {
LOG4CXX_ERROR(pLogger, "Compilation: " << " a core dump is generated");
}
}
else if(WEXITSTATUS(status) != 0) {
anError = true;
LOG4CXX_ERROR(pLogger, "Compilation: " << outputCommand);
LOG4CXX_ERROR(pLogger, "Compilation: " << "End with an error " << status);
}
return anError;
}
} /* namespace Parameters */
} /* namespace AMDA */