File: volume.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 (476 lines) | stat: -rw-r--r-- 17,177 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Copyright 2013 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/volume.h"

#include <string_view>

#include "ash/constants/ash_features.h"
#include "base/strings/strcat.h"
#include "chrome/browser/ash/arc/fileapi/arc_documents_provider_util.h"
#include "chrome/browser/ash/arc/fileapi/arc_media_view_util.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/chromeos/strings/grit/ui_chromeos_strings.h"

namespace file_manager {
namespace {

using l10n_util::GetStringUTF8;
const char kMtpVolumeIdPrefix[] = "mtp:";

VolumeType MountTypeToVolumeType(ash::MountType type) {
  switch (type) {
    case ash::MountType::kInvalid:
      // A zip mount with an invalid path will return type kInvalid. We can use
      // a default VolumeType in this case.
      return VOLUME_TYPE_DOWNLOADS_DIRECTORY;
    case ash::MountType::kDevice:
      return VOLUME_TYPE_REMOVABLE_DISK_PARTITION;
    case ash::MountType::kArchive:
      return VOLUME_TYPE_MOUNTED_ARCHIVE_FILE;
    case ash::MountType::kNetworkStorage:
      // Network storage mounts are handled by their mounters so
      // MountType::kNetworkStorage should never need to be handled
      // here.
      break;
  }

  NOTREACHED();
}

// Returns a string representation of the given volume type.
std::string_view VolumeTypeToString(const VolumeType type) {
  switch (type) {
    case VOLUME_TYPE_GOOGLE_DRIVE:
      return "drive";
    case VOLUME_TYPE_DOWNLOADS_DIRECTORY:
      return "downloads";
    case VOLUME_TYPE_REMOVABLE_DISK_PARTITION:
      return "removable";
    case VOLUME_TYPE_MOUNTED_ARCHIVE_FILE:
      return "archive";
    case VOLUME_TYPE_PROVIDED:
      return "provided";
    case VOLUME_TYPE_MTP:
      return "mtp";
    case VOLUME_TYPE_MEDIA_VIEW:
      return "media_view";
    case VOLUME_TYPE_ANDROID_FILES:
      return "android_files";
    case VOLUME_TYPE_DOCUMENTS_PROVIDER:
      return "documents_provider";
    case VOLUME_TYPE_TESTING:
      return "testing";
    case VOLUME_TYPE_CROSTINI:
      return "crostini";
    case VOLUME_TYPE_SMB:
      return "smb";
    case VOLUME_TYPE_SYSTEM_INTERNAL:
      return "system_internal";
    case VOLUME_TYPE_GUEST_OS:
      return "guest_os";
    case NUM_VOLUME_TYPE:
      break;
  }

  NOTREACHED() << "Unexpected VolumeType value "
               << static_cast<std::underlying_type_t<VolumeType>>(type);
}

// Generates a unique volume ID for the given volume info.
std::string GenerateVolumeId(const Volume& volume) {
  // For the same volume type, base names are unique, as mount points are
  // flat for the same volume type.
  return base::StrCat({VolumeTypeToString(volume.type()), ":",
                       volume.mount_path().BaseName().AsUTF8Unsafe()});
}

// Returns the localized label for a given media view.
std::string MediaViewRootIdToLabel(std::string_view root_id) {
  if (root_id == arc::kAudioRootId) {
    return GetStringUTF8(IDS_FILE_BROWSER_MEDIA_VIEW_AUDIO_ROOT_LABEL);
  }

  if (root_id == arc::kImagesRootId) {
    return GetStringUTF8(IDS_FILE_BROWSER_MEDIA_VIEW_IMAGES_ROOT_LABEL);
  }

  if (root_id == arc::kVideosRootId) {
    return GetStringUTF8(IDS_FILE_BROWSER_MEDIA_VIEW_VIDEOS_ROOT_LABEL);
  }

  if (root_id == arc::kDocumentsRootId) {
    return GetStringUTF8(IDS_FILE_BROWSER_MEDIA_VIEW_DOCUMENTS_ROOT_LABEL);
  }

  NOTREACHED() << "Unexpected root ID: " << root_id;
}

}  // namespace

std::ostream& operator<<(std::ostream& out, const VolumeType type) {
  return out << VolumeTypeToString(type);
}

Volume::Volume() = default;
Volume::~Volume() = default;

// static
std::unique_ptr<Volume> Volume::CreateForDrive(base::FilePath drive_path) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->type_ = VOLUME_TYPE_GOOGLE_DRIVE;
  volume->source_path_ = drive_path;
  volume->source_ = SOURCE_NETWORK;
  volume->mount_path_ = std::move(drive_path);
  volume->volume_id_ = GenerateVolumeId(*volume);
  volume->volume_label_ = GetStringUTF8(IDS_FILE_BROWSER_DRIVE_DIRECTORY_LABEL);
  volume->watchable_ = true;
  return volume;
}

