Enable MSVC warning for unused locals.
[chromium-blink-merge.git] / chrome / installer / util / move_tree_work_item.cc
blob5d6c070339339300335650d50bf58d0c3dcaa56a
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
3 // found in the LICENSE file.
5 #include "chrome/installer/util/move_tree_work_item.h"
7 #include <shlwapi.h>
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "chrome/installer/util/duplicate_tree_detector.h"
12 #include "chrome/installer/util/logging_installer.h"
14 MoveTreeWorkItem::~MoveTreeWorkItem() {
17 MoveTreeWorkItem::MoveTreeWorkItem(const base::FilePath& source_path,
18 const base::FilePath& dest_path,
19 const base::FilePath& temp_dir,
20 MoveTreeOption duplicate_option)
21 : source_path_(source_path),
22 dest_path_(dest_path),
23 temp_dir_(temp_dir),
24 duplicate_option_(duplicate_option),
25 moved_to_dest_path_(false),
26 moved_to_backup_(false),
27 source_moved_to_backup_(false) {
30 bool MoveTreeWorkItem::Do() {
31 if (!base::PathExists(source_path_)) {
32 LOG(ERROR) << source_path_.value() << " does not exist";
33 return false;
36 // If dest_path_ exists, we can do one of two things:
37 // 1) If the contents of src_path_are already fully contained in dest_path_
38 // then do nothing and return success. Fully contained means the full
39 // file structure with identical files is contained in dest_path_. For
40 // Chrome, if dest_path_ exists, this is expected to be the common case.
41 // 2) If the contents of src_path_ are NOT fully contained in dest_path_, we
42 // attempt to backup dest_path_ and replace it with src_path_. This will
43 // fail if files in dest_path_ are in use.
44 if (base::PathExists(dest_path_)) {
45 // Generate a backup path that can keep the original files under dest_path_.
46 if (!backup_path_.CreateUniqueTempDirUnderPath(temp_dir_)) {
47 PLOG(ERROR) << "Failed to get backup path in folder "
48 << temp_dir_.value();
49 return false;
51 base::FilePath backup = backup_path_.path().Append(dest_path_.BaseName());
53 if (duplicate_option_ == CHECK_DUPLICATES) {
54 if (installer::IsIdenticalFileHierarchy(source_path_, dest_path_)) {
55 // The files we are moving are already present in the destination path.
56 // We most likely don't need to do anything. As such, just move the
57 // source files to the temp folder as backup.
58 if (base::Move(source_path_, backup)) {
59 source_moved_to_backup_ = true;
60 VLOG(1) << "Moved source " << source_path_.value()
61 << " to backup path " << backup.value();
62 return true;
63 } else {
64 // We failed to move the source tree to the backup path. This is odd
65 // but just fall through and attempt the regular behaviour as well.
66 LOG(ERROR) << "Failed to backup source " << source_path_.value()
67 << " to backup path " << backup.value()
68 << " for duplicate trees. Trying regular Move instead.";
70 } else {
71 VLOG(1) << "Source path " << source_path_.value()
72 << " differs from " << dest_path_.value()
73 << ", updating now.";
77 if (base::Move(dest_path_, backup)) {
78 moved_to_backup_ = true;
79 VLOG(1) << "Moved destination " << dest_path_.value()
80 << " to backup path " << backup.value();
81 } else {
82 PLOG(ERROR) << "failed moving " << dest_path_.value()
83 << " to " << backup.value();
84 return false;
88 // Now move source to destination.
89 if (base::Move(source_path_, dest_path_)) {
90 moved_to_dest_path_ = true;
91 VLOG(1) << "Moved source " << source_path_.value()
92 << " to destination " << dest_path_.value();
93 } else {
94 PLOG(ERROR) << "failed move " << source_path_.value()
95 << " to " << dest_path_.value();
96 return false;
99 return true;
102 void MoveTreeWorkItem::Rollback() {
103 if (moved_to_dest_path_ && !base::Move(dest_path_, source_path_))
104 LOG(ERROR) << "Can not move " << dest_path_.value()
105 << " to " << source_path_.value();
107 base::FilePath backup = backup_path_.path().Append(dest_path_.BaseName());
108 if (moved_to_backup_ && !base::Move(backup, dest_path_)) {
109 LOG(ERROR) << "failed move " << backup.value()
110 << " to " << dest_path_.value();
113 if (source_moved_to_backup_ && !base::Move(backup, source_path_)) {
114 LOG(ERROR) << "Can not restore " << backup.value()
115 << " to " << source_path_.value();