Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Unified Diff: services/nacl/content_handler_main_nonsfi.cc

Issue 1303343007: Created a blocking copy to temporary file function. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Less NULL, more nullptr Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: services/nacl/content_handler_main_nonsfi.cc
diff --git a/services/nacl/content_handler_main_nonsfi.cc b/services/nacl/content_handler_main_nonsfi.cc
index 9960c62cef71d03c837c20e2cc04d9cc3bdc6051..5c588369d835cb9d5d9326c1d899db93de867d11 100644
--- a/services/nacl/content_handler_main_nonsfi.cc
+++ b/services/nacl/content_handler_main_nonsfi.cc
@@ -17,35 +17,6 @@
namespace nacl {
namespace content_handler {
-namespace {
-
-// Copies response (input) into new temporary file open by fd (output).
-bool URLResponseToTempFile(mojo::URLResponsePtr& response, int* fd) {
- base::FilePath path;
- if (!base::CreateTemporaryFile(&path)) {
- return false;
- }
-
- if (!mojo::common::BlockingCopyToFile(response->body.Pass(), path)) {
- base::DeleteFile(path, false);
- return false;
- }
- *fd = open(path.value().c_str(), O_RDONLY);
- if (*fd < 0) {
- LOG(FATAL) << "Failed to open " << path.value().c_str() << ": "
- << strerror(errno) << "\n";
- }
-
- // Since we unlink an open file, the temp file will be removed as soon as the
- // fd is closed.
- if (unlink(path.value().c_str())) {
- LOG(FATAL) << "Failed to unlink " << path.value().c_str() << ": "
- << strerror(errno) << "\n";
- }
- return true;
-}
-
-} // namespace
class NaClContentHandler : public mojo::ApplicationDelegate,
public mojo::ContentHandlerFactory::Delegate {
@@ -70,9 +41,22 @@ class NaClContentHandler : public mojo::ApplicationDelegate,
// Needed to use Mojo interfaces on this thread.
base::MessageLoop loop(mojo::common::MessagePumpMojo::Create());
// Acquire the nexe.
- int fd;
- if (!URLResponseToTempFile(response, &fd)) {
- LOG(FATAL) << "could not redirect nexe to temp file";
+ base::ScopedFILE nexe_fp =
+ mojo::common::BlockingCopyToTempFile(response->body.Pass());
+ if (nexe_fp == nullptr) {
Mark Seaborn 2015/09/03 06:33:16 Ditto
Sean Klein 2015/09/03 15:51:50 Done.
+ LOG(FATAL) << "Could not redirect nexe to temp file";
+ }
+ FILE* nexe_file_stream = nexe_fp.release();
+ int fd = fileno(nexe_file_stream);
+ if (fd == -1) {
+ LOG(FATAL) << "Could not open the stream pointer's file descriptor";
+ }
+ fd = dup(fd);
+ if (fd == -1) {
+ LOG(FATAL) << "Could not dup the file descriptor";
+ }
+ if (fclose(nexe_file_stream)) {
+ LOG(FATAL) << "Failed to close temp file";
}
// Run -- also, closes the fd, removing the temp file.
« services/nacl/content_handler_main.cc ('K') | « services/nacl/content_handler_main.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698