// static
std::unique_ptr<Volume> Volume::CreateForDownloads(
    base::FilePath downloads_path,
    base::FilePath optional_fusebox_path,
    const char* optional_fusebox_volume_label,
    bool read_only) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->type_ = VOLUME_TYPE_DOWNLOADS_DIRECTORY;
  // Keep source_path empty.
  volume->source_ = SOURCE_SYSTEM;
  volume->mount_path_ = std::move(downloads_path);
  volume->volume_id_ = GenerateVolumeId(*volume);
  volume->volume_label_ = GetStringUTF8(IDS_FILE_BROWSER_MY_FILES_ROOT_LABEL);
  volume->watchable_ = true;
  volume->is_read_only_ = read_only;

  if (!optional_fusebox_path.empty()) {
    // Leaving the type_ as VOLUME_TYPE_DOWNLOADS_DIRECTORY means that, for
    // some unknown reason, it doesn't show up in the CrOS Files app. Use a
    // different but arbitrary type instead.
    //
    // It doesn't need to be well polished. It's just for debugging FuseBox. We
    // wouldn't normally need a FuseBox wrapper (exposing to the kernel-level
    // file system) for something like Downloads that's typically on local disk
    // (and hence already on the kernel-level file system).
    volume->type_ = VOLUME_TYPE_MTP;

    volume->file_system_type_ = util::kFuseBox;
    volume->mount_path_ = std::move(optional_fusebox_path);
    volume->volume_id_ =
        base::StrCat({util::kFuseBox, std::move(volume->volume_id_)});
    if (optional_fusebox_volume_label) {
      volume->volume_label_ = optional_fusebox_volume_label;
    }
  }

  return volume;
}

// static
std::unique_ptr<Volume> Volume::CreateForRemovable(
    const ash::disks::DiskMountManager::MountPoint& mount_point,
    const ash::disks::Disk* disk) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->type_ = MountTypeToVolumeType(mount_point.mount_type);
  volume->source_path_ = base::FilePath(mount_point.source_path);
  volume->source_ = mount_point.mount_type == ash::MountType::kArchive
                        ? SOURCE_FILE
                        : SOURCE_DEVICE;
  volume->mount_path_ = base::FilePath(mount_point.mount_path);
  volume->mount_condition_ = mount_point.mount_error;

  if (disk) {
    volume->file_system_type_ = disk->file_system_type();
    volume->volume_label_ = disk->device_label();
    volume->device_type_ = disk->device_type();
    volume->storage_device_path_ = base::FilePath(disk->storage_device_path());
    volume->is_parent_ = disk->is_parent();
    volume->is_read_only_ = disk->is_read_only();
    volume->is_read_only_removable_device_ = disk->is_read_only_hardware();
    volume->has_media_ = disk->has_media();
    volume->drive_label_ = disk->drive_label();
  } else {
    volume->volume_label_ = volume->mount_path().BaseName().AsUTF8Unsafe();
    volume->is_read_only_ =
        (mount_point.mount_type == ash::MountType::kArchive);
  }
  volume->volume_id_ = GenerateVolumeId(*volume);
  volume->watchable_ = true;
  return volume;
}

// static
std::unique_ptr<Volume> Volume::CreateForProvidedFileSystem(
    const ash::file_system_provider::ProvidedFileSystemInfo& file_system_info,
    MountContext mount_context,
    base::FilePath optional_fusebox_path) {
  std::unique_ptr<Volume> volume(new Volume());

  switch (file_system_info.source()) {
    case extensions::SOURCE_FILE:
      volume->source_ = SOURCE_FILE;
      break;
    case extensions::SOURCE_DEVICE:
      volume->source_ = SOURCE_DEVICE;
      break;
    case extensions::SOURCE_NETWORK:
      volume->source_ = SOURCE_NETWORK;
      break;
  }

  volume->volume_label_ = file_system_info.display_name();
  volume->type_ = VOLUME_TYPE_PROVIDED;
  volume->mount_path_ = file_system_info.mount_path();
  volume->mount_context_ = mount_context;

  volume->is_parent_ = true;
  volume->is_read_only_ = !file_system_info.writable();
  volume->configurable_ = file_system_info.configurable();
  volume->watchable_ = file_system_info.watchable();
  volume->icon_set_ = file_system_info.icon_set();

  volume->volume_id_ = GenerateVolumeId(*volume);
  volume->file_system_id_ = file_system_info.file_system_id();
  volume->provider_id_ = file_system_info.provider_id();

  if (!optional_fusebox_path.empty()) {
    volume->file_system_type_ = util::kFuseBox;
    if (ash::features::IsFileManagerFuseBoxDebugEnabled()) {
      volume->volume_label_.insert(0, "fusebox ");
    }
    volume->mount_path_ = std::move(optional_fusebox_path);
    // Even though the underlying FSP may support watchers, fusebox needs
    // to implement watchers in order to match the capability of the FSP.
    // TODO(crbug.com/1353673): Add watcher support to fusebox.
    volume->watchable_ = false;
    // "fusebox" prefix the original FSP volume id.
    volume->volume_id_ =
        base::StrCat({util::kFuseBox, GenerateVolumeId(*volume)});
  }

  return volume;
}

