| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/nacl/nonsfi/file_util.h" |
| 6 |
| 5 #include <fcntl.h> | 7 #include <fcntl.h> |
| 6 #include <unistd.h> | 8 #include <unistd.h> |
| 9 |
| 10 #include <memory> |
| 7 #include <vector> | 11 #include <vector> |
| 8 | 12 |
| 9 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 10 #include "mojo/services/files/interfaces/files.mojom.h" | 14 #include "mojo/services/files/interfaces/files.mojom.h" |
| 11 #include "mojo/tools/embed/data.h" | 15 #include "mojo/tools/embed/data.h" |
| 12 | 16 |
| 13 namespace nacl { | 17 namespace nacl { |
| 14 | 18 |
| 15 int DataToTempFileDescriptor(const mojo::embed::Data& data) { | 19 int DataToTempFileDescriptor(const mojo::embed::Data& data) { |
| 16 base::FilePath path; | 20 base::FilePath path; |
| 17 CHECK(CreateTemporaryFile(&path)) << "Could not create temp file for data"; | 21 CHECK(base::CreateTemporaryFile(&path)) |
| 22 << "Could not create temp file for data"; |
| 18 | 23 |
| 19 int fd = open(path.value().c_str(), O_RDWR); | 24 int fd = open(path.value().c_str(), O_RDWR); |
| 20 CHECK_GE(fd, 0) << "Could not open temporary file"; | 25 CHECK_GE(fd, 0) << "Could not open temporary file"; |
| 21 | 26 |
| 22 size_t bytes_left_to_write = data.size; | 27 size_t bytes_left_to_write = data.size; |
| 23 const char* source = data.data; | 28 const char* source = data.data; |
| 24 while (bytes_left_to_write > 0) { | 29 while (bytes_left_to_write > 0) { |
| 25 ssize_t bytes = HANDLE_EINTR(write(fd, source, bytes_left_to_write)); | 30 ssize_t bytes = HANDLE_EINTR(write(fd, source, bytes_left_to_write)); |
| 26 CHECK_GE(bytes, 0) << "Error writing data to temp file"; | 31 CHECK_GE(bytes, 0) << "Error writing data to temp file"; |
| 27 bytes_left_to_write -= bytes; | 32 bytes_left_to_write -= bytes; |
| 28 source += bytes; | 33 source += bytes; |
| 29 } | 34 } |
| 30 | 35 |
| 31 CHECK(!unlink(path.value().c_str())) << "Could not unlink temporary file"; | 36 CHECK(!unlink(path.value().c_str())) << "Could not unlink temporary file"; |
| 32 return fd; | 37 return fd; |
| 33 } | 38 } |
| 34 | 39 |
| 35 int MojoFileToTempFileDescriptor(mojo::files::FilePtr mojo_file) { | 40 int MojoFileToTempFileDescriptor(mojo::files::FilePtr mojo_file) { |
| 36 base::FilePath path; | 41 base::FilePath path; |
| 37 CHECK(CreateTemporaryFile(&path)) << "Could not create temp file for data"; | 42 CHECK(base::CreateTemporaryFile(&path)) |
| 43 << "Could not create temp file for data"; |
| 38 | 44 |
| 39 int fd = open(path.value().c_str(), O_RDWR); | 45 int fd = open(path.value().c_str(), O_RDWR); |
| 40 CHECK_GE(fd, 0) << "Could not open temporary file"; | 46 CHECK_GE(fd, 0) << "Could not open temporary file"; |
| 41 CHECK(!unlink(path.value().c_str())) << "Could not unlink temporary file"; | 47 CHECK(!unlink(path.value().c_str())) << "Could not unlink temporary file"; |
| 42 | 48 |
| 43 mojo::files::Error error = mojo::files::Error::INTERNAL; | 49 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 44 mojo::files::FileInformationPtr file_info; | 50 mojo::files::FileInformationPtr file_info; |
| 45 mojo_file->Stat([&error, &file_info](mojo::files::Error e, | 51 mojo_file->Stat([&error, &file_info](mojo::files::Error e, |
| 46 mojo::files::FileInformationPtr f) { | 52 mojo::files::FileInformationPtr f) { |
| 47 error = e; | 53 error = e; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 77 CHECK(mojo_file.WaitForIncomingResponse()); | 83 CHECK(mojo_file.WaitForIncomingResponse()); |
| 78 CHECK_EQ(mojo::files::Error::OK, error); | 84 CHECK_EQ(mojo::files::Error::OK, error); |
| 79 | 85 |
| 80 return fd; | 86 return fd; |
| 81 } | 87 } |
| 82 | 88 |
| 83 void FileDescriptorToMojoFile(int fd, mojo::files::FilePtr mojo_file) { | 89 void FileDescriptorToMojoFile(int fd, mojo::files::FilePtr mojo_file) { |
| 84 mojo::files::Error error = mojo::files::Error::INTERNAL; | 90 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 85 | 91 |
| 86 static const size_t kBufferSize = 0x100000; | 92 static const size_t kBufferSize = 0x100000; |
| 87 scoped_ptr<uint8_t[]> buf(new uint8_t[kBufferSize]); | 93 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufferSize]); |
| 88 int64_t offset = 0; | 94 int64_t offset = 0; |
| 89 while (true) { | 95 while (true) { |
| 90 ssize_t bytes = HANDLE_EINTR(read(fd, buf.get(), kBufferSize)); | 96 ssize_t bytes = HANDLE_EINTR(read(fd, buf.get(), kBufferSize)); |
| 91 CHECK_GE(bytes, 0); | 97 CHECK_GE(bytes, 0); |
| 92 if (bytes == 0) | 98 if (bytes == 0) |
| 93 break; | 99 break; |
| 94 std::vector<uint8_t> source_v; | 100 std::vector<uint8_t> source_v; |
| 95 source_v.assign(buf.get(), buf.get() + bytes); | 101 source_v.assign(buf.get(), buf.get() + bytes); |
| 96 ssize_t num_bytes_written = 0; | 102 ssize_t num_bytes_written = 0; |
| 97 mojo_file->Write( | 103 mojo_file->Write( |
| 98 mojo::Array<uint8_t>::From(source_v), offset, | 104 mojo::Array<uint8_t>::From(source_v), offset, |
| 99 mojo::files::Whence::FROM_START, | 105 mojo::files::Whence::FROM_START, |
| 100 [&error, &num_bytes_written](mojo::files::Error e, ssize_t n) { | 106 [&error, &num_bytes_written](mojo::files::Error e, ssize_t n) { |
| 101 error = e; | 107 error = e; |
| 102 num_bytes_written = n; | 108 num_bytes_written = n; |
| 103 }); | 109 }); |
| 104 CHECK(mojo_file.WaitForIncomingResponse()); | 110 CHECK(mojo_file.WaitForIncomingResponse()); |
| 105 CHECK_EQ(mojo::files::Error::OK, error); | 111 CHECK_EQ(mojo::files::Error::OK, error); |
| 106 CHECK_EQ(num_bytes_written, bytes); | 112 CHECK_EQ(num_bytes_written, bytes); |
| 107 offset += bytes; | 113 offset += bytes; |
| 108 } | 114 } |
| 109 | 115 |
| 110 mojo_file->Close([&error](mojo::files::Error e) { error = e; }); | 116 mojo_file->Close([&error](mojo::files::Error e) { error = e; }); |
| 111 CHECK(mojo_file.WaitForIncomingResponse()); | 117 CHECK(mojo_file.WaitForIncomingResponse()); |
| 112 CHECK_EQ(mojo::files::Error::OK, error); | 118 CHECK_EQ(mojo::files::Error::OK, error); |
| 113 } | 119 } |
| 114 | 120 |
| 115 } // namespace nacl | 121 } // namespace nacl |
| OLD | NEW |