summaryrefslogtreecommitdiffstats
path: root/chrome/installer/util/move_tree_work_item.cc
blob: 5d6c070339339300335650d50bf58d0c3dcaa56a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/installer/util/move_tree_work_item.h"

#include <shlwapi.h>

#include "base/files/file_util.h"
#include "base/logging.h"
#include "chrome/installer/util/duplicate_tree_detector.h"
#include "chrome/installer/util/logging_installer.h"

MoveTreeWorkItem::~MoveTreeWorkItem() {
}

MoveTreeWorkItem::MoveTreeWorkItem(const base::FilePath& source_path,
                                   const base::FilePath& dest_path,
                                   const base::FilePath& temp_dir,
                                   MoveTreeOption duplicate_option)
    : source_path_(source_path),
      dest_path_(dest_path),
      temp_dir_(temp_dir),
      duplicate_option_(duplicate_option),
      moved_to_dest_path_(false),
      moved_to_backup_(false),
      source_moved_to_backup_(false) {
}

bool MoveTreeWorkItem::Do() {
  if (!base::PathExists(source_path_)) {
    LOG(ERROR) << source_path_.value() << " does not exist";
    return false;
  }

  // If dest_path_ exists, we can do one of two things:
  // 1) If the contents of src_path_are already fully contained in dest_path_
  //    then do nothing and return success. Fully contained means the full
  //    file structure with identical files is contained in dest_path_. For
  //    Chrome, if dest_path_ exists, this is expected to be the common case.
  // 2) If the contents of src_path_ are NOT fully contained in dest_path_, we
  //    attempt to backup dest_path_ and replace it with src_path_. This will
  //    fail if files in dest_path_ are in use.
  if (base::PathExists(dest_path_)) {
    // Generate a backup path that can keep the original files under dest_path_.
    if (!backup_path_.CreateUniqueTempDirUnderPath(temp_dir_)) {
      PLOG(ERROR) << "Failed to get backup path in folder "
                  << temp_dir_.value();
      return false;
    }
    base::FilePath backup = backup_path_.path().Append(dest_path_.BaseName());

    if (duplicate_option_ == CHECK_DUPLICATES) {
      if (installer::IsIdenticalFileHierarchy(source_path_, dest_path_)) {
        // The files we are moving are already present in the destination path.
        // We most likely don't need to do anything. As such, just move the
        // source files to the temp folder as backup.
        if (base::Move(source_path_, backup)) {
          source_moved_to_backup_ = true;
          VLOG(1) << "Moved source " << source_path_.value()
                  << " to backup path " << backup.value();
          return true;
        } else {
          // We failed to move the source tree to the backup path. This is odd
          // but just fall through and attempt the regular behaviour as well.
          LOG(ERROR) << "Failed to backup source " << source_path_.value()
                     << " to backup path " << backup.value()
                     << " for duplicate trees. Trying regular Move instead.";
        }
      } else {
        VLOG(1) << "Source path " << source_path_.value()
                << " differs from " << dest_path_.value()
                << ", updating now.";
      }
    }

    if (base::Move(dest_path_, backup)) {
      moved_to_backup_ = true;
      VLOG(1) << "Moved destination " << dest_path_.value()
              << " to backup path " << backup.value();
    } else {
      PLOG(ERROR) << "failed moving " << dest_path_.value()
                  << " to " << backup.value();
      return false;
    }
  }

  // Now move source to destination.
  if (base::Move(source_path_, dest_path_)) {
    moved_to_dest_path_ = true;
    VLOG(1) << "Moved source " << source_path_.value()
            << " to destination " << dest_path_.value();
  } else {
    PLOG(ERROR) << "failed move " << source_path_.value()
                << " to " << dest_path_.value();
    return false;
  }

  return true;
}

void MoveTreeWorkItem::Rollback() {
  if (moved_to_dest_path_ && !base::Move(dest_path_, source_path_))
    LOG(ERROR) << "Can not move " << dest_path_.value()
               << " to " << source_path_.value();

  base::FilePath backup = backup_path_.path().Append(dest_path_.BaseName());
  if (moved_to_backup_ && !base::Move(backup, dest_path_)) {
    LOG(ERROR) << "failed move " << backup.value()
               << " to " << dest_path_.value();
  }

  if (source_moved_to_backup_ && !base::Move(backup, source_path_)) {
    LOG(ERROR) << "Can not restore " << backup.value()
               << " to " << source_path_.value();
  }
}