1// Copyright (c) 2012 Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14//     * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include <stdio.h>
31
32#include "client/linux/handler/minidump_descriptor.h"
33
34#include "common/linux/guid_creator.h"
35
36namespace google_breakpad {
37
38//static
39const MinidumpDescriptor::MicrodumpOnConsole
40    MinidumpDescriptor::kMicrodumpOnConsole = {};
41
42MinidumpDescriptor::MinidumpDescriptor(const MinidumpDescriptor& descriptor)
43    : mode_(descriptor.mode_),
44      fd_(descriptor.fd_),
45      directory_(descriptor.directory_),
46      c_path_(NULL),
47      size_limit_(descriptor.size_limit_) {
48  // The copy constructor is not allowed to be called on a MinidumpDescriptor
49  // with a valid path_, as getting its c_path_ would require the heap which
50  // can cause problems in compromised environments.
51  assert(descriptor.path_.empty());
52}
53
54MinidumpDescriptor& MinidumpDescriptor::operator=(
55    const MinidumpDescriptor& descriptor) {
56  assert(descriptor.path_.empty());
57
58  mode_ = descriptor.mode_;
59  fd_ = descriptor.fd_;
60  directory_ = descriptor.directory_;
61  path_.clear();
62  if (c_path_) {
63    // This descriptor already had a path set, so generate a new one.
64    c_path_ = NULL;
65    UpdatePath();
66  }
67  size_limit_ = descriptor.size_limit_;
68  return *this;
69}
70
71void MinidumpDescriptor::UpdatePath() {
72  assert(mode_ == kWriteMinidumpToFile && !directory_.empty());
73
74  GUID guid;
75  char guid_str[kGUIDStringLength + 1];
76  if (!CreateGUID(&guid) || !GUIDToString(&guid, guid_str, sizeof(guid_str))) {
77    assert(false);
78  }
79
80  path_.clear();
81  path_ = directory_ + "/" + guid_str + ".dmp";
82  c_path_ = path_.c_str();
83}
84
85}  // namespace google_breakpad
86