From cc378a1cc341ecfb60aeaba995ea0b442c9bc83e Mon Sep 17 00:00:00 2001 From: "kinuko@chromium.org" Date: Mon, 11 Oct 2010 22:00:55 +0000 Subject: Make sure we close the file_handle when we create (but not open) a new file 1. Add FileUtilProxy::Create that does not leave a file_handle opened. 2. Update the file_util_operation to use FileUtilProxy::Create instead of FileUtilProxy::CreateOrOpen so that no other tasks get queued in before we close the handle. BUG=58424,58473 TEST=fast/filesystem/ Review URL: http://codereview.chromium.org/3717001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@62192 0039d316-1c4b-4281-b951-d872f2087c98 --- base/file_util_proxy.cc | 23 ++++++++++++++++++++++- base/file_util_proxy.h | 7 +++++++ 2 files changed, 29 insertions(+), 1 deletion(-) (limited to 'base') diff --git a/base/file_util_proxy.cc b/base/file_util_proxy.cc index b3b18f8..9cfff63 100644 --- a/base/file_util_proxy.cc +++ b/base/file_util_proxy.cc @@ -119,12 +119,14 @@ class RelayCreateOrOpen : public MessageLoopRelay { scoped_refptr message_loop_proxy, const FilePath& file_path, int file_flags, + bool return_no_handle, base::FileUtilProxy::CreateOrOpenCallback* callback) : message_loop_proxy_(message_loop_proxy), file_path_(file_path), file_flags_(file_flags), callback_(callback), file_handle_(base::kInvalidPlatformFileValue), + return_no_handle_(return_no_handle), created_(false) { DCHECK(callback); } @@ -139,6 +141,13 @@ class RelayCreateOrOpen : public MessageLoopRelay { base::PlatformFileError error_code = base::PLATFORM_FILE_OK; file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, &created_, &error_code); + // If the return_no_handle is true the caller is not interested + // in the file_handle_. Close it right now. + if (return_no_handle_ && file_handle_ != base::kInvalidPlatformFileValue) { + // We don't check the return value here. + base::ClosePlatformFile(file_handle_); + file_handle_ = base::kInvalidPlatformFileValue; + } set_error_code(error_code); } @@ -154,6 +163,7 @@ class RelayCreateOrOpen : public MessageLoopRelay { int file_flags_; base::FileUtilProxy::CreateOrOpenCallback* callback_; base::PlatformFile file_handle_; + bool return_no_handle_; bool created_; }; @@ -680,7 +690,18 @@ bool FileUtilProxy::CreateOrOpen( const FilePath& file_path, int file_flags, CreateOrOpenCallback* callback) { return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( - message_loop_proxy, file_path, file_flags, callback)); + message_loop_proxy, file_path, file_flags, false /* return_no_handle */, + callback)); +} + +// static +bool FileUtilProxy::Create( + scoped_refptr message_loop_proxy, + const FilePath& file_path, int file_flags, + CreateOrOpenCallback* callback) { + return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( + message_loop_proxy, file_path, file_flags, true /* return_no_handle */, + callback)); } // static diff --git a/base/file_util_proxy.h b/base/file_util_proxy.h index e716ab7..11fc988 100644 --- a/base/file_util_proxy.h +++ b/base/file_util_proxy.h @@ -48,6 +48,13 @@ class FileUtilProxy { int file_flags, CreateOrOpenCallback* callback); + // Creates a file with the given flags. This one is a variation of + // CreateOrOpen but it doesn't return a file handle. + static bool Create(scoped_refptr message_loop_proxy, + const FilePath& file_path, + int file_flags, + CreateOrOpenCallback* callback); + // Creates a temporary file for writing. The path and an open file handle // are returned. It is invalid to pass NULL for the callback. typedef Callback3