File: restore_io_task.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 (282 lines) | stat: -rw-r--r-- 10,159 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2022 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/browser/ash/file_manager/restore_io_task.h"

#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/functional/callback.h"
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/ash/file_manager/io_task_util.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/file_manager/trash_common_util.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"

namespace file_manager::io_task {

namespace {

base::File::Error CreateNestedPath(
    const base::FilePath& absolute_restore_path) {
  if (base::PathExists(absolute_restore_path)) {
    return base::File::FILE_OK;
  }
  base::File::Error status;
  if (!base::CreateDirectoryAndGetError(absolute_restore_path, &status)) {
    return status;
  }
  return base::File::FILE_OK;
}

}  // namespace

RestoreIOTask::RestoreIOTask(
    std::vector<storage::FileSystemURL> file_urls,
    Profile* profile,
    scoped_refptr<storage::FileSystemContext> file_system_context,
    const base::FilePath base_path,
    bool show_notification)
    : IOTask(show_notification),
      file_system_context_(file_system_context),
      profile_(profile),
      base_path_(base_path) {
  progress_.state = State::kQueued;
  progress_.type = OperationType::kRestore;
  progress_.bytes_transferred = 0;
  progress_.total_bytes = 0;

  for (const auto& url : file_urls) {
    progress_.sources.emplace_back(url, std::nullopt);
  }

  if (file_urls.size() > 0) {
    base::FilePath source_path =
        util::GetDisplayablePath(profile_, file_urls.front())
            .value_or(base::FilePath())
            .BaseName();

    if (source_path.MatchesFinalExtension(trash::kTrashInfoExtension)) {
      source_path = source_path.RemoveFinalExtension();
    }

    progress_.source_name = source_path.value();
  }
}

RestoreIOTask::~RestoreIOTask() {
  if (operation_id_) {
    content::GetIOThreadTaskRunner({})->PostTask(
        FROM_HERE,
        base::BindOnce(
            [](scoped_refptr<storage::FileSystemContext> file_system_context,
               storage::FileSystemOperationRunner::OperationID operation_id) {
              file_system_context->operation_runner()->Cancel(
                  operation_id, base::DoNothing());
            },
            file_system_context_, *operation_id_));
  }
}

void RestoreIOTask::Execute(IOTask::ProgressCallback progress_callback,
                            IOTask::CompleteCallback complete_callback) {
  progress_callback_ = std::move(progress_callback);
  complete_callback_ = std::move(complete_callback);

  if (progress_.sources.size() == 0) {
    Complete(State::kSuccess);
    return;
  }

  progress_.state = State::kInProgress;
  validator_ = std::make_unique<trash::TrashInfoValidator>(profile_);
  validator_->SetDisconnectHandler(base::BindOnce(
      &RestoreIOTask::Complete, weak_ptr_factory_.GetWeakPtr(), State::kError));

  ValidateTrashInfo(0);
}

// Calls the completion callback for the task. `progress_` should not be
// accessed after calling this. If the `trash_service_` is disconnected, it will
// end up here so avoid accessing `trash_service_` here.
void RestoreIOTask::Complete(State state) {
  progress_.state = state;
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(std::move(complete_callback_), std::move(progress_)));
}

void RestoreIOTask::ValidateTrashInfo(size_t idx) {
  const base::FilePath& trash_info =
      (base_path_.empty())
          ? progress_.sources[idx].url.path()
          : base_path_.Append(progress_.sources[idx].url.path());

  auto on_parsed_callback =
      base::BindOnce(&RestoreIOTask::EnsureParentRestorePathExists,
                     weak_ptr_factory_.GetWeakPtr(), idx);

  validator_->ValidateAndParseTrashInfo(std::move(trash_info),
                                        std::move(on_parsed_callback));
}

void RestoreIOTask::EnsureParentRestorePathExists(
    size_t idx,
    trash::ParsedTrashInfoDataOrError parsed_data_or_error) {
  if (!parsed_data_or_error.has_value()) {
    progress_.sources[idx].error =
        trash::ValidationErrorToFileError(parsed_data_or_error.error());
    Complete(State::kError);
    return;
  }

  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock()},
      base::BindOnce(
          &CreateNestedPath,
          parsed_data_or_error.value().absolute_restore_path.DirName()),
      base::BindOnce(&RestoreIOTask::OnParentRestorePathExists,
                     weak_ptr_factory_.GetWeakPtr(), idx,
                     parsed_data_or_error.value().trashed_file_path,
                     parsed_data_or_error.value().absolute_restore_path));
}

void RestoreIOTask::OnParentRestorePathExists(
    size_t idx,
    const base::FilePath& trashed_file_location,
    const base::FilePath& absolute_restore_path,
    base::File::Error status) {
  if (status != base::File::FILE_OK) {
    progress_.sources[idx].error = status;
    Complete(State::kError);
    return;
  }

  GenerateDestinationURL(idx, trashed_file_location, absolute_restore_path);
}

