| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/file_utils/file_util.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 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" | |
| 12 | |
| 13 namespace file_utils { | |
| 14 | |
| 15 mojo::files::FilePtr CreateTemporaryFileInDir( | |
| 16 mojo::files::DirectoryPtr* directory, | |
| 17 std::string* path_name) { | |
| 18 std::string internal_path_name; | |
| 19 const std::string charset("abcdefghijklmnopqrstuvwxyz1234567890"); | |
| 20 const unsigned kSuffixLength = 6; | |
| 21 const unsigned kMaxNumAttempts = 10; | |
| 22 mojo::files::FilePtr temp_file; | |
| 23 mojo::files::Error error = mojo::files::Error::INTERNAL; | |
| 24 uint64_t random_value = 0; | |
| 25 for (unsigned attempt = 0; attempt < kMaxNumAttempts; attempt++) { | |
| 26 internal_path_name = ".org.chromium.Mojo."; | |
| 27 uint64_t microseconds = static_cast<uint64_t>(MojoGetTimeTicksNow()); | |
| 28 for (unsigned i = 0; i < kSuffixLength; i++) { | |
| 29 // This roughly matches glibc's mapping from time to "random_time_bits", | |
| 30 // it seems to be good enough for creating temporary files. | |
| 31 random_value += (microseconds << 16) ^ (microseconds / 1000000); | |
| 32 internal_path_name.push_back(charset[random_value % charset.length()]); | |
| 33 } | |
| 34 (*directory) | |
| 35 ->OpenFile(internal_path_name, GetProxy(&temp_file), | |
| 36 mojo::files::kOpenFlagWrite | mojo::files::kOpenFlagRead | | |
| 37 mojo::files::kOpenFlagCreate | | |
| 38 mojo::files::kOpenFlagExclusive, | |
| 39 [&error](mojo::files::Error e) { error = e; }); | |
| 40 if (!directory->WaitForIncomingResponse()) | |
| 41 return nullptr; | |
| 42 // 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. | |
| 44 if (error == mojo::files::Error::OK) { | |
| 45 *path_name = internal_path_name; | |
| 46 return temp_file; | |
| 47 } | |
| 48 } | |
| 49 return nullptr; | |
| 50 } | |
| 51 | |
| 52 } // namespace file_utils | |
| OLD | NEW |