File: create_dir_work_item.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (69 lines) | stat: -rw-r--r-- 1,874 bytes parent folder | download | duplicates (6)
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
// Copyright 2010 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

#include "base/files/file_util.h"
#include "base/logging.h"

CreateDirWorkItem::~CreateDirWorkItem() = default;

CreateDirWorkItem::CreateDirWorkItem(const base::FilePath& path)
    : path_(path), rollback_needed_(false) {}

void CreateDirWorkItem::GetTopDirToCreate() {
  if (base::PathExists(path_)) {
    top_path_ = base::FilePath();
    return;
  }

  base::FilePath parent_dir(path_);
  do {
    top_path_ = parent_dir;
    parent_dir = parent_dir.DirName();
  } while ((parent_dir != top_path_) && !base::PathExists(parent_dir));
  return;
}

bool CreateDirWorkItem::DoImpl() {
  VLOG(1) << "creating directory " << path_.value();
  GetTopDirToCreate();
  if (top_path_.empty())
    return true;

  VLOG(1) << "Top directory that needs to be created: " << top_path_.value();
  bool result = base::CreateDirectory(path_);
  if (result)
    VLOG(1) << "Created directory";
  else
    PLOG(ERROR) << "Failed to create directory " << top_path_.value();

  rollback_needed_ = true;

  return result;
}

void CreateDirWorkItem::RollbackImpl() {
  if (!rollback_needed_)
    return;

  // Delete all the directories we created to rollback.
  // Note we can not recursively delete top_path_ since we don't want to
  // delete non-empty directory. (We may have created a shared directory).
  // Instead we walk through path_ to top_path_ and delete directories
  // along the way.
  base::FilePath path_to_delete(path_);

  while (1) {
    if (base::PathExists(path_to_delete)) {
      if (!RemoveDirectory(path_to_delete.value().c_str()))
        break;
    }
    if (path_to_delete == top_path_)
      break;
    path_to_delete = path_to_delete.DirName();
  }

  return;
}