Commit da17193a authored by Kenton Varda's avatar Kenton Varda

Make sure compiler can accept source files over pipes by falling back to reading into a buffer.

parent 21a508bb
...@@ -80,6 +80,8 @@ kj::Array<const char> mmapForRead(kj::StringPtr filename) { ...@@ -80,6 +80,8 @@ kj::Array<const char> mmapForRead(kj::StringPtr filename) {
struct stat stats; struct stat stats;
KJ_SYSCALL(fstat(fd, &stats)); KJ_SYSCALL(fstat(fd, &stats));
if (S_ISREG(stats.st_mode)) {
// Regular file. Just mmap() it.
const void* mapping = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0); const void* mapping = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (mapping == MAP_FAILED) { if (mapping == MAP_FAILED) {
KJ_FAIL_SYSCALL("mmap", errno, filename); KJ_FAIL_SYSCALL("mmap", errno, filename);
...@@ -87,6 +89,21 @@ kj::Array<const char> mmapForRead(kj::StringPtr filename) { ...@@ -87,6 +89,21 @@ kj::Array<const char> mmapForRead(kj::StringPtr filename) {
return kj::Array<const char>( return kj::Array<const char>(
reinterpret_cast<const char*>(mapping), stats.st_size, mmapDisposer); reinterpret_cast<const char*>(mapping), stats.st_size, mmapDisposer);
} else {
// This could be a stream of some sort, like a pipe. Fall back to read().
// TODO(cleanup): This does a lot of copies. Not sure I care.
kj::Vector<char> data(8192);
char buffer[4096];
for (;;) {
ssize_t n;
KJ_SYSCALL(n = read(fd, buffer, sizeof(buffer)));
if (n == 0) break;
data.addAll(buffer, buffer + n);
}
return data.releaseAsArray();
}
} }
static char* canonicalizePath(char* path) { static char* canonicalizePath(char* path) {
......
...@@ -350,6 +350,8 @@ kj::Array<const char> SchemaFile::DiskFileReader::read(kj::StringPtr path) const ...@@ -350,6 +350,8 @@ kj::Array<const char> SchemaFile::DiskFileReader::read(kj::StringPtr path) const
struct stat stats; struct stat stats;
KJ_SYSCALL(fstat(fd, &stats)); KJ_SYSCALL(fstat(fd, &stats));
if (S_ISREG(stats.st_mode)) {
// Regular file. Just mmap() it.
const void* mapping = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0); const void* mapping = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (mapping == MAP_FAILED) { if (mapping == MAP_FAILED) {
KJ_FAIL_SYSCALL("mmap", errno, path); KJ_FAIL_SYSCALL("mmap", errno, path);
...@@ -357,6 +359,21 @@ kj::Array<const char> SchemaFile::DiskFileReader::read(kj::StringPtr path) const ...@@ -357,6 +359,21 @@ kj::Array<const char> SchemaFile::DiskFileReader::read(kj::StringPtr path) const
return kj::Array<const char>( return kj::Array<const char>(
reinterpret_cast<const char*>(mapping), stats.st_size, mmapDisposer); reinterpret_cast<const char*>(mapping), stats.st_size, mmapDisposer);
} else {
// This could be a stream of some sort, like a pipe. Fall back to read().
// TODO(cleanup): This does a lot of copies. Not sure I care.
kj::Vector<char> data(8192);
char buffer[4096];
for (;;) {
ssize_t n;
KJ_SYSCALL(n = ::read(fd, buffer, sizeof(buffer)));
if (n == 0) break;
data.addAll(buffer, buffer + n);
}
return data.releaseAsArray();
}
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment