llvm.org GIT mirror llvm / 7895353
MemoryBuffer: Support reading named pipes in getFile(). - We only support this when the client didn't claim to know the file size. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@167407 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 6 years ago
1 changed file(s) with 30 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
200200 };
201201 }
202202
203 static error_code getMemoryBufferForStream(int FD,
204 StringRef BufferName,
205 OwningPtr &result) {
206 const ssize_t ChunkSize = 4096*4;
207 SmallString Buffer;
208 ssize_t ReadBytes;
209 // Read into Buffer until we hit EOF.
210 do {
211 Buffer.reserve(Buffer.size() + ChunkSize);
212 ReadBytes = read(FD, Buffer.end(), ChunkSize);
213 if (ReadBytes == -1) {
214 if (errno == EINTR) continue;
215 return error_code(errno, posix_category());
216 }
217 Buffer.set_size(Buffer.size() + ReadBytes);
218 } while (ReadBytes != 0);
219
220 result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
221 return error_code::success();
222 }
223
203224 error_code MemoryBuffer::getFile(StringRef Filename,
204225 OwningPtr &result,
205226 int64_t FileSize,
296317 if (fstat(FD, &FileInfo) == -1) {
297318 return error_code(errno, posix_category());
298319 }
320
321 // If this is a named pipe, we can't trust the size. Create the memory
322 // buffer by copying off the stream.
323 if (FileInfo.st_mode & S_IFIFO) {
324 return getMemoryBufferForStream(FD, Filename, result);
325 }
326
299327 FileSize = FileInfo.st_size;
300328 }
301329 MapSize = FileSize;
369397 // fallback if it fails.
370398 sys::Program::ChangeStdinToBinary();
371399
372 const ssize_t ChunkSize = 4096*4;
373 SmallString Buffer;
374 ssize_t ReadBytes;
375 // Read into Buffer until we hit EOF.
376 do {
377 Buffer.reserve(Buffer.size() + ChunkSize);
378 ReadBytes = read(0, Buffer.end(), ChunkSize);
379 if (ReadBytes == -1) {
380 if (errno == EINTR) continue;
381 return error_code(errno, posix_category());
382 }
383 Buffer.set_size(Buffer.size() + ReadBytes);
384 } while (ReadBytes != 0);
385
386 result.reset(getMemBufferCopy(Buffer, ""));
387 return error_code::success();
388 }
400 return getMemoryBufferForStream(0, "", result);
401 }