File: file_system_url.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 (339 lines) | stat: -rw-r--r-- 11,493 bytes parent folder | download | duplicates (5)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "storage/browser/file_system/file_system_url.h"

#include <compare>
#include <sstream>

#include "base/check.h"
#include "base/files/safe_base_name.h"
#include "base/strings/escape.h"
#include "base/strings/string_util.h"
#include "storage/common/file_system/file_system_types.h"
#include "storage/common/file_system/file_system_util.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace storage {

namespace {

bool AreSameStorageKey(const FileSystemURL& a, const FileSystemURL& b) {
  // TODO(crbug.com/40249324): Make the `storage_key_` member optional.
  // This class improperly uses a StorageKey with an opaque origin to indicate a
  // lack of origin for FileSystemURLs corresponding to non-sandboxed file
  // systems. This leads to unexpected behavior when comparing two non-sandboxed
  // FileSystemURLs which differ only in the nonce of their default-constructed
  // StorageKey.
  return a.storage_key() == b.storage_key() ||
         (a.type() == b.type() &&
          (a.type() == storage::kFileSystemTypeExternal ||
           a.type() == storage::kFileSystemTypeLocal) &&
          a.storage_key().origin().opaque() &&
          b.storage_key().origin().opaque());
}

}  // namespace

FileSystemURL::FileSystemURL()
    : is_null_(true),
      is_valid_(false),
      mount_type_(kFileSystemTypeUnknown),
      type_(kFileSystemTypeUnknown),
      mount_option_(FlushPolicy::NO_FLUSH_ON_COMPLETION) {}

FileSystemURL::FileSystemURL(const FileSystemURL&) = default;

FileSystemURL::FileSystemURL(FileSystemURL&&) noexcept = default;

FileSystemURL& FileSystemURL::operator=(const FileSystemURL&) = default;

FileSystemURL& FileSystemURL::operator=(FileSystemURL&&) noexcept = default;

FileSystemURL::~FileSystemURL() = default;

FileSystemURL FileSystemURL::CreateSibling(
    const base::SafeBaseName& sibling_name) const {
  const base::FilePath& new_base_name = sibling_name.path();
  if (!is_valid_ ||
      new_base_name.empty()
#if BUILDFLAG(IS_ANDROID)
      // Android content-URIs do not support siblings.
      || path().IsContentUri()
#endif
  ) {
    return FileSystemURL();
  }

  const base::FilePath old_base_name = VirtualPath::BaseName(virtual_path_);
  if (!path_.empty() && (path_.BaseName() != old_base_name)) {
    return FileSystemURL();
  }

  FileSystemURL sibling(*this);
  sibling.virtual_path_ =
      VirtualPath::DirName(virtual_path_).Append(new_base_name);
  if (!path_.empty()) {
    sibling.path_ = path_.DirName().Append(new_base_name);
  }
  return sibling;
}

// static
FileSystemURL FileSystemURL::CreateForTest(const GURL& url) {
  return FileSystemURL(
      url, blink::StorageKey::CreateFirstParty(url::Origin::Create(url)));
}

// static
FileSystemURL FileSystemURL::CreateForTest(const blink::StorageKey& storage_key,
                                           FileSystemType mount_type,
                                           const base::FilePath& virtual_path) {
  return FileSystemURL(storage_key, mount_type, virtual_path);
}

// static
FileSystemURL FileSystemURL::CreateForTest(
    const blink::StorageKey& storage_key,
    FileSystemType mount_type,
    const base::FilePath& virtual_path,
    const std::string& mount_filesystem_id,
    FileSystemType cracked_type,
    const base::FilePath& cracked_path,
    const std::string& filesystem_id,
    const FileSystemMountOption& mount_option) {
  return FileSystemURL(storage_key, mount_type, virtual_path,
                       mount_filesystem_id, cracked_type, cracked_path,
                       filesystem_id, mount_option);
}

