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
|
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include "TestSupport.h"
namespace TestSupport {
void createServerInstanceDirAndGeneration(ServerInstanceDirPtr &serverInstanceDir,
ServerInstanceDir::GenerationPtr &generation)
{
serverInstanceDir.reset(new ServerInstanceDir(getpid()));
generation = serverInstanceDir->newGeneration(geteuid() == 0,
"nobody", getPrimaryGroupName("nobody"),
geteuid(), getegid());
}
string
readAll(const string &filename) {
FILE *f = fopen(filename.c_str(), "rb");
if (f != NULL) {
try {
string result = readAll(fileno(f));
fclose(f);
return result;
} catch (...) {
fclose(f);
throw;
}
} else {
int e = errno;
throw FileSystemException("Cannot open '" + filename + "' for reading",
e, filename);
}
}
string
readAll(int fd) {
string result;
char buf[1024 * 32];
ssize_t ret;
while (true) {
do {
ret = read(fd, buf, sizeof(buf));
} while (ret == -1 && errno == EINTR);
if (ret == 0) {
break;
} else if (ret == -1) {
throw SystemException("Cannot read from socket", errno);
} else {
result.append(buf, ret);
}
}
return result;
}
void
writeUntilFull(int fd) {
int flags, ret;
char buf[1024];
memset(buf, 0, sizeof(buf));
flags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
while (true) {
ret = write(fd, buf, sizeof(buf));
if (ret == -1) {
int e = errno;
if (e == EAGAIN) {
break;
} else {
throw SystemException("write() failed", e);
}
}
}
while (true) {
ret = write(fd, buf, 50);
if (ret == -1) {
int e = errno;
if (e == EAGAIN) {
break;
} else {
throw SystemException("write() failed", e);
}
}
}
while (true) {
ret = write(fd, buf, 1);
if (ret == -1) {
int e = errno;
if (e == EAGAIN) {
break;
} else {
throw SystemException("write() failed", e);
}
}
}
fcntl(fd, F_SETFL, flags);
}
string
replaceString(const string &str, const string &toFind, const string &replaceWith) {
string::size_type pos = str.find(toFind);
if (pos == string::npos) {
return str;
} else {
string result(str);
return result.replace(pos, toFind.size(), replaceWith);
}
}
void
replaceStringInFile(const char *filename, const string &toFind, const string &replaceWith) {
FILE *f = fopen(filename, "r");
if (f == NULL) {
int e = errno;
string message = "Cannot open file '";
message.append(filename);
message.append("' for reading");
throw FileSystemException(message, e, filename);
}
string content(readAll(fileno(f)));
fclose(f);
f = fopen(filename, "w");
if (f == NULL) {
int e = errno;
string message = "Cannot open file '";
message.append(filename);
message.append("' for writing");
throw FileSystemException(message, e, filename);
}
content = replaceString(content, toFind, replaceWith);
fwrite(content.data(), 1, content.size(), f);
fclose(f);
}
void
writeFile(const string &filename, const string &contents) {
FILE *f = fopen(filename.c_str(), "w");
if (f == NULL) {
int e = errno;
string message = "Cannot open file '";
message.append(filename);
message.append("' for writing");
throw FileSystemException(message, e, filename);
}
fwrite(contents.data(), 1, contents.size(), f);
fclose(f);
}
void
touchFile(const char *filename, time_t timestamp) {
FILE *f = fopen(filename, "a");
if (f != NULL) {
fclose(f);
} else if (errno != EISDIR) {
int e = errno;
string message = "Cannot touch file '";
message.append(filename);
message.append("'");
throw FileSystemException(message, e, filename);
}
if (timestamp != (time_t) -1) {
struct utimbuf times;
times.actime = timestamp;
times.modtime = timestamp;
utime(filename, ×);
}
}
vector<string>
listDir(const string &path) {
vector<string> result;
DIR *d = opendir(path.c_str());
struct dirent *ent;
if (d == NULL) {
int e = errno;
throw FileSystemException("Cannot open directory " + path,
e, path);
}
while ((ent = readdir(d)) != NULL) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
continue;
}
result.push_back(ent->d_name);
}
return result;
}
string
getPrimaryGroupName(const string &username) {
struct passwd *user;
struct group *group;
user = getpwnam(username.c_str());
if (user == NULL) {
throw RuntimeException(string("User '") + username + "' does not exist.");
}
group = getgrgid(user->pw_gid);
if (group == NULL) {
throw RuntimeException(string("Primary group for user '") + username + "' does not exist.");
}
return group->gr_name;
}
} // namespace TestSupport
|