File: trash_info_validator.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 (194 lines) | stat: -rw-r--r-- 6,780 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
// 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/trash_info_validator.h"

#include <string_view>

#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/notreached.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"

class Profile;

namespace file_manager::trash {

namespace {

void RunCallbackWithError(ValidationError error,
                          ValidateAndParseTrashInfoCallback callback) {
  std::move(callback).Run(base::unexpected<ValidationError>(std::move(error)));
}

}  // namespace

ParsedTrashInfoData::ParsedTrashInfoData() = default;
ParsedTrashInfoData::~ParsedTrashInfoData() = default;

ParsedTrashInfoData::ParsedTrashInfoData(ParsedTrashInfoData&& other) = default;
ParsedTrashInfoData& ParsedTrashInfoData::operator=(
    ParsedTrashInfoData&& other) = default;

std::ostream& operator<<(std::ostream& out, const ValidationError& value) {
  switch (value) {
    case ValidationError::kFileNotExist:
      out << "kFileNotExist";
      break;
    case ValidationError::kInfoNotExist:
      out << "kInfoNotExist";
      break;
    case ValidationError::kInfoFileInvalidLocation:
      out << "kInfoFileInvalidLocation";
      break;
    case ValidationError::kInfoFileInvalid:
      out << "kInfoFileInvalid";
      break;
    default:
      NOTREACHED();
  }
  return out;
}

base::File::Error ValidationErrorToFileError(ValidationError error) {
  switch (error) {
    case ValidationError::kFileNotExist:
      return base::File::FILE_ERROR_NOT_FOUND;
    case ValidationError::kInfoNotExist:
      return base::File::FILE_ERROR_NOT_FOUND;
    case ValidationError::kInfoFileInvalidLocation:
      return base::File::FILE_ERROR_INVALID_URL;
    case ValidationError::kInfoFileInvalid:
      return base::File::FILE_ERROR_INVALID_OPERATION;
    default:
      NOTREACHED();
  }
}

TrashInfoValidator::TrashInfoValidator(Profile* profile) {
  enabled_trash_locations_ =
      trash::GenerateEnabledTrashLocationsForProfile(profile);

  parser_ = std::make_unique<ash::trash_service::TrashInfoParser>();
}

TrashInfoValidator::~TrashInfoValidator() = default;

void TrashInfoValidator::SetDisconnectHandler(
    base::OnceCallback<void()> disconnect_callback) {
  DCHECK(parser_) << "Parser should not be null here";
  if (parser_) {
    parser_->SetDisconnectHandler(std::move(disconnect_callback));
  }
}

void TrashInfoValidator::ValidateAndParseTrashInfo(
    const base::FilePath& trash_info_path,
    ValidateAndParseTrashInfoCallback callback) {
  // Validates the supplied file ends in a .trashinfo extension.
  if (trash_info_path.FinalExtension() != kTrashInfoExtension) {
    RunCallbackWithError(ValidationError::kInfoFileInvalid,
                         std::move(callback));
    return;
  }

  // Validate the .trashinfo file belongs in an enabled trash location.
  base::FilePath trash_folder_location;
  base::FilePath mount_point_path;
  for (const auto& [parent_path, info] : enabled_trash_locations_) {
    if (parent_path.Append(info.relative_folder_path)
            .IsParent(trash_info_path)) {
      trash_folder_location = parent_path.Append(info.relative_folder_path);
      mount_point_path = info.mount_point_path;
      break;
    }
  }

  if (mount_point_path.empty() || trash_folder_location.empty()) {
    RunCallbackWithError(ValidationError::kInfoFileInvalidLocation,
                         std::move(callback));
    return;
  }

  // Ensure the corresponding file that this metadata file refers to actually
  // exists.
  base::FilePath trashed_file_location =
      trash_folder_location.Append(kFilesFolderName)
          .Append(trash_info_path.BaseName().RemoveFinalExtension());

  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock()},
      base::BindOnce(&base::PathExists, trashed_file_location),
      base::BindOnce(&TrashInfoValidator::OnTrashedFileExists,
                     weak_ptr_factory_.GetWeakPtr(), mount_point_path,
                     trashed_file_location, std::move(trash_info_path),
                     std::move(callback)));
}

void TrashInfoValidator::OnTrashedFileExists(
    const base::FilePath& mount_point_path,
    const base::FilePath& trashed_file_location,
    const base::FilePath& trash_info_path,
    ValidateAndParseTrashInfoCallback callback,
    bool exists) {
  if (!exists) {
    RunCallbackWithError(ValidationError::kFileNotExist, std::move(callback));
    return;
  }

  auto complete_callback = base::BindPostTaskToCurrentDefault(base::BindOnce(
      &TrashInfoValidator::OnTrashInfoParsed, weak_ptr_factory_.GetWeakPtr(),
      trash_info_path, mount_point_path, trashed_file_location,
      std::move(callback)));

  parser_->ParseTrashInfoFile(trash_info_path, std::move(complete_callback));
}

void TrashInfoValidator::OnTrashInfoParsed(
    const base::FilePath& trash_info_path,
    const base::FilePath& mount_point_path,
    const base::FilePath& trashed_file_location,
    ValidateAndParseTrashInfoCallback callback,
    base::File::Error status,
    const base::FilePath& restore_path,
    base::Time deletion_date) {
  if (status != base::File::FILE_OK) {
    RunCallbackWithError((status == base::File::FILE_ERROR_NOT_FOUND)
                             ? ValidationError::kInfoNotExist
                             : ValidationError::kInfoFileInvalid,
                         std::move(callback));
    return;
  }

  // The restore path that was parsed could be empty, not have a leading "/" or
  // only consist of "/".
  if (restore_path.empty() ||
      restore_path.value()[0] != base::FilePath::kSeparators[0] ||
      (restore_path.value().size() == 1 &&
       restore_path.value()[0] == base::FilePath::kSeparators[0])) {
    RunCallbackWithError(ValidationError::kInfoFileInvalid,
                         std::move(callback));
    return;
  }

  // Remove the leading "/" character to make the restore path relative from the
  // known trash parent path.
  std::string_view relative_path =
      std::string_view(restore_path.value()).substr(1);
  base::FilePath absolute_restore_path = mount_point_path.Append(relative_path);

  ParsedTrashInfoData parsed_data;
  parsed_data.trash_info_path = std::move(trash_info_path);
  parsed_data.trashed_file_path = std::move(trashed_file_location);
  parsed_data.absolute_restore_path = std::move(absolute_restore_path);
  parsed_data.deletion_date = std::move(deletion_date);

  std::move(callback).Run(
      base::ok<ParsedTrashInfoData>(std::move(parsed_data)));
}

}  // namespace file_manager::trash