summaryrefslogtreecommitdiffstats
path: root/lldb/source/Core/FileSpec.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lldb/source/Core/FileSpec.cpp')
-rw-r--r--lldb/source/Core/FileSpec.cpp48
1 files changed, 42 insertions, 6 deletions
diff --git a/lldb/source/Core/FileSpec.cpp b/lldb/source/Core/FileSpec.cpp
index 91ad6b04351..ea3d3af3c75 100644
--- a/lldb/source/Core/FileSpec.cpp
+++ b/lldb/source/Core/FileSpec.cpp
@@ -496,6 +496,41 @@ FileSpec::MemorySize() const
return m_filename.MemorySize() + m_directory.MemorySize();
}
+
+size_t
+FileSpec::ReadFileContents (off_t file_offset, void *dst, size_t dst_len) const
+{
+ size_t bytes_read = 0;
+ char resolved_path[PATH_MAX];
+ if (GetPath(resolved_path, sizeof(resolved_path)))
+ {
+ int fd = ::open (resolved_path, O_RDONLY, 0);
+ if (fd != -1)
+ {
+ struct stat file_stats;
+ if (::fstat (fd, &file_stats) == 0)
+ {
+ // Read bytes directly into our basic_string buffer
+ if (file_stats.st_size > 0)
+ {
+ off_t lseek_result = 0;
+ if (file_offset > 0)
+ lseek_result = ::lseek (fd, file_offset, SEEK_SET);
+
+ if (lseek_result == file_offset)
+ {
+ ssize_t n = ::read (fd, dst, dst_len);
+ if (n >= 0)
+ bytes_read = n;
+ }
+ }
+ }
+ }
+ close(fd);
+ }
+ return bytes_read;
+}
+
//------------------------------------------------------------------
// Returns a shared pointer to a data buffer that contains all or
// part of the contents of a file. The data copies into a heap based
@@ -508,7 +543,7 @@ FileSpec::MemorySize() const
// verified using the DataBuffer::GetByteSize() function.
//------------------------------------------------------------------
DataBufferSP
-FileSpec::ReadFileContents(off_t file_offset, size_t file_size) const
+FileSpec::ReadFileContents (off_t file_offset, size_t file_size) const
{
DataBufferSP data_sp;
char resolved_path[PATH_MAX];
@@ -520,7 +555,6 @@ FileSpec::ReadFileContents(off_t file_offset, size_t file_size) const
struct stat file_stats;
if (::fstat (fd, &file_stats) == 0)
{
- // Read bytes directly into our basic_string buffer
if (file_stats.st_size > 0)
{
off_t lseek_result = 0;
@@ -533,11 +567,13 @@ FileSpec::ReadFileContents(off_t file_offset, size_t file_size) const
}
else if (lseek_result == file_offset)
{
+ const size_t bytes_left = file_stats.st_size - file_offset;
+ size_t num_bytes_to_read = file_size;
+ if (num_bytes_to_read > bytes_left)
+ num_bytes_to_read = bytes_left;
+
std::auto_ptr<DataBufferHeap> data_heap_ap;
- if (file_stats.st_size < file_size)
- data_heap_ap.reset(new DataBufferHeap(file_stats.st_size, '\0'));
- else
- data_heap_ap.reset(new DataBufferHeap(file_size, '\0'));
+ data_heap_ap.reset(new DataBufferHeap(num_bytes_to_read, '\0'));
if (data_heap_ap.get())
{
OpenPOWER on IntegriCloud