File: mount_path_util.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 (199 lines) | stat: -rw-r--r-- 6,334 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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "chrome/browser/ash/file_system_provider/mount_path_util.h"

#include <stddef.h>

#include <vector>

#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/file_system_provider/provided_file_system.h"
#include "chrome/browser/ash/file_system_provider/provided_file_system_info.h"
#include "chrome/browser/ash/file_system_provider/service.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

namespace ash::file_system_provider::util {

namespace {

// Root mount path for all of the provided file systems.
const base::FilePath::CharType kProvidedMountPointRoot[] =
    FILE_PATH_LITERAL("/provided");

}  // namespace

// Escapes the file system id so it can be used as a file/directory name.
// This is based on net/base/escape.cc: net::(anonymous namespace)::Escape
std::string EscapeFileSystemId(const std::string& file_system_id) {
  std::string escaped;
  for (char c : file_system_id) {
    if (c == '%' || c == '.' || c == '/') {
      base::StringAppendF(&escaped, "%%%02X", c);
    } else {
      escaped.push_back(c);
    }
  }
  return escaped;
}

base::FilePath GetMountPath(Profile* profile,
                            const ProviderId& provider_id,
                            const std::string& file_system_id) {
  const user_manager::User* const user =
      user_manager::UserManager::IsInitialized()
          ? ProfileHelper::Get()->GetUserByProfile(
                profile->GetOriginalProfile())
          : nullptr;
  const std::string safe_file_system_id = EscapeFileSystemId(file_system_id);
  const std::string username_suffix = user ? user->username_hash() : "";
  return base::FilePath(kProvidedMountPointRoot)
      .AppendASCII(provider_id.ToString() + ":" + safe_file_system_id + ":" +
                   username_suffix);
}

bool IsFileSystemProviderLocalPath(const base::FilePath& local_path) {
  std::vector<base::FilePath::StringType> components =
      local_path.GetComponents();

  if (components.size() < 3)
    return false;

  if (components[0] != FILE_PATH_LITERAL("/"))
    return false;

  if (components[1] != kProvidedMountPointRoot + 1 /* no leading slash */)
    return false;

  return true;
}

FileSystemURLParser::FileSystemURLParser(const storage::FileSystemURL& url)
    : url_(url), file_system_(nullptr) {}

FileSystemURLParser::~FileSystemURLParser() = default;

bool FileSystemURLParser::Parse() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  switch (url_.type()) {
    case storage::kFileSystemTypeFuseBox:
    case storage::kFileSystemTypeProvided:
      break;
    default:
      return false;
  }

  std::string filesystem_id = url_.filesystem_id();
  base::FilePath path = url_.path();

  // Convert fusebox URL to its backing (FSP) file system provider URL.
  if (url_.type() == storage::kFileSystemTypeFuseBox) {
    const size_t prefix_len =
        strlen(file_manager::util::kFuseBoxMountNamePrefix) +
        strlen(file_manager::util::kFuseBoxSubdirPrefixFSP);
    const std::string& virtual_path = url_.virtual_path().value();
    if ((filesystem_id.size() < prefix_len) ||
        (virtual_path.size() < prefix_len)) {
      return false;
    }
    filesystem_id = filesystem_id.substr(prefix_len);
    path = base::FilePath::FromUTF8Unsafe(base::JoinString(
        {kProvidedMountPointRoot, virtual_path.substr(prefix_len)}, "/"));
  }

  // Find the service that handles the provider URL mount point.
  for (Profile* profile :
       g_browser_process->profile_manager()->GetLoadedProfiles()) {
    Profile* original_profile = profile->GetOriginalProfile();

    if (original_profile != profile ||
        !ProfileHelper::IsUserProfile(original_profile)) {
      continue;
    }

    Service* const service = Service::Get(original_profile);
    if (!service)
      continue;

    ProvidedFileSystemInterface* const file_system =
        service->GetProvidedFileSystem(filesystem_id);
    if (!file_system)
      continue;

    // Strip the mount path name from the local path, to extract the file path
    // within the provided file system.
    file_system_ = file_system;
    std::vector<base::FilePath::StringType> components = path.GetComponents();
    if (components.size() < 3)
      return false;

    file_path_ = base::FilePath(FILE_PATH_LITERAL("/"));
    for (size_t i = 3; i < components.size(); ++i) {
      file_path_ = file_path_.Append(components[i]);
    }

    return true;
  }

  // Nothing has been found.
  return false;
}

LocalPathParser::LocalPathParser(Profile* profile,
                                 const base::FilePath& local_path)
    : profile_(profile), local_path_(local_path), file_system_(nullptr) {}

LocalPathParser::~LocalPathParser() = default;

bool LocalPathParser::Parse() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  if (!IsFileSystemProviderLocalPath(local_path_))
    return false;

  std::vector<base::FilePath::StringType> components =
      local_path_.GetComponents();

  if (components.size() < 3)
    return false;

  const std::string mount_point_name = components[2];

  Service* const service = Service::Get(profile_);
  if (!service)
    return false;

  ProvidedFileSystemInterface* const file_system =
      service->GetProvidedFileSystem(mount_point_name);
  if (!file_system)
    return false;

  // Strip the mount point path from the virtual path, to extract the file
  // path within the provided file system.
  file_system_ = file_system;
  file_path_ = base::FilePath(FILE_PATH_LITERAL("/"));
  for (size_t i = 3; i < components.size(); ++i) {
    file_path_ = file_path_.Append(components[i]);
  }

  return true;
}

}  // namespace ash::file_system_provider::util