void RestoreIOTask::GenerateDestinationURL(
    size_t idx,
    const base::FilePath& trashed_file_location,
    const base::FilePath& absolute_restore_path) {
  const storage::FileSystemURL item_folder_location = CreateFileSystemURL(
      progress_.sources[idx].url,
      MakeRelativeFromBasePath(absolute_restore_path.DirName()));
  util::GenerateUnusedFilename(item_folder_location,
                               absolute_restore_path.BaseName(),
                               file_system_context_,
                               base::BindOnce(&RestoreIOTask::RestoreItem,
                                              weak_ptr_factory_.GetWeakPtr(),
                                              idx, trashed_file_location));
}

void RestoreIOTask::RestoreItem(
    size_t idx,
    base::FilePath trashed_file_location,
    base::FileErrorOr<storage::FileSystemURL> destination_result) {
  storage::FileSystemURL source_url =
      CreateFileSystemURL(progress_.sources[idx].url,
                          MakeRelativeFromBasePath(trashed_file_location));
  if (!destination_result.has_value()) {
    progress_.outputs.emplace_back(source_url, std::nullopt);
    OnRestoreItem(idx, destination_result.error());
    return;
  }
  progress_.outputs.emplace_back(destination_result.value(), std::nullopt);

  // File browsers generally default to preserving mtimes on copy/move so we
  // should do the same.
  storage::FileSystemOperation::CopyOrMoveOptionSet options = {
      storage::FileSystemOperation::CopyOrMoveOption::kPreserveLastModified};

  auto complete_callback = base::BindPostTaskToCurrentDefault(base::BindOnce(
      &RestoreIOTask::OnRestoreItem, weak_ptr_factory_.GetWeakPtr(), idx));

  // For move operations that occur on the same file system, the progress
  // callback is never invoked.
  content::GetIOThreadTaskRunner({})->PostTaskAndReplyWithResult(
      FROM_HERE,
      base::BindOnce(&StartMoveFileLocalOnIOThread, file_system_context_,
                     source_url, destination_result.value(), options,
                     std::move(complete_callback)),
      base::BindOnce(&RestoreIOTask::SetCurrentOperationID,
                     weak_ptr_factory_.GetWeakPtr()));
}

void RestoreIOTask::OnRestoreItem(size_t idx, base::File::Error error) {
  if (error != base::File::FILE_OK) {
    RestoreComplete(idx, error);
    return;
  }

  auto complete_callback = base::BindPostTaskToCurrentDefault(base::BindOnce(
      &RestoreIOTask::RestoreComplete, weak_ptr_factory_.GetWeakPtr(), idx));

  // On successful file restore, there is a dangling trashinfo file, remove this
  // before restoration is considered complete.
  content::GetIOThreadTaskRunner({})->PostTaskAndReplyWithResult(
      FROM_HERE,
      base::BindOnce(&StartDeleteOnIOThread, file_system_context_,
                     progress_.sources[idx].url, std::move(complete_callback)),
      base::BindOnce(&RestoreIOTask::SetCurrentOperationID,
                     weak_ptr_factory_.GetWeakPtr()));
}

void RestoreIOTask::RestoreComplete(size_t idx, base::File::Error error) {
  DCHECK(idx < progress_.sources.size());
  DCHECK(idx < progress_.outputs.size());
  operation_id_.reset();
  progress_.sources[idx].error = error;
  progress_.outputs[idx].error = error;

  if (idx < progress_.sources.size() - 1) {
    progress_callback_.Run(progress_);
    ValidateTrashInfo(idx + 1);
  } else {
    for (const auto& source : progress_.sources) {
      if (source.error != base::File::FILE_OK) {
        Complete(State::kError);
        return;
      }
    }
    Complete(State::kSuccess);
  }
}

const storage::FileSystemURL RestoreIOTask::CreateFileSystemURL(
    const storage::FileSystemURL& original_url,
    const base::FilePath& path) {
  return file_system_context_->CreateCrackedFileSystemURL(
      original_url.storage_key(), original_url.type(), path);
}

base::FilePath RestoreIOTask::MakeRelativeFromBasePath(
    const base::FilePath& absolute_path) {
  if (base_path_.empty() || !base_path_.IsParent(absolute_path)) {
    return absolute_path;
  }
  std::string relative_path = absolute_path.value();
  if (!file_manager::util::ReplacePrefix(
          &relative_path, base_path_.AsEndingWithSeparator().value(), "")) {
    LOG(ERROR) << "Failed to make absolute path relative";
    return absolute_path;
  }
  return base::FilePath(relative_path);
}

void RestoreIOTask::Cancel() {
  progress_.state = State::kCancelled;
  // Any inflight operation will be cancelled when the task is destroyed.
}

void RestoreIOTask::SetCurrentOperationID(
    storage::FileSystemOperationRunner::OperationID id) {
  operation_id_.emplace(id);
}

}  // namespace file_manager::io_task