Sha256: 92314e12392ce5727ca2842adfaa8bc95b0fbb23c50695975cec5088b7ef2ea2

Contents?: true

Size: 1.84 KB

Versions: 18

Compression:

Stored size: 1.84 KB

Contents

#include <unistd.h>
#include "Support.h"

namespace Test {

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;
}

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
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, &times);
	}
}

} // namespace Test

Version data entries

18 entries across 18 versions & 4 rubygems

Version Path
vanity-1.7.1 vendor/ruby/1.9.1/gems/passenger-2.2.15/test/support/Support.cpp
passenger-2.2.15 test/support/Support.cpp
passenger-2.2.14 test/support/Support.cpp
passenger-2.2.13 test/support/Support.cpp
passenger-2.2.12 test/support/Support.cpp
colouringcode-passenger-0.2 test/support/Support.cpp
passenger-2.2.11 test/support/Support.cpp
passenger-jmazzi-2.2.10 test/support/Support.cpp
passenger-2.2.10 test/support/Support.cpp
passenger-jmazzi-2.2.9 test/support/Support.cpp
passenger-2.2.9 test/support/Support.cpp
passenger-2.2.8 test/support/Support.cpp
colouringcode-passenger-0.1 test/support/Support.cpp
passenger-2.2.7 test/support/Support.cpp
passenger-2.2.6 test/support/Support.cpp
passenger-2.2.5 test/support/Support.cpp
passenger-2.2.3 test/support/Support.cpp
passenger-2.2.4 test/support/Support.cpp