aboutsummaryrefslogtreecommitdiff
path: root/src/common/tests
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/tests')
-rw-r--r--src/common/tests/file_utils.cc55
-rw-r--r--src/common/tests/file_utils.h3
2 files changed, 58 insertions, 0 deletions
diff --git a/src/common/tests/file_utils.cc b/src/common/tests/file_utils.cc
index 0d9b04ad..1264b078 100644
--- a/src/common/tests/file_utils.cc
+++ b/src/common/tests/file_utils.cc
@@ -40,6 +40,61 @@
namespace google_breakpad {
+bool CopyFile(const char* from_path, const char* to_path) {
+ int infile = HANDLE_EINTR(open(from_path, O_RDONLY));
+ if (infile < 0) {
+ perror("open");
+ return false;
+ }
+
+ int outfile = HANDLE_EINTR(creat(to_path, 0666));
+ if (outfile < 0) {
+ perror("creat");
+ if (HANDLE_EINTR(close(infile)) < 0) {
+ perror("close");
+ }
+ return false;
+ }
+
+ char buffer[1024];
+ bool result = true;
+
+ while (result) {
+ ssize_t bytes_read = HANDLE_EINTR(read(infile, buffer, sizeof(buffer)));
+ if (bytes_read < 0) {
+ perror("read");
+ result = false;
+ break;
+ }
+ if (bytes_read == 0)
+ break;
+ ssize_t bytes_written_per_read = 0;
+ do {
+ ssize_t bytes_written_partial = HANDLE_EINTR(write(
+ outfile,
+ &buffer[bytes_written_per_read],
+ bytes_read - bytes_written_per_read));
+ if (bytes_written_partial < 0) {
+ perror("write");
+ result = false;
+ break;
+ }
+ bytes_written_per_read += bytes_written_partial;
+ } while (bytes_written_per_read < bytes_read);
+ }
+
+ if (HANDLE_EINTR(close(infile)) == -1) {
+ perror("close");
+ result = false;
+ }
+ if (HANDLE_EINTR(close(outfile)) == -1) {
+ perror("close");
+ result = false;
+ }
+
+ return result;
+}
+
bool ReadFile(const char* path, void* buffer, ssize_t* buffer_size) {
int fd = HANDLE_EINTR(open(path, O_RDONLY));
if (fd == -1) {
diff --git a/src/common/tests/file_utils.h b/src/common/tests/file_utils.h
index 52e8b009..c98a9bfa 100644
--- a/src/common/tests/file_utils.h
+++ b/src/common/tests/file_utils.h
@@ -35,6 +35,9 @@
namespace google_breakpad {
+// Copies a file from |from_path| to |to_path|. Returns true on success.
+bool CopyFile(const char* from_path, const char* to_path);
+
// Reads the content of a file at |path| into |buffer|. |buffer_size| specifies
// the size of |buffer| in bytes and returns the number of bytes read from the
// file on success. Returns true on success.