Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: chrome/installer/util/move_tree_work_item.cc

Issue 6538025: Temp dir cleanup:... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "chrome/installer/util/move_tree_work_item.h" 5 #include "chrome/installer/util/move_tree_work_item.h"
6 6
7 #include <shlwapi.h> 7 #include <shlwapi.h>
8 8
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "chrome/installer/util/logging_installer.h" 11 #include "chrome/installer/util/logging_installer.h"
12 12
13 MoveTreeWorkItem::~MoveTreeWorkItem() { 13 MoveTreeWorkItem::~MoveTreeWorkItem() {
14 if (file_util::PathExists(backup_path_)) {
15 file_util::Delete(backup_path_, true);
16 }
17 } 14 }
18 15
19 MoveTreeWorkItem::MoveTreeWorkItem(const FilePath& source_path, 16 MoveTreeWorkItem::MoveTreeWorkItem(const FilePath& source_path,
20 const FilePath& dest_path, 17 const FilePath& dest_path,
21 const FilePath& temp_dir) 18 const FilePath& temp_dir)
22 : source_path_(source_path), 19 : source_path_(source_path),
23 dest_path_(dest_path), 20 dest_path_(dest_path),
24 temp_dir_(temp_dir), 21 temp_dir_(temp_dir),
25 moved_to_dest_path_(false), 22 moved_to_dest_path_(false),
26 moved_to_backup_(false) { 23 moved_to_backup_(false) {
27 } 24 }
28 25
29 bool MoveTreeWorkItem::Do() { 26 bool MoveTreeWorkItem::Do() {
30 if (!file_util::PathExists(source_path_)) { 27 if (!file_util::PathExists(source_path_)) {
31 LOG(ERROR) << source_path_.value() << " does not exist"; 28 LOG(ERROR) << source_path_.value() << " does not exist";
32 return false; 29 return false;
33 } 30 }
34 31
35 // If dest_path_ exists, move destination to a backup path. 32 // If dest_path_ exists, move destination to a backup path.
36 if (file_util::PathExists(dest_path_)) { 33 if (file_util::PathExists(dest_path_)) {
37 // Generate a backup path that can keep the original files under dest_path_. 34 // Generate a backup path that can keep the original files under dest_path_.
38 if (!file_util::CreateTemporaryFileInDir(FilePath(temp_dir_), 35 if (!backup_path_.CreateUniqueTempDirUnderPath(temp_dir_)) {
39 &backup_path_)) {
40 LOG(ERROR) << "Failed to get backup path in folder " << temp_dir_.value(); 36 LOG(ERROR) << "Failed to get backup path in folder " << temp_dir_.value();
41 return false; 37 return false;
42 } 38 }
43 39
44 if (file_util::Move(dest_path_, backup_path_)) { 40 FilePath backup = backup_path_.path().Append(dest_path_.BaseName());
41
42 if (file_util::Move(dest_path_, backup)) {
45 moved_to_backup_ = true; 43 moved_to_backup_ = true;
46 VLOG(1) << "Moved destination " << dest_path_.value() 44 VLOG(1) << "Moved destination " << dest_path_.value()
47 << " to backup path " << backup_path_.value(); 45 << " to backup path " << backup.value();
48 } else { 46 } else {
49 LOG(ERROR) << "failed moving " << dest_path_.value() 47 LOG(ERROR) << "failed moving " << dest_path_.value()
50 << " to " << backup_path_.value(); 48 << " to " << backup.value();
51 return false; 49 return false;
52 } 50 }
53 } 51 }
54 52
55 // Now move source to destination. 53 // Now move source to destination.
56 if (file_util::Move(source_path_, dest_path_)) { 54 if (file_util::Move(source_path_, dest_path_)) {
57 moved_to_dest_path_ = true; 55 moved_to_dest_path_ = true;
58 VLOG(1) << "Moved source " << source_path_.value() 56 VLOG(1) << "Moved source " << source_path_.value()
59 << " to destination " << dest_path_.value(); 57 << " to destination " << dest_path_.value();
60 } else { 58 } else {
61 LOG(ERROR) << "failed move " << source_path_.value() 59 LOG(ERROR) << "failed move " << source_path_.value()
62 << " to " << dest_path_.value(); 60 << " to " << dest_path_.value();
63 return false; 61 return false;
64 } 62 }
65 63
66 return true; 64 return true;
67 } 65 }
68 66
69 void MoveTreeWorkItem::Rollback() { 67 void MoveTreeWorkItem::Rollback() {
70 if (moved_to_dest_path_ && !file_util::Move(dest_path_, source_path_)) 68 if (moved_to_dest_path_ && !file_util::Move(dest_path_, source_path_))
71 LOG(ERROR) << "Can not move " << dest_path_.value() 69 LOG(ERROR) << "Can not move " << dest_path_.value()
72 << " to " << source_path_.value(); 70 << " to " << source_path_.value();
73 71
74 if (moved_to_backup_ && !file_util::Move(backup_path_, dest_path_)) 72 if (moved_to_backup_) {
75 LOG(ERROR) << "failed move " << backup_path_.value() 73 FilePath backup = backup_path_.path().Append(dest_path_.BaseName());
76 << " to " << dest_path_.value(); 74 if (!file_util::Move(backup, dest_path_)) {
75 LOG(ERROR) << "failed move " << backup.value()
76 << " to " << dest_path_.value();
77 }
78 }
77 } 79 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698