| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/installer/util/file_from_template_work_item.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/string_piece.h" |
| 13 #include "base/string_split.h" |
| 14 |
| 15 namespace { |
| 16 const char16 kVariableDefiner(L'$'); |
| 17 } // namespace |
| 18 |
| 19 FileFromTemplateWorkItem::~FileFromTemplateWorkItem() { |
| 20 } |
| 21 |
| 22 FileFromTemplateWorkItem::FileFromTemplateWorkItem( |
| 23 const FilePath& source_path, |
| 24 const FilePath& dest_path, |
| 25 const FilePath& temp_dir, |
| 26 const std::map<string16, string16>& mappings) |
| 27 : source_path_(source_path), |
| 28 dest_path_(dest_path), |
| 29 temp_dir_(temp_dir), |
| 30 mappings_(mappings), |
| 31 moved_to_backup_(false) { |
| 32 } |
| 33 |
| 34 bool FileFromTemplateWorkItem::Do() { |
| 35 if (!file_util::PathExists(source_path_)) { |
| 36 LOG(ERROR) << source_path_.value() << " does not exist"; |
| 37 return false; |
| 38 } |
| 39 |
| 40 // All templates need to end with the .template extension as they are |
| 41 // validated in a presubmit check if so. |
| 42 DCHECK(source_path_.MatchesExtension(L".template")); |
| 43 |
| 44 // If |dest_path_| exists, move it to a backup directory to be able to |
| 45 // rollback if necessary. |
| 46 if (file_util::PathExists(dest_path_)) { |
| 47 if (!backup_path_.CreateUniqueTempDirUnderPath(temp_dir_)) { |
| 48 PLOG(ERROR) << "Failed to get backup path in folder " |
| 49 << temp_dir_.value(); |
| 50 return false; |
| 51 } |
| 52 FilePath backup = backup_path_.path().Append(dest_path_.BaseName()); |
| 53 |
| 54 if (file_util::Move(dest_path_, backup)) { |
| 55 moved_to_backup_ = true; |
| 56 VLOG(1) << "Moved destination " << dest_path_.value() |
| 57 << " to backup path " << backup.value(); |
| 58 } else { |
| 59 PLOG(ERROR) << "failed moving " << dest_path_.value() |
| 60 << " to " << backup.value(); |
| 61 return false; |
| 62 } |
| 63 } |
| 64 |
| 65 // Proceed with reading the template at |src_path_| and writing the resulting |
| 66 // file to |dest_path_|. |
| 67 string16 template_str16; |
| 68 { |
| 69 std::string template_str; |
| 70 if (!file_util::ReadFileToString(source_path_, &template_str)) { |
| 71 PLOG(ERROR) << "failed to read template file" << source_path_.value(); |
| 72 return false; |
| 73 } |
| 74 // We know the template read is correctly formed as it was verified in |
| 75 // a presubmit check. |
| 76 // TODO (gab): Write the actual presubmit check. |
| 77 template_str16 = UTF8ToUTF16(template_str); |
| 78 } |
| 79 |
| 80 // Split the contents on kVariableDefiner such that entries in |contents| will |
| 81 // be alternating between template text and template variables to be replaced. |
| 82 std::vector<string16> contents; |
| 83 base::SplitString(template_str16, kVariableDefiner, &contents); |
| 84 |
| 85 string16 out_str16; |
| 86 // The first string in |contents| is only a variable if the template starts |
| 87 // with kVariableDefiner. |
| 88 bool is_var = (template_str16[0] == kVariableDefiner); |
| 89 for (std::vector<string16>::iterator it = contents.begin(); |
| 90 it != contents.end(); ++it, is_var = !is_var) { |
| 91 if (is_var) { |
| 92 if (mappings_.count(*it) == 0) { |
| 93 LOG(ERROR) << "Template variable '" << *it |
| 94 << "' not found in mappings."; |
| 95 return false; |
| 96 } else { |
| 97 const string16& val = mappings_.find(*it)->second; |
| 98 out_str16.append(val); |
| 99 VLOG(1) << "FileFromTemplate for " << source_path_.value() << ": '" |
| 100 << *it << "' replaced by '" << val << "'."; |
| 101 } |
| 102 } else { |
| 103 out_str16.append(*it); |
| 104 } |
| 105 } |
| 106 |
| 107 const std::string out_str(UTF16ToUTF8(out_str16)); |
| 108 const int out_size = static_cast<int>(out_str.size()); |
| 109 const int bytes_written = |
| 110 file_util::WriteFile(dest_path_, out_str.c_str(), out_size); |
| 111 if (bytes_written == out_size) { |
| 112 VLOG(1) << "Successfully extracted template from " << source_path_.value() |
| 113 << " to " << dest_path_.value(); |
| 114 return true; |
| 115 } else { |
| 116 LOG(ERROR) << "Error writing result to " << dest_path_.value(); |
| 117 if (bytes_written >= 0) { |
| 118 LOG(ERROR) << bytes_written << " bytes were written when " << out_size |
| 119 << " were expected to be."; |
| 120 } |
| 121 return false; |
| 122 } |
| 123 } |
| 124 |
| 125 void FileFromTemplateWorkItem::Rollback() { |
| 126 FilePath backup = backup_path_.path().Append(dest_path_.BaseName()); |
| 127 if (moved_to_backup_ && !file_util::Move(backup, dest_path_)) { |
| 128 LOG(ERROR) << "failed move " << backup.value() |
| 129 << " back to " << dest_path_.value(); |
| 130 } |
| 131 } |
| OLD | NEW |