Adding wrapper class WKBackForwardListItemHolder
[chromium-blink-merge.git] / tools / gn / function_write_file.cc
blobd24abc159ac5270046cf7a915b9bac70edea0163
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
3 // found in the LICENSE file.
5 #include <iostream>
6 #include <sstream>
8 #include "base/files/file_util.h"
9 #include "base/strings/string_split.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "tools/gn/err.h"
13 #include "tools/gn/filesystem_utils.h"
14 #include "tools/gn/functions.h"
15 #include "tools/gn/input_file.h"
16 #include "tools/gn/parse_tree.h"
17 #include "tools/gn/scheduler.h"
19 namespace functions {
21 namespace {
23 // On Windows, provide a custom implementation of base::WriteFile. Sometimes
24 // the base version fails, especially on the bots. The guess is that Windows
25 // Defender or other antivirus programs still have the file open (after
26 // checking for the read) when the write happens immediately after. This
27 // version opens with FILE_SHARE_READ (normally not what you want when
28 // replacing the entire contents of the file) which lets us continue even if
29 // another program has the file open for reading. See http://crbug.com/468437
30 #if defined(OS_WIN)
31 int DoWriteFile(const base::FilePath& filename, const char* data, int size) {
32 base::win::ScopedHandle file(::CreateFile(
33 filename.value().c_str(),
34 GENERIC_WRITE,
35 FILE_SHARE_READ,
36 NULL,
37 CREATE_ALWAYS,
39 NULL));
40 if (!file.IsValid()) {
41 PLOG(ERROR) << "CreateFile failed for path "
42 << base::UTF16ToUTF8(filename.value());
43 return -1;
46 DWORD written;
47 BOOL result = ::WriteFile(file.Get(), data, size, &written, NULL);
48 if (result && static_cast<int>(written) == size)
49 return written;
51 if (!result) {
52 // WriteFile failed.
53 PLOG(ERROR) << "writing file " << base::UTF16ToUTF8(filename.value())
54 << " failed";
55 } else {
56 // Didn't write all the bytes.
57 LOG(ERROR) << "wrote" << written << " bytes to "
58 << base::UTF16ToUTF8(filename.value()) << " expected " << size;
60 return -1;
62 #else
63 int DoWriteFile(const base::FilePath& filename, const char* data, int size) {
64 return base::WriteFile(filename, data, size);
66 #endif
68 } // namespace
70 const char kWriteFile[] = "write_file";
71 const char kWriteFile_HelpShort[] =
72 "write_file: Write a file to disk.";
73 const char kWriteFile_Help[] =
74 "write_file: Write a file to disk.\n"
75 "\n"
76 " write_file(filename, data)\n"
77 "\n"
78 " If data is a list, the list will be written one-item-per-line with no\n"
79 " quoting or brackets.\n"
80 "\n"
81 " If the file exists and the contents are identical to that being\n"
82 " written, the file will not be updated. This will prevent unnecessary\n"
83 " rebuilds of targets that depend on this file.\n"
84 "\n"
85 " TODO(brettw) we probably need an optional third argument to control\n"
86 " list formatting.\n"
87 "\n"
88 "Arguments:\n"
89 "\n"
90 " filename\n"
91 " Filename to write. This must be within the output directory.\n"
92 "\n"
93 " data:\n"
94 " The list or string to write.\n";
96 Value RunWriteFile(Scope* scope,
97 const FunctionCallNode* function,
98 const std::vector<Value>& args,
99 Err* err) {
100 if (args.size() != 2) {
101 *err = Err(function->function(), "Wrong number of arguments to write_file",
102 "I expected two arguments.");
103 return Value();
106 // Compute the file name and make sure it's in the output dir.
107 const SourceDir& cur_dir = scope->GetSourceDir();
108 SourceFile source_file = cur_dir.ResolveRelativeFile(args[0], err,
109 scope->settings()->build_settings()->root_path_utf8());
110 if (err->has_error())
111 return Value();
112 if (!EnsureStringIsInOutputDir(
113 scope->settings()->build_settings()->build_dir(),
114 source_file.value(), args[0].origin(), err))
115 return Value();
116 g_scheduler->AddWrittenFile(source_file); // Track that we wrote this file.
118 // Track how to recreate this file, since we write it a gen time.
119 // Note this is a hack since the correct output is not a dependency proper,
120 // but an addition of this file to the output of the gn rule that writes it.
121 // This dependency will, however, cause the gen step to be re-run and the
122 // build restarted if the file is missing.
123 g_scheduler->AddGenDependency(
124 scope->settings()->build_settings()->GetFullPath(source_file));
126 // Compute output.
127 std::ostringstream contents;
128 if (args[1].type() == Value::LIST) {
129 const std::vector<Value>& list = args[1].list_value();
130 for (const auto& cur : list)
131 contents << cur.ToString(false) << std::endl;
132 } else {
133 contents << args[1].ToString(false);
135 const std::string& new_contents = contents.str();
136 base::FilePath file_path =
137 scope->settings()->build_settings()->GetFullPath(source_file);
139 // Make sure we're not replacing the same contents.
140 std::string existing_contents;
141 if (base::ReadFileToString(file_path, &existing_contents) &&
142 existing_contents == new_contents)
143 return Value(); // Nothing to do.
145 // Write file, creating the directory if necessary.
146 if (!base::CreateDirectory(file_path.DirName())) {
147 *err = Err(function->function(), "Unable to create directory.",
148 "I was using \"" + FilePathToUTF8(file_path.DirName()) + "\".");
149 return Value();
152 int int_size = static_cast<int>(new_contents.size());
153 if (DoWriteFile(file_path, new_contents.c_str(), int_size)
154 != int_size) {
155 *err = Err(function->function(), "Unable to write file.",
156 "I was writing \"" + FilePathToUTF8(file_path) + "\".");
157 return Value();
159 return Value();
162 } // namespace functions