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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 // Copyright (c) 2011, 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.
    30 // file_utils.cc: Implement utility functions for file manipulation.
    31 // See file_utils.h for details.
    33 #include <fcntl.h>
    34 #include <stdio.h>
    35 #include <string.h>
    36 #include <sys/stat.h>
    37 #include <unistd.h>
    39 #include "common/linux/eintr_wrapper.h"
    40 #include "common/tests/file_utils.h"
    42 namespace google_breakpad {
    44 bool CopyFile(const char* from_path, const char* to_path) {
    45   int infile = HANDLE_EINTR(open(from_path, O_RDONLY));
    46   if (infile < 0) {
    47     perror("open");
    48     return false;
    49   }
    51   int outfile = HANDLE_EINTR(creat(to_path, 0666));
    52   if (outfile < 0) {
    53     perror("creat");
    54     if (HANDLE_EINTR(close(infile)) < 0) {
    55       perror("close");
    56     }
    57     return false;
    58   }
    60   char buffer[1024];
    61   bool result = true;
    63   while (result) {
    64     ssize_t bytes_read = HANDLE_EINTR(read(infile, buffer, sizeof(buffer)));
    65     if (bytes_read < 0) {
    66       perror("read");
    67       result = false;
    68       break;
    69     }
    70     if (bytes_read == 0)
    71       break;
    72     ssize_t bytes_written_per_read = 0;
    73     do {
    74       ssize_t bytes_written_partial = HANDLE_EINTR(write(
    75           outfile,
    76           &buffer[bytes_written_per_read],
    77           bytes_read - bytes_written_per_read));
    78       if (bytes_written_partial < 0) {
    79         perror("write");
    80         result = false;
    81         break;
    82       }
    83       bytes_written_per_read += bytes_written_partial;
    84     } while (bytes_written_per_read < bytes_read);
    85   }
    87   if (HANDLE_EINTR(close(infile)) == -1) {
    88     perror("close");
    89     result = false;
    90   }
    91   if (HANDLE_EINTR(close(outfile)) == -1) {
    92     perror("close");
    93     result = false;
    94   }
    96   return result;
    97 }
    99 bool ReadFile(const char* path, void* buffer, ssize_t* buffer_size) {
   100   int fd = HANDLE_EINTR(open(path, O_RDONLY));
   101   if (fd == -1) {
   102     perror("open");
   103     return false;
   104   }
   106   bool ok = true;
   107   if (buffer && buffer_size && *buffer_size > 0) {
   108     memset(buffer, 0, sizeof(*buffer_size));
   109     *buffer_size = HANDLE_EINTR(read(fd, buffer, *buffer_size));
   110     if (*buffer_size == -1) {
   111       perror("read");
   112       ok = false;
   113     }
   114   }
   115   if (HANDLE_EINTR(close(fd)) == -1) {
   116     perror("close");
   117     ok = false;
   118   }
   119   return ok;
   120 }
   122 bool WriteFile(const char* path, const void* buffer, size_t buffer_size) {
   123   int fd = HANDLE_EINTR(open(path, O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU));
   124   if (fd == -1) {
   125     perror("open");
   126     return false;
   127   }
   129   bool ok = true;
   130   if (buffer) {
   131     size_t bytes_written_total = 0;
   132     ssize_t bytes_written_partial = 0;
   133     const char* data = reinterpret_cast<const char*>(buffer);
   134     while (bytes_written_total < buffer_size) {
   135       bytes_written_partial =
   136           HANDLE_EINTR(write(fd, data + bytes_written_total,
   137                              buffer_size - bytes_written_total));
   138       if (bytes_written_partial < 0) {
   139         perror("write");
   140         ok = false;
   141         break;
   142       }
   143       bytes_written_total += bytes_written_partial;
   144     }
   145   }
   146   if (HANDLE_EINTR(close(fd)) == -1) {
   147     perror("close");
   148     ok = false;
   149   }
   150   return ok;
   151 }
   153 }  // namespace google_breakpad

mercurial