// static
std::unique_ptr<Volume> Volume::CreateForMTP(base::FilePath mount_path,
                                             std::string label,
                                             bool read_only,
                                             bool use_fusebox) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->type_ = VOLUME_TYPE_MTP;
  volume->mount_path_ = mount_path;
  volume->is_parent_ = true;
  volume->is_read_only_ = read_only;
  volume->volume_id_ = base::StrCat({kMtpVolumeIdPrefix, label});
  volume->volume_label_ = std::move(label);
  volume->source_path_ = std::move(mount_path);
  volume->source_ = SOURCE_DEVICE;
  volume->device_type_ = ash::DeviceType::kMobile;

  // MTP does have watcher support via WatcherManager but it doesn't
  // seem to work (perhaps something missing in mtpd).
  volume->watchable_ = false;

  if (use_fusebox) {
    volume->file_system_type_ = util::kFuseBox;
    volume->volume_id_ =
        base::StrCat({util::kFuseBox, std::move(volume->volume_id_)});
    if (ash::features::IsFileManagerFuseBoxDebugEnabled()) {
      volume->volume_label_.insert(0, "fusebox ");
    }
  }

  return volume;
}

// static
std::unique_ptr<Volume> Volume::CreateForMediaView(const std::string& root_id) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->type_ = VOLUME_TYPE_MEDIA_VIEW;
  volume->source_ = SOURCE_SYSTEM;
  volume->mount_path_ = arc::GetDocumentsProviderMountPath(
      arc::kMediaDocumentsProviderAuthority, root_id);
  volume->volume_label_ = MediaViewRootIdToLabel(root_id);
  volume->is_read_only_ = false;
  volume->watchable_ = false;
  volume->volume_id_ = arc::GetMediaViewVolumeId(root_id);
  return volume;
}

// static
std::unique_ptr<Volume> Volume::CreateForSshfsCrostini(
    base::FilePath sshfs_mount_path,
    base::FilePath remote_mount_path) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->type_ = VOLUME_TYPE_CROSTINI;
  // Keep source_path empty.
  volume->source_ = SOURCE_SYSTEM;
  volume->mount_path_ = std::move(sshfs_mount_path);
  volume->remote_mount_path_ = std::move(remote_mount_path);
  volume->volume_id_ = GenerateVolumeId(*volume);
  volume->volume_label_ =
      GetStringUTF8(IDS_FILE_BROWSER_LINUX_FILES_ROOT_LABEL);
  volume->watchable_ = true;
  return volume;
}

// static
std::unique_ptr<Volume> Volume::CreateForSftpGuestOs(
    std::string display_name,
    base::FilePath sftp_mount_path,
    base::FilePath remote_mount_path,
    const guest_os::VmType vm_type) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->type_ = vm_type == guest_os::VmType::ARCVM ? VOLUME_TYPE_ANDROID_FILES
                                                     : VOLUME_TYPE_GUEST_OS;
  // Keep source_path empty.
  volume->source_ = SOURCE_SYSTEM;
  volume->mount_path_ = std::move(sftp_mount_path);
  volume->remote_mount_path_ = std::move(remote_mount_path);
  volume->volume_id_ = GenerateVolumeId(*volume);
  volume->volume_label_ = std::move(display_name);
  volume->watchable_ = true;
  volume->vm_type_ = vm_type;
  return volume;
}

// static
std::unique_ptr<Volume> Volume::CreateForAndroidFiles(
    base::FilePath mount_path) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->type_ = VOLUME_TYPE_ANDROID_FILES;
  // Keep source_path empty.
  volume->source_ = SOURCE_SYSTEM;
  volume->mount_path_ = std::move(mount_path);
  volume->volume_id_ = GenerateVolumeId(*volume);
  volume->volume_label_ =
      GetStringUTF8(IDS_FILE_BROWSER_ANDROID_FILES_ROOT_LABEL);
  volume->watchable_ = true;
  return volume;
}

