| 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/file_utils/file_util.h" | 5 #include "mojo/file_utils/file_util.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "mojo/services/files/interfaces/directory.mojom.h" | |
| 10 #include "mojo/services/files/interfaces/file.mojom.h" | |
| 11 #include "mojo/services/files/interfaces/types.mojom.h" | 9 #include "mojo/services/files/interfaces/types.mojom.h" |
| 12 | 10 |
| 13 namespace file_utils { | 11 namespace file_utils { |
| 14 | 12 |
| 15 mojo::files::FilePtr CreateTemporaryFileInDir( | 13 mojo::InterfaceHandle<mojo::files::File> CreateTemporaryFileInDir( |
| 16 mojo::files::DirectoryPtr* directory, | 14 mojo::SynchronousInterfacePtr<mojo::files::Directory>* directory, |
| 17 std::string* path_name) { | 15 std::string* path_name) { |
| 18 std::string internal_path_name; | 16 std::string internal_path_name; |
| 19 const std::string charset("abcdefghijklmnopqrstuvwxyz1234567890"); | 17 const std::string charset("abcdefghijklmnopqrstuvwxyz1234567890"); |
| 20 const unsigned kSuffixLength = 6; | 18 const unsigned kSuffixLength = 6; |
| 21 const unsigned kMaxNumAttempts = 10; | 19 const unsigned kMaxNumAttempts = 10; |
| 22 mojo::files::FilePtr temp_file; | 20 mojo::InterfaceHandle<mojo::files::File> temp_file; |
| 23 mojo::files::Error error = mojo::files::Error::INTERNAL; | 21 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 24 uint64_t random_value = 0; | 22 uint64_t random_value = 0; |
| 25 for (unsigned attempt = 0; attempt < kMaxNumAttempts; attempt++) { | 23 for (unsigned attempt = 0; attempt < kMaxNumAttempts; attempt++) { |
| 26 internal_path_name = ".org.chromium.Mojo."; | 24 internal_path_name = ".org.chromium.Mojo."; |
| 27 uint64_t microseconds = static_cast<uint64_t>(MojoGetTimeTicksNow()); | 25 uint64_t microseconds = static_cast<uint64_t>(MojoGetTimeTicksNow()); |
| 28 for (unsigned i = 0; i < kSuffixLength; i++) { | 26 for (unsigned i = 0; i < kSuffixLength; i++) { |
| 29 // This roughly matches glibc's mapping from time to "random_time_bits", | 27 // This roughly matches glibc's mapping from time to "random_time_bits", |
| 30 // it seems to be good enough for creating temporary files. | 28 // it seems to be good enough for creating temporary files. |
| 31 random_value += (microseconds << 16) ^ (microseconds / 1000000); | 29 random_value += (microseconds << 16) ^ (microseconds / 1000000); |
| 32 internal_path_name.push_back(charset[random_value % charset.length()]); | 30 internal_path_name.push_back(charset[random_value % charset.length()]); |
| 33 } | 31 } |
| 34 (*directory) | 32 if (!(*directory) |
| 35 ->OpenFile(internal_path_name, GetProxy(&temp_file), | 33 ->OpenFile(internal_path_name, GetProxy(&temp_file), |
| 36 mojo::files::kOpenFlagWrite | mojo::files::kOpenFlagRead | | 34 mojo::files::kOpenFlagWrite | |
| 37 mojo::files::kOpenFlagCreate | | 35 mojo::files::kOpenFlagRead | |
| 38 mojo::files::kOpenFlagExclusive, | 36 mojo::files::kOpenFlagCreate | |
| 39 [&error](mojo::files::Error e) { error = e; }); | 37 mojo::files::kOpenFlagExclusive, |
| 40 if (!directory->WaitForIncomingResponse()) | 38 &error)) |
| 41 return nullptr; | 39 return nullptr; |
| 42 // TODO(smklein): React to error code when ErrnoToError can return something | 40 // TODO(smklein): React to error code when ErrnoToError can return something |
| 43 // other than Error::UNKNOWN. We only want to retry when "EEXIST" is thrown. | 41 // other than Error::UNKNOWN. We only want to retry when "EEXIST" is thrown. |
| 44 if (error == mojo::files::Error::OK) { | 42 if (error == mojo::files::Error::OK) { |
| 45 *path_name = internal_path_name; | 43 *path_name = internal_path_name; |
| 46 return temp_file; | 44 return temp_file; |
| 47 } | 45 } |
| 48 } | 46 } |
| 49 return nullptr; | 47 return nullptr; |
| 50 } | 48 } |
| 51 | 49 |
| 52 } // namespace file_utils | 50 } // namespace file_utils |
| OLD | NEW |