OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #if defined _MSC_VER && _MSC_VER == 1800 | 5 #if defined _MSC_VER && _MSC_VER == 1800 |
6 // TODO(scottmg): Internal errors on VS2013 RC in LTCG. This should be removed | 6 // TODO(scottmg): Internal errors on VS2013 RC in LTCG. This should be removed |
7 // after RTM. http://crbug.com/288948 | 7 // after RTM. http://crbug.com/288948 |
8 #pragma optimize("", off) | 8 #pragma optimize("", off) |
9 #endif | 9 #endif |
10 | 10 |
11 #include "base/files/important_file_writer.h" | 11 #include "base/files/important_file_writer.h" |
12 | 12 |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 | 14 |
15 #include <string> | 15 #include <string> |
16 | 16 |
17 #include "base/bind.h" | 17 #include "base/bind.h" |
18 #include "base/critical_closure.h" | 18 #include "base/critical_closure.h" |
19 #include "base/file_util.h" | 19 #include "base/file_util.h" |
| 20 #include "base/files/file.h" |
20 #include "base/files/file_path.h" | 21 #include "base/files/file_path.h" |
21 #include "base/logging.h" | 22 #include "base/logging.h" |
22 #include "base/metrics/histogram.h" | 23 #include "base/metrics/histogram.h" |
23 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
24 #include "base/task_runner.h" | 25 #include "base/task_runner.h" |
25 #include "base/threading/thread.h" | 26 #include "base/threading/thread.h" |
26 #include "base/time/time.h" | 27 #include "base/time/time.h" |
27 | 28 |
28 namespace base { | 29 namespace base { |
29 | 30 |
(...skipping 26 matching lines...) Expand all Loading... |
56 // Write the data to a temp file then rename to avoid data loss if we crash | 57 // Write the data to a temp file then rename to avoid data loss if we crash |
57 // while writing the file. Ensure that the temp file is on the same volume | 58 // while writing the file. Ensure that the temp file is on the same volume |
58 // as target file, so it can be moved in one step, and that the temp file | 59 // as target file, so it can be moved in one step, and that the temp file |
59 // is securely created. | 60 // is securely created. |
60 FilePath tmp_file_path; | 61 FilePath tmp_file_path; |
61 if (!base::CreateTemporaryFileInDir(path.DirName(), &tmp_file_path)) { | 62 if (!base::CreateTemporaryFileInDir(path.DirName(), &tmp_file_path)) { |
62 LogFailure(path, FAILED_CREATING, "could not create temporary file"); | 63 LogFailure(path, FAILED_CREATING, "could not create temporary file"); |
63 return false; | 64 return false; |
64 } | 65 } |
65 | 66 |
66 int flags = PLATFORM_FILE_OPEN | PLATFORM_FILE_WRITE; | 67 File tmp_file(tmp_file_path, File::FLAG_OPEN | File::FLAG_WRITE); |
67 PlatformFile tmp_file = | 68 if (!tmp_file.IsValid()) { |
68 CreatePlatformFile(tmp_file_path, flags, NULL, NULL); | |
69 if (tmp_file == kInvalidPlatformFileValue) { | |
70 LogFailure(path, FAILED_OPENING, "could not open temporary file"); | 69 LogFailure(path, FAILED_OPENING, "could not open temporary file"); |
71 return false; | 70 return false; |
72 } | 71 } |
73 | 72 |
74 // If this happens in the wild something really bad is going on. | 73 // If this happens in the wild something really bad is going on. |
75 CHECK_LE(data.length(), static_cast<size_t>(kint32max)); | 74 CHECK_LE(data.length(), static_cast<size_t>(kint32max)); |
76 int bytes_written = WritePlatformFile( | 75 int bytes_written = tmp_file.Write(0, data.data(), |
77 tmp_file, 0, data.data(), static_cast<int>(data.length())); | 76 static_cast<int>(data.length())); |
78 FlushPlatformFile(tmp_file); // Ignore return value. | 77 tmp_file.Flush(); // Ignore return value. |
79 | 78 tmp_file.Close(); |
80 if (!ClosePlatformFile(tmp_file)) { | |
81 LogFailure(path, FAILED_CLOSING, "failed to close temporary file"); | |
82 base::DeleteFile(tmp_file_path, false); | |
83 return false; | |
84 } | |
85 | 79 |
86 if (bytes_written < static_cast<int>(data.length())) { | 80 if (bytes_written < static_cast<int>(data.length())) { |
87 LogFailure(path, FAILED_WRITING, "error writing, bytes_written=" + | 81 LogFailure(path, FAILED_WRITING, "error writing, bytes_written=" + |
88 IntToString(bytes_written)); | 82 IntToString(bytes_written)); |
89 base::DeleteFile(tmp_file_path, false); | 83 base::DeleteFile(tmp_file_path, false); |
90 return false; | 84 return false; |
91 } | 85 } |
92 | 86 |
93 if (!base::ReplaceFile(tmp_file_path, path, NULL)) { | 87 if (!base::ReplaceFile(tmp_file_path, path, NULL)) { |
94 LogFailure(path, FAILED_RENAMING, "could not rename temporary file"); | 88 LogFailure(path, FAILED_RENAMING, "could not rename temporary file"); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 if (serializer_->SerializeData(&data)) { | 158 if (serializer_->SerializeData(&data)) { |
165 WriteNow(data); | 159 WriteNow(data); |
166 } else { | 160 } else { |
167 DLOG(WARNING) << "failed to serialize data to be saved in " | 161 DLOG(WARNING) << "failed to serialize data to be saved in " |
168 << path_.value().c_str(); | 162 << path_.value().c_str(); |
169 } | 163 } |
170 serializer_ = NULL; | 164 serializer_ = NULL; |
171 } | 165 } |
172 | 166 |
173 } // namespace base | 167 } // namespace base |
OLD | NEW |