// static
std::unique_ptr<Volume> Volume::CreateForDocumentsProvider(
    const std::string& authority,
    const std::string& root_id,
    const std::string& title,
    const std::string& summary,
    const GURL& icon_url,
    bool read_only,
    const std::string& optional_fusebox_subdir) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->type_ = VOLUME_TYPE_DOCUMENTS_PROVIDER;
  // Keep source_path empty.
  volume->source_ = SOURCE_SYSTEM;
  volume->mount_path_ = arc::GetDocumentsProviderMountPath(authority, root_id);
  volume->volume_label_ = title;
  volume->is_read_only_ = read_only;
  volume->watchable_ = false;
  volume->volume_id_ = arc::GetDocumentsProviderVolumeId(authority, root_id);
  if (!icon_url.is_empty()) {
    ash::file_system_provider::IconSet icon_set;
    icon_set.SetIcon(ash::file_system_provider::IconSet::IconSize::SIZE_32x32,
                     icon_url);
    volume->icon_set_ = icon_set;
  }

  if (!optional_fusebox_subdir.empty()) {
    volume->file_system_type_ = util::kFuseBox;
    volume->volume_id_.insert(0, util::kFuseBox);
    volume->mount_path_ =
        base::FilePath(util::kFuseBoxMediaPath).Append(optional_fusebox_subdir);
    if (ash::features::IsFileManagerFuseBoxDebugEnabled()) {
      volume->volume_label_.insert(0, "fusebox ");
    }
  }

  return volume;
}

// static
std::unique_ptr<Volume> Volume::CreateForSmb(base::FilePath mount_point,
                                             std::string display_name) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->type_ = VOLUME_TYPE_SMB;
  // Keep source_path empty.
  volume->source_ = SOURCE_NETWORK;
  volume->mount_path_ = std::move(mount_point);
  volume->volume_id_ = GenerateVolumeId(*volume);
  volume->volume_label_ = std::move(display_name);
  volume->watchable_ = false;
  volume->is_read_only_ = false;
  return volume;
}

// ShareCache is not visible in the file manager and so this volume does not
// represent a real, user-visible Volume. However, shared files can be read
// through ImageLoader, which needs a Volume present to be able to read from the
// directory.
// static
std::unique_ptr<Volume> Volume::CreateForShareCache(base::FilePath mount_path) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->type_ = VOLUME_TYPE_SYSTEM_INTERNAL;
  // Keep source_path empty.
  volume->source_ = SOURCE_SYSTEM;
  volume->mount_path_ = std::move(mount_path);
  volume->volume_id_ = GenerateVolumeId(*volume);
  volume->watchable_ = false;
  volume->is_read_only_ = true;
  volume->hidden_ = true;
  return volume;
}

// static
std::unique_ptr<Volume> Volume::CreateForTesting(base::FilePath path,
                                                 VolumeType volume_type,
                                                 ash::DeviceType device_type,
                                                 bool read_only,
                                                 base::FilePath device_path,
                                                 std::string drive_label,
                                                 std::string file_system_type,
                                                 bool hidden,
                                                 bool watchable) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->type_ = volume_type;
  volume->device_type_ = device_type;
  // Keep source_path empty.
  volume->source_ = SOURCE_DEVICE;
  volume->mount_path_ = std::move(path);
  volume->storage_device_path_ = std::move(device_path);
  volume->is_read_only_ = read_only;
  volume->volume_id_ = GenerateVolumeId(*volume);
  volume->drive_label_ = std::move(drive_label);
  volume->file_system_type_ = std::move(file_system_type);
  volume->hidden_ = hidden;
  volume->watchable_ = watchable;
  return volume;
}

// static
std::unique_ptr<Volume> Volume::CreateForTesting(base::FilePath device_path,
                                                 base::FilePath mount_path) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->storage_device_path_ = std::move(device_path);
  volume->mount_path_ = std::move(mount_path);
  return volume;
}

// static
std::unique_ptr<Volume> Volume::CreateForTesting(
    base::FilePath path,
    VolumeType volume_type,
    std::optional<guest_os::VmType> vm_type,
    base::FilePath source_path) {
  std::unique_ptr<Volume> volume(new Volume());
  volume->mount_path_ = std::move(path);
  volume->type_ = volume_type;
  volume->vm_type_ = vm_type;
  volume->volume_id_ = GenerateVolumeId(*volume);
  volume->source_path_ = std::move(source_path);
  return volume;
}

}  // namespace file_manager