// static
bool FileSystemURL::TypeImpliesPathIsReal(FileSystemType type) {
  switch (type) {
    // Public enum values, also exposed to JavaScript.
    case kFileSystemTypeTemporary:
    case kFileSystemTypePersistent:
    case kFileSystemTypeIsolated:
    case kFileSystemTypeExternal:
      break;

      // Everything else is a private (also known as internal) enum value.

    case kFileSystemInternalTypeEnumStart:
    case kFileSystemInternalTypeEnumEnd:
      NOTREACHED();

    case kFileSystemTypeLocal:
    case kFileSystemTypeLocalMedia:
    case kFileSystemTypeLocalForPlatformApp:
    case kFileSystemTypeDriveFs:
    case kFileSystemTypeSmbFs:
    case kFileSystemTypeFuseBox:
      return true;

    case kFileSystemTypeUnknown:
    case kFileSystemTypeTest:
    case kFileSystemTypeDragged:
    case kFileSystemTypeDeviceMedia:
    case kFileSystemTypeSyncable:
    case kFileSystemTypeSyncableForInternalSync:
    case kFileSystemTypeForTransientFile:
    case kFileSystemTypeProvided:
    case kFileSystemTypeDeviceMediaAsFileStorage:
    case kFileSystemTypeArcContent:
    case kFileSystemTypeArcDocumentsProvider:
      break;

      // We don't use a "default:" case. Whenever `FileSystemType` gains a
      // new enum value, raise a compiler error (with -Werror,-Wswitch) unless
      // this switch statement is also updated.
  }
  return false;
}

FileSystemURL::FileSystemURL(const GURL& url,
                             const blink::StorageKey& storage_key)
    : is_null_(false),
      mount_type_(kFileSystemTypeUnknown),
      type_(kFileSystemTypeUnknown),
      mount_option_(FlushPolicy::NO_FLUSH_ON_COMPLETION) {
  GURL origin_url;
  // URL should be able to be parsed and the parsed origin should match the
  // StorageKey's origin member.
  is_valid_ = ParseFileSystemSchemeURL(url, &origin_url, &mount_type_,
                                       &virtual_path_) &&
              storage_key.origin().IsSameOriginWith(origin_url);
  storage_key_ = storage_key;
  path_ = virtual_path_;
  type_ = mount_type_;
}

FileSystemURL::FileSystemURL(const blink::StorageKey& storage_key,
                             FileSystemType mount_type,
                             const base::FilePath& virtual_path)
    : is_null_(false),
      is_valid_(true),
      storage_key_(storage_key),
      mount_type_(mount_type),
      virtual_path_(virtual_path.NormalizePathSeparators()),
      type_(mount_type),
      path_(virtual_path.NormalizePathSeparators()),
      mount_option_(FlushPolicy::NO_FLUSH_ON_COMPLETION) {}

FileSystemURL::FileSystemURL(const blink::StorageKey& storage_key,
                             FileSystemType mount_type,
                             const base::FilePath& virtual_path,
                             const std::string& mount_filesystem_id,
                             FileSystemType cracked_type,
                             const base::FilePath& cracked_path,
                             const std::string& filesystem_id,
                             const FileSystemMountOption& mount_option)
    : is_null_(false),
      is_valid_(true),
      storage_key_(storage_key),
      mount_type_(mount_type),
      virtual_path_(virtual_path.NormalizePathSeparators()),
      mount_filesystem_id_(mount_filesystem_id),
      type_(cracked_type),
      path_(cracked_path.NormalizePathSeparators()),
      filesystem_id_(filesystem_id),
      mount_option_(mount_option) {}

GURL FileSystemURL::ToGURL() const {
  if (!is_valid_) {
    return GURL();
  }

  GURL url = GetFileSystemRootURI(storage_key_.origin().GetURL(), mount_type_);
  if (!url.is_valid()) {
    return GURL();
  }

  std::string url_string = url.spec();

  // Exactly match with DOMFileSystemBase::createFileSystemURL()'s encoding
  // behavior, where the path is escaped by KURL::encodeWithURLEscapeSequences
  // which is essentially encodeURIComponent except '/'.
  std::string escaped = base::EscapeQueryParamValue(
      virtual_path_.NormalizePathSeparatorsTo('/').AsUTF8Unsafe(),
      false /* use_plus */);
  base::ReplaceSubstringsAfterOffset(&escaped, 0, "%2F", "/");
  url_string.append(escaped);

  // Build nested GURL.
  return GURL(url_string);
}

