X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FSupport%2FMemoryBuffer.cpp;h=cb587d59c9291adb8fc1228525303bdc6167ce40;hb=2b8150318d66f21cd6ac36b2813165af833565a5;hp=e7080701eb4a4eaf75c8fe0e4afc75dbe8012bbb;hpb=a96a1824747632ce87ef065b4a13fb777d2b14d6;p=oota-llvm.git diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp index e7080701eb4..cb587d59c92 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/lib/Support/MemoryBuffer.cpp @@ -15,24 +15,27 @@ #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/SmallString.h" #include "llvm/Config/config.h" -#include "llvm/Support/MathExtras.h" #include "llvm/Support/Errno.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/MathExtras.h" #include "llvm/Support/Path.h" #include "llvm/Support/Process.h" #include "llvm/Support/Program.h" #include "llvm/Support/system_error.h" #include +#include #include #include -#include #include -#include #include +#include #if !defined(_MSC_VER) && !defined(__MINGW32__) #include #else #include +#ifndef S_ISFIFO +#define S_ISFIFO(x) (0) +#endif #endif #include using namespace llvm; @@ -201,6 +204,27 @@ public: }; } +static error_code getMemoryBufferForStream(int FD, + StringRef BufferName, + OwningPtr &result) { + const ssize_t ChunkSize = 4096*4; + SmallString Buffer; + ssize_t ReadBytes; + // Read into Buffer until we hit EOF. + do { + Buffer.reserve(Buffer.size() + ChunkSize); + ReadBytes = read(FD, Buffer.end(), ChunkSize); + if (ReadBytes == -1) { + if (errno == EINTR) continue; + return error_code(errno, posix_category()); + } + Buffer.set_size(Buffer.size() + ReadBytes); + } while (ReadBytes != 0); + + result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName)); + return error_code::success(); +} + error_code MemoryBuffer::getFile(StringRef Filename, OwningPtr &result, int64_t FileSize, @@ -297,6 +321,13 @@ error_code MemoryBuffer::getOpenFile(int FD, const char *Filename, if (fstat(FD, &FileInfo) == -1) { return error_code(errno, posix_category()); } + + // If this is a named pipe, we can't trust the size. Create the memory + // buffer by copying off the stream. + if (S_ISFIFO(FileInfo.st_mode)) { + return getMemoryBufferForStream(FD, Filename, result); + } + FileSize = FileInfo.st_size; } MapSize = FileSize; @@ -370,20 +401,5 @@ error_code MemoryBuffer::getSTDIN(OwningPtr &result) { // fallback if it fails. sys::Program::ChangeStdinToBinary(); - const ssize_t ChunkSize = 4096*4; - SmallString Buffer; - ssize_t ReadBytes; - // Read into Buffer until we hit EOF. - do { - Buffer.reserve(Buffer.size() + ChunkSize); - ReadBytes = read(0, Buffer.end(), ChunkSize); - if (ReadBytes == -1) { - if (errno == EINTR) continue; - return error_code(errno, posix_category()); - } - Buffer.set_size(Buffer.size() + ReadBytes); - } while (ReadBytes != 0); - - result.reset(getMemBufferCopy(Buffer, "")); - return error_code::success(); + return getMemoryBufferForStream(0, "", result); }