toolkit/crashreporter/google-breakpad/src/common/tests/file_utils.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/tests/file_utils.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,153 @@
     1.4 +// Copyright (c) 2011, Google Inc.
     1.5 +// All rights reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +
    1.33 +// file_utils.cc: Implement utility functions for file manipulation.
    1.34 +// See file_utils.h for details.
    1.35 +
    1.36 +#include <fcntl.h>
    1.37 +#include <stdio.h>
    1.38 +#include <string.h>
    1.39 +#include <sys/stat.h>
    1.40 +#include <unistd.h>
    1.41 +
    1.42 +#include "common/linux/eintr_wrapper.h"
    1.43 +#include "common/tests/file_utils.h"
    1.44 +
    1.45 +namespace google_breakpad {
    1.46 +
    1.47 +bool CopyFile(const char* from_path, const char* to_path) {
    1.48 +  int infile = HANDLE_EINTR(open(from_path, O_RDONLY));
    1.49 +  if (infile < 0) {
    1.50 +    perror("open");
    1.51 +    return false;
    1.52 +  }
    1.53 +
    1.54 +  int outfile = HANDLE_EINTR(creat(to_path, 0666));
    1.55 +  if (outfile < 0) {
    1.56 +    perror("creat");
    1.57 +    if (HANDLE_EINTR(close(infile)) < 0) {
    1.58 +      perror("close");
    1.59 +    }
    1.60 +    return false;
    1.61 +  }
    1.62 +
    1.63 +  char buffer[1024];
    1.64 +  bool result = true;
    1.65 +
    1.66 +  while (result) {
    1.67 +    ssize_t bytes_read = HANDLE_EINTR(read(infile, buffer, sizeof(buffer)));
    1.68 +    if (bytes_read < 0) {
    1.69 +      perror("read");
    1.70 +      result = false;
    1.71 +      break;
    1.72 +    }
    1.73 +    if (bytes_read == 0)
    1.74 +      break;
    1.75 +    ssize_t bytes_written_per_read = 0;
    1.76 +    do {
    1.77 +      ssize_t bytes_written_partial = HANDLE_EINTR(write(
    1.78 +          outfile,
    1.79 +          &buffer[bytes_written_per_read],
    1.80 +          bytes_read - bytes_written_per_read));
    1.81 +      if (bytes_written_partial < 0) {
    1.82 +        perror("write");
    1.83 +        result = false;
    1.84 +        break;
    1.85 +      }
    1.86 +      bytes_written_per_read += bytes_written_partial;
    1.87 +    } while (bytes_written_per_read < bytes_read);
    1.88 +  }
    1.89 +
    1.90 +  if (HANDLE_EINTR(close(infile)) == -1) {
    1.91 +    perror("close");
    1.92 +    result = false;
    1.93 +  }
    1.94 +  if (HANDLE_EINTR(close(outfile)) == -1) {
    1.95 +    perror("close");
    1.96 +    result = false;
    1.97 +  }
    1.98 +
    1.99 +  return result;
   1.100 +}
   1.101 +
   1.102 +bool ReadFile(const char* path, void* buffer, ssize_t* buffer_size) {
   1.103 +  int fd = HANDLE_EINTR(open(path, O_RDONLY));
   1.104 +  if (fd == -1) {
   1.105 +    perror("open");
   1.106 +    return false;
   1.107 +  }
   1.108 +
   1.109 +  bool ok = true;
   1.110 +  if (buffer && buffer_size && *buffer_size > 0) {
   1.111 +    memset(buffer, 0, sizeof(*buffer_size));
   1.112 +    *buffer_size = HANDLE_EINTR(read(fd, buffer, *buffer_size));
   1.113 +    if (*buffer_size == -1) {
   1.114 +      perror("read");
   1.115 +      ok = false;
   1.116 +    }
   1.117 +  }
   1.118 +  if (HANDLE_EINTR(close(fd)) == -1) {
   1.119 +    perror("close");
   1.120 +    ok = false;
   1.121 +  }
   1.122 +  return ok;
   1.123 +}
   1.124 +
   1.125 +bool WriteFile(const char* path, const void* buffer, size_t buffer_size) {
   1.126 +  int fd = HANDLE_EINTR(open(path, O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU));
   1.127 +  if (fd == -1) {
   1.128 +    perror("open");
   1.129 +    return false;
   1.130 +  }
   1.131 +
   1.132 +  bool ok = true;
   1.133 +  if (buffer) {
   1.134 +    size_t bytes_written_total = 0;
   1.135 +    ssize_t bytes_written_partial = 0;
   1.136 +    const char* data = reinterpret_cast<const char*>(buffer);
   1.137 +    while (bytes_written_total < buffer_size) {
   1.138 +      bytes_written_partial =
   1.139 +          HANDLE_EINTR(write(fd, data + bytes_written_total,
   1.140 +                             buffer_size - bytes_written_total));
   1.141 +      if (bytes_written_partial < 0) {
   1.142 +        perror("write");
   1.143 +        ok = false;
   1.144 +        break;
   1.145 +      }
   1.146 +      bytes_written_total += bytes_written_partial;
   1.147 +    }
   1.148 +  }
   1.149 +  if (HANDLE_EINTR(close(fd)) == -1) {
   1.150 +    perror("close");
   1.151 +    ok = false;
   1.152 +  }
   1.153 +  return ok;
   1.154 +}
   1.155 +
   1.156 +}  // namespace google_breakpad

mercurial