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
3 // found in the LICENSE file.
5 #include "base/files/file_util.h"
6 #include "base/logging.h"
7 #include "chrome/installer/util/create_dir_work_item.h"
8 #include "chrome/installer/util/logging_installer.h"
10 CreateDirWorkItem::~CreateDirWorkItem() {
13 CreateDirWorkItem::CreateDirWorkItem(const base::FilePath
& path
)
15 rollback_needed_(false) {
18 void CreateDirWorkItem::GetTopDirToCreate() {
19 if (base::PathExists(path_
)) {
20 top_path_
= base::FilePath();
24 base::FilePath
parent_dir(path_
);
26 top_path_
= parent_dir
;
27 parent_dir
= parent_dir
.DirName();
28 } while ((parent_dir
!= top_path_
) && !base::PathExists(parent_dir
));
32 bool CreateDirWorkItem::Do() {
33 VLOG(1) << "creating directory " << path_
.value();
35 if (top_path_
.empty())
38 VLOG(1) << "top directory that needs to be created: " << top_path_
.value();
39 bool result
= base::CreateDirectory(path_
);
40 VLOG(1) << "directory creation result: " << result
;
42 rollback_needed_
= true;
47 void CreateDirWorkItem::Rollback() {
48 if (!rollback_needed_
)
51 // Delete all the directories we created to rollback.
52 // Note we can not recusively delete top_path_ since we don't want to
53 // delete non-empty directory. (We may have created a shared directory).
54 // Instead we walk through path_ to top_path_ and delete directories
56 base::FilePath
path_to_delete(path_
);
59 if (base::PathExists(path_to_delete
)) {
60 if (!RemoveDirectory(path_to_delete
.value().c_str()))
63 if (path_to_delete
== top_path_
)
65 path_to_delete
= path_to_delete
.DirName();