-
Notifications
You must be signed in to change notification settings - Fork 545
/
Utils.cpp
107 lines (95 loc) · 2.07 KB
/
Utils.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
#include <sys/types.h>
#include <sys/stat.h>
#include <cstdlib>
#include <climits>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include "Utils.h"
#define SPAWN_SERVER_SCRIPT_NAME "passenger-spawn-server"
namespace Passenger {
ostream *_debugStream = NULL;
void
initDebugging(const char *logFile) {
#ifdef PASSENGER_DEBUG
if (logFile != NULL) {
ostream *stream = new ofstream(logFile, ios_base::out | ios_base::app);
if (stream->fail()) {
delete stream;
} else {
if (_debugStream != NULL && _debugStream != &cerr) {
delete _debugStream;
}
_debugStream = stream;
}
} else {
_debugStream = &cerr;
}
#endif
}
int
atoi(const string &s) {
return ::atoi(s.c_str());
}
void
split(const string &str, char sep, vector<string> &output) {
string::size_type start, pos;
start = 0;
output.clear();
while ((pos = str.find(sep, start)) != string::npos) {
output.push_back(str.substr(start, pos - start));
start = pos + 1;
}
output.push_back(str.substr(start));
}
bool fileExists(const char *filename) {
struct stat buf;
if (stat(filename, &buf) == 0) {
return S_ISREG(buf.st_mode);
} else {
return false;
}
}
string
findSpawnServer() {
const char *path = getenv("PATH");
if (path == NULL) {
return "";
}
vector<string> paths;
split(getenv("PATH"), ':', paths);
for (vector<string>::const_iterator it(paths.begin()); it != paths.end(); it++) {
if (!it->empty() && (*it).at(0) == '/') {
string filename(*it);
filename.append("/" SPAWN_SERVER_SCRIPT_NAME);
if (fileExists(filename.c_str())) {
return filename;
}
}
}
return "";
}
string
canonicalizePath(const string &path) {
#ifdef __GLIBC__
// We're using a GNU extension here. See the 'BUGS'
// section of the realpath(3) Linux manpage for
// rationale.
char *tmp = realpath(path.c_str(), NULL);
if (tmp == NULL) {
return "";
} else {
string result(tmp);
free(tmp);
return result;
}
#else
char tmp[PATH_MAX];
if (realpath(path.c_str(), tmp) == NULL) {
return "";
} else {
return tmp;
}
#endif
}
} // namespace Passenger