std::string FileSystemURL::DebugString() const {
  if (!is_valid_) {
    return "invalid filesystem: URL";
  }
  std::ostringstream ss;
  switch (mount_type_) {
    // Include GURL if GURL serialization is possible.
    case kFileSystemTypeTemporary:
    case kFileSystemTypePersistent:
    case kFileSystemTypeExternal:
    case kFileSystemTypeIsolated:
    case kFileSystemTypeTest:
      ss << "{ uri: ";
      ss << GetFileSystemRootURI(storage_key_.origin().GetURL(), mount_type_);
      break;
    // Otherwise list the origin and path separately.
    default:
      ss << "{ path: ";
  }

  // filesystem_id_ will be non empty for (and only for) cracked URLs.
  if (!filesystem_id_.empty()) {
    ss << virtual_path_.value();
    ss << " (";
    ss << GetFileSystemTypeString(type_) << "@" << filesystem_id_ << ":";
    ss << path_.value();
    ss << ")";
  } else {
    ss << path_.value();
  }
  ss << ", storage key: " << storage_key_.GetDebugString();
  if (bucket_.has_value()) {
    ss << ", bucket id: " << bucket_->id;
  }
  ss << " }";
  return ss.str();
}

BucketLocator FileSystemURL::GetBucket() const {
  if (bucket()) {
    return *bucket_;
  }

  auto bucket = storage::BucketLocator::ForDefaultBucket(storage_key());
  return bucket;
}

bool FileSystemURL::IsParent(const FileSystemURL& child) const {
  return IsInSameFileSystem(child) &&
         (path().IsParent(child.path()) ||
          (VirtualPath::IsRootPath(path()) &&
           !VirtualPath::IsRootPath(child.path())));
}

bool FileSystemURL::IsInSameFileSystem(const FileSystemURL& other) const {
  // Invalid FileSystemURLs should never be considered of the same file system.
  return AreSameStorageKey(*this, other) && is_valid() && other.is_valid() &&
         type() == other.type() && filesystem_id() == other.filesystem_id() &&
         bucket() == other.bucket()
#if BUILDFLAG(IS_ANDROID)
         // Android content-URIs do not support same-FS ops such as rename().
         && !path().IsContentUri() && !other.path().IsContentUri()
#endif
      ;
}

bool FileSystemURL::operator==(const FileSystemURL& that) const {
  if (is_null_ && that.is_null_) {
    return true;
  }

  return AreSameStorageKey(*this, that) && type_ == that.type_ &&
         path_ == that.path_ && filesystem_id_ == that.filesystem_id_ &&
         is_valid_ == that.is_valid_ && bucket_ == that.bucket_;
}

std::weak_ordering FileSystemURL::operator<=>(const FileSystemURL& that) const {
  if (is_null_ && that.is_null_) {
    return std::weak_ordering::equivalent;
  }

  if (!AreSameStorageKey(*this, that)) {
    return storage_key_ < that.storage_key_ ? std::weak_ordering::less
                                            : std::weak_ordering::greater;
  }

  return std::tie(type_, path_, filesystem_id_, is_valid_, bucket_) <=>
         std::tie(that.type_, that.path_, that.filesystem_id_, that.is_valid_,
                  that.bucket_);
}

bool FileSystemURL::Comparator::operator()(const FileSystemURL& lhs,
                                           const FileSystemURL& rhs) const {
  DCHECK(lhs.is_valid_ && rhs.is_valid_);
  if (!AreSameStorageKey(lhs, rhs)) {
    return lhs.storage_key() < rhs.storage_key();
  }
  if (lhs.type_ != rhs.type_) {
    return lhs.type_ < rhs.type_;
  }
  if (lhs.filesystem_id_ != rhs.filesystem_id_) {
    return lhs.filesystem_id_ < rhs.filesystem_id_;
  }
  if (lhs.bucket_ != rhs.bucket_) {
    return lhs.bucket_ < rhs.bucket_;
  }
  return lhs.path_ < rhs.path_;
}

}  // namespace storage