OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <iostream> | 5 #include <iostream> |
6 #include <sstream> | 6 #include <sstream> |
7 | 7 |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
11 #include "tools/gn/err.h" | 12 #include "tools/gn/err.h" |
12 #include "tools/gn/filesystem_utils.h" | 13 #include "tools/gn/filesystem_utils.h" |
13 #include "tools/gn/functions.h" | 14 #include "tools/gn/functions.h" |
14 #include "tools/gn/input_file.h" | 15 #include "tools/gn/input_file.h" |
15 #include "tools/gn/parse_tree.h" | 16 #include "tools/gn/parse_tree.h" |
16 #include "tools/gn/scheduler.h" | 17 #include "tools/gn/scheduler.h" |
17 | 18 |
18 namespace functions { | 19 namespace functions { |
19 | 20 |
| 21 namespace { |
| 22 |
| 23 // On Windows, provide a custom implementation of base::WriteFile. Sometimes |
| 24 // the base version would fail, and this alternate implementation provides |
| 25 // additional logging. See http://crbug.com/468437 |
| 26 #if defined(OS_WIN) |
| 27 int DoWriteFile(const base::FilePath& filename, const char* data, int size) { |
| 28 base::win::ScopedHandle file(::CreateFile( |
| 29 filename.value().c_str(), |
| 30 GENERIC_WRITE, |
| 31 FILE_SHARE_READ, // Not present in the base version, speculative fix. |
| 32 NULL, |
| 33 CREATE_ALWAYS, |
| 34 0, |
| 35 NULL)); |
| 36 if (!file.IsValid()) { |
| 37 PLOG(ERROR) << "CreateFile failed for path " |
| 38 << base::UTF16ToUTF8(filename.value()); |
| 39 return -1; |
| 40 } |
| 41 |
| 42 DWORD written; |
| 43 BOOL result = ::WriteFile(file.Get(), data, size, &written, NULL); |
| 44 if (result && static_cast<int>(written) == size) |
| 45 return written; |
| 46 |
| 47 if (!result) { |
| 48 // WriteFile failed. |
| 49 PLOG(ERROR) << "writing file " << base::UTF16ToUTF8(filename.value()) |
| 50 << " failed"; |
| 51 } else { |
| 52 // Didn't write all the bytes. |
| 53 LOG(ERROR) << "wrote" << written << " bytes to " |
| 54 << base::UTF16ToUTF8(filename.value()) << " expected " << size; |
| 55 } |
| 56 return -1; |
| 57 } |
| 58 #else |
| 59 int DoWriteFile(const base::FilePath& filename, const char* data, int size) { |
| 60 return base::WriteFile(filename, data, size); |
| 61 } |
| 62 #endif |
| 63 |
| 64 } // namespace |
| 65 |
20 const char kWriteFile[] = "write_file"; | 66 const char kWriteFile[] = "write_file"; |
21 const char kWriteFile_HelpShort[] = | 67 const char kWriteFile_HelpShort[] = |
22 "write_file: Write a file to disk."; | 68 "write_file: Write a file to disk."; |
23 const char kWriteFile_Help[] = | 69 const char kWriteFile_Help[] = |
24 "write_file: Write a file to disk.\n" | 70 "write_file: Write a file to disk.\n" |
25 "\n" | 71 "\n" |
26 " write_file(filename, data)\n" | 72 " write_file(filename, data)\n" |
27 "\n" | 73 "\n" |
28 " If data is a list, the list will be written one-item-per-line with no\n" | 74 " If data is a list, the list will be written one-item-per-line with no\n" |
29 " quoting or brackets.\n" | 75 " quoting or brackets.\n" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 return Value(); // Nothing to do. | 130 return Value(); // Nothing to do. |
85 | 131 |
86 // Write file, creating the directory if necessary. | 132 // Write file, creating the directory if necessary. |
87 if (!base::CreateDirectory(file_path.DirName())) { | 133 if (!base::CreateDirectory(file_path.DirName())) { |
88 *err = Err(function->function(), "Unable to create directory.", | 134 *err = Err(function->function(), "Unable to create directory.", |
89 "I was using \"" + FilePathToUTF8(file_path.DirName()) + "\"."); | 135 "I was using \"" + FilePathToUTF8(file_path.DirName()) + "\"."); |
90 return Value(); | 136 return Value(); |
91 } | 137 } |
92 | 138 |
93 int int_size = static_cast<int>(new_contents.size()); | 139 int int_size = static_cast<int>(new_contents.size()); |
94 if (base::WriteFile(file_path, new_contents.c_str(), int_size) | 140 if (DoWriteFile(file_path, new_contents.c_str(), int_size) |
95 != int_size) { | 141 != int_size) { |
96 *err = Err(function->function(), "Unable to write file.", | 142 *err = Err(function->function(), "Unable to write file.", |
97 "I was writing \"" + FilePathToUTF8(file_path) + "\"."); | 143 "I was writing \"" + FilePathToUTF8(file_path) + "\"."); |
98 return Value(); | 144 return Value(); |
99 } | 145 } |
100 return Value(); | 146 return Value(); |
101 } | 147 } |
102 | 148 |
103 } // namespace functions | 149 } // namespace functions |
OLD | NEW |