From 6dd21d3baf490a34af0b1b1f5a48f8443dcb1ea6 Mon Sep 17 00:00:00 2001 From: mmentovai Date: Fri, 8 Sep 2006 16:41:17 +0000 Subject: Change interface for providing files to Minidump (#19). r=bryner - Interface change: Minidump constructor now accepts a const string& path argument instead of int fd. Minidump will open the file on first Read and close it upon destruction. - Adapt callers to new interface, no longer leaking file descriptors. http://groups.google.com/group/airbag-dev/browse_thread/thread/ff24dbcde7db8ae3 git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@20 4c0a9323-5329-0410-9bdc-e9ce6186880e --- src/processor/minidump.cc | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'src/processor/minidump.cc') diff --git a/src/processor/minidump.cc b/src/processor/minidump.cc index 6c4226b3..1a82e0f0 100644 --- a/src/processor/minidump.cc +++ b/src/processor/minidump.cc @@ -19,14 +19,18 @@ // Author: Mark Mentovai +#include #include #include #include #ifdef _WIN32 #include typedef SSIZE_T ssize_t; +#define open _open #define read _read #define lseek _lseek +#else // _WIN32 +#define O_BINARY 0 #endif // _WIN32 #include @@ -1693,11 +1697,12 @@ void MinidumpMiscInfo::Print() { // -Minidump::Minidump(int fd) +Minidump::Minidump(const string& path) : header_(), directory_(NULL), stream_map_(NULL), - fd_(fd), + path_(path), + fd_(-1), swap_(false), valid_(false) { } @@ -1706,6 +1711,25 @@ Minidump::Minidump(int fd) Minidump::~Minidump() { delete directory_; delete stream_map_; + if (fd_ != -1) + close(fd_); +} + + +bool Minidump::Open() { + if (fd_ != -1) { + // The file is already open. Seek to the beginning, which is the position + // the file would be at if it were opened anew. + return SeekSet(0); + } + + // O_BINARY is useful (and defined) on Windows. On other platforms, it's + // useless, and because it's defined as 0 above, harmless. + fd_ = open(path_.c_str(), O_RDONLY | O_BINARY); + if (fd_ == -1) + return false; + + return true; } @@ -1718,6 +1742,9 @@ bool Minidump::Read() { valid_ = false; + if (!Open()) + return false; + if (!ReadBytes(&header_, sizeof(MDRawHeader))) return false; -- cgit